From 196494632b1b972078a8859c9471bfd107a56281 Mon Sep 17 00:00:00 2001 From: Aljaz Kovac Date: Sun, 26 Jan 2025 15:49:32 +0100 Subject: [PATCH] FromNand2Tetris: Change names of custom lexers and fix Hack lexer. --- _plugins/rouge/hack.rb | 13 +++++++------ _plugins/rouge/hdl.rb | 6 +++--- _posts/2024-08-30-FromNand2Tetris.md | 6 +++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/_plugins/rouge/hack.rb b/_plugins/rouge/hack.rb index a2abb87..030b839 100644 --- a/_plugins/rouge/hack.rb +++ b/_plugins/rouge/hack.rb @@ -3,17 +3,17 @@ module Rouge module Lexers - class HackAssembly < RegexLexer - tag 'hackassembly' + class HackTetris < RegexLexer + tag 'hacktetris' # Unique tag for your lexer filenames '*.hack', '*.asm' - mimetypes 'text/x-hackassembly' + mimetypes 'text/x-hacktetris' # Registers and predefined symbols - registers = %w(A D M AM AD AMD) + registers = %w(A D M AM AD AMD MD) # Added MD, AM, AD, etc. symbols = %w(SP LCL ARG THIS THAT SCREEN KBD R0 R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R13 R14 R15) # C-instruction components - dest = %w(A D M AM AD AMD) + dest = %w(A D M AM AD AMD MD) # Added MD, AM, AD, etc. comp = %w(0 1 -1 D A !D !A -D -A D+1 A+1 D-1 A-1 D+A D-A A-D D&A D\|A) jump = %w(JGT JEQ JGE JLT JNE JLE JMP) @@ -25,8 +25,9 @@ class HackAssembly < RegexLexer rule %r/\b(#{dest.join('|')})\b(?=\s*=)/, Keyword::Declaration # Destinations (e.g., D=) rule %r/\b(#{comp.join('|')})\b/, Keyword::Reserved # Computations (e.g., D+A) rule %r/\b(#{jump.join('|')})\b/, Keyword::Type # Jumps (e.g., JGT) - rule %r/\b(#{registers.join('|')})\b/, Name::Builtin # Registers (A, D, M) + rule %r/\b(#{registers.join('|')})\b/, Name::Builtin # Registers (A, D, M, MD, etc.) rule %r/\b(#{symbols.join('|')})\b/, Name::Constant # Predefined symbols (SP, R0) + rule %r/[+\-*\/!]/, Operator # Explicitly handle +, -, *, /, and ! rule %r/[=;]/, Operator rule %r/\d+/, Num rule %r/\s+/, Text::Whitespace diff --git a/_plugins/rouge/hdl.rb b/_plugins/rouge/hdl.rb index b6afff3..edc4e01 100644 --- a/_plugins/rouge/hdl.rb +++ b/_plugins/rouge/hdl.rb @@ -3,10 +3,10 @@ module Rouge module Lexers - class HDL < RegexLexer - tag 'hdl' + class HDLTetris < RegexLexer + tag 'hdltetris' filenames '*.hdl' - mimetypes 'text/x-hdl' + mimetypes 'text/x-hdltetris' # Keywords keywords = %w( diff --git a/_posts/2024-08-30-FromNand2Tetris.md b/_posts/2024-08-30-FromNand2Tetris.md index 945b64d..0b83a1f 100644 --- a/_posts/2024-08-30-FromNand2Tetris.md +++ b/_posts/2024-08-30-FromNand2Tetris.md @@ -55,7 +55,7 @@ and division of two integers, etc. It is a simple and elegant design. Here is my implementation in less than twenty lines of HDL code: -```hdl +```hdltetris * ALU (Arithmetic Logic Unit): * Computes out = one of the following functions: * 0, 1, -1, @@ -172,7 +172,7 @@ into the latter by an assembler. In this project, I wrote two simple assembly programs, one that blackens the screen if any key is pressed, and whitens it if the key is released; and one that multiples two numbers. Here is the first program: -```hackassembly +```hacktetris // This file is part of www.nand2tetris.org // and the book "The Elements of Computing Systems" // by Nisan and Schocken, MIT Press. @@ -309,7 +309,7 @@ we had to come up with a logic gate architecture that realizes the following beh Here is my rather simplistic implementation: -```hackassembly +```hdltetris CHIP CPU { IN inM[16], // M value input (M = contents of RAM[A])