diff --git a/compiler/__init__.py b/compiler/__init__.py
index 87c976c..fbde61b 100644
--- a/compiler/__init__.py
+++ b/compiler/__init__.py
@@ -1,12 +1,15 @@
from enum import Enum
import re
+import sys
+from html import escape
class Token(Enum):
KEYWORD = 1
SYMBOL = 2
IDENTIFIER = 3
- INT_CONST = 4
+ INTEGERCONSTANT = 4
STRING_CONST = 5
+ UNKNOWN = 6
class Keyword(Enum):
CLASS = 1
@@ -37,16 +40,24 @@ class JackAnalyzer:
class JackTokenizer:
- # KEYWORD_REGEXES='(class|constructor|function|method|field|static|var|int|char|boolean|void|true|false|null|this|let|do|if|else|while|return)'
-
- # SYMBOL_REGEXES = [
- # "{","}","\(","\)","]","["
- # ]
-
""" Returns the type of the current token """
def tokenType(self):
+ t = self.current_token()
+ if t in ['class','constructor','function','method','field','static','var','int','char','boolean','void','true','false','null','this','let','do','if','else','while','return']:
+ return Token.KEYWORD
+ elif re.compile("(\(|\)|\[|\]|,|\+|-|;|<|>|=|~|&|{|}|\*|\/|\||\.)").match(t):
+ return Token.SYMBOL
+ elif re.compile("\d+").match(t):
+ return Token.INTEGERCONSTANT
+ elif re.compile("\".*\"").match(t):
+ return Token.STRING_CONST
+ else:
+ return Token.IDENTIFIER
pass
+ def printable_token(self):
+ return escape(self.current_token(), True)
+
""" Returns the character which is the current token """
def symbol(self):
if self.tokenType() != Token.SYMBOL:
@@ -59,8 +70,8 @@ class JackTokenizer:
""" Returns the integer value of the current token """
def intVal(self):
- if self.tokenType() != Token.INT_CONST:
- raise RuntimeError("Should only be called when tokenType is INT_CONST")
+ if self.tokenType() != Token.INTEGERCONSTANT:
+ raise RuntimeError("Should only be called when tokenType is INTEGERCONSTANT")
return int(self.token)
""" Returns a list of tokens for that line """
@@ -102,25 +113,45 @@ class JackTokenizer:
if len(line) == 0:
return []
else:
- regex = re.compile("(class|constructor|function|method|field|static|var|int|char|boolean|void|true|false|null|this|let|do|if|else|while|return)|(\(|\)|\[|\]|,|\+|-|;|<|>|=|~|&|{|}|\*|\/|\|)")
+ regex = re.compile("(class|constructor|function|method|field|static|var|int|char|boolean|void|true|false|null|this|let|do|if|else|while|return)|(\(|\)|\[|\]|,|\+|-|;|<|>|=|~|&|{|}|\*|\/|\||\.)|\s+")
tokens = regex.split(line)
return [e.strip() for e in tokens if e != None and e.strip()!='']
+ def has_more_tokens(self):
+ return self.ptr < len(self.tokens)
+
+ def current_token(self):
+ return self.tokens[self.ptr]
+
def advance(self):
+ self.ptr += 1
+
+ def __init__(self, filename, print_xml=False):
+ self.ptr = 0
+ self.insideMultiLineComment = False
+ self.file = open(filename, 'r')
self.tokens = []
for line in self.file:
self.tokens += self.parse_line(line)
- print(self.tokens)
+ if(print_xml):
+ self.print_xml(self.xml_file(filename))
- def __init__(self, filename):
- self.insideMultiLineComment = False
- self.file = open(filename, 'r')
+ def xml_file(self, jack_file):
+ return jack_file + "T.xml"
+
+ def print_xml(self, xml_filename):
+ with open(xml_filename, 'w') as f:
+ f.write("\n")
+ while self.has_more_tokens():
+ f.write("<{type}> {value} {type}>\n".format(type=self.tokenType().name.lower(), value=self.printable_token()))
+ self.advance()
+ f.write("\n")
class CompilationEngine:
def __init__(self):
pass
if __name__ == '__main__':
- jt = JackTokenizer("../projects/10/Square/Square.jack")
- jt.advance()
+ jt = JackTokenizer(sys.argv[1], True)
+
diff --git a/projects/10/Square/Square.jackT.xml b/projects/10/Square/Square.jackT.xml
new file mode 100644
index 0000000..349bfb8
--- /dev/null
+++ b/projects/10/Square/Square.jackT.xml
@@ -0,0 +1,561 @@
+
+ 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
+ this
+ ;
+ }
+ method
+ void
+ dispose
+ (
+ )
+ {
+ do
+ Memory
+ .
+ deAlloc
+ (
+ this
+ )
+ ;
+ return
+ ;
+ }
+ method
+ void
+ draw
+ (
+ )
+ {
+ do
+ Screen
+ .
+ setColor
+ (
+ true
+ )
+ ;
+ do
+ Screen
+ .
+ drawRectangle
+ (
+ x
+ ,
+ y
+ ,
+ x
+ +
+ size
+ ,
+ y
+ +
+ size
+ )
+ ;
+ return
+ ;
+ }
+ method
+ void
+ erase
+ (
+ )
+ {
+ do
+ Screen
+ .
+ setColor
+ (
+ false
+ )
+ ;
+ do
+ Screen
+ .
+ drawRectangle
+ (
+ x
+ ,
+ y
+ ,
+ x
+ +
+ size
+ ,
+ y
+ +
+ size
+ )
+ ;
+ return
+ ;
+ }
+ method
+ void
+ incSize
+ (
+ )
+ {
+ if
+ (
+ (
+ (
+ y
+ +
+ size
+ )
+ <
+ 254
+ )
+ &
+ (
+ (
+ x
+ +
+ size
+ )
+ <
+ 510
+ )
+ )
+ {
+ do
+ erase
+ (
+ )
+ ;
+ let
+ size
+ =
+ size
+ +
+ 2
+ ;
+ do
+ draw
+ (
+ )
+ ;
+ }
+ return
+ ;
+ }
+ method
+ void
+ decSize
+ (
+ )
+ {
+ if
+ (
+ size
+ >
+ 2
+ )
+ {
+ do
+ erase
+ (
+ )
+ ;
+ let
+ size
+ =
+ size
+ -
+ 2
+ ;
+ do
+ draw
+ (
+ )
+ ;
+ }
+ return
+ ;
+ }
+ method
+ void
+ moveUp
+ (
+ )
+ {
+ if
+ (
+ y
+ >
+ 1
+ )
+ {
+ do
+ Screen
+ .
+ setColor
+ (
+ false
+ )
+ ;
+ do
+ Screen
+ .
+ drawRectangle
+ (
+ x
+ ,
+ (
+ y
+ +
+ size
+ )
+ -
+ 1
+ ,
+ x
+ +
+ size
+ ,
+ y
+ +
+ size
+ )
+ ;
+ let
+ y
+ =
+ y
+ -
+ 2
+ ;
+ do
+ Screen
+ .
+ setColor
+ (
+ true
+ )
+ ;
+ do
+ Screen
+ .
+ drawRectangle
+ (
+ x
+ ,
+ y
+ ,
+ x
+ +
+ size
+ ,
+ y
+ +
+ 1
+ )
+ ;
+ }
+ return
+ ;
+ }
+ method
+ void
+ moveDown
+ (
+ )
+ {
+ if
+ (
+ (
+ y
+ +
+ size
+ )
+ <
+ 254
+ )
+ {
+ do
+ Screen
+ .
+ setColor
+ (
+ false
+ )
+ ;
+ do
+ Screen
+ .
+ drawRectangle
+ (
+ x
+ ,
+ y
+ ,
+ x
+ +
+ size
+ ,
+ y
+ +
+ 1
+ )
+ ;
+ let
+ y
+ =
+ y
+ +
+ 2
+ ;
+ do
+ Screen
+ .
+ setColor
+ (
+ true
+ )
+ ;
+ do
+ Screen
+ .
+ drawRectangle
+ (
+ x
+ ,
+ (
+ y
+ +
+ size
+ )
+ -
+ 1
+ ,
+ x
+ +
+ size
+ ,
+ y
+ +
+ size
+ )
+ ;
+ }
+ return
+ ;
+ }
+ method
+ void
+ moveLeft
+ (
+ )
+ {
+ if
+ (
+ x
+ >
+ 1
+ )
+ {
+ do
+ Screen
+ .
+ setColor
+ (
+ false
+ )
+ ;
+ do
+ Screen
+ .
+ drawRectangle
+ (
+ (
+ x
+ +
+ size
+ )
+ -
+ 1
+ ,
+ y
+ ,
+ x
+ +
+ size
+ ,
+ y
+ +
+ size
+ )
+ ;
+ let
+ x
+ =
+ x
+ -
+ 2
+ ;
+ do
+ Screen
+ .
+ setColor
+ (
+ true
+ )
+ ;
+ do
+ Screen
+ .
+ drawRectangle
+ (
+ x
+ ,
+ y
+ ,
+ x
+ +
+ 1
+ ,
+ y
+ +
+ size
+ )
+ ;
+ }
+ return
+ ;
+ }
+ method
+ void
+ moveRight
+ (
+ )
+ {
+ if
+ (
+ (
+ x
+ +
+ size
+ )
+ <
+ 510
+ )
+ {
+ do
+ Screen
+ .
+ setColor
+ (
+ false
+ )
+ ;
+ do
+ Screen
+ .
+ drawRectangle
+ (
+ x
+ ,
+ y
+ ,
+ x
+ +
+ 1
+ ,
+ y
+ +
+ size
+ )
+ ;
+ let
+ x
+ =
+ x
+ +
+ 2
+ ;
+ do
+ Screen
+ .
+ setColor
+ (
+ true
+ )
+ ;
+ do
+ Screen
+ .
+ drawRectangle
+ (
+ (
+ x
+ +
+ size
+ )
+ -
+ 1
+ ,
+ y
+ ,
+ x
+ +
+ size
+ ,
+ y
+ +
+ size
+ )
+ ;
+ }
+ return
+ ;
+ }
+ }
+