From 02cb984fece41785e74053f8d9bd4b98c18de9d8 Mon Sep 17 00:00:00 2001 From: Nemo Date: Mon, 6 Jul 2020 21:44:22 +0530 Subject: [PATCH] Fix empty expression tags, and add directory support --- compiler/__init__.py | 9 +- compiler/engine.py | 27 +- compiler/grammar.py | 13 +- .../10/ExpressionLessSquare/Main.jack.xml | 62 +- .../10/ExpressionLessSquare/Square.jack.xml | 967 ++++++++++++++++++ .../ExpressionLessSquare/SquareGame.jack.xml | 544 ++++++++++ 6 files changed, 1579 insertions(+), 43 deletions(-) create mode 100644 projects/10/ExpressionLessSquare/Square.jack.xml create mode 100644 projects/10/ExpressionLessSquare/SquareGame.jack.xml diff --git a/compiler/__init__.py b/compiler/__init__.py index 30539c6..975db70 100644 --- a/compiler/__init__.py +++ b/compiler/__init__.py @@ -1,6 +1,13 @@ from engine import Engine import sys +from pathlib import Path if __name__ == '__main__': - Engine(sys.argv[1]).compileClass() + p = Path(sys.argv[1]) + if p.is_dir(): + for f in p.glob("*.jack"): + print("compiling %s" % f) + Engine(f.as_posix()).compileClass() + elif p.is_file(): + Engine(p.as_posix()).compileClass() diff --git a/compiler/engine.py b/compiler/engine.py index c5f5b21..3595c4a 100644 --- a/compiler/engine.py +++ b/compiler/engine.py @@ -29,8 +29,8 @@ class Engine: def ZeroOrMany(self, grammarList, matchOnly): # print("ZOM called") ret = self.compile(grammarList[0], matchOnly) - if ret and matchOnly: - return True + if matchOnly: + return ret elif ret: # We now expect the whole of it for e in grammarList: @@ -39,7 +39,7 @@ class Engine: self.ZeroOrMany(grammarList, False) return True else: - return None + return False def write(self, line, end = "\n"): self.file.write(self.i*" " + line + end) @@ -54,6 +54,7 @@ class Engine: # We don't have to move the cursor for LL0 grammar if matchOnly: + assert(lookahead == 1) return lookup_keys in dictionary for _ in range(lookahead-1): @@ -85,7 +86,10 @@ class Engine: return True def ZeroOrOne(self, grammarTuple, matchOnly): - if self.compile(grammarTuple[0], True): + ret = self.compile(grammarTuple[0], True) + if matchOnly: + return ret + elif ret: for e in grammarTuple: self.compile(e) return True @@ -98,8 +102,8 @@ class Engine: current = self.atom() # We use in here to accomodate for bitmasks match = current in expected - if match and matchOnly: - return True + if matchOnly: + return match elif match: self.write(self.jt.xml_row(), end="") self.advance() @@ -118,20 +122,17 @@ class Engine: """ If you set matchOnly = true, the cursor will not move forward - if it is forced to move forward, it will instead RAISE AN ERROR + If it is forced to move forward (LL1 grammar for eg,) it will raise an error instead """ def compile(self, thing, matchOnly = False): - # TODO: OPEN TAGS if isinstance(thing, Element): - ret = False - if self.compile(thing.grammar[0], True): + ret = self.compile(thing.grammar[0], True) + if (matchOnly == False and ret) or thing.empty: self.open(thing) for e in thing.grammar: ret = self.compile(e) self.close(thing) - return ret - else: - return ret + return ret elif callable(thing): grammar = thing() return self.compile(grammar, matchOnly) diff --git a/compiler/grammar.py b/compiler/grammar.py index e5e898c..c519f9a 100644 --- a/compiler/grammar.py +++ b/compiler/grammar.py @@ -26,6 +26,7 @@ class Element: assert(type(grammar)==list) self.name = name self.grammar = grammar + self.empty = False def __repr__(self): return self.name @@ -51,8 +52,14 @@ CONSTANT = Atom.TRUE | Atom.FALSE|Atom.NULL|Atom.THIS """ Pseudo-element to help define subroutine declarations """ RETURN_TYPES= Atom.INT | Atom.CHAR|Atom.BOOLEAN|Atom.IDENTIFIER|Atom.VOID -# TODO: This is missing a lot of stuff -TERM = Element('term', [Atom.INTEGERCONSTANT | Atom.STRINGCONSTANT | Atom.TRUE | Atom.FALSE | Atom.IDENTIFIER]) +# TODO: This is missing the following: +# var [expression] +# subRoutineCall +# (expressions in parenthes) +# unaryOP TERM +TERM = Element('term', [ + Atom.INTEGERCONSTANT | Atom.STRINGCONSTANT | Atom.TRUE | Atom.FALSE | Atom.NULL| Atom.THIS | Atom.IDENTIFIER +]) EXPRESSION = Element('expression', [TERM, [OP, TERM]]) @@ -132,6 +139,8 @@ PARAMETER_LIST = Element('parameterList', [( [Atom.COMMA, Atom.INT | Atom.CHAR|Atom.BOOLEAN|Atom.IDENTIFIER, Atom.IDENTIFIER] )]) +EXPRESSIONLIST.empty = PARAMETER_LIST.empty = True + SUBROUTINEDEC = Element('subroutineDec', [ # (constructor | function | method) (void | type) subRoutineName '(' parameterList ')' # subroutineBody diff --git a/projects/10/ExpressionLessSquare/Main.jack.xml b/projects/10/ExpressionLessSquare/Main.jack.xml index f444332..f679c3c 100644 --- a/projects/10/ExpressionLessSquare/Main.jack.xml +++ b/projects/10/ExpressionLessSquare/Main.jack.xml @@ -13,6 +13,8 @@ void main ( + + ) { @@ -27,10 +29,10 @@ let game = - - game - + + game + ; @@ -40,6 +42,8 @@ . run ( + + ) ; @@ -49,6 +53,8 @@ . dispose ( + + ) ; @@ -65,6 +71,8 @@ void test ( + + ) { @@ -92,10 +100,10 @@ if ( - - i - + + i + ) { @@ -104,10 +112,10 @@ let s = - - i - + + i + ; @@ -115,10 +123,10 @@ let s = - - j - + + j + ; @@ -126,17 +134,17 @@ let a [ - - i - + + i + ] = - - j - + + j + ; @@ -149,10 +157,10 @@ let i = - - i - + + i + ; @@ -160,10 +168,10 @@ let j = - - j - + + j + ; @@ -171,10 +179,10 @@ let i = - - i - + + i + | j diff --git a/projects/10/ExpressionLessSquare/Square.jack.xml b/projects/10/ExpressionLessSquare/Square.jack.xml new file mode 100644 index 0000000..d98f181 --- /dev/null +++ b/projects/10/ExpressionLessSquare/Square.jack.xml @@ -0,0 +1,967 @@ + + class + Square + { + + field + int + x + , + y + ; + + + field + int + size + ; + + + constructor + Square + new + ( + + int + Ax + , + int + Ay + , + int + Asize + + ) + + { + + + let + x + = + + + Ax + + + ; + + + let + y + = + + + Ay + + + ; + + + let + size + = + + + Asize + + + ; + + + do + draw + ( + + + ) + ; + + + return + + + x + + + ; + + + } + + + + method + void + dispose + ( + + + ) + + { + + + do + Memory + . + deAlloc + ( + + + + this + + + + ) + ; + + + return + ; + + + } + + + + method + void + draw + ( + + + ) + + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + return + ; + + + } + + + + method + void + erase + ( + + + ) + + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + return + ; + + + } + + + + method + void + incSize + ( + + + ) + + { + + + if + ( + + + x + + + ) + { + + + do + erase + ( + + + ) + ; + + + let + size + = + + + size + + + ; + + + do + draw + ( + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + decSize + ( + + + ) + + { + + + if + ( + + + size + + + ) + { + + + do + erase + ( + + + ) + ; + + + let + size + = + + + size + + + ; + + + do + draw + ( + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveUp + ( + + + ) + + { + + + if + ( + + + y + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + y + = + + + y + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveDown + ( + + + ) + + { + + + if + ( + + + y + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + y + = + + + y + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveLeft + ( + + + ) + + { + + + if + ( + + + x + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + x + = + + + x + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveRight + ( + + + ) + + { + + + if + ( + + + x + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + x + = + + + x + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + } + diff --git a/projects/10/ExpressionLessSquare/SquareGame.jack.xml b/projects/10/ExpressionLessSquare/SquareGame.jack.xml new file mode 100644 index 0000000..11e02c7 --- /dev/null +++ b/projects/10/ExpressionLessSquare/SquareGame.jack.xml @@ -0,0 +1,544 @@ + + class + SquareGame + { + + field + Square + square + ; + + + field + int + direction + ; + + + constructor + SquareGame + new + ( + + + ) + + { + + + let + square + = + + + square + + + ; + + + let + direction + = + + + direction + + + ; + + + return + + + square + + + ; + + + } + + + + method + void + dispose + ( + + + ) + + { + + + do + square + . + dispose + ( + + + ) + ; + + + do + Memory + . + deAlloc + ( + + + + square + + + + ) + ; + + + return + ; + + + } + + + + method + void + moveSquare + ( + + + ) + + { + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveUp + ( + + + ) + ; + + + } + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveDown + ( + + + ) + ; + + + } + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveLeft + ( + + + ) + ; + + + } + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveRight + ( + + + ) + ; + + + } + + + do + Sys + . + wait + ( + + + + direction + + + + ) + ; + + + return + ; + + + } + + + + method + void + run + ( + + + ) + + { + + var + char + key + ; + + + var + boolean + exit + ; + + + + let + exit + = + + + key + + + ; + + + while + ( + + + exit + + + ) + { + + + while + ( + + + key + + + ) + { + + + let + key + = + + + key + + + ; + + + do + moveSquare + ( + + + ) + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + exit + = + + + exit + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + do + square + . + decSize + ( + + + ) + ; + + + } + + + if + ( + + + key + + + ) + { + + + do + square + . + incSize + ( + + + ) + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + exit + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + key + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + square + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + direction + + + ; + + + } + + + while + ( + + + key + + + ) + { + + + let + key + = + + + key + + + ; + + + do + moveSquare + ( + + + ) + ; + + + } + + + } + + + return + ; + + + } + + + } +