Rename Elements to Atom

master
Nemo 2 years ago
parent deeebda14d
commit e862b318ba
  1. 6
      compiler/engine.py
  2. 92
      compiler/grammar.py
  3. 83
      compiler/keywords.py

@ -13,3 +13,9 @@ class Engine:
def xml_file(self, input_file):
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:
@ -23,67 +23,67 @@ class Element:
self.grammar = grammar
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', [
# static|field type (, name)* ;
Keyword.STATIC | Keyword.FIELD,
Atom.STATIC | Atom.FIELD,
TYPES,
[Keyword.COMMA, Keyword.IDENTIFIER],
Keyword.SEMICOLON
[Atom.COMMA, Atom.IDENTIFIER],
Atom.SEMICOLON
])
VARDEC = Element('varDec', [Keyword.VAR, TYPES, Keyword.IDENTIFIER,
[Keyword.COMMA, Keyword.IDENTIFIER],
Keyword.SEMICOLON
VARDEC = Element('varDec', [Atom.VAR, TYPES, Atom.IDENTIFIER,
[Atom.COMMA, Atom.IDENTIFIER],
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]])
EXPRESSIONLIST = Element('expressionList', (EXPRESSION, [Keyword.COMMA, EXPRESSION]))
EXPRESSIONLIST = Element('expressionList', (EXPRESSION, [Atom.COMMA, EXPRESSION]))
SUBROUTINE_CALL = Element('subroutineCall', {
(Keyword.IDENTIFIER, Keyword.PARAN_OPEN): [
(Atom.IDENTIFIER, Atom.PARAN_OPEN): [
EXPRESSIONLIST,
Keyword.PARAN_CLOSE,
Atom.PARAN_CLOSE,
],
(Keyword.IDENTIFIER, Keyword.DOT): [
Keyword.IDENTIFIER,
Keyword.PARAN_OPEN,
(Atom.IDENTIFIER, Atom.DOT): [
Atom.IDENTIFIER,
Atom.PARAN_OPEN,
EXPRESSIONLIST,
Keyword.PARAN_CLOSE
Atom.PARAN_CLOSE
]
})
STATEMENT = Element('statement', {
(Keyword.LET): [Keyword.IDENTIFIER, (Keyword.SQUARE_OPEN, EXPRESSION, Keyword.SQUARE_CLOSE)],
(Keyword.IF): [
Keyword.PARAN_OPEN,
(Atom.LET): [Atom.IDENTIFIER, (Atom.SQUARE_OPEN, EXPRESSION, Atom.SQUARE_CLOSE)],
(Atom.IF): [
Atom.PARAN_OPEN,
EXPRESSION,
Keyword.PARAN_CLOSE,
Keyword.BRACE_OPEN,
Atom.PARAN_CLOSE,
Atom.BRACE_OPEN,
lambda: STATEMENTS,
Keyword.BRACE_CLOSE,
Atom.BRACE_CLOSE,
# 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): [
Keyword.PARAN_OPEN,
(Atom.WHILE): [
Atom.PARAN_OPEN,
EXPRESSION,
Keyword.PARAN_CLOSE,
Keyword.BRACE_OPEN,
Atom.PARAN_CLOSE,
Atom.BRACE_OPEN,
lambda: STATEMENTS,
Keyword.BRACE_CLOSE,
Atom.BRACE_CLOSE,
],
(Keyword.DO): SUBROUTINE_CALL,
(Keyword.RETURN): [(EXPRESSION), Keyword.SEMICOLON]
(Atom.DO): SUBROUTINE_CALL,
(Atom.RETURN): [(EXPRESSION), Atom.SEMICOLON]
})
STATEMENTS = Element('statements', [STATEMENT])
@ -96,7 +96,7 @@ SUBROUTINE_BODY = Element('subroutineBody', [
])
""" 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 =
# (
@ -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
PARAMETER_LIST = Element('parameterList', (
TYPES,
Keyword.IDENTIFIER,
[Keyword.COMMA, TYPES, Keyword.IDENTIFIER]
Atom.IDENTIFIER,
[Atom.COMMA, TYPES, Atom.IDENTIFIER]
))
SUBROUTINEDEC = Element('subroutineDec', [
# (constructor | function | method) (void | type) subRoutineName '(' parameterList ')'
# subroutineBody
Keyword.CONSTRUCTOR | Keyword.FUNCTION | Keyword.METHOD,
Atom.CONSTRUCTOR | Atom.FUNCTION | Atom.METHOD,
RETURN_TYPES,
Keyword.IDENTIFIER,
Keyword.PARAN_OPEN,
Atom.IDENTIFIER,
Atom.PARAN_OPEN,
PARAMETER_LIST,
Keyword.PARAN_CLOSE,
Atom.PARAN_CLOSE,
# Subroutine Body {
Keyword.BRACE_OPEN,
Atom.BRACE_OPEN,
SUBROUTINE_BODY,
Keyword.BRACE_CLOSE,
Atom.BRACE_CLOSE,
])
CLASS = Element('class', [
# class className {
Keyword.CLASS,
Keyword.IDENTIFIER,
Keyword.BRACE_OPEN,
Atom.CLASS,
Atom.IDENTIFIER,
Atom.BRACE_OPEN,
# class Variable Declarations (one or more) = list
CLASSVARDEC,
# subroutine declarations (one or more) = list
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()
METHOD = auto()
FUNCTION = auto()
@ -47,31 +49,54 @@ class Keyword(Flag):
INTEGERCONSTANT = auto()
STRINGCONSTANT = auto()
class Symbol(Flag):
# Symbols Start here
BRACE_OPEN = Keyword.BRACE_OPEN
BRACE_CLOSE = Keyword.BRACE_CLOSE
PARAN_OPEN = Keyword.PARAN_OPEN
PARAN_CLOSE = Keyword.PARAN_CLOSE
SQUARE_OPEN = Keyword.SQUARE_OPEN
SQUARE_CLOSE = Keyword.SQUARE_CLOSE
DOT = Keyword.DOT
SEMICOLON = Keyword.SEMICOLON
PLUS = Keyword.PLUS
MINUS = Keyword.MINUS
MUL = Keyword.MUL
DIV = Keyword.DIV
AND = Keyword.AND
OR = Keyword.OR
LT = Keyword.LT
GT = Keyword.GT
EQ = Keyword.EQ
NOT = Keyword.NOT
COMMA = Keyword.COMMA
class Keyword(IntFlag):
CLASS = Atom.CLASS.value
METHOD = Atom.METHOD.value
FUNCTION = Atom.FUNCTION.value
CONSTRUCTOR = Atom.CONSTRUCTOR.value
INT = Atom.INT.value
BOOLEAN = Atom.BOOLEAN.value
CHAR = Atom.CHAR.value
VOID = Atom.VOID.value
VAR = Atom.VAR.value
STATIC = Atom.STATIC.value
FIELD = Atom.FIELD.value
LET = Atom.LET.value
DO = Atom.DO.value
IF = Atom.IF.value
ELSE = Atom.ELSE.value
WHILE = Atom.WHILE.value
RETURN = Atom.RETURN.value
TRUE = Atom.TRUE.value
FALSE = Atom.FALSE.value
NULL = Atom.NULL.value
THIS = Atom.THIS.value
class Token(Flag):
KEYWORD = auto()
SYMBOL = auto()
IDENTIFIER = Keyword.IDENTIFIER
INTEGERCONSTANT = Keyword.INTEGERCONSTANT
STRINGCONSTANT = Keyword.STRINGCONSTANT
class Symbol(IntFlag):
# 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()
KEYWORD = auto()

Loading…
Cancel
Save