diff options
-rw-r--r-- | Classes.py | 145 | ||||
-rw-r--r-- | doc/mylang.ebnf | 24 | ||||
-rw-r--r-- | doc/praes1/main.tex | 22 | ||||
-rw-r--r-- | src/front/__init__.py | 0 | ||||
-rw-r--r-- | src/front/scope.py | 24 | ||||
-rw-r--r-- | src/front/symbol.py | 32 | ||||
-rw-r--r-- | src/front/type.py | 12 |
7 files changed, 43 insertions, 216 deletions
diff --git a/Classes.py b/Classes.py deleted file mode 100644 index f144e77..0000000 --- a/Classes.py +++ /dev/null @@ -1,145 +0,0 @@ -class KlassType: - def __init__(self, name): - self.name = name - - def __str__(self): - return self.name - - def __repr__(self): - return "<KlassType: %s>" % self - -KlassType.CONST = KlassType("const") -KlassType.VAR = KlassType("var") -KlassType.FUNCNAME = KlassTupe("function name") - -class OperatorType: - def __init__(self, name): - self.name = name - - def __str__(self): - return self.name - - def __repr__(self): - return "<OperatorType: %s>" % self - -OperatorType.EQ = OperatorType("==") -OperatorType.NE = OperatorType("!=") -OperatorType.GE = OperatorType(">=") -OperatorType.LE = OperatorType("<=") -OperatorType.GT = OperatorType(">") -OperatorType.LT = OperatorType("<") -OperatorType.ADD = OperatorType("+") -OperatorType.SUB = OperatorType("-") -OperatorType.MUL = OperatorType("*") -OperatorType.DIV = OperatorType("/") -OperatorType.MOD = OperatorType("%") -OperatorType.AND = OperatorType("&&") -OperatorType.OR = OperatorType("||") - -class TokenType: - def __init__(self, name): - self.name = name - - def __str__(self): - return self.name - - def __repr__(self): - return "<TokenType: %s>" % self - -TokenType.FUN = TokenType("fun") -TokenType.IDENT = TokenType("identifier") -TokenType.LBRAK = TokenType("[") -TokenType.RBRAK = TokenType("]") -TokenType.LPAREN = TokenType("(") -TokenType.RPAREN = TokenType(")") -TokenType.NEWLINE = TokenType("newline") -TokenType.IF = TokenType("if") -TokenType.TAB = TokenType("tab") -TokenType.OPERATOR = TokenType("operator") -TokenType.NUMBER = TokenType("number") -TokenType.RETURN = TokenType("return") -TokenType.END = TokenType("end") - -class NonTerminalType: - def __init__(self, name): - self.name = name - - def __str__(self): - return self.name - - def __repr__(self): - return "<NonTerminalType: %s>" % self - -NonTerminalType.FACTOR = NonTerminalType("factor") -NonTerminalType.TERM = NonTerminalType("term") -NonTerminalType.SIMPLE_EXPRESSION = NonTerminalType("simple_expression") -NonTerminalType.EXPRESSION = NonTerminalType("expression") -NonTerminalType.IDENT_LIST = NonTerminalType("ident_list") -NonTerminalType.EXPRESSION_LIST = NonTerminalType("expression_list") -NonTerminalType.PROGRAM = NonTerminalType("program") -NonTerminalType.FUNCTION = NonTerminalType("function") -NonTerminalType.STATEMENT = NonTerminalType("statement") -NonTerminalType.STATEMENTS = NonTerminalType("statements") -NonTerminalType.IF_STATEMENT = NonTerminalType("if_statement") -NonTerminalType.WHILE_STATEMENT = NonTerminalType("while_statement") -NonTerminalType.RETURN_STATEMENT = NonTerminalType("return_statement") -NonTerminalType.ASSIGNMENT = NonTerminalType("assignment") -NonTerminalType.FUNCTION_CALL = NonTerminalType("function_call") - -class NonTerminal: - # bsp: <FUNCTION, Zwischencode> - def __init__(self,type,code=None): - self.type = type - self.code = code - -class Token: - # bsp: <identifier,0> - def __init__(self,type,value=None): - self.type = type - self.value = value - -class Tokenlist: - tokenlist = [] - - def __init__(self,source): - self.source = source - - def getNextToken(self): - #... irgendwann.. - return tokenlist.next() - - def getValue(self,key) - # ... blublub - return somevalue - -class Symbol: - name = None - value = None - type = None # int, string,.. - klass = None # const, var, functionName - - def __init__(self, name): - self.name = name - -class ParseTree: - def __init__(self, root): - self.root = root - -class ParseTreeNode: - children = [] - value = None # Token oder NonTerminal - - def __init__(self, value): - self.value = value - -class ThreeAddressCodeToken: - - -class ThreeAddressCode: - codelist = [] - - # returns code or -1 if end of list - getNextCode(): - return someCode - -############################### diff --git a/doc/mylang.ebnf b/doc/mylang.ebnf index 0ab786d..7c67bbb 100644 --- a/doc/mylang.ebnf +++ b/doc/mylang.ebnf @@ -8,25 +8,25 @@ nl = "\n". ident = letter { letter | digit }. integer = digit { digit }. -bool = bool "||" join | join. -join = join "&&" equality | equality. -equality = equality "==" relation | equality "!=" relation | relation. -relation = expression "<" expression | expression "<=" expression | expression ">=" expression | expression ">" expression | expression. -expression = expression "+" term | expression "-" term | term. -term = term "*" unary | term "/" unary | term "%" unary | unary. -unary = "!" unary | "-" unary | factor. -factor = "(" bool ")" | ident | integer | "true" | "false". +disjunction = conjunction { "||" conjunction }. +conjunction = boolean { "&&" boolean }. +boolean = "!" disjunction | comparison | "(" disjunction ")" | "true" | "false". +comparison = expression ( "==" | "!=" | "<" | "<=" | "=>" | ">" ) expression. + +expression = term { ( "+" | "-" ) term }. +term = unary { ( "*" | "/" | "%" ) unary }. +unary = ident | integer | "-" unary | "(" expression ")". ident_list = ident { "," ident }. expression_list = expression { "," expression }. -function_list = function { [ nl ] function }. +function_list = function { nl function }. statement_list = statement { nl statement }. -function = "fun" ident "[" [ ident_list ] "]" [ nl ] statement_list [ nl ] "end". +function = "fun" ident "[" [ ident_list ] "]" nl statement_list nl "end". statement = [ if_statement | while_statement | assign_statement | function_call | return_statement ]. -if_statement = "if" expression [ nl ] statement_list [ nl ] [ "else" statement_list [ nl ] ] "end". -while_statement = "while" expression [ nl ] statement_list [ nl ] "end". +if_statement = "if" expression nl statement_list nl [ "else" statement_list nl ] "end". +while_statement = "while" expression nl statement_list nl "end". return_statement = "@" expression. assign_statement = ident "=" expression. function_call = ident "[" [ expression_list ] "]". diff --git a/doc/praes1/main.tex b/doc/praes1/main.tex index e924838..2f85422 100644 --- a/doc/praes1/main.tex +++ b/doc/praes1/main.tex @@ -107,17 +107,17 @@ end \pause\item Top-Down-Parsing \pause\item Ableitungsbaum: \begin{itemize} - \pause\item\color{fu-red} Program(Node) - \pause\item\color{fu-red} Function(Node) - \pause\item\color{fu-red} Statement(Node) - \pause\item\color{fu-red} IfStatement(Statement) - \pause\item\color{fu-red} WhileStatement(Statement) - \pause\item\color{fu-red} ReturnStatement(Statement) - \pause\item\color{fu-red} AssignStatement(Statement) - \pause\item\color{fu-red} FunctionCall(Statement) - \pause\item\color{fu-red} Expression(Node) - \pause\item\color{fu-red} UnaryExpression(Expression) - \pause\item\color{fu-red} BinaryExpression(Expression) + \pause\item\color{orange} Program(Node) + \pause\item\color{orange} Function(Node) + \pause\item\color{orange} Statement(Node) + \pause\item\color{orange} IfStatement(Statement) + \pause\item\color{orange} WhileStatement(Statement) + \pause\item\color{orange} ReturnStatement(Statement) + \pause\item\color{orange} AssignStatement(Statement) + \pause\item\color{orange} FunctionCall(Statement) + \pause\item\color{orange} Expression(Node) + \pause\item\color{orange} UnaryExpression(Expression) + \pause\item\color{orange} BinaryExpression(Expression) \end{itemize} \end{itemize} diff --git a/src/front/__init__.py b/src/front/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/src/front/__init__.py +++ /dev/null diff --git a/src/front/scope.py b/src/front/scope.py index 2cc3018..0df8729 100644 --- a/src/front/scope.py +++ b/src/front/scope.py @@ -1,20 +1,6 @@ -class Scope(object, DictMixin): - def __init__(self, parent): - self.parent = parent - self.__symbols = {} +class Scope(object): + __shared_state = {} + functions = {} - def __getitem__(self, key): - try: - return self.__symbols[key] - except KeyError: - return self.parent[key] - - def __setitem__(self, key, item): - self.__symbols[key] = item - - def keys(self): - keys = self.__symbols.keys() - for key in self.parent.keys(): - if key not in keys: - keys.append(key) - return keys + def __init__(self): + self.__dict__ = self.__shared_state diff --git a/src/front/symbol.py b/src/front/symbol.py index 879403f..b93b778 100644 --- a/src/front/symbol.py +++ b/src/front/symbol.py @@ -1,34 +1,8 @@ -class SymbolClass(object): - def __init__(self, name): - self.name = name - - def __str__(self): - return self.name - - def __repr__(self): - return "<SymbolClass: %s>" % self - -SymbolClass.CONST = SymbolClass("CONST") -SymbolClass.VAR = SymbolClass("VAR") -SymbolClass.FUNC = SymbolClass("FUNC") - -class SymbolType(object): - def __init__(self, name): - self.name = name - - def __str__(self): - return self.name - - def __repr__(self): - return "<SymbolType: %s>" % self - -SymbolType.INT = SymbolType("INT") -SymbolType.BOOL = SymbolType("BOOL") +from type import DataType class Symbol(object): - __slots__ = ['cls', 'type', 'value'] + __slots__ = ['type', 'value'] - def __init__(self, value, type = SymbolType.INT, cls = SymbolClass.VAR): + def __init__(self, value, type = DataType.INT): self.value = value self.type = type - self.cls = cls diff --git a/src/front/type.py b/src/front/type.py new file mode 100644 index 0000000..a8bd8c7 --- /dev/null +++ b/src/front/type.py @@ -0,0 +1,12 @@ +class DataType(object): + def __init__(self, name): + self.name = name + + def __str__(self): + return self.name + + def __repr__(self): + return "<DataType: %s>" % self + +DataType.INT = DataType("INT") +DataType.BOOL = DataType("BOOL") |