Skip to content

Commit

Permalink
FromNand2Tetris: Try using a custom HDL lexer.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazkovac committed Jan 26, 2025
1 parent bea6033 commit b1c4b74
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 3 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,6 @@ jekyll-archives:
permalinks:
tag: /tags/:name/
category: /categories/:name/

plugins:
- rouge
41 changes: 41 additions & 0 deletions _plugins/rouge/hdl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Rouge
module Lexers
class HDLCustom < RegexLexer
tag 'hdlcustom'
filenames '*.hdl'
mimetypes 'text/x-hdl'

# Keywords
keywords = %w(
CHIP IN OUT BUILTIN PARTS
)

# Built-in gates
builtins = %w(
Nand And Or Not Xor Mux DMux
Add16 Inc16 ALU Register RAM8 RAM64
)

# Pin directions and types
pins = %w(in out)

state :root do
rule %r(//.*), Comment::Single
rule %r(/\*), Comment::Multiline, :comment
rule %r/\b(#{keywords.join('|')})\b/, Keyword
rule %r/\b(#{builtins.join('|')})\b/, Name::Builtin
rule %r/\b(#{pins.join('|')})\b/, Keyword::Pseudo
rule %r/[a-zA-Z_]\w*/, Name::Variable
rule %r/\d+/, Num
rule %r/[\[\]\(\)\{\},;=]/, Punctuation
rule %r/\s+/, Text::Whitespace
end

state :comment do
rule %r/\*\//, Comment::Multiline, :pop!
rule %r/[^\*]+/, Comment::Multiline
rule %r/\*/, Comment::Multiline
end
end
end
end
2 changes: 1 addition & 1 deletion _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:

```verilog
```hdlcustom
* ALU (Arithmetic Logic Unit):
* Computes out = one of the following functions:
* 0, 1, -1,
Expand Down

0 comments on commit b1c4b74

Please sign in to comment.