2020-07-08 14:26:28 +00:00
|
|
|
from constants import Atom
|
2020-06-18 12:51:06 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
The grammar is defined by the following constructs:
|
|
|
|
|
2020-07-06 11:51:13 +00:00
|
|
|
The top level object is called GRAMMAR, which is the grammar for a class.
|
|
|
|
It is a instance of the Element class
|
|
|
|
The element class contains a grammar element, which is always defined as a list
|
|
|
|
for an element class.
|
2020-06-18 12:51:06 +00:00
|
|
|
|
|
|
|
Inside this list, each element can be any of the following:
|
|
|
|
|
|
|
|
- a token (denoted by a Keyword enum)
|
|
|
|
- a bitwise mask of the Keyword enum to denote multiple possibilities
|
|
|
|
- Another list, to denote zero-or-more of a inner-sequence
|
|
|
|
- A tuple, to denote zero-or-one of a inner-sequence
|
2020-06-24 12:42:36 +00:00
|
|
|
- A lambda denotes a non-terminal part of the grammar
|
2020-06-18 12:51:06 +00:00
|
|
|
|
|
|
|
This is basically an attempt to translate Figure 10.5 from the book into
|
|
|
|
a Python structure.
|
|
|
|
|
|
|
|
"""
|
2020-07-06 21:53:06 +00:00
|
|
|
|
|
|
|
class Sequence(list):
|
|
|
|
def first(self):
|
|
|
|
return self[0]
|
|
|
|
|
2020-07-06 09:06:41 +00:00
|
|
|
class Element:
|
2020-07-06 11:51:13 +00:00
|
|
|
# Usually I avoid inverted boolean variable names, but this is much cleaner
|
2020-07-06 10:57:19 +00:00
|
|
|
def __init__(self, name, grammar):
|
2020-07-06 21:53:06 +00:00
|
|
|
# Since Any derives from list, this ought to work
|
|
|
|
assert(isinstance(grammar, list) or isinstance(grammar, dict))
|
2020-07-06 09:06:41 +00:00
|
|
|
self.name = name
|
|
|
|
self.grammar = grammar
|
2020-07-06 16:14:22 +00:00
|
|
|
self.empty = False
|
2020-06-18 12:51:06 +00:00
|
|
|
|
2020-07-06 21:53:06 +00:00
|
|
|
def first(self):
|
|
|
|
if isinstance(self.grammar, list):
|
|
|
|
return self.grammar[0]
|
|
|
|
elif isinstance(self.grammar, dict):
|
|
|
|
return list(self.grammar.keys())[0]
|
|
|
|
|
2020-07-06 15:42:00 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return self.name
|
|
|
|
|
2020-07-06 21:53:06 +00:00
|
|
|
CLASSVARDEC = Element('classVarDec', Sequence([
|
2020-07-06 09:06:41 +00:00
|
|
|
# static|field type (, name)* ;
|
2020-07-06 09:30:36 +00:00
|
|
|
Atom.STATIC | Atom.FIELD,
|
2020-07-06 11:51:13 +00:00
|
|
|
Atom.INT | Atom.CHAR | Atom.BOOLEAN | Atom.IDENTIFIER,
|
|
|
|
Atom.IDENTIFIER,
|
2020-07-06 21:53:06 +00:00
|
|
|
# Zero or one of these
|
2020-07-06 09:30:36 +00:00
|
|
|
[Atom.COMMA, Atom.IDENTIFIER],
|
|
|
|
Atom.SEMICOLON
|
2020-07-06 21:53:06 +00:00
|
|
|
]))
|
2020-07-06 09:06:41 +00:00
|
|
|
|
2020-07-06 21:53:06 +00:00
|
|
|
VARDEC = Element('varDec', Sequence([
|
|
|
|
Atom.VAR, Atom.INT | Atom.CHAR | Atom.BOOLEAN | Atom.IDENTIFIER, Atom.IDENTIFIER,
|
|
|
|
# Zero or one of these
|
2020-07-06 09:30:36 +00:00
|
|
|
[Atom.COMMA, Atom.IDENTIFIER],
|
|
|
|
Atom.SEMICOLON
|
2020-07-06 21:53:06 +00:00
|
|
|
]))
|
2020-06-18 12:51:06 +00:00
|
|
|
|
2020-07-06 21:53:06 +00:00
|
|
|
# Since these are not a non-terminal, we can just write it as a constant
|
2020-07-06 11:51:13 +00:00
|
|
|
OP = Atom.PLUS | Atom.MINUS | Atom.MUL | Atom.DIV | Atom.AND | Atom.OR | Atom.GT | Atom.LT | Atom.EQ
|
|
|
|
UNARY_OP = Atom.NOT | Atom.MINUS
|
2020-07-06 21:53:06 +00:00
|
|
|
CONSTANT = Atom.TRUE | Atom.FALSE | Atom.NULL | Atom.THIS
|
2020-07-06 11:51:13 +00:00
|
|
|
""" Pseudo-element to help define subroutine declarations """
|
2020-07-06 21:53:06 +00:00
|
|
|
RETURN_TYPES= Atom.INT | Atom.CHAR | Atom.BOOLEAN | Atom.IDENTIFIER | Atom.VOID
|
|
|
|
|
|
|
|
# This is a flattened version of the Term structure
|
|
|
|
TERM = Element('term', {
|
|
|
|
(Atom.INTEGERCONSTANT,): None,
|
|
|
|
(Atom.STRINGCONSTANT,): None,
|
|
|
|
(Atom.TRUE,): None,
|
|
|
|
(Atom.FALSE,): None,
|
|
|
|
(Atom.NULL,): None,
|
|
|
|
(Atom.THIS,): None,
|
|
|
|
# unaryOp TERM
|
|
|
|
(Atom.NOT,): Sequence([lambda: TERM]),
|
|
|
|
(Atom.MINUS,): Sequence([lambda: TERM]),
|
|
|
|
# (expression)
|
|
|
|
(Atom.PAREN_OPEN,): Sequence([lambda: EXPRESSION, Atom.PAREN_CLOSE]),
|
|
|
|
(Atom.IDENTIFIER,): {
|
|
|
|
# array lookup
|
|
|
|
(Atom.SQUARE_OPEN,): Sequence([lambda: EXPRESSION, Atom.SQUARE_CLOSE]),
|
|
|
|
# Subroutine call, but with class name
|
|
|
|
(Atom.DOT,): Sequence([
|
|
|
|
Atom.IDENTIFIER,
|
|
|
|
Atom.PAREN_OPEN,
|
|
|
|
lambda: EXPRESSIONLIST,
|
|
|
|
Atom.PAREN_CLOSE
|
|
|
|
]),
|
|
|
|
# Subroutine call, but to same class
|
|
|
|
(Atom.PAREN_OPEN,): Sequence([
|
|
|
|
lambda: EXPRESSIONLIST,
|
|
|
|
Atom.PAREN_CLOSE
|
|
|
|
])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
EXPRESSION = Element('expression', Sequence([TERM, [OP, TERM]]))
|
|
|
|
|
|
|
|
EXPRESSIONLIST = Element('expressionList', Sequence([
|
|
|
|
(EXPRESSION, [Atom.COMMA, EXPRESSION])
|
|
|
|
]))
|
|
|
|
|
|
|
|
SUBROUTINE_CALL = [
|
|
|
|
(Atom.IDENTIFIER, Atom.DOT),
|
|
|
|
Atom.IDENTIFIER,
|
|
|
|
Atom.PAREN_OPEN,
|
|
|
|
EXPRESSIONLIST,
|
|
|
|
Atom.PAREN_CLOSE
|
|
|
|
]
|
|
|
|
|
|
|
|
DO_STATEMENT = Element('doStatement', Sequence([SUBROUTINE_CALL,Atom.SEMICOLON]))
|
|
|
|
|
|
|
|
LET_STATEMENT = Element('letStatement', Sequence([
|
2020-07-06 15:42:00 +00:00
|
|
|
Atom.IDENTIFIER,
|
|
|
|
(Atom.SQUARE_OPEN, EXPRESSION, Atom.SQUARE_CLOSE),
|
|
|
|
Atom.EQ,
|
|
|
|
EXPRESSION,
|
|
|
|
Atom.SEMICOLON
|
2020-07-06 21:53:06 +00:00
|
|
|
]))
|
2020-07-06 11:51:13 +00:00
|
|
|
|
2020-07-06 21:53:06 +00:00
|
|
|
IF_STATEMENT = Element('ifStatement', Sequence([
|
2020-07-06 15:42:00 +00:00
|
|
|
Atom.PAREN_OPEN,
|
2020-07-06 11:51:13 +00:00
|
|
|
EXPRESSION,
|
2020-07-06 15:42:00 +00:00
|
|
|
Atom.PAREN_CLOSE,
|
2020-07-06 11:51:13 +00:00
|
|
|
Atom.BRACE_OPEN,
|
|
|
|
lambda: STATEMENTS,
|
|
|
|
Atom.BRACE_CLOSE,
|
|
|
|
# This is the tricky one
|
2020-07-06 15:42:00 +00:00
|
|
|
( Atom.ELSE, Atom.BRACE_OPEN, lambda:STATEMENTS, Atom.BRACE_CLOSE)
|
2020-07-06 21:53:06 +00:00
|
|
|
]))
|
2020-07-06 11:51:13 +00:00
|
|
|
|
2020-07-06 21:53:06 +00:00
|
|
|
WHILE_STATEMENT = Element('whileStatement', Sequence([
|
2020-07-06 15:42:00 +00:00
|
|
|
Atom.PAREN_OPEN,
|
2020-07-06 11:51:13 +00:00
|
|
|
EXPRESSION,
|
2020-07-06 15:42:00 +00:00
|
|
|
Atom.PAREN_CLOSE,
|
2020-07-06 11:51:13 +00:00
|
|
|
Atom.BRACE_OPEN,
|
|
|
|
lambda: STATEMENTS,
|
|
|
|
Atom.BRACE_CLOSE,
|
2020-07-06 21:53:06 +00:00
|
|
|
]))
|
2020-07-06 11:51:13 +00:00
|
|
|
|
2020-07-06 21:53:06 +00:00
|
|
|
RETURN_STATEMENT = Element('returnStatement', Sequence([
|
|
|
|
(EXPRESSION), Atom.SEMICOLON
|
|
|
|
]))
|
2020-06-18 12:51:06 +00:00
|
|
|
|
2020-07-06 11:51:13 +00:00
|
|
|
# Just a constant, since this isn't a non-terminal
|
|
|
|
STATEMENT = {
|
2020-07-06 15:42:00 +00:00
|
|
|
(Atom.LET,): LET_STATEMENT,
|
|
|
|
(Atom.IF,): IF_STATEMENT,
|
|
|
|
(Atom.WHILE,): WHILE_STATEMENT,
|
|
|
|
(Atom.DO,): DO_STATEMENT,
|
|
|
|
(Atom.RETURN,): RETURN_STATEMENT
|
2020-07-06 11:51:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:53:06 +00:00
|
|
|
STATEMENTS = Element('statements', Sequence([[STATEMENT]]))
|
2020-07-06 09:06:41 +00:00
|
|
|
|
2020-07-06 21:53:06 +00:00
|
|
|
SUBROUTINE_BODY = Element('subroutineBody', Sequence([
|
2020-07-06 09:06:41 +00:00
|
|
|
# One or more variable declarations
|
|
|
|
# `var type varName (, varName)* ;`
|
2020-07-06 11:51:13 +00:00
|
|
|
Atom.BRACE_OPEN,
|
|
|
|
[VARDEC],
|
|
|
|
STATEMENTS,
|
|
|
|
Atom.BRACE_CLOSE
|
2020-07-06 21:53:06 +00:00
|
|
|
]))
|
2020-07-06 09:06:41 +00:00
|
|
|
|
|
|
|
# Parameter List =
|
|
|
|
# (
|
|
|
|
# (type varName) (, type varName)*
|
|
|
|
# )?
|
|
|
|
# we use tuples for zero OR one of a sequence
|
2020-07-06 21:53:06 +00:00
|
|
|
PARAMETER_LIST = Element('parameterList', Sequence([(
|
2020-07-06 11:51:13 +00:00
|
|
|
Atom.INT | Atom.CHAR | Atom.BOOLEAN | Atom.IDENTIFIER,
|
2020-07-06 09:30:36 +00:00
|
|
|
Atom.IDENTIFIER,
|
2020-07-06 21:53:06 +00:00
|
|
|
# Zero or one of the following:
|
2020-07-06 11:51:13 +00:00
|
|
|
[Atom.COMMA, Atom.INT | Atom.CHAR|Atom.BOOLEAN|Atom.IDENTIFIER, Atom.IDENTIFIER]
|
2020-07-06 21:53:06 +00:00
|
|
|
)]))
|
2020-07-06 09:06:41 +00:00
|
|
|
|
2020-07-06 16:14:22 +00:00
|
|
|
EXPRESSIONLIST.empty = PARAMETER_LIST.empty = True
|
|
|
|
|
2020-07-06 21:53:06 +00:00
|
|
|
SUBROUTINEDEC = Element('subroutineDec', Sequence([
|
2020-07-06 09:06:41 +00:00
|
|
|
# (constructor | function | method) (void | type) subRoutineName '(' parameterList ')'
|
|
|
|
# subroutineBody
|
2020-07-06 09:30:36 +00:00
|
|
|
Atom.CONSTRUCTOR | Atom.FUNCTION | Atom.METHOD,
|
2020-07-06 09:06:41 +00:00
|
|
|
RETURN_TYPES,
|
2020-07-06 09:30:36 +00:00
|
|
|
Atom.IDENTIFIER,
|
2020-07-06 15:42:00 +00:00
|
|
|
Atom.PAREN_OPEN,
|
2020-07-06 09:06:41 +00:00
|
|
|
PARAMETER_LIST,
|
2020-07-06 15:42:00 +00:00
|
|
|
Atom.PAREN_CLOSE,
|
2020-07-06 09:06:41 +00:00
|
|
|
SUBROUTINE_BODY,
|
2020-07-06 21:53:06 +00:00
|
|
|
]))
|
2020-07-06 09:06:41 +00:00
|
|
|
|
2020-07-06 21:53:06 +00:00
|
|
|
CLASS = Element('class', Sequence([
|
2020-07-06 09:30:36 +00:00
|
|
|
Atom.CLASS,
|
|
|
|
Atom.IDENTIFIER,
|
|
|
|
Atom.BRACE_OPEN,
|
2020-07-06 10:57:19 +00:00
|
|
|
[CLASSVARDEC],
|
|
|
|
[SUBROUTINEDEC],
|
2020-07-06 09:30:36 +00:00
|
|
|
Atom.BRACE_CLOSE
|
2020-07-06 21:53:06 +00:00
|
|
|
]))
|