Create evaluators
This commit is contained in:
34
AST.py
34
AST.py
@@ -1,20 +1,5 @@
|
|||||||
from enum import Enum
|
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:
|
class Node:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.children = []
|
self.children = []
|
||||||
@@ -37,7 +22,6 @@ class Node:
|
|||||||
class Program(Node):
|
class Program(Node):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
Node.__init__(self)
|
Node.__init__(self)
|
||||||
self.type = NodeType.PROGRAM
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Program:\n" + "\n".join([str(e) for e in self.children])
|
return "Program:\n" + "\n".join([str(e) for e in self.children])
|
||||||
@@ -46,23 +30,20 @@ class Program(Node):
|
|||||||
class BlockNode(Node):
|
class BlockNode(Node):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
Node.__init__(self)
|
Node.__init__(self)
|
||||||
self.type = NodeType.BLOCK
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "B{\n" + "\n".join([str(e) for e in self.children]) + "\n}"
|
return "B{\n" + "\n".join([str(e) for e in self.children]) + "\n}"
|
||||||
|
|
||||||
|
|
||||||
class ArgumentsNode(Node):
|
class ListNode(Node):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
Node.__init__(self)
|
Node.__init__(self)
|
||||||
self.type = NodeType.ARGUMENTS
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "@(" + ", ".join([str(e) for e in self.children]) + ")"
|
return "@(" + ", ".join([str(e) for e in self.children]) + ")"
|
||||||
|
|
||||||
class IdentifierNode(Node):
|
class IdentifierNode(Node):
|
||||||
def __init__(self, identifier):
|
def __init__(self, identifier):
|
||||||
self.type = NodeType.IDENTIFIER
|
|
||||||
self.identifier = identifier
|
self.identifier = identifier
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@@ -70,7 +51,6 @@ class IdentifierNode(Node):
|
|||||||
|
|
||||||
class AssignExpression(Node):
|
class AssignExpression(Node):
|
||||||
def __init__(self, target, value):
|
def __init__(self, target, value):
|
||||||
self.type = NodeType.ASSIGN
|
|
||||||
self.target = target
|
self.target = target
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
@@ -79,7 +59,6 @@ class AssignExpression(Node):
|
|||||||
|
|
||||||
class AsteriskStatementNode(Node):
|
class AsteriskStatementNode(Node):
|
||||||
def __init__(self, iterator, statement):
|
def __init__(self, iterator, statement):
|
||||||
self.type = NodeType.ASTERISK
|
|
||||||
self.iterator = iterator
|
self.iterator = iterator
|
||||||
self.statement = statement
|
self.statement = statement
|
||||||
|
|
||||||
@@ -88,7 +67,6 @@ class AsteriskStatementNode(Node):
|
|||||||
|
|
||||||
class ColonNode(Node):
|
class ColonNode(Node):
|
||||||
def __init__(self, a, b):
|
def __init__(self, a, b):
|
||||||
self.type = NodeType.COLON
|
|
||||||
self.a = a
|
self.a = a
|
||||||
self.b = b
|
self.b = b
|
||||||
|
|
||||||
@@ -102,7 +80,6 @@ class ExpressionNode(Node):
|
|||||||
|
|
||||||
class IntegerLiteralNode(ExpressionNode):
|
class IntegerLiteralNode(ExpressionNode):
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
self.type = NodeType.INTEGER
|
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@@ -110,7 +87,6 @@ class IntegerLiteralNode(ExpressionNode):
|
|||||||
|
|
||||||
class StringLiteralNode(ExpressionNode):
|
class StringLiteralNode(ExpressionNode):
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
self.type = NodeType.STRING
|
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@@ -118,15 +94,13 @@ class StringLiteralNode(ExpressionNode):
|
|||||||
|
|
||||||
class NoteLiteralNode(ExpressionNode):
|
class NoteLiteralNode(ExpressionNode):
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
self.type = NodeType.NOTE
|
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"n'{self.value}'"
|
return f"n'{self.value.note}[{self.value.octave}, {self.value.duration}]'"
|
||||||
|
|
||||||
class FunctionCallNode(Node):
|
class FunctionCallNode(Node):
|
||||||
def __init__(self, identifier, arguments):
|
def __init__(self, identifier, arguments):
|
||||||
self.type = NodeType.FUNCTION_CALL
|
|
||||||
self.identifier = identifier
|
self.identifier = identifier
|
||||||
self.arguments = arguments
|
self.arguments = arguments
|
||||||
|
|
||||||
@@ -134,15 +108,11 @@ class FunctionCallNode(Node):
|
|||||||
return f"F({self.identifier}: {self.arguments})"
|
return f"F({self.identifier}: {self.arguments})"
|
||||||
|
|
||||||
class CommaNode(Node):
|
class CommaNode(Node):
|
||||||
def __init__(self):
|
|
||||||
self.type = NodeType.COMMA
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "[,]"
|
return "[,]"
|
||||||
|
|
||||||
class PercentNode(Node):
|
class PercentNode(Node):
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
self.type = NodeType.PERCENT
|
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
def __str__(self):
|
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 enum import Enum
|
||||||
from parser import ParseError
|
from Error import ParseError
|
||||||
|
|
||||||
class NotePitch(Enum):
|
class NotePitch(Enum):
|
||||||
C = 1
|
C = 1
|
||||||
@@ -15,6 +15,12 @@ class NotePitch(Enum):
|
|||||||
AIS = 11
|
AIS = 11
|
||||||
H = 12
|
H = 12
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return self.__str__()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def toPitch(string):
|
def toPitch(string):
|
||||||
try:
|
try:
|
||||||
@@ -27,6 +33,13 @@ class NotePitch(Enum):
|
|||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
raise ParseError(f"Note '{string}' does not exist")
|
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:
|
class Note:
|
||||||
def __init__(self, note, octave, duration):
|
def __init__(self, note, octave, duration):
|
||||||
if type(note) == str:
|
if type(note) == str:
|
||||||
@@ -37,4 +50,4 @@ class Note:
|
|||||||
self.duration = duration
|
self.duration = duration
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.note}[{self.octave}, {self.duration}]"
|
return self.note.name
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
from Tokenizer import *
|
from Tokenizer import *
|
||||||
from Note import *
|
from Note import *
|
||||||
from AST import *
|
from AST import *
|
||||||
|
from Error import ParseError
|
||||||
class ParseError(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def expectedFound(expected, found):
|
def expectedFound(expected, found):
|
||||||
raise ParseError(f"Expected: {expected}, found: {found}")
|
raise ParseError(f"Expected: {expected}, found: {found}")
|
||||||
@@ -41,23 +39,22 @@ def parseComma(input, parent):
|
|||||||
input.pop(0)
|
input.pop(0)
|
||||||
return CommaNode()
|
return CommaNode()
|
||||||
|
|
||||||
def parseArguments(input, parent):
|
def parseList(input, parent):
|
||||||
#import pdb; pdb.set_trace()
|
|
||||||
input.pop(0)
|
input.pop(0)
|
||||||
|
|
||||||
arguments = ArgumentsNode()
|
node = ListNode()
|
||||||
|
|
||||||
while input[0].type != TokenType.CLOSE_PAREN:
|
while input[0].type != TokenType.CLOSE_PAREN:
|
||||||
argument = parseArrayElement(input, arguments)
|
element = parseArrayElement(input, node)
|
||||||
if argument is None:
|
if element is None:
|
||||||
raise ParseError(f"Line: {input[0].pos[0]+1}, col: {input[0].pos[1]+1}: Invalid function argument '{input[0].value}'")
|
raise ParseError(f"Line: {input[0].pos[0]+1}, col: {input[0].pos[1]+1}: Invalid element'{input[0].value}'")
|
||||||
arguments.append(argument) #TODO: parseExpression
|
node.append(element)
|
||||||
|
|
||||||
if input[0].type != TokenType.CLOSE_PAREN:
|
if input[0].type != TokenType.CLOSE_PAREN:
|
||||||
expectedFound(TokenType.CLOSE_PAREN, input[0].type)
|
expectedFound(TokenType.CLOSE_PAREN, input[0].type)
|
||||||
input.pop(0)
|
input.pop(0)
|
||||||
|
|
||||||
return arguments
|
return node
|
||||||
|
|
||||||
def parseBlock(input, parent):
|
def parseBlock(input, parent):
|
||||||
input.pop(0)
|
input.pop(0)
|
||||||
@@ -95,7 +92,7 @@ def parseFunctionCallOrAssignOrIdentifier(input, parent):
|
|||||||
identifier = IdentifierNode(input.pop(0).value)
|
identifier = IdentifierNode(input.pop(0).value)
|
||||||
# Function call
|
# Function call
|
||||||
if len(input) > 0 and input[0].type == TokenType.OPEN_PAREN:
|
if len(input) > 0 and input[0].type == TokenType.OPEN_PAREN:
|
||||||
arguments = parseArguments(input, parent)
|
arguments = parseList(input, parent)
|
||||||
return FunctionCallNode(identifier, arguments)
|
return FunctionCallNode(identifier, arguments)
|
||||||
# Assign
|
# Assign
|
||||||
if len(input) > 1 and input[0].type == TokenType.ASSIGN:
|
if len(input) > 1 and input[0].type == TokenType.ASSIGN:
|
||||||
@@ -125,7 +122,7 @@ def parseExpression(input, parent):
|
|||||||
if type == TokenType.PERCENT:
|
if type == TokenType.PERCENT:
|
||||||
return parsePercent(input, parent)
|
return parsePercent(input, parent)
|
||||||
if type == TokenType.OPEN_PAREN:
|
if type == TokenType.OPEN_PAREN:
|
||||||
return parseArguments(input, parent)
|
return parseList(input, parent)
|
||||||
if type == TokenType.COLON:
|
if type == TokenType.COLON:
|
||||||
return parseColon(input, parent)
|
return parseColon(input, parent)
|
||||||
|
|
||||||
Reference in New Issue
Block a user