This is a simple programming language interpreter for basic arithmetic operations. The interpreter includes a lexer, parser, and abstract syntax tree (AST) nodes for performing arithmetic operations.
- Create a file called
yeep.py
. - Copy the provided code into the file.
- Run the file with Python3 using the command
python3 yeep.py
. - Type in an expression (e.g.,
2 + 2
) and press enter. - The interpreter will print the result of the expression.
TT_PLUS
: Represents the plus operator '+'TT_MINUS
: Represents the minus operator '-'TT_DIV
: Represents the division operator '/'TT_MUL
: Represents the multiplication operator '*'TT_LPAREN
: Represents the left parenthesis '('TT_RPAREN
: Represents the right parenthesis ')'TT_INT
: Represents an integer numberTT_FLOAT
: Represents a floating-point numberTT_EOF
: Represents the end of the file
Tokens
: Represents a token with a type and optional valueLexer
: Converts source code into tokensNumberNode
: Represents a number in the ASTBinOpNode
: Represents a binary operation in the ASTUnaryOpNode
: Represents a unary operation in the ASTVarAccessNode
: Represents a variable access in the ASTParser
: Converts tokens into an ASTError
: Base class for different types of errorsIllegalCharError
: Represents an error for encountering an illegal characterInvalidSyntaxError
: Represents an error for encountering invalid syntaxExpectedTokenError
: Represents an error for expecting a specific tokenPosition
: Represents the position of a character in the source code
# Example usage in the shell
import yeep
while True:
text = input("yeep >> ")
result = yeep.run("<stdin>", text)
if isinstance(result, tuple):
ast, error = result
if error:
print(error.as_string())
else:
print(ast)