Rename Elements to Atom
This commit is contained in:
parent
deeebda14d
commit
e862b318ba
|
@ -13,3 +13,9 @@ class Engine:
|
||||||
|
|
||||||
def xml_file(self, input_file):
|
def xml_file(self, input_file):
|
||||||
return input_file + ".xml"
|
return input_file + ".xml"
|
||||||
|
|
||||||
|
""" Throughout the compilation engine, we work using atoms"""
|
||||||
|
def atom(self):
|
||||||
|
token = self.jt.tokenType()
|
||||||
|
return Atom(token.value)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from keywords import Keyword
|
from keywords import Atom
|
||||||
|
|
||||||
"""
|
"""
|
||||||
The grammar is defined by the following constructs:
|
The grammar is defined by the following constructs:
|
||||||
|
@ -23,67 +23,67 @@ class Element:
|
||||||
self.grammar = grammar
|
self.grammar = grammar
|
||||||
self.terminal = terminal
|
self.terminal = terminal
|
||||||
|
|
||||||
TYPES = Element('type', Keyword.INT | Keyword.CHAR | Keyword.BOOLEAN | Keyword.IDENTIFIER, True)
|
TYPES = Element('type', Atom.INT | Atom.CHAR | Atom.BOOLEAN | Atom.IDENTIFIER, True)
|
||||||
|
|
||||||
CLASSVARDEC = Element('classVarDec', [
|
CLASSVARDEC = Element('classVarDec', [
|
||||||
# static|field type (, name)* ;
|
# static|field type (, name)* ;
|
||||||
Keyword.STATIC | Keyword.FIELD,
|
Atom.STATIC | Atom.FIELD,
|
||||||
TYPES,
|
TYPES,
|
||||||
[Keyword.COMMA, Keyword.IDENTIFIER],
|
[Atom.COMMA, Atom.IDENTIFIER],
|
||||||
Keyword.SEMICOLON
|
Atom.SEMICOLON
|
||||||
])
|
])
|
||||||
|
|
||||||
VARDEC = Element('varDec', [Keyword.VAR, TYPES, Keyword.IDENTIFIER,
|
VARDEC = Element('varDec', [Atom.VAR, TYPES, Atom.IDENTIFIER,
|
||||||
[Keyword.COMMA, Keyword.IDENTIFIER],
|
[Atom.COMMA, Atom.IDENTIFIER],
|
||||||
Keyword.SEMICOLON
|
Atom.SEMICOLON
|
||||||
])
|
])
|
||||||
UNARY_OP = Element('unaryOp', Keyword.NOT | Keyword.MINUS, True)
|
UNARY_OP = Element('unaryOp', Atom.NOT | Atom.MINUS, True)
|
||||||
|
|
||||||
CONSTANT = Element('KeywordConstant', Keyword.TRUE | Keyword.FALSE|Keyword.NULL|Keyword.THIS, True)
|
CONSTANT = Element('KeywordConstant', Atom.TRUE | Atom.FALSE|Atom.NULL|Atom.THIS, True)
|
||||||
|
|
||||||
TERM = Element('term', Keyword.INTEGERCONSTANT | Keyword.STRINGCONSTANT | Keyword.TRUE | Keyword.FALSE | Keyword.IDENTIFIER)
|
TERM = Element('term', Atom.INTEGERCONSTANT | Atom.STRINGCONSTANT | Atom.TRUE | Atom.FALSE | Atom.IDENTIFIER)
|
||||||
|
|
||||||
OP = Element('op', Keyword.PLUS | Keyword.MINUS | Keyword.MUL | Keyword.DIV | Keyword.AND | Keyword.OR | Keyword.GT | Keyword.LT | Keyword.EQ, True)
|
OP = Element('op', Atom.PLUS | Atom.MINUS | Atom.MUL | Atom.DIV | Atom.AND | Atom.OR | Atom.GT | Atom.LT | Atom.EQ, True)
|
||||||
|
|
||||||
EXPRESSION = Element('expression', [TERM, [OP, TERM]])
|
EXPRESSION = Element('expression', [TERM, [OP, TERM]])
|
||||||
|
|
||||||
EXPRESSIONLIST = Element('expressionList', (EXPRESSION, [Keyword.COMMA, EXPRESSION]))
|
EXPRESSIONLIST = Element('expressionList', (EXPRESSION, [Atom.COMMA, EXPRESSION]))
|
||||||
|
|
||||||
SUBROUTINE_CALL = Element('subroutineCall', {
|
SUBROUTINE_CALL = Element('subroutineCall', {
|
||||||
(Keyword.IDENTIFIER, Keyword.PARAN_OPEN): [
|
(Atom.IDENTIFIER, Atom.PARAN_OPEN): [
|
||||||
EXPRESSIONLIST,
|
EXPRESSIONLIST,
|
||||||
Keyword.PARAN_CLOSE,
|
Atom.PARAN_CLOSE,
|
||||||
],
|
],
|
||||||
(Keyword.IDENTIFIER, Keyword.DOT): [
|
(Atom.IDENTIFIER, Atom.DOT): [
|
||||||
Keyword.IDENTIFIER,
|
Atom.IDENTIFIER,
|
||||||
Keyword.PARAN_OPEN,
|
Atom.PARAN_OPEN,
|
||||||
EXPRESSIONLIST,
|
EXPRESSIONLIST,
|
||||||
Keyword.PARAN_CLOSE
|
Atom.PARAN_CLOSE
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
STATEMENT = Element('statement', {
|
STATEMENT = Element('statement', {
|
||||||
(Keyword.LET): [Keyword.IDENTIFIER, (Keyword.SQUARE_OPEN, EXPRESSION, Keyword.SQUARE_CLOSE)],
|
(Atom.LET): [Atom.IDENTIFIER, (Atom.SQUARE_OPEN, EXPRESSION, Atom.SQUARE_CLOSE)],
|
||||||
(Keyword.IF): [
|
(Atom.IF): [
|
||||||
Keyword.PARAN_OPEN,
|
Atom.PARAN_OPEN,
|
||||||
EXPRESSION,
|
EXPRESSION,
|
||||||
Keyword.PARAN_CLOSE,
|
Atom.PARAN_CLOSE,
|
||||||
Keyword.BRACE_OPEN,
|
Atom.BRACE_OPEN,
|
||||||
lambda: STATEMENTS,
|
lambda: STATEMENTS,
|
||||||
Keyword.BRACE_CLOSE,
|
Atom.BRACE_CLOSE,
|
||||||
# This is the tricky one
|
# This is the tricky one
|
||||||
( Keyword.ELSE, Keyword.BRACE_OPEN, lambda:STATEMENT, Keyword.BRACE_CLOSE)
|
( Atom.ELSE, Atom.BRACE_OPEN, lambda:STATEMENT, Atom.BRACE_CLOSE)
|
||||||
],
|
],
|
||||||
(Keyword.WHILE): [
|
(Atom.WHILE): [
|
||||||
Keyword.PARAN_OPEN,
|
Atom.PARAN_OPEN,
|
||||||
EXPRESSION,
|
EXPRESSION,
|
||||||
Keyword.PARAN_CLOSE,
|
Atom.PARAN_CLOSE,
|
||||||
Keyword.BRACE_OPEN,
|
Atom.BRACE_OPEN,
|
||||||
lambda: STATEMENTS,
|
lambda: STATEMENTS,
|
||||||
Keyword.BRACE_CLOSE,
|
Atom.BRACE_CLOSE,
|
||||||
],
|
],
|
||||||
(Keyword.DO): SUBROUTINE_CALL,
|
(Atom.DO): SUBROUTINE_CALL,
|
||||||
(Keyword.RETURN): [(EXPRESSION), Keyword.SEMICOLON]
|
(Atom.RETURN): [(EXPRESSION), Atom.SEMICOLON]
|
||||||
})
|
})
|
||||||
|
|
||||||
STATEMENTS = Element('statements', [STATEMENT])
|
STATEMENTS = Element('statements', [STATEMENT])
|
||||||
|
@ -96,7 +96,7 @@ SUBROUTINE_BODY = Element('subroutineBody', [
|
||||||
])
|
])
|
||||||
|
|
||||||
""" Pseudo-element to help define subroutine declarations """
|
""" Pseudo-element to help define subroutine declarations """
|
||||||
RETURN_TYPES= Keyword.INT | Keyword.CHAR|Keyword.BOOLEAN|Keyword.IDENTIFIER|Keyword.VOID
|
RETURN_TYPES= Atom.INT | Atom.CHAR|Atom.BOOLEAN|Atom.IDENTIFIER|Atom.VOID
|
||||||
|
|
||||||
# Parameter List =
|
# Parameter List =
|
||||||
# (
|
# (
|
||||||
|
@ -105,34 +105,34 @@ RETURN_TYPES= Keyword.INT | Keyword.CHAR|Keyword.BOOLEAN|Keyword.IDENTIFIER|Keyw
|
||||||
# we use tuples for zero OR one of a sequence
|
# we use tuples for zero OR one of a sequence
|
||||||
PARAMETER_LIST = Element('parameterList', (
|
PARAMETER_LIST = Element('parameterList', (
|
||||||
TYPES,
|
TYPES,
|
||||||
Keyword.IDENTIFIER,
|
Atom.IDENTIFIER,
|
||||||
[Keyword.COMMA, TYPES, Keyword.IDENTIFIER]
|
[Atom.COMMA, TYPES, Atom.IDENTIFIER]
|
||||||
))
|
))
|
||||||
|
|
||||||
SUBROUTINEDEC = Element('subroutineDec', [
|
SUBROUTINEDEC = Element('subroutineDec', [
|
||||||
# (constructor | function | method) (void | type) subRoutineName '(' parameterList ')'
|
# (constructor | function | method) (void | type) subRoutineName '(' parameterList ')'
|
||||||
# subroutineBody
|
# subroutineBody
|
||||||
Keyword.CONSTRUCTOR | Keyword.FUNCTION | Keyword.METHOD,
|
Atom.CONSTRUCTOR | Atom.FUNCTION | Atom.METHOD,
|
||||||
RETURN_TYPES,
|
RETURN_TYPES,
|
||||||
Keyword.IDENTIFIER,
|
Atom.IDENTIFIER,
|
||||||
Keyword.PARAN_OPEN,
|
Atom.PARAN_OPEN,
|
||||||
PARAMETER_LIST,
|
PARAMETER_LIST,
|
||||||
Keyword.PARAN_CLOSE,
|
Atom.PARAN_CLOSE,
|
||||||
# Subroutine Body {
|
# Subroutine Body {
|
||||||
Keyword.BRACE_OPEN,
|
Atom.BRACE_OPEN,
|
||||||
SUBROUTINE_BODY,
|
SUBROUTINE_BODY,
|
||||||
Keyword.BRACE_CLOSE,
|
Atom.BRACE_CLOSE,
|
||||||
])
|
])
|
||||||
|
|
||||||
CLASS = Element('class', [
|
CLASS = Element('class', [
|
||||||
# class className {
|
# class className {
|
||||||
Keyword.CLASS,
|
Atom.CLASS,
|
||||||
Keyword.IDENTIFIER,
|
Atom.IDENTIFIER,
|
||||||
Keyword.BRACE_OPEN,
|
Atom.BRACE_OPEN,
|
||||||
# class Variable Declarations (one or more) = list
|
# class Variable Declarations (one or more) = list
|
||||||
CLASSVARDEC,
|
CLASSVARDEC,
|
||||||
# subroutine declarations (one or more) = list
|
# subroutine declarations (one or more) = list
|
||||||
SUBROUTINEDEC,
|
SUBROUTINEDEC,
|
||||||
# }
|
# }
|
||||||
Keyword.BRACE_CLOSE
|
Atom.BRACE_CLOSE
|
||||||
])
|
])
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
from enum import Enum,Flag,auto
|
from enum import IntFlag,auto
|
||||||
|
|
||||||
class Keyword(Flag):
|
""" Super class for everything """
|
||||||
|
class Atom(IntFlag):
|
||||||
|
# Keywords
|
||||||
CLASS = auto()
|
CLASS = auto()
|
||||||
METHOD = auto()
|
METHOD = auto()
|
||||||
FUNCTION = auto()
|
FUNCTION = auto()
|
||||||
|
@ -47,31 +49,54 @@ class Keyword(Flag):
|
||||||
INTEGERCONSTANT = auto()
|
INTEGERCONSTANT = auto()
|
||||||
STRINGCONSTANT = auto()
|
STRINGCONSTANT = auto()
|
||||||
|
|
||||||
class Symbol(Flag):
|
class Keyword(IntFlag):
|
||||||
# Symbols Start here
|
CLASS = Atom.CLASS.value
|
||||||
BRACE_OPEN = Keyword.BRACE_OPEN
|
METHOD = Atom.METHOD.value
|
||||||
BRACE_CLOSE = Keyword.BRACE_CLOSE
|
FUNCTION = Atom.FUNCTION.value
|
||||||
PARAN_OPEN = Keyword.PARAN_OPEN
|
CONSTRUCTOR = Atom.CONSTRUCTOR.value
|
||||||
PARAN_CLOSE = Keyword.PARAN_CLOSE
|
INT = Atom.INT.value
|
||||||
SQUARE_OPEN = Keyword.SQUARE_OPEN
|
BOOLEAN = Atom.BOOLEAN.value
|
||||||
SQUARE_CLOSE = Keyword.SQUARE_CLOSE
|
CHAR = Atom.CHAR.value
|
||||||
DOT = Keyword.DOT
|
VOID = Atom.VOID.value
|
||||||
SEMICOLON = Keyword.SEMICOLON
|
VAR = Atom.VAR.value
|
||||||
PLUS = Keyword.PLUS
|
STATIC = Atom.STATIC.value
|
||||||
MINUS = Keyword.MINUS
|
FIELD = Atom.FIELD.value
|
||||||
MUL = Keyword.MUL
|
LET = Atom.LET.value
|
||||||
DIV = Keyword.DIV
|
DO = Atom.DO.value
|
||||||
AND = Keyword.AND
|
IF = Atom.IF.value
|
||||||
OR = Keyword.OR
|
ELSE = Atom.ELSE.value
|
||||||
LT = Keyword.LT
|
WHILE = Atom.WHILE.value
|
||||||
GT = Keyword.GT
|
RETURN = Atom.RETURN.value
|
||||||
EQ = Keyword.EQ
|
TRUE = Atom.TRUE.value
|
||||||
NOT = Keyword.NOT
|
FALSE = Atom.FALSE.value
|
||||||
COMMA = Keyword.COMMA
|
NULL = Atom.NULL.value
|
||||||
|
THIS = Atom.THIS.value
|
||||||
|
|
||||||
class Token(Flag):
|
class Symbol(IntFlag):
|
||||||
KEYWORD = auto()
|
# Symbols Start here
|
||||||
|
BRACE_OPEN = Atom.BRACE_OPEN.value
|
||||||
|
BRACE_CLOSE = Atom.BRACE_CLOSE.value
|
||||||
|
PARAN_OPEN = Atom.PARAN_OPEN.value
|
||||||
|
PARAN_CLOSE = Atom.PARAN_CLOSE.value
|
||||||
|
SQUARE_OPEN = Atom.SQUARE_OPEN.value
|
||||||
|
SQUARE_CLOSE = Atom.SQUARE_CLOSE.value
|
||||||
|
DOT = Atom.DOT.value
|
||||||
|
SEMICOLON = Atom.SEMICOLON.value
|
||||||
|
PLUS = Atom.PLUS.value
|
||||||
|
MINUS = Atom.MINUS.value
|
||||||
|
MUL = Atom.MUL.value
|
||||||
|
DIV = Atom.DIV.value
|
||||||
|
AND = Atom.AND.value
|
||||||
|
OR = Atom.OR.value
|
||||||
|
LT = Atom.LT.value
|
||||||
|
GT = Atom.GT.value
|
||||||
|
EQ = Atom.EQ.value
|
||||||
|
NOT = Atom.NOT.value
|
||||||
|
COMMA = Atom.COMMA.value
|
||||||
|
|
||||||
|
class Token(IntFlag):
|
||||||
|
IDENTIFIER = Atom.IDENTIFIER.value
|
||||||
|
INTEGERCONSTANT = Atom.INTEGERCONSTANT.value
|
||||||
|
STRINGCONSTANT = Atom.STRINGCONSTANT.value
|
||||||
SYMBOL = auto()
|
SYMBOL = auto()
|
||||||
IDENTIFIER = Keyword.IDENTIFIER
|
KEYWORD = auto()
|
||||||
INTEGERCONSTANT = Keyword.INTEGERCONSTANT
|
|
||||||
STRINGCONSTANT = Keyword.STRINGCONSTANT
|
|
||||||
|
|
Loading…
Reference in New Issue