added let, about to add types
This commit is contained in:
@@ -1,9 +1,24 @@
|
||||
import sys
|
||||
import enum
|
||||
from typing import Union
|
||||
from typing import Union, List
|
||||
from dataclasses import dataclass, field
|
||||
from lark import Lark, Transformer
|
||||
|
||||
def pretty_print(clas, indent=0):
|
||||
print(' ' * indent + type(clas).__name__ + ':')
|
||||
indent += 2
|
||||
for k,v in clas.__dict__.items():
|
||||
if '__dict__' in dir(v):
|
||||
print(' ' * indent + k + ': ')
|
||||
pretty_print(v,indent+2)
|
||||
elif type(v) == list:
|
||||
print(' ' * indent + k + ': ' "[")
|
||||
for e in v:
|
||||
pretty_print(e, indent+2)
|
||||
print(' ' * indent + "]")
|
||||
else:
|
||||
print(' ' * indent + k + ': ' + str(v))
|
||||
|
||||
|
||||
@dataclass
|
||||
class Identifier:
|
||||
@@ -25,7 +40,7 @@ class LiteralInt:
|
||||
@dataclass
|
||||
class FunctionCall:
|
||||
name: Identifier
|
||||
arguments: list['Expression'] = field(default_factory=list)
|
||||
arguments: List['Expression'] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -41,10 +56,21 @@ class Expression:
|
||||
|
||||
|
||||
@dataclass
|
||||
class Block:
|
||||
class LetStatement:
|
||||
variable_name: Identifier
|
||||
expression: Expression
|
||||
|
||||
|
||||
@dataclass
|
||||
class Statement:
|
||||
statement: Union[LetStatement, Expression]
|
||||
|
||||
|
||||
@dataclass
|
||||
class Block:
|
||||
statements: List[Statement]
|
||||
|
||||
|
||||
@dataclass
|
||||
class VariableDeclaration:
|
||||
name: Identifier
|
||||
@@ -53,7 +79,7 @@ class VariableDeclaration:
|
||||
@dataclass
|
||||
class Function:
|
||||
name: Identifier
|
||||
arguments: list[VariableDeclaration]
|
||||
arguments: List[VariableDeclaration]
|
||||
block: Block
|
||||
|
||||
|
||||
@@ -91,7 +117,12 @@ boring_grammar = r"""
|
||||
| function_call
|
||||
| "(" expression ")"
|
||||
|
||||
block : "{" expression "}"
|
||||
let_statement : "let" identifier "=" expression ";"
|
||||
|
||||
statement : let_statement
|
||||
| expression
|
||||
|
||||
block : "{" (statement)* "}"
|
||||
|
||||
variable_declaration : identifier
|
||||
|
||||
@@ -106,65 +137,76 @@ boring_grammar = r"""
|
||||
"""
|
||||
|
||||
class TreeToBoring(Transformer):
|
||||
def plus(self, p):
|
||||
def plus(self, p) -> Operator:
|
||||
return Operator.plus
|
||||
|
||||
def minus(self, m):
|
||||
def minus(self, m) -> Operator:
|
||||
return Operator.minus
|
||||
|
||||
def mult(self, m):
|
||||
def mult(self, m) -> Operator:
|
||||
return Operator.mult
|
||||
|
||||
def div(self, d):
|
||||
def div(self, d) -> Operator:
|
||||
return Operator.div
|
||||
|
||||
def literal_int(self, n):
|
||||
def literal_int(self, n) -> LiteralInt:
|
||||
(n,) = n
|
||||
return LiteralInt(value=int(n))
|
||||
|
||||
def identifier(self, i):
|
||||
def identifier(self, i) -> Identifier:
|
||||
(i,) = i
|
||||
return Identifier(name=str(i))
|
||||
|
||||
def function_call(self, call):
|
||||
def function_call(self, call) -> FunctionCall:
|
||||
return FunctionCall(name=call[0], arguments=call[1:])
|
||||
|
||||
def add_expression(self, ae):
|
||||
def add_expression(self, ae) -> Operation:
|
||||
return Operation(left=ae[0], op=ae[1], right=ae[2])
|
||||
|
||||
def sub_expression(self, se):
|
||||
def sub_expression(self, se) -> Operation:
|
||||
return Operation(left=se[0], op=se[1], right=se[2])
|
||||
|
||||
def mult_expression(self, se):
|
||||
def mult_expression(self, se) -> Operation:
|
||||
return Operation(left=se[0], op=se[1], right=se[2])
|
||||
|
||||
def div_expression(self, se):
|
||||
def div_expression(self, se) -> Operation:
|
||||
return Operation(left=se[0], op=se[1], right=se[2])
|
||||
|
||||
def expression(self, exp):
|
||||
def expression(self, exp) -> Expression:
|
||||
(exp,) = exp
|
||||
if type(exp) == Expression:
|
||||
return exp
|
||||
return Expression(expression=exp)
|
||||
|
||||
def factor(self, factor):
|
||||
def factor(self, factor) -> Expression:
|
||||
(factor,) = factor
|
||||
if type(factor) == Expression:
|
||||
return factor
|
||||
return Expression(factor)
|
||||
|
||||
def term(self, term):
|
||||
def term(self, term) -> Expression:
|
||||
(term,) = term
|
||||
return Expression(term)
|
||||
|
||||
def block(self, block):
|
||||
(block,) = block
|
||||
return Block(expression=block)
|
||||
def let_statement(self, let_statement) -> LetStatement:
|
||||
(variable_name, expression) = let_statement
|
||||
return LetStatement(variable_name=variable_name, expression=expression)
|
||||
|
||||
def variable_declaration(self, identifier):
|
||||
def statement(self, statement) -> Statement:
|
||||
(statement,) = statement
|
||||
return Statement(statement=statement)
|
||||
|
||||
def block(self, block) -> Block:
|
||||
return Block(statements=block)
|
||||
|
||||
def variable_declaration(self, identifier) -> VariableDeclaration:
|
||||
(identifier,) = identifier
|
||||
return VariableDeclaration(name=identifier)
|
||||
|
||||
def function(self, function):
|
||||
def function(self, function) -> Function:
|
||||
return Function(name=function[0], arguments=function[1:-1], block=function[-1])
|
||||
|
||||
def module(self, functions):
|
||||
def module(self, functions) -> Module:
|
||||
return Module(functions=functions)
|
||||
|
||||
|
||||
@@ -173,5 +215,6 @@ boring_parser = Lark(boring_grammar, start='module', lexer='standard')
|
||||
if __name__ == '__main__':
|
||||
with open(sys.argv[1]) as f:
|
||||
tree = boring_parser.parse(f.read())
|
||||
print(tree)
|
||||
print(TreeToBoring().transform(tree))
|
||||
# print(tree)
|
||||
result = TreeToBoring().transform(tree)
|
||||
pretty_print(result)
|
||||
|
||||
Reference in New Issue
Block a user