Create evaluators
This commit is contained in:
62
AST.py
62
AST.py
@@ -1,20 +1,5 @@
|
||||
from enum import Enum
|
||||
|
||||
class NodeType(Enum):
|
||||
INTEGER = 1
|
||||
STRING = 2
|
||||
NOTE = 3
|
||||
BLOCK = 4
|
||||
ARGUMENTS = 5
|
||||
IDENTIFIER = 6
|
||||
ASSIGN = 7
|
||||
PROGRAM = 8
|
||||
ASTERISK = 9
|
||||
COLON = 10
|
||||
FUNCTION_CALL = 11
|
||||
COMMA = 12
|
||||
PERCENT = 13
|
||||
|
||||
class Node:
|
||||
def __init__(self):
|
||||
self.children = []
|
||||
@@ -27,7 +12,7 @@ class Node:
|
||||
|
||||
def __getitem__(self, index):
|
||||
return self.children[index]
|
||||
|
||||
|
||||
def append(self, node):
|
||||
self.children.append(node)
|
||||
|
||||
@@ -36,8 +21,7 @@ class Node:
|
||||
|
||||
class Program(Node):
|
||||
def __init__(self):
|
||||
Node.__init__(self)
|
||||
self.type = NodeType.PROGRAM
|
||||
Node.__init__(self)
|
||||
|
||||
def __str__(self):
|
||||
return "Program:\n" + "\n".join([str(e) for e in self.children])
|
||||
@@ -45,32 +29,28 @@ class Program(Node):
|
||||
|
||||
class BlockNode(Node):
|
||||
def __init__(self):
|
||||
Node.__init__(self)
|
||||
self.type = NodeType.BLOCK
|
||||
Node.__init__(self)
|
||||
|
||||
def __str__(self):
|
||||
return "B{\n" + "\n".join([str(e) for e in self.children]) + "\n}"
|
||||
|
||||
|
||||
class ArgumentsNode(Node):
|
||||
class ListNode(Node):
|
||||
def __init__(self):
|
||||
Node.__init__(self)
|
||||
self.type = NodeType.ARGUMENTS
|
||||
Node.__init__(self)
|
||||
|
||||
def __str__(self):
|
||||
return "@(" + ", ".join([str(e) for e in self.children]) + ")"
|
||||
|
||||
class IdentifierNode(Node):
|
||||
def __init__(self, identifier):
|
||||
self.type = NodeType.IDENTIFIER
|
||||
def __init__(self, identifier):
|
||||
self.identifier = identifier
|
||||
|
||||
def __str__(self):
|
||||
return f"L'{self.identifier}'"
|
||||
|
||||
class AssignExpression(Node):
|
||||
def __init__(self, target, value):
|
||||
self.type = NodeType.ASSIGN
|
||||
def __init__(self, target, value):
|
||||
self.target = target
|
||||
self.value = value
|
||||
|
||||
@@ -78,8 +58,7 @@ class AssignExpression(Node):
|
||||
return f"A[{self.target} = {self.value}]"
|
||||
|
||||
class AsteriskStatementNode(Node):
|
||||
def __init__(self, iterator, statement):
|
||||
self.type = NodeType.ASTERISK
|
||||
def __init__(self, iterator, statement):
|
||||
self.iterator = iterator
|
||||
self.statement = statement
|
||||
|
||||
@@ -87,8 +66,7 @@ class AsteriskStatementNode(Node):
|
||||
return f"*({self.iterator}: {self.statement})"
|
||||
|
||||
class ColonNode(Node):
|
||||
def __init__(self, a, b):
|
||||
self.type = NodeType.COLON
|
||||
def __init__(self, a, b):
|
||||
self.a = a
|
||||
self.b = b
|
||||
|
||||
@@ -101,48 +79,40 @@ class ExpressionNode(Node):
|
||||
|
||||
|
||||
class IntegerLiteralNode(ExpressionNode):
|
||||
def __init__(self, value):
|
||||
self.type = NodeType.INTEGER
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __str__(self):
|
||||
return f"i'{self.value}'"
|
||||
|
||||
class StringLiteralNode(ExpressionNode):
|
||||
def __init__(self, value):
|
||||
self.type = NodeType.STRING
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __str__(self):
|
||||
return f"s'{self.value}'"
|
||||
|
||||
class NoteLiteralNode(ExpressionNode):
|
||||
def __init__(self, value):
|
||||
self.type = NodeType.NOTE
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __str__(self):
|
||||
return f"n'{self.value}'"
|
||||
return f"n'{self.value.note}[{self.value.octave}, {self.value.duration}]'"
|
||||
|
||||
class FunctionCallNode(Node):
|
||||
def __init__(self, identifier, arguments):
|
||||
self.type = NodeType.FUNCTION_CALL
|
||||
def __init__(self, identifier, arguments):
|
||||
self.identifier = identifier
|
||||
self.arguments = arguments
|
||||
|
||||
def __str__(self):
|
||||
return f"F({self.identifier}: {self.arguments})"
|
||||
|
||||
class CommaNode(Node):
|
||||
def __init__(self):
|
||||
self.type = NodeType.COMMA
|
||||
|
||||
class CommaNode(Node):
|
||||
def __str__(self):
|
||||
return "[,]"
|
||||
|
||||
class PercentNode(Node):
|
||||
def __init__(self, value):
|
||||
self.type = NodeType.PERCENT
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __str__(self):
|
||||
|
||||
133
Evaulator.py
Normal file
133
Evaulator.py
Normal file
@@ -0,0 +1,133 @@
|
||||
from Tokenizer import tokenize, TokenType
|
||||
from Parser import parse
|
||||
from AST import *
|
||||
import sys
|
||||
from Note import *
|
||||
import random
|
||||
|
||||
class RuntimeException(Exception):
|
||||
pass
|
||||
|
||||
class Environment():
|
||||
def __init__(self, scopes, functions):
|
||||
self.scopes = scopes
|
||||
self.functions = functions
|
||||
|
||||
def evaluateProgram(program, environment):
|
||||
for node in program.children:
|
||||
evaluate(node, environment)
|
||||
|
||||
def evaluateInteger(integer, environment):
|
||||
return integer.value
|
||||
|
||||
def evaluatePercent(percent, environment):
|
||||
pass
|
||||
|
||||
def evaluateIdentifier(identifier, environment):
|
||||
value = findVariable(identifier.identifier, environment)
|
||||
return value
|
||||
|
||||
def findVariable(name, environment):
|
||||
for scope in reversed(environment.scopes):
|
||||
if name in scope:
|
||||
return scope[name]
|
||||
raise RuntimeException(f"Variable '{name}' is not declared")
|
||||
|
||||
def evaluateString(string, environment):
|
||||
value = string.value
|
||||
for scope in reversed(environment.scopes):
|
||||
for k, v in scope.items():
|
||||
value = value.replace('{' + k + '}', str(v))
|
||||
return value
|
||||
|
||||
def evaluateNote(note, environment):
|
||||
return note.value
|
||||
|
||||
def evaluateFunctionCall(functionCall, environment):
|
||||
function = functionCall.identifier.identifier
|
||||
arguments = evaluateList(functionCall.arguments, environment)
|
||||
#TODO: example
|
||||
for name, definition in environment.functions.items():
|
||||
if name == function:
|
||||
return definition(arguments)
|
||||
raise RuntimeException(f"Function '{function}' does not exist")
|
||||
|
||||
|
||||
def evaluateComma(comma, environment):
|
||||
pass
|
||||
|
||||
def evaluateBlock(block, environment):
|
||||
environment.scopes.append({})
|
||||
for node in block.children:
|
||||
evaluate(node, environment)
|
||||
environment.scopes.pop(-1)
|
||||
|
||||
def evaluateList(list, environment):
|
||||
return [evaluate(e, environment) for e in list if not isinstance(e, CommaNode)]
|
||||
|
||||
def evaluateAssignment(assignment, environment):
|
||||
target = assignment.target.identifier
|
||||
value = evaluate(assignment.value, environment)
|
||||
environment.scopes[-1][target] = value
|
||||
|
||||
def evaluateAsterisk(asterisk, environment):
|
||||
count = evaluate(asterisk.iterator, environment)
|
||||
for i in range(count):
|
||||
if isinstance(asterisk.iterator, IdentifierNode):
|
||||
environment.scopes[-1][f"_{asterisk.iterator.identifier}"] = i+1
|
||||
evaluate(asterisk.statement, environment)
|
||||
|
||||
def evaluateColon(colon, environment):
|
||||
if isinstance(colon.a, IntegerLiteralNode) and isinstance(colon.b, IntegerLiteralNode):
|
||||
return list(range(evaluateInteger(colon.a, environment), evaluateInteger(colon.b, environment)+1))
|
||||
if isinstance(colon.a, NoteLiteralNode) and isinstance(colon.b, NoteLiteralNode):
|
||||
return NotePitch.range(colon.a.value.note, colon.b.value.note)
|
||||
raise RuntimeException("Range can be created using only note or integer literals")
|
||||
|
||||
def evaluate(input, environment):
|
||||
if isinstance(input, Program):
|
||||
return evaluateProgram(input, environment)
|
||||
if isinstance(input, IntegerLiteralNode):
|
||||
return evaluateInteger(input, environment)
|
||||
if isinstance(input, PercentNode):
|
||||
return evaluatePercent(input, environment)
|
||||
if isinstance(input, StringLiteralNode):
|
||||
return evaluateString(input, environment)
|
||||
if isinstance(input, NoteLiteralNode):
|
||||
return evaluateNote(input, environment)
|
||||
if isinstance(input, FunctionCallNode):
|
||||
return evaluateFunctionCall(input, environment)
|
||||
if isinstance(input, CommaNode):
|
||||
return evaluateComma(input, environment)
|
||||
if isinstance(input, BlockNode):
|
||||
return evaluateBlock(input, environment)
|
||||
if isinstance(input, ListNode):
|
||||
return evaluateList(input, environment)
|
||||
if isinstance(input, AssignExpression):
|
||||
return evaluateAssignment(input, environment)
|
||||
if isinstance(input, AsteriskStatementNode):
|
||||
return evaluateAsterisk(input, environment)
|
||||
if isinstance(input, ColonNode):
|
||||
return evaluateColon(input, environment)
|
||||
if isinstance(input, IdentifierNode):
|
||||
return evaluateIdentifier(input, environment)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
functions = {
|
||||
'print': lambda args: print("".join([str(arg) for arg in args])),
|
||||
'midi': lambda args: print(":".join([str(type(arg)) for arg in args])),
|
||||
'random': lambda args: args[0][int(random.uniform(0, len(args[0])))][1]
|
||||
}
|
||||
|
||||
with open(sys.argv[1], 'r') as source:
|
||||
lines = [line.rstrip('\n') for line in source.readlines()]
|
||||
|
||||
tokens = [token for token in tokenize(lines) if token.type != TokenType.COMMENT]
|
||||
|
||||
ast = parse(tokens)
|
||||
|
||||
environment = Environment([{}], functions)
|
||||
evaluate(ast, environment)
|
||||
|
||||
17
Note.py
17
Note.py
@@ -1,5 +1,5 @@
|
||||
from enum import Enum
|
||||
from parser import ParseError
|
||||
from Error import ParseError
|
||||
|
||||
class NotePitch(Enum):
|
||||
C = 1
|
||||
@@ -15,6 +15,12 @@ class NotePitch(Enum):
|
||||
AIS = 11
|
||||
H = 12
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
@staticmethod
|
||||
def toPitch(string):
|
||||
try:
|
||||
@@ -26,6 +32,13 @@ class NotePitch(Enum):
|
||||
return map[string.lower()]
|
||||
except KeyError as e:
|
||||
raise ParseError(f"Note '{string}' does not exist")
|
||||
|
||||
@staticmethod
|
||||
def range(a, b):
|
||||
aValue = a.value
|
||||
bValue = b.value
|
||||
|
||||
return [note for note in NotePitch.__members__.items() if note[1].value >= aValue and note[1].value <= bValue]
|
||||
|
||||
class Note:
|
||||
def __init__(self, note, octave, duration):
|
||||
@@ -37,4 +50,4 @@ class Note:
|
||||
self.duration = duration
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.note}[{self.octave}, {self.duration}]"
|
||||
return self.note.name
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
from Tokenizer import *
|
||||
from Note import *
|
||||
from AST import *
|
||||
|
||||
class ParseError(Exception):
|
||||
pass
|
||||
from Error import ParseError
|
||||
|
||||
def expectedFound(expected, found):
|
||||
raise ParseError(f"Expected: {expected}, found: {found}")
|
||||
@@ -41,23 +39,22 @@ def parseComma(input, parent):
|
||||
input.pop(0)
|
||||
return CommaNode()
|
||||
|
||||
def parseArguments(input, parent):
|
||||
#import pdb; pdb.set_trace()
|
||||
def parseList(input, parent):
|
||||
input.pop(0)
|
||||
|
||||
arguments = ArgumentsNode()
|
||||
node = ListNode()
|
||||
|
||||
while input[0].type != TokenType.CLOSE_PAREN:
|
||||
argument = parseArrayElement(input, arguments)
|
||||
if argument is None:
|
||||
raise ParseError(f"Line: {input[0].pos[0]+1}, col: {input[0].pos[1]+1}: Invalid function argument '{input[0].value}'")
|
||||
arguments.append(argument) #TODO: parseExpression
|
||||
element = parseArrayElement(input, node)
|
||||
if element is None:
|
||||
raise ParseError(f"Line: {input[0].pos[0]+1}, col: {input[0].pos[1]+1}: Invalid element'{input[0].value}'")
|
||||
node.append(element)
|
||||
|
||||
if input[0].type != TokenType.CLOSE_PAREN:
|
||||
expectedFound(TokenType.CLOSE_PAREN, input[0].type)
|
||||
input.pop(0)
|
||||
|
||||
return arguments
|
||||
return node
|
||||
|
||||
def parseBlock(input, parent):
|
||||
input.pop(0)
|
||||
@@ -95,7 +92,7 @@ def parseFunctionCallOrAssignOrIdentifier(input, parent):
|
||||
identifier = IdentifierNode(input.pop(0).value)
|
||||
# Function call
|
||||
if len(input) > 0 and input[0].type == TokenType.OPEN_PAREN:
|
||||
arguments = parseArguments(input, parent)
|
||||
arguments = parseList(input, parent)
|
||||
return FunctionCallNode(identifier, arguments)
|
||||
# Assign
|
||||
if len(input) > 1 and input[0].type == TokenType.ASSIGN:
|
||||
@@ -125,7 +122,7 @@ def parseExpression(input, parent):
|
||||
if type == TokenType.PERCENT:
|
||||
return parsePercent(input, parent)
|
||||
if type == TokenType.OPEN_PAREN:
|
||||
return parseArguments(input, parent)
|
||||
return parseList(input, parent)
|
||||
if type == TokenType.COLON:
|
||||
return parseColon(input, parent)
|
||||
|
||||
Reference in New Issue
Block a user