Add support for creating custom functions
This commit is contained in:
138
AST.py
138
AST.py
@@ -1,8 +1,10 @@
|
||||
from enum import Enum
|
||||
from Note import Note
|
||||
|
||||
class Node:
|
||||
def __init__(self, pos):
|
||||
def __init__(self, parent, pos):
|
||||
self.children = []
|
||||
self.parent = parent
|
||||
self.pos = pos
|
||||
|
||||
def __repr__(self):
|
||||
@@ -19,116 +21,170 @@ class Node:
|
||||
|
||||
def pop(self, index):
|
||||
return self.children.pop(index)
|
||||
|
||||
def _print(self, level):
|
||||
print(f"{pad(level)}{self.__class__.__name__}({self.parent.__class__.__name__}):")
|
||||
for child in self.children:
|
||||
if isinstance(child, str) or isinstance(child, int) or isinstance(child, Note):
|
||||
print(pad(level+1) + f"'{child}'")
|
||||
else:
|
||||
child._print(level+1)
|
||||
|
||||
def pad(level):
|
||||
return (" " * level)
|
||||
|
||||
class Program(Node):
|
||||
def __init__(self):
|
||||
Node.__init__(self, (-1, -1))
|
||||
Node.__init__(self, None, (-1, -1))
|
||||
|
||||
def __str__(self):
|
||||
return "Program:\n" + "\n".join([str(e) for e in self.children])
|
||||
|
||||
def print(self):
|
||||
self._print(0)
|
||||
|
||||
class BlockNode(Node):
|
||||
def __init__(self, pos):
|
||||
Node.__init__(self, pos)
|
||||
def __init__(self, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
|
||||
def __str__(self):
|
||||
return "B{\n" + "\n".join([str(e) for e in self.children]) + "\n}"
|
||||
|
||||
|
||||
class ListNode(Node):
|
||||
def __init__(self, pos):
|
||||
Node.__init__(self, pos)
|
||||
def __init__(self, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
|
||||
def __str__(self):
|
||||
return "@(" + ", ".join([str(e) for e in self.children]) + ")"
|
||||
|
||||
class IdentifierNode(Node):
|
||||
def __init__(self, identifier, pos):
|
||||
Node.__init__(self, pos)
|
||||
self.identifier = identifier
|
||||
def __init__(self, identifier, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
self.children.append(identifier)
|
||||
|
||||
self.identifier = self.children[0]
|
||||
|
||||
def __str__(self):
|
||||
return f"L'{self.identifier}'"
|
||||
|
||||
class AssignExpression(Node):
|
||||
def __init__(self, target, value, pos):
|
||||
Node.__init__(self, pos)
|
||||
self.target = target
|
||||
self.value = value
|
||||
def __init__(self, target, value, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
self.children.extend([target, value])
|
||||
|
||||
self.target = self.children[0]
|
||||
self.value = self.children[1]
|
||||
|
||||
def __str__(self):
|
||||
return f"A[{self.target} = {self.value}]"
|
||||
|
||||
class AsteriskStatementNode(Node):
|
||||
def __init__(self, iterator, statement, pos):
|
||||
Node.__init__(self, pos)
|
||||
self.iterator = iterator
|
||||
self.statement = statement
|
||||
def __init__(self, iterator, statement, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
self.children.extend([iterator, statement])
|
||||
|
||||
self.iterator = self.children[0]
|
||||
self.statement = self.children[1]
|
||||
|
||||
def __str__(self):
|
||||
return f"*({self.iterator}: {self.statement})"
|
||||
|
||||
class ColonNode(Node):
|
||||
def __init__(self, a, b, pos):
|
||||
Node.__init__(self, pos)
|
||||
self.a = a
|
||||
self.b = b
|
||||
def __init__(self, a, b, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
self.children.extend([a, b])
|
||||
|
||||
self.a = self.children[0]
|
||||
self.b = self.children[1]
|
||||
|
||||
def __str__(self):
|
||||
return f":({self.a}, {self.b})"
|
||||
|
||||
class ExpressionNode(Node):
|
||||
def __init__(self, pos):
|
||||
Node.__init__(self, pos)
|
||||
def __init__(self, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.__class__.__name__}('{self.value}')"
|
||||
|
||||
|
||||
class IntegerLiteralNode(ExpressionNode):
|
||||
def __init__(self, value, pos):
|
||||
Node.__init__(self, pos)
|
||||
self.value = value
|
||||
def __init__(self, value, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
self.children.append(value)
|
||||
|
||||
self.value = self.children[0]
|
||||
|
||||
def __str__(self):
|
||||
return f"i'{self.value}'"
|
||||
|
||||
class StringLiteralNode(ExpressionNode):
|
||||
def __init__(self, value, pos):
|
||||
Node.__init__(self, pos)
|
||||
self.value = value
|
||||
def __init__(self, value, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
self.children.append(value)
|
||||
|
||||
self.value = self.children[0]
|
||||
|
||||
def __str__(self):
|
||||
return f"s'{self.value}'"
|
||||
|
||||
class NoteLiteralNode(ExpressionNode):
|
||||
def __init__(self, value, pos):
|
||||
Node.__init__(self, pos)
|
||||
self.value = value
|
||||
def __init__(self, value, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
self.children.append(value)
|
||||
|
||||
self.value = self.children[0]
|
||||
|
||||
def __str__(self):
|
||||
return f"n'{self.value.note}[{self.value.octave}, {self.value.duration}]'"
|
||||
|
||||
class FunctionCallNode(Node):
|
||||
def __init__(self, identifier, arguments, pos):
|
||||
Node.__init__(self, pos)
|
||||
self.identifier = identifier
|
||||
self.arguments = arguments
|
||||
def __init__(self, identifier, arguments, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
self.children.extend([identifier, arguments])
|
||||
|
||||
self.identifier = self.children[0]
|
||||
self.arguments = self.children[1]
|
||||
|
||||
def __str__(self):
|
||||
return f"F({self.identifier}: {self.arguments})"
|
||||
|
||||
class CommaNode(Node):
|
||||
def __init__(self, pos):
|
||||
Node.__init__(self, pos)
|
||||
def __init__(self, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
|
||||
def __str__(self):
|
||||
return "[,]"
|
||||
|
||||
class PercentNode(Node):
|
||||
def __init__(self, value, pos):
|
||||
Node.__init__(self, pos)
|
||||
self.value = value
|
||||
def __init__(self, value, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
self.children.append(value)
|
||||
|
||||
self.value = self.children[0]
|
||||
|
||||
def __str__(self):
|
||||
return f"%'{self.value}'"
|
||||
|
||||
class FunctionDefinitionNode(Node):
|
||||
def __init__(self, name, parameters, body, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
self.children.extend([name, parameters, body])
|
||||
|
||||
self.name = self.children[0]
|
||||
self.parameters = self.children[1]
|
||||
self.body = self.children[2]
|
||||
|
||||
def __str__(self):
|
||||
return f"$F'{self.name}{self.parameters}{self.body}"
|
||||
|
||||
class ReturnNode(Node):
|
||||
def __init__(self, value, parent, pos):
|
||||
Node.__init__(self, parent, pos)
|
||||
self.children.append(value)
|
||||
|
||||
self.value = self.children[0]
|
||||
|
||||
def __str__(self):
|
||||
return f"Ret({self.value})"
|
||||
|
||||
@@ -22,6 +22,8 @@ class Environment():
|
||||
def __init__(self, scopes, functions):
|
||||
self.scopes = scopes
|
||||
self.functions = functions
|
||||
self.customFunctions = {}
|
||||
self.callStack = []
|
||||
|
||||
def findVariable(self, name, type=None):
|
||||
for scope in reversed(self.scopes):
|
||||
|
||||
53
Evaluator.py
53
Evaluator.py
@@ -43,14 +43,49 @@ def objectString(obj):
|
||||
def evaluateNote(note, environment):
|
||||
return note.value
|
||||
|
||||
def evaluateFunctionCall(functionCall, environment):
|
||||
function = functionCall.identifier.identifier
|
||||
arguments = evaluateList(functionCall.arguments, environment)
|
||||
for name, definition in environment.functions.items():
|
||||
if name == function:
|
||||
return definition(arguments, environment)
|
||||
raise RuntimeException(functionCall.pos, f"Function '{function}' does not exist")
|
||||
def evaluateFunctionDefinition(definition, environment):
|
||||
name = definition.name
|
||||
params = list([p for p in definition.parameters.children if not isinstance(p, CommaNode)])
|
||||
body = definition.body.children
|
||||
|
||||
if not isinstance(definition.parent, Program):
|
||||
raise RuntimeException(name.pos, f"Functions can be defined only on the top level of script")
|
||||
|
||||
for p in params:
|
||||
if not isinstance(p, IdentifierNode):
|
||||
raise RuntimeException(p.pos, "Parameter of function definition must be an identifier")
|
||||
|
||||
if name.identifier in environment.customFunctions or name.identifier in environment.functions:
|
||||
raise RuntimeException(name.pos, f"Function '{name.identifier}' already exists")
|
||||
|
||||
environment.customFunctions[name.identifier] = {
|
||||
'params': params,
|
||||
'body': body
|
||||
}
|
||||
|
||||
def evaluateFunctionCall(functionCall, environment):
|
||||
funcName = functionCall.identifier.identifier
|
||||
arguments = evaluateList(functionCall.arguments, environment)
|
||||
for name, function in environment.customFunctions.items():
|
||||
if funcName == name:
|
||||
if len(function['params']) != len(arguments):
|
||||
raise RuntimeException(functionCall.pos, f"Calling '{funcName}' requires {len(function['params'])} and {len(arguments)} was passed")
|
||||
environment.scopes.append({ function['params'][i].identifier: v for i, v in enumerate(arguments) })
|
||||
returnValue = None
|
||||
for node in function['body']:
|
||||
if not isinstance(node, ReturnNode):
|
||||
evaluate(node, environment)
|
||||
else:
|
||||
returnValue = evaluateReturn(node, environment)
|
||||
environment.scopes.pop(-1)
|
||||
return returnValue
|
||||
for name, definition in environment.functions.items():
|
||||
if name == funcName:
|
||||
return definition(arguments, environment)
|
||||
raise RuntimeException(functionCall.pos, f"Function '{funcName}' does not exist")
|
||||
|
||||
def evaluateReturn(returnNode, environment):
|
||||
return evaluate(returnNode.value, environment)
|
||||
|
||||
def evaluateComma(comma, environment):
|
||||
pass
|
||||
@@ -109,7 +144,7 @@ def evaluateColon(colon, environment):
|
||||
return list(range(colon.a.value, colon.b.value+1))
|
||||
raise RuntimeException(colon.pos, "Invalid colon arguments")
|
||||
|
||||
def evaluate(input, environment):
|
||||
def evaluate(input, environment):
|
||||
if isinstance(input, Program):
|
||||
return evaluateProgram(input, environment)
|
||||
if isinstance(input, IntegerLiteralNode):
|
||||
@@ -120,6 +155,8 @@ def evaluate(input, environment):
|
||||
return evaluateString(input, environment)
|
||||
if isinstance(input, NoteLiteralNode):
|
||||
return evaluateNote(input, environment)
|
||||
if isinstance(input, FunctionDefinitionNode):
|
||||
return evaluateFunctionDefinition(input, environment)
|
||||
if isinstance(input, FunctionCallNode):
|
||||
return evaluateFunctionCall(input, environment)
|
||||
if isinstance(input, CommaNode):
|
||||
|
||||
106
Parser.py
106
Parser.py
@@ -6,13 +6,17 @@ from Error import SyntaxException
|
||||
def expectedFound(expected, found):
|
||||
raise SyntaxException(None, f"Expected: {expected}, found: {found}")
|
||||
|
||||
def assertType(expected, found):
|
||||
if expected != found:
|
||||
raise SyntaxException(None, f"Expected: {expected}, found: {found}")
|
||||
|
||||
def parseInteger(input, parent):
|
||||
token = input.pop(0)
|
||||
return IntegerLiteralNode(int(token.value), token.pos)
|
||||
return IntegerLiteralNode(int(token.value), parent, token.pos)
|
||||
|
||||
def parseString(input, parent):
|
||||
token = input.pop(0)
|
||||
return StringLiteralNode(token.value[1:-1], token.pos)
|
||||
return StringLiteralNode(token.value[1:-1], parent, token.pos)
|
||||
|
||||
def parseNote(input, parent):
|
||||
token = input.pop(0)
|
||||
@@ -40,16 +44,16 @@ def parseNote(input, parent):
|
||||
dot = True
|
||||
consumedChars += 1
|
||||
|
||||
return NoteLiteralNode(Note(notePitch, octave, duration, dot), token.pos)
|
||||
return NoteLiteralNode(Note(notePitch, octave, duration, dot), parent, token.pos)
|
||||
|
||||
def parseComma(input, parent):
|
||||
token = input.pop(0)
|
||||
return CommaNode(token.pos)
|
||||
return CommaNode(parent, token.pos)
|
||||
|
||||
def parseList(input, parent):
|
||||
token = input.pop(0)
|
||||
|
||||
node = ListNode(token.pos)
|
||||
node = ListNode(parent, token.pos)
|
||||
|
||||
while input[0].type != TokenType.CLOSE_PAREN:
|
||||
element = parseArrayElement(input, node)
|
||||
@@ -66,7 +70,7 @@ def parseList(input, parent):
|
||||
def parseBlock(input, parent):
|
||||
token = input.pop(0)
|
||||
|
||||
block = BlockNode(token.pos)
|
||||
block = BlockNode(parent, token.pos)
|
||||
|
||||
while input[0].type != TokenType.CLOSE_BRACKET:
|
||||
block.append(parseToken(input, block))
|
||||
@@ -82,75 +86,119 @@ def parseAsterisk(input, parent):
|
||||
token = input.pop(0)
|
||||
|
||||
iterator = parent.pop(-1)
|
||||
value = parseStatement(input, parent) #TODO: only statements! (?)
|
||||
value = parseStatement(input, parent)
|
||||
|
||||
return AsteriskStatementNode(iterator, value, token.pos)
|
||||
asterisk = AsteriskStatementNode(iterator, value, parent, token.pos)
|
||||
iterator.parent = asterisk
|
||||
value.parent = asterisk
|
||||
return asterisk
|
||||
|
||||
def parseNoteOrColon(input, parent):
|
||||
note = parseNote(input, parent)
|
||||
if len(input) > 1 and input[0].type == TokenType.COLON:
|
||||
token = input.pop(0)
|
||||
b = parseNote(input, parent) #TODO: only expressions!
|
||||
b = parseNote(input, parent)
|
||||
if b is None:
|
||||
raise SyntaxException(input[0].pos, f"Invalid colon argument '{input[0].value}'")
|
||||
return ColonNode(note, b, token.pos)
|
||||
colon = ColonNode(note, b, parent, token.pos)
|
||||
note.parent = colon
|
||||
b.parent = colon
|
||||
return colon
|
||||
|
||||
return note
|
||||
|
||||
def parseIntegerOrColon(input, parent):
|
||||
def parseIntegerOrColonOrPercent(input, parent):
|
||||
integer = parseInteger(input, parent)
|
||||
if len(input) > 1 and input[0].type == TokenType.COLON:
|
||||
token = input.pop(0)
|
||||
b = parseInteger(input, parent) #TODO: only expressions!
|
||||
b = parseInteger(input, parent)
|
||||
if b is None:
|
||||
raise SyntaxException(input[0].pos, f"Invalid colon argument '{input[0].value}'")
|
||||
return ColonNode(integer, b, token.pos)
|
||||
colon = ColonNode(integer, b, parent, token.pos)
|
||||
integer.parent = colon
|
||||
b.parent = colon
|
||||
return colon
|
||||
|
||||
if len(input) > 0 and input[0].type == TokenType.PERCENT:
|
||||
input.pop(0)
|
||||
percent = PercentNode(integer, parent, integer.pos)
|
||||
integer.parent = percent
|
||||
return percent
|
||||
|
||||
return integer
|
||||
|
||||
def parseFunctionCallOrAssignOrIdentifier(input, parent):
|
||||
token = input.pop(0)
|
||||
identifier = IdentifierNode(token.value, token.pos)
|
||||
identifier = IdentifierNode(token.value, parent, token.pos)
|
||||
# Function call
|
||||
if len(input) > 0 and input[0].type == TokenType.OPEN_PAREN:
|
||||
arguments = parseList(input, parent)
|
||||
return FunctionCallNode(identifier, arguments, token.pos)
|
||||
func = FunctionCallNode(identifier, arguments, parent, token.pos)
|
||||
identifier.parent = func
|
||||
arguments.parent = func
|
||||
return func
|
||||
# Assign
|
||||
if len(input) > 1 and input[0].type == TokenType.ASSIGN:
|
||||
token = input.pop(0)
|
||||
value = parseExpression(input, parent) #TODO: only expressions!
|
||||
return AssignExpression(identifier, value, token.pos)
|
||||
value = parseExpression(input, parent) #
|
||||
assign = AssignExpression(identifier, value, parent, token.pos)
|
||||
identifier.parent = assign
|
||||
value.parent = assign
|
||||
return assign
|
||||
|
||||
return identifier
|
||||
|
||||
def parsePercent(input, parent):
|
||||
token = input.pop(0)
|
||||
|
||||
value = parent.pop(-1)
|
||||
|
||||
return PercentNode(value, token.pos)
|
||||
|
||||
def parseMinus(input, parent):
|
||||
token = input.pop(0)
|
||||
|
||||
value = parseInteger(input, parent)
|
||||
|
||||
return IntegerLiteralNode(-value.value, token.pos)
|
||||
return IntegerLiteralNode(-value.value, parent, token.pos)
|
||||
|
||||
def parseFunctionDefinition(input, parent):
|
||||
input.pop(0)
|
||||
|
||||
assertType(TokenType.IDENTIFIER, input[0].type)
|
||||
token = input.pop(0)
|
||||
name = IdentifierNode(token.value, parent, token.pos)
|
||||
|
||||
assertType(TokenType.OPEN_PAREN, input[0].type)
|
||||
parameters = parseList(input, parent)
|
||||
|
||||
assertType(TokenType.OPEN_BRACKET, input[0].type)
|
||||
body = parseBlock(input, parent)
|
||||
|
||||
func = FunctionDefinitionNode(name, parameters, body, parent, token.pos)
|
||||
name.parent = func
|
||||
parameters.parent = func
|
||||
body.parent = func
|
||||
return func
|
||||
|
||||
def parseReturn(input, parent):
|
||||
token = input.pop(0)
|
||||
|
||||
value = parseExpression(input, parent)
|
||||
|
||||
returnNode = ReturnNode(value, parent, token.pos)
|
||||
value.parent = returnNode
|
||||
return returnNode
|
||||
|
||||
def parseExpression(input, parent):
|
||||
type = input[0].type
|
||||
if type == TokenType.FUNCTION:
|
||||
return parseFunctionDefinition(input, parent)
|
||||
if type == TokenType.RETURN:
|
||||
return parseReturn(input, parent)
|
||||
if type == TokenType.MINUS:
|
||||
return parseMinus(input, parent)
|
||||
if type == TokenType.INTEGER:
|
||||
return parseIntegerOrColon(input, parent)
|
||||
return parseIntegerOrColonOrPercent(input, parent)
|
||||
if type == TokenType.STRING:
|
||||
return parseString(input, parent)
|
||||
if type == TokenType.NOTE:
|
||||
return parseNoteOrColon(input, parent)
|
||||
if type == TokenType.IDENTIFIER:
|
||||
return parseFunctionCallOrAssignOrIdentifier(input, parent)
|
||||
if type == TokenType.PERCENT:
|
||||
return parsePercent(input, parent)
|
||||
return parseFunctionCallOrAssignOrIdentifier(input, parent)
|
||||
if type == TokenType.OPEN_PAREN:
|
||||
return parseList(input, parent)
|
||||
raise SyntaxException(input[0].pos, f"Unexpected character '{input[0].value}'")
|
||||
|
||||
15
Tokenizer.py
15
Tokenizer.py
@@ -20,6 +20,8 @@ class TokenType(Enum):
|
||||
COMMENT = 13
|
||||
PERCENT = 14
|
||||
MINUS = 15
|
||||
FUNCTION = 16
|
||||
RETURN = 17
|
||||
|
||||
class Token:
|
||||
def __init__(self, type, value, pos):
|
||||
@@ -154,11 +156,24 @@ def tokenizeMinus(input, current, line):
|
||||
return (1, Token(TokenType.MINUS, input[current], (line, current)))
|
||||
return (0, None)
|
||||
|
||||
def tokenizeFunction(input, current, line):
|
||||
return tokenizeKeyword(TokenType.FUNCTION, 'function', input, current, line)
|
||||
|
||||
def tokenizeKeyword(type, keyword, input, current, line):
|
||||
if len(input) >= current+len(keyword) and input[current:current+len(keyword)] == keyword:
|
||||
return (len(keyword), Token(type, keyword, (line, current)))
|
||||
return (0, None)
|
||||
|
||||
def tokenizeReturn(input, current, line):
|
||||
return tokenizeKeyword(TokenType.RETURN, 'return', input, current, line)
|
||||
|
||||
tokenizers = (
|
||||
tokenizeOpenParen,
|
||||
tokenizeCloseParen,
|
||||
tokenizeAsterisk,
|
||||
tokenizeString,
|
||||
tokenizeFunction,
|
||||
tokenizeReturn,
|
||||
tokenizeInteger,
|
||||
tokenizeNote,
|
||||
tokenizeIdentifier,
|
||||
|
||||
Reference in New Issue
Block a user