51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
from typing import Any
|
|
|
|
from src.ast.expr import Expr, BinaryExpr, UnaryExpr, LiteralExpr, GroupingExpr
|
|
from src.ast.printer import AstPrinter
|
|
from src.interpreter.interpreter import Interpreter
|
|
from src.lexer import Lexer
|
|
from src.parser.parser import Parser
|
|
from src.token import Token, TokenType
|
|
|
|
|
|
def main():
|
|
source: str = """() {} +- += / /= // sefs + {, )
|
|
}:: *
|
|
"This is a string"
|
|
3.1415
|
|
123
|
|
"This is
|
|
another string" """
|
|
path: str = "examples/07_math.peb"
|
|
with open(path, "r") as f:
|
|
source = f.read()
|
|
lexer: Lexer = Lexer()
|
|
tokens: list[Token] = lexer.process(source, path)
|
|
print(tokens)
|
|
|
|
printer: AstPrinter = AstPrinter()
|
|
ast: Expr = BinaryExpr(
|
|
UnaryExpr(
|
|
Token(TokenType.MINUS, "-", None, None),
|
|
LiteralExpr(123)
|
|
),
|
|
Token(TokenType.STAR, "*", None, None),
|
|
GroupingExpr(LiteralExpr(45.67))
|
|
)
|
|
print(printer.print(ast))
|
|
|
|
parser: Parser = Parser()
|
|
ast = parser.parse(tokens)
|
|
|
|
if ast is None:
|
|
return
|
|
|
|
print(printer.print(ast))
|
|
interpreter: Interpreter = Interpreter()
|
|
result: Any = interpreter.interpret(ast)
|
|
print(f"Result: {result}")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|