Skip to content

Commit

Permalink
Fix bug in jack lexer. Fix cli for compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
happytomatoe committed Oct 17, 2024
1 parent ce31d3a commit bf7acd0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { testRunner } from "./testrunner.js";
import { NodeFileSystemAdapter } from "@davidsouther/jiffies/lib/esm/fs_node.js";
import { FileSystem } from "@davidsouther/jiffies/lib/esm/fs.js";
import * as fsCore from "fs";
import { compile } from "@nand2tetris/simulator/jack/compiler.js";
import { compile } from "@nand2tetris/simulator/jack/anltr.compiler.js";

yargs(hideBin(process.argv))
.usage("$0 <cmd>")
Expand Down
19 changes: 8 additions & 11 deletions simulator/src/languages/grammars/JackLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ ELSE: 'else';
WHILE: 'while';
RETURN: 'return';


LBRACE: '{';
RBRACE: '}';
LPAREN: '(';
Expand All @@ -41,22 +40,20 @@ TILDE: '~';
LESS_THAN: '<';
GREATER_THAN: '>';

WS: [ \t\r\n]+ -> skip;
COMMENT: '/*' .*? '*/' -> skip;
LINE_COMMENT: '//' ~[\r\n]* -> skip;
WHITESPACE: [ \t\r\n\f]+ -> channel(HIDDEN);
BLOCK_COMMENT: '/*' .*? ('*/' | EOF) -> channel(HIDDEN);
LINE_COMMENT: '//' ~[\r\n]* -> channel(HIDDEN);

INTEGER_LITERAL: [0-9]+;
TRUE: 'true';
TRUE: 'true';
FALSE: 'false';
NULL_LITERAL: 'null';
THIS_LITERAL: 'this';

IDENTIFIER: [a-zA-Z_] [a-zA-Z0-9_]*;

STRING_LITERAL
: UnterminatedStringLiteral '"'
;
UnterminatedStringLiteral
: '"' ~["\\\r\n]*
;


STRING_LITERAL: '"' (~["\r\n])* '"';
// STRING_LITERAL: UnterminatedStringLiteral '"';
// UnterminatedStringLiteral: '"' ~["\\\r\n]*;
12 changes: 6 additions & 6 deletions simulator/src/languages/grammars/JackParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ options {
tokenVocab = JackLexer;
}
@header {
import { SubroutineScope, LocalSymbolTable } from "../symbol";
import { SubroutineScope, LocalSymbolTable } from "../symbol";
}

program: classDeclaration EOF;
Expand Down Expand Up @@ -34,7 +34,6 @@ rBrace: RBRACE;
varDeclaration:
VAR varType varNameInDeclaration (COMMA varNameInDeclaration)* SEMICOLON;
varNameInDeclaration: IDENTIFIER;
varName: IDENTIFIER;
statements: statement*;
statement:
letStatement
Expand Down Expand Up @@ -75,16 +74,17 @@ expression:
| expression binaryOperator expression
| groupedExpression;

groupedExpression: LPAREN expression RPAREN;
unaryOperation: unaryOperator expression;
arrayAccess: varName LBRACKET expression RBRACKET;

constant:
INTEGER_LITERAL
| STRING_LITERAL
| booleanLiteral
| NULL_LITERAL
| THIS_LITERAL;
varName: IDENTIFIER;
arrayAccess: varName LBRACKET expression RBRACKET;
unaryOperation: unaryOperator expression;
groupedExpression: LPAREN expression RPAREN;

booleanLiteral: TRUE | FALSE;
unaryOperator: TILDE | MINUS;
binaryOperator:
Expand Down

0 comments on commit bf7acd0

Please sign in to comment.