Skip to content

Commit

Permalink
FromNand2Tetris: Change names of custom lexers and fix Hack lexer.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazkovac committed Jan 26, 2025
1 parent a4f4027 commit 1964946
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
13 changes: 7 additions & 6 deletions _plugins/rouge/hack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions _plugins/rouge/hdl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions _posts/2024-08-30-FromNand2Tetris.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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])
Expand Down

0 comments on commit 1964946

Please sign in to comment.