Compare commits
4 Commits
add-polyph
...
new-parser
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
49e4c4487e | ||
|
|
e7674a4834 | ||
|
|
ed2c8dc6dd | ||
|
|
f91e2a75de |
@@ -1,6 +1,7 @@
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.ast.parser import Parsers
|
||||
from smnp.token.type import TokenType
|
||||
from smnp.util.singleton import SingletonParser
|
||||
|
||||
|
||||
class Atom(Node):
|
||||
@@ -27,10 +28,6 @@ class IntegerLiteral(Atom):
|
||||
pass
|
||||
|
||||
|
||||
class FloatLiteral(Atom):
|
||||
pass
|
||||
|
||||
|
||||
class StringLiteral(Atom):
|
||||
pass
|
||||
|
||||
@@ -47,63 +44,60 @@ class TypeLiteral(Atom):
|
||||
pass
|
||||
|
||||
|
||||
def IntegerParser(input):
|
||||
return Parser.terminal(TokenType.INTEGER, createNode=IntegerLiteral.withValue)(input)
|
||||
|
||||
|
||||
def FloatParser(input):
|
||||
return Parser.terminal(TokenType.FLOAT, createNode=FloatLiteral.withValue)(input)
|
||||
|
||||
|
||||
def StringParser(input):
|
||||
return Parser.terminal(TokenType.STRING, createNode=StringLiteral.withValue)(input)
|
||||
|
||||
|
||||
def NoteParser(input):
|
||||
return Parser.terminal(TokenType.NOTE, createNode=NoteLiteral.withValue)(input)
|
||||
|
||||
|
||||
def BoolParser(input):
|
||||
return Parser.terminal(TokenType.BOOL, createNode=BoolLiteral.withValue)(input)
|
||||
|
||||
|
||||
def TypeLiteralParser(input):
|
||||
return Parser.terminal(TokenType.TYPE, createNode=TypeLiteral.withValue)(input)
|
||||
|
||||
|
||||
def LiteralParser(input):
|
||||
return Parser.oneOf(
|
||||
IntegerParser,
|
||||
FloatParser,
|
||||
StringParser,
|
||||
NoteParser,
|
||||
BoolParser,
|
||||
TypeLiteralParser,
|
||||
name="literal"
|
||||
)(input)
|
||||
|
||||
|
||||
def AtomParser(input):
|
||||
from smnp.ast.node.identifier import IdentifierParser
|
||||
from smnp.ast.node.list import ListParser
|
||||
from smnp.ast.node.map import MapParser
|
||||
from smnp.ast.node.expression import ExpressionParser
|
||||
|
||||
parentheses = Parser.allOf(
|
||||
Parser.terminal(TokenType.OPEN_PAREN),
|
||||
Parser.doAssert(ExpressionParser, "expression"),
|
||||
Parser.terminal(TokenType.CLOSE_PAREN),
|
||||
createNode=lambda open, expr, close: expr,
|
||||
name="grouping parentheses"
|
||||
@SingletonParser
|
||||
def IntegerParser():
|
||||
return Parsers.oneOf(
|
||||
Parsers.terminal(TokenType.INTEGER, lambda val, pos: IntegerLiteral.withValue(int(val), pos)),
|
||||
Parsers.allOf(
|
||||
Parsers.terminal(TokenType.MINUS),
|
||||
Parsers.terminal(TokenType.INTEGER, lambda val, pos: IntegerLiteral.withValue(int(val), pos)),
|
||||
createNode=lambda minus, integer: IntegerLiteral.withValue(-integer.value, minus.pos),
|
||||
name="negativeInteger"
|
||||
),
|
||||
name="int"
|
||||
)
|
||||
|
||||
return Parser.oneOf(
|
||||
parentheses,
|
||||
LiteralParser,
|
||||
IdentifierParser,
|
||||
ListParser,
|
||||
MapParser,
|
||||
|
||||
@SingletonParser
|
||||
def StringParser():
|
||||
return Parsers.terminal(TokenType.STRING, createNode=StringLiteral.withValue)
|
||||
|
||||
|
||||
@SingletonParser
|
||||
def NoteParser():
|
||||
return Parsers.terminal(TokenType.NOTE, createNode=NoteLiteral.withValue)
|
||||
|
||||
|
||||
@SingletonParser
|
||||
def BoolParser():
|
||||
return Parsers.terminal(TokenType.BOOL, createNode=BoolLiteral.withValue)
|
||||
|
||||
|
||||
@SingletonParser
|
||||
def TypeParser():
|
||||
return Parsers.terminal(TokenType.TYPE, createNode=TypeLiteral.withValue)
|
||||
|
||||
|
||||
@SingletonParser
|
||||
def LiteralParser():
|
||||
return Parsers.oneOf(
|
||||
IntegerParser(),
|
||||
StringParser(),
|
||||
NoteParser(),
|
||||
BoolParser(),
|
||||
TypeParser(),
|
||||
name="literal"
|
||||
)
|
||||
|
||||
|
||||
@SingletonParser
|
||||
def AtomParser():
|
||||
from smnp.ast.node.identifier import IdentifierParser
|
||||
|
||||
return Parsers.oneOf(
|
||||
LiteralParser(),
|
||||
IdentifierParser(),
|
||||
name="atom"
|
||||
)(input)
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -9,9 +9,11 @@ class Block(Node):
|
||||
|
||||
|
||||
def BlockParser(input):
|
||||
return Parser.loop(
|
||||
Parser.terminal(TokenType.OPEN_CURLY),
|
||||
parser = Parser.loop(
|
||||
Parser.terminalParser(TokenType.OPEN_CURLY),
|
||||
Parser.doAssert(StatementParser, f"statement or '{TokenType.CLOSE_CURLY.key}'"),
|
||||
Parser.terminal(TokenType.CLOSE_CURLY),
|
||||
Parser.terminalParser(TokenType.CLOSE_CURLY),
|
||||
createNode=lambda open, statements, close: Block.withChildren(statements, open.pos)
|
||||
)(input)
|
||||
)
|
||||
|
||||
return Parser(parser, "block", [parser])(input)
|
||||
|
||||
32
smnp/ast/node/chain.py
Normal file
32
smnp/ast/node/chain.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from smnp.ast.node.list import ListParser
|
||||
from smnp.ast.node.operator import BinaryOperator
|
||||
from smnp.ast.node.valuable import Valuable
|
||||
from smnp.ast.parser import Parsers
|
||||
from smnp.token.type import TokenType
|
||||
from smnp.util.singleton import SingletonParser
|
||||
|
||||
|
||||
class Chain(Valuable):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@SingletonParser
|
||||
def ChainParser():
|
||||
from smnp.ast.node.atom import AtomParser
|
||||
|
||||
itemParser = Parsers.oneOf(
|
||||
ListParser,
|
||||
#MapParser,
|
||||
AtomParser,
|
||||
name="chainItem"
|
||||
)
|
||||
|
||||
return Parsers.leftAssociativeOperatorParser(
|
||||
itemParser,
|
||||
[TokenType.DOT],
|
||||
itemParser,
|
||||
lambda left, op, right: Chain.withValue(BinaryOperator.withValues(left, op, right)),
|
||||
name="chain"
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from smnp.ast.node.expression import ExpressionParser
|
||||
from smnp.ast.node.expression import MaxPrecedenceExpressionParser
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.ast.node.none import NoneNode
|
||||
from smnp.ast.node.statement import StatementParser
|
||||
@@ -46,23 +46,23 @@ class IfElse(Node):
|
||||
|
||||
def IfElseStatementParser(input):
|
||||
ifStatementParser = Parser.allOf(
|
||||
Parser.terminal(TokenType.IF),
|
||||
Parser.terminal(TokenType.OPEN_PAREN),
|
||||
ExpressionParser,
|
||||
Parser.terminal(TokenType.CLOSE_PAREN),
|
||||
Parser.terminalParser(TokenType.IF),
|
||||
Parser.terminalParser(TokenType.OPEN_PAREN),
|
||||
MaxPrecedenceExpressionParser,
|
||||
Parser.terminalParser(TokenType.CLOSE_PAREN),
|
||||
StatementParser,
|
||||
createNode=lambda _, __, condition, ___, ifStatement: IfElse.createNode(ifStatement, condition),
|
||||
name="if statement"
|
||||
)
|
||||
|
||||
ifElseStatementParser = Parser.allOf(
|
||||
Parser.terminal(TokenType.IF),
|
||||
Parser.terminal(TokenType.OPEN_PAREN, doAssert=True),
|
||||
Parser.doAssert(ExpressionParser, "expression"),
|
||||
Parser.terminal(TokenType.CLOSE_PAREN, doAssert=True),
|
||||
Parser.doAssert(StatementParser, "statement"),
|
||||
Parser.terminal(TokenType.ELSE),
|
||||
Parser.doAssert(StatementParser, "statement"),
|
||||
Parser.terminalParser(TokenType.IF),
|
||||
Parser.terminalParser(TokenType.OPEN_PAREN),
|
||||
MaxPrecedenceExpressionParser,
|
||||
Parser.terminalParser(TokenType.CLOSE_PAREN),
|
||||
StatementParser,
|
||||
Parser.terminalParser(TokenType.ELSE),
|
||||
StatementParser,
|
||||
createNode=lambda _, __, condition, ___, ifStatement, ____, elseStatement: IfElse.createNode(ifStatement, condition, elseStatement),
|
||||
name="if-else statement"
|
||||
)
|
||||
|
||||
@@ -1,128 +1,63 @@
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.ast.node.none import NoneNode
|
||||
from smnp.ast.node.operator import BinaryOperator, Operator
|
||||
from smnp.ast.node.operator import BinaryOperator
|
||||
from smnp.ast.node.term import TermParser
|
||||
from smnp.ast.node.valuable import Valuable
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
class Sum(BinaryOperator):
|
||||
class Expression(Valuable):
|
||||
pass
|
||||
|
||||
|
||||
class Relation(BinaryOperator):
|
||||
pass
|
||||
|
||||
|
||||
class And(BinaryOperator):
|
||||
pass
|
||||
|
||||
|
||||
class Or(BinaryOperator):
|
||||
pass
|
||||
|
||||
|
||||
class Loop(BinaryOperator):
|
||||
def __init__(self, pos):
|
||||
super().__init__(pos)
|
||||
self.children.extend([NoneNode(), NoneNode()])
|
||||
|
||||
@property
|
||||
def parameters(self):
|
||||
return self[3]
|
||||
|
||||
@parameters.setter
|
||||
def parameters(self, value):
|
||||
self[3] = value
|
||||
|
||||
@property
|
||||
def filter(self):
|
||||
return self[4]
|
||||
|
||||
@filter.setter
|
||||
def filter(self, value):
|
||||
self[4] = value
|
||||
|
||||
@classmethod
|
||||
def loop(cls, left, parameters, operator, right, filter):
|
||||
node = cls(left.pos)
|
||||
node.left = left
|
||||
node.parameters = parameters
|
||||
node.operator = operator
|
||||
node.right = right
|
||||
node.filter = filter
|
||||
return node
|
||||
|
||||
|
||||
class LoopParameters(Node):
|
||||
pass
|
||||
|
||||
|
||||
def ExpressionWithoutLoopParser(input):
|
||||
expr1 = Parser.leftAssociativeOperatorParser(
|
||||
TermParser,
|
||||
[TokenType.PLUS, TokenType.MINUS],
|
||||
TermParser,
|
||||
lambda left, op, right: Sum.withValues(left, op, right)
|
||||
)
|
||||
|
||||
expr2 = Parser.leftAssociativeOperatorParser(
|
||||
expr1,
|
||||
[TokenType.RELATION, TokenType.OPEN_ANGLE, TokenType.CLOSE_ANGLE],
|
||||
expr1,
|
||||
lambda left, op, right: Relation.withValues(left, op, right)
|
||||
)
|
||||
|
||||
expr3 = Parser.leftAssociativeOperatorParser(
|
||||
expr2,
|
||||
[TokenType.AND],
|
||||
expr2,
|
||||
lambda left, op, right: And.withValues(left, op, right)
|
||||
)
|
||||
|
||||
return Parser.leftAssociativeOperatorParser(
|
||||
expr3,
|
||||
[TokenType.OR],
|
||||
expr3,
|
||||
lambda left, op, right: Or.withValues(left, op, right)
|
||||
)(input)
|
||||
|
||||
|
||||
def LoopParser(input):
|
||||
from smnp.ast.node.identifier import IdentifierLiteralParser
|
||||
from smnp.ast.node.iterable import abstractIterableParser
|
||||
from smnp.ast.node.statement import StatementParser
|
||||
|
||||
loopParameters = Parser.allOf(
|
||||
Parser.terminal(TokenType.AS),
|
||||
Parser.oneOf(
|
||||
Parser.wrap(IdentifierLiteralParser, lambda id: LoopParameters.withChildren([id], id.pos)),
|
||||
abstractIterableParser(LoopParameters, TokenType.OPEN_PAREN, TokenType.CLOSE_PAREN, IdentifierLiteralParser)
|
||||
),
|
||||
createNode=lambda asKeyword, parameters: parameters,
|
||||
name="loop parameters"
|
||||
)
|
||||
|
||||
loopFilter = Parser.allOf(
|
||||
Parser.terminal(TokenType.PERCENT),
|
||||
Parser.doAssert(ExpressionWithoutLoopParser, "filter as bool expression"),
|
||||
createNode=lambda percent, expr: expr,
|
||||
name="loop filter"
|
||||
)
|
||||
|
||||
return Parser.allOf(
|
||||
ExpressionWithoutLoopParser,
|
||||
Parser.optional(loopParameters),
|
||||
Parser.terminal(TokenType.DASH, createNode=Operator.withValue),
|
||||
StatementParser,
|
||||
Parser.optional(loopFilter),
|
||||
createNode=Loop.loop,
|
||||
name="dash-loop"
|
||||
)(input)
|
||||
|
||||
|
||||
def ExpressionParser(input):
|
||||
return Parser.oneOf(
|
||||
LoopParser,
|
||||
ExpressionWithoutLoopParser
|
||||
return Parser.leftAssociativeOperatorParser(
|
||||
TermParser,
|
||||
[TokenType.PLUS, TokenType.MINUS],
|
||||
TermParser,
|
||||
lambda left, op, right: Expression.withValue(BinaryOperator.withValues(left, op, right))
|
||||
)(input)
|
||||
|
||||
|
||||
def Expression2Parser(input):
|
||||
return Parser.leftAssociativeOperatorParser(
|
||||
ExpressionParser,
|
||||
[TokenType.RELATION],
|
||||
ExpressionParser,
|
||||
lambda left, op, right: Expression.withValue(BinaryOperator.withValues(left, op, right))
|
||||
)(input)
|
||||
|
||||
|
||||
def Expression3Parser(input):
|
||||
return Parser.leftAssociativeOperatorParser(
|
||||
Expression2Parser,
|
||||
[TokenType.AND],
|
||||
Expression2Parser,
|
||||
lambda left, op, right: Expression.withValue(BinaryOperator.withValues(left, op, right))
|
||||
)(input)
|
||||
|
||||
|
||||
def Expression4Parser(input):
|
||||
from smnp.ast.node.condition import IfElse
|
||||
exprParser = Parser.leftAssociativeOperatorParser(
|
||||
Expression3Parser,
|
||||
[TokenType.OR],
|
||||
Expression3Parser,
|
||||
lambda left, op, right: Expression.withValue(BinaryOperator.withValues(left, op, right))
|
||||
)
|
||||
|
||||
ifElseExpression = Parser.allOf(
|
||||
exprParser,
|
||||
Parser.terminalParser(TokenType.IF),
|
||||
Expression4Parser,
|
||||
Parser.terminalParser(TokenType.ELSE),
|
||||
Expression4Parser,
|
||||
createNode=lambda ifNode, _, condition, __, elseNode: IfElse.createNode(ifNode, condition, elseNode)
|
||||
)
|
||||
|
||||
return Parser.oneOf(
|
||||
ifElseExpression,
|
||||
exprParser,
|
||||
)(input)
|
||||
|
||||
|
||||
MaxPrecedenceExpressionParser = Expression4Parser
|
||||
@@ -1,88 +1,70 @@
|
||||
from smnp.ast.node.block import Block
|
||||
from smnp.ast.node.function import FunctionDefinitionParser
|
||||
from smnp.ast.node.identifier import IdentifierLiteralParser
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.ast.node.none import NoneNode
|
||||
from smnp.ast.node.type import TypeParser
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
class Extend(Node):
|
||||
def __init__(self, pos):
|
||||
super().__init__(pos)
|
||||
self.children = [NoneNode(), NoneNode(), NoneNode()]
|
||||
|
||||
@property
|
||||
def type(self):
|
||||
return self[0]
|
||||
|
||||
@type.setter
|
||||
def type(self, value):
|
||||
self[0] = value
|
||||
|
||||
@property
|
||||
def variable(self):
|
||||
return self[1]
|
||||
|
||||
@variable.setter
|
||||
def variable(self, value):
|
||||
self[1] = value
|
||||
|
||||
@property
|
||||
def methods(self):
|
||||
return self[2]
|
||||
|
||||
@methods.setter
|
||||
def methods(self, value):
|
||||
self[2] = value
|
||||
|
||||
@classmethod
|
||||
def withValues(cls, pos, type, variable, methods):
|
||||
node = cls(pos)
|
||||
node.type = type
|
||||
node.variable = variable
|
||||
node.methods = methods
|
||||
return node
|
||||
|
||||
|
||||
def ExtendParser(input):
|
||||
|
||||
simpleExtend = Parser.allOf(
|
||||
Parser.terminal(TokenType.EXTEND),
|
||||
TypeParser,
|
||||
Parser.terminal(TokenType.AS),
|
||||
IdentifierLiteralParser,
|
||||
Parser.terminal(TokenType.WITH),
|
||||
Parser.doAssert(Parser.wrap(FunctionDefinitionParser, lambda method: Block.withChildren([ method ], method.pos)), "method definition"),
|
||||
createNode=lambda extend, type, _, variable, __, methods: Extend.withValues(extend.pos, type, variable, methods),
|
||||
name="simple extend"
|
||||
)
|
||||
|
||||
multiExtend = Parser.allOf(
|
||||
Parser.terminal(TokenType.EXTEND),
|
||||
Parser.doAssert(TypeParser, "type being extended"),
|
||||
Parser.terminal(TokenType.AS, doAssert=True),
|
||||
Parser.doAssert(IdentifierLiteralParser, "variable name"),
|
||||
Parser.doAssert(MethodsDeclarationParser, f"block with methods definitions or '{TokenType.WITH.key}' keyword"),
|
||||
createNode=lambda extend, type, _, variable, methods: Extend.withValues(extend.pos, type, variable, methods),
|
||||
name="multiple extend"
|
||||
)
|
||||
|
||||
|
||||
return Parser.oneOf(
|
||||
simpleExtend,
|
||||
multiExtend,
|
||||
name="extend"
|
||||
)(input)
|
||||
|
||||
|
||||
def MethodsDeclarationParser(input):
|
||||
return Parser.loop(
|
||||
Parser.terminal(TokenType.OPEN_CURLY),
|
||||
Parser.doAssert(FunctionDefinitionParser, f"method definition or '{TokenType.CLOSE_CURLY.key}'"),
|
||||
Parser.terminal(TokenType.CLOSE_CURLY),
|
||||
createNode=lambda open, methods, close: Block.withChildren(methods, open.pos),
|
||||
name="methods block"
|
||||
)(input)
|
||||
|
||||
# from smnp.ast.node.block import BlockNode
|
||||
# from smnp.ast.node.function import FunctionDefinitionNode
|
||||
# from smnp.ast.node.identifier import IdentifierNode
|
||||
# from smnp.ast.node.none import NoneNode
|
||||
# from smnp.ast.node.statement import StatementNode
|
||||
# from smnp.ast.node.type import TypeNode
|
||||
# from smnp.ast.parser import Parser
|
||||
# from smnp.token.type import TokenType
|
||||
#
|
||||
#
|
||||
# class ExtendNode(StatementNode):
|
||||
# def __init__(self, pos):
|
||||
# super().__init__(pos)
|
||||
# self.children = [NoneNode(), NoneNode(), NoneNode()]
|
||||
#
|
||||
# @property
|
||||
# def type(self):
|
||||
# return self[0]
|
||||
#
|
||||
# @type.setter
|
||||
# def type(self, value):
|
||||
# self[0] = value
|
||||
#
|
||||
# @property
|
||||
# def variable(self):
|
||||
# return self[1]
|
||||
#
|
||||
# @variable.setter
|
||||
# def variable(self, value):
|
||||
# self[1] = value
|
||||
#
|
||||
# @property
|
||||
# def methods(self):
|
||||
# return self[2]
|
||||
#
|
||||
# @methods.setter
|
||||
# def methods(self, value):
|
||||
# self[2] = value
|
||||
#
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# def createNode(extend, type, asKeyword, variable, methods):
|
||||
# node = ExtendNode(extend.pos)
|
||||
# node.type = type
|
||||
# node.variable = variable
|
||||
# node.methods = methods
|
||||
# return node
|
||||
#
|
||||
# return Parser.allOf(
|
||||
# Parser.terminalParser(TokenType.EXTEND),
|
||||
# Parser.doAssert(TypeNode.parse, "type being extended"),
|
||||
# Parser.terminalParser(TokenType.AS, doAssert=True),
|
||||
# Parser.doAssert(IdentifierNode.identifierParser(), "variable name"),
|
||||
# Parser.doAssert(cls._methodsDeclarationsParser(), "methods declarations"),
|
||||
# createNode=createNode
|
||||
# )(input)
|
||||
#
|
||||
# @classmethod
|
||||
# def _methodsDeclarationsParser(cls):
|
||||
# def createNode(openBracket, items, closeBracket):
|
||||
# node = BlockNode(openBracket.pos)
|
||||
# node.children = items
|
||||
# return node
|
||||
#
|
||||
# return Parser.loop(
|
||||
# Parser.terminalParser(TokenType.OPEN_CURLY),
|
||||
# Parser.doAssert(FunctionDefinitionNode.parse, f"method declaration or '{TokenType.CLOSE_CURLY.key}'"),
|
||||
# Parser.terminalParser(TokenType.CLOSE_CURLY),
|
||||
# createNode=createNode
|
||||
# )
|
||||
@@ -1,34 +1,64 @@
|
||||
from smnp.ast.node.chain import ChainParser
|
||||
from smnp.ast.node.operator import BinaryOperator, Operator, UnaryOperator
|
||||
from smnp.ast.node.unit import UnitParser
|
||||
from smnp.ast.node.valuable import Valuable
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
class Factor(Valuable):
|
||||
pass
|
||||
|
||||
|
||||
class NotOperator(UnaryOperator):
|
||||
pass
|
||||
|
||||
|
||||
class Power(BinaryOperator):
|
||||
class Loop(BinaryOperator):
|
||||
pass
|
||||
|
||||
|
||||
def FactorParser(input):
|
||||
from smnp.ast.node.expression import MaxPrecedenceExpressionParser
|
||||
from smnp.ast.node.statement import StatementParser
|
||||
|
||||
powerFactor = Parser.leftAssociativeOperatorParser(
|
||||
UnitParser,
|
||||
ChainParser,
|
||||
[TokenType.DOUBLE_ASTERISK],
|
||||
UnitParser,
|
||||
lambda left, op, right: Power.withValues(left, op, right),
|
||||
ChainParser,
|
||||
lambda left, op, right: Factor.withValue(BinaryOperator.withValues(left, op, right)),
|
||||
name="power operator"
|
||||
)
|
||||
|
||||
notOperator = Parser.allOf(
|
||||
Parser.terminal(TokenType.NOT, Operator.withValue),
|
||||
exprFactor = Parser.allOf(
|
||||
Parser.terminalParser(TokenType.OPEN_PAREN),
|
||||
MaxPrecedenceExpressionParser,
|
||||
Parser.terminalParser(TokenType.CLOSE_PAREN),
|
||||
createNode=lambda open, expr, close: expr,
|
||||
name="grouping parentheses"
|
||||
)
|
||||
|
||||
factorParser = Parser.oneOf(
|
||||
powerFactor,
|
||||
exprFactor,
|
||||
name="basic factor"
|
||||
)
|
||||
|
||||
notOperator = Parser.allOf(
|
||||
Parser.terminalParser(TokenType.NOT, Operator.withValue),
|
||||
factorParser,
|
||||
createNode=NotOperator.withValues,
|
||||
name="not"
|
||||
)
|
||||
|
||||
loopFactor = Parser.allOf(
|
||||
factorParser,
|
||||
Parser.terminalParser(TokenType.DASH, createNode=Operator.withValue),
|
||||
StatementParser,
|
||||
createNode=Loop.withValues,
|
||||
name="dash-loop"
|
||||
)
|
||||
|
||||
return Parser.oneOf(
|
||||
loopFactor,
|
||||
notOperator,
|
||||
powerFactor,
|
||||
name="factor"
|
||||
factorParser
|
||||
)(input)
|
||||
|
||||
@@ -1,165 +1,128 @@
|
||||
from smnp.ast.node.block import BlockParser
|
||||
from smnp.ast.node.expression import ExpressionParser
|
||||
from smnp.ast.node.identifier import IdentifierLiteralParser
|
||||
from smnp.ast.node.iterable import abstractIterableParser
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.ast.node.none import NoneNode
|
||||
from smnp.ast.node.type import TypeParser, Type
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
class ArgumentsDeclaration(Node):
|
||||
pass
|
||||
|
||||
|
||||
class Argument(Node):
|
||||
|
||||
def __init__(self, pos):
|
||||
super().__init__(pos)
|
||||
self.children = [NoneNode(), NoneNode(), False, NoneNode()]
|
||||
|
||||
@property
|
||||
def type(self):
|
||||
return self[0]
|
||||
|
||||
|
||||
@type.setter
|
||||
def type(self, value):
|
||||
self[0] = value
|
||||
|
||||
|
||||
@property
|
||||
def variable(self):
|
||||
return self[1]
|
||||
|
||||
|
||||
@variable.setter
|
||||
def variable(self, value):
|
||||
self[1] = value
|
||||
|
||||
|
||||
@property
|
||||
def vararg(self):
|
||||
return self[2]
|
||||
|
||||
|
||||
@vararg.setter
|
||||
def vararg(self, value):
|
||||
self[2] = value
|
||||
|
||||
|
||||
@property
|
||||
def optionalValue(self):
|
||||
return self[3]
|
||||
|
||||
@optionalValue.setter
|
||||
def optionalValue(self, value):
|
||||
self[3] = value
|
||||
|
||||
class VarargNode(Node):
|
||||
pass
|
||||
|
||||
|
||||
class FunctionDefinition(Node):
|
||||
def __init__(self, pos):
|
||||
super().__init__(pos)
|
||||
self.children = [NoneNode(), NoneNode(), NoneNode()]
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self[0]
|
||||
|
||||
@name.setter
|
||||
def name(self, value):
|
||||
self[0] = value
|
||||
|
||||
@property
|
||||
def arguments(self):
|
||||
return self[1]
|
||||
|
||||
@arguments.setter
|
||||
def arguments(self, value):
|
||||
self[1] = value
|
||||
|
||||
@property
|
||||
def body(self):
|
||||
return self[2]
|
||||
|
||||
@body.setter
|
||||
def body(self, value):
|
||||
self[2] = value
|
||||
|
||||
@classmethod
|
||||
def withValues(cls, name, arguments, body):
|
||||
node = cls(name.pos)
|
||||
node.name = name
|
||||
node.arguments = arguments
|
||||
node.body = body
|
||||
return node
|
||||
|
||||
|
||||
def RegularArgumentParser(input):
|
||||
def createNode(type, variable, vararg):
|
||||
pos = type.pos if isinstance(type, Type) else variable.pos
|
||||
node = Argument(pos)
|
||||
node.type = type
|
||||
node.variable = variable
|
||||
node.vararg = vararg is True
|
||||
return node
|
||||
|
||||
return Parser.allOf(
|
||||
Parser.optional(TypeParser),
|
||||
Parser.doAssert(IdentifierLiteralParser, "argument name"),
|
||||
Parser.optional(Parser.terminal(TokenType.DOTS, lambda val, pos: True)),
|
||||
createNode=createNode,
|
||||
name="regular function argument"
|
||||
)(input)
|
||||
|
||||
|
||||
def OptionalArgumentParser(input):
|
||||
def createNode(type, variable, _, optional):
|
||||
pos = type.pos if isinstance(type, Type) else variable.pos
|
||||
node = Argument(pos)
|
||||
node.type = type
|
||||
node.variable = variable
|
||||
node.optionalValue = optional
|
||||
return node
|
||||
|
||||
return Parser.allOf(
|
||||
Parser.optional(TypeParser),
|
||||
Parser.doAssert(IdentifierLiteralParser, "argument name"),
|
||||
Parser.terminal(TokenType.ASSIGN),
|
||||
Parser.doAssert(ExpressionParser, "expression"),
|
||||
createNode=createNode,
|
||||
name="optional function argument"
|
||||
)(input)
|
||||
|
||||
|
||||
def ArgumentParser(input):
|
||||
return Parser.oneOf(
|
||||
OptionalArgumentParser,
|
||||
RegularArgumentParser,
|
||||
name="function argument"
|
||||
)(input)
|
||||
|
||||
|
||||
def ArgumentsDeclarationParser(input):
|
||||
return abstractIterableParser(
|
||||
ArgumentsDeclaration,
|
||||
TokenType.OPEN_PAREN,
|
||||
TokenType.CLOSE_PAREN,
|
||||
Parser.doAssert(ArgumentParser, "function/method argument")
|
||||
)(input)
|
||||
|
||||
|
||||
def FunctionDefinitionParser(input):
|
||||
return Parser.allOf(
|
||||
Parser.terminal(TokenType.FUNCTION),
|
||||
Parser.doAssert(IdentifierLiteralParser, "function/method name"),
|
||||
Parser.doAssert(ArgumentsDeclarationParser, "function/method arguments"),
|
||||
Parser.doAssert(BlockParser, "function/method body"),
|
||||
createNode=lambda _, name, args, body: FunctionDefinition.withValues(name, args, body),
|
||||
name="function definition"
|
||||
)(input)
|
||||
|
||||
# from smnp.ast.node.block import BlockNode
|
||||
# from smnp.ast.node.expression import ExpressionNode
|
||||
# from smnp.ast.node.identifier import IdentifierNode
|
||||
# from smnp.ast.node.iterable import abstractIterableParser
|
||||
# from smnp.ast.node.model import Node
|
||||
# from smnp.ast.node.none import NoneNode
|
||||
# from smnp.ast.node.statement import StatementNode
|
||||
# from smnp.ast.node.type import TypeNode, TypeSpecifier
|
||||
# from smnp.ast.parser import Parser
|
||||
# from smnp.token.type import TokenType
|
||||
#
|
||||
#
|
||||
# class ArgumentsDeclarationNode(Node):
|
||||
#
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# raise RuntimeError("This class is not supposed to be automatically called")
|
||||
#
|
||||
#
|
||||
# class VarargNode(Node):
|
||||
# pass
|
||||
#
|
||||
#
|
||||
# class ArgumentDefinitionNode(ExpressionNode):
|
||||
# def __init__(self, pos):
|
||||
# super().__init__(pos)
|
||||
# self.children.extend([NoneNode(), False])
|
||||
#
|
||||
# @property
|
||||
# def type(self):
|
||||
# return self[0]
|
||||
#
|
||||
# @type.setter
|
||||
# def type(self, value):
|
||||
# self[0] = value
|
||||
#
|
||||
# @property
|
||||
# def variable(self):
|
||||
# return self[1]
|
||||
#
|
||||
# @variable.setter
|
||||
# def variable(self, value):
|
||||
# self[1] = value
|
||||
#
|
||||
# @property
|
||||
# def vararg(self):
|
||||
# return self[2]
|
||||
#
|
||||
# @vararg.setter
|
||||
# def vararg(self, value):
|
||||
# self[2] = value
|
||||
#
|
||||
#
|
||||
# @classmethod
|
||||
# def parser(cls):
|
||||
# def createNode(type, variable, dots):
|
||||
# node = ArgumentDefinitionNode(type.pos)
|
||||
# node.type = type
|
||||
# node.variable = variable
|
||||
# node.vararg = isinstance(dots, VarargNode)
|
||||
# return node
|
||||
#
|
||||
# return Parser.allOf(
|
||||
# Parser.optional(Parser.oneOf(
|
||||
# TypeNode.parse,
|
||||
# TypeSpecifier.parse
|
||||
# )),
|
||||
# Parser.doAssert(IdentifierNode.identifierParser(), "variable name"),
|
||||
# Parser.optional(Parser.terminalParser(TokenType.DOTS, lambda val, pos: VarargNode(pos))),
|
||||
# createNode=createNode
|
||||
# )
|
||||
#
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# #TODO
|
||||
# raise RuntimeError("Not implemented yet. There is still required work to correctly build AST related to IdentifierNode")
|
||||
#
|
||||
#
|
||||
#
|
||||
# class FunctionDefinitionNode(StatementNode):
|
||||
# def __init__(self, pos):
|
||||
# super().__init__(pos)
|
||||
# self.children = [NoneNode(), NoneNode(), NoneNode()]
|
||||
#
|
||||
# @property
|
||||
# def name(self):
|
||||
# return self[0]
|
||||
#
|
||||
# @name.setter
|
||||
# def name(self, value):
|
||||
# self[0] = value
|
||||
#
|
||||
# @property
|
||||
# def arguments(self):
|
||||
# return self[1]
|
||||
#
|
||||
# @arguments.setter
|
||||
# def arguments(self, value):
|
||||
# self[1] = value
|
||||
#
|
||||
# @property
|
||||
# def body(self):
|
||||
# return self[2]
|
||||
#
|
||||
# @body.setter
|
||||
# def body(self, value):
|
||||
# self[2] = value
|
||||
#
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# def createNode(function, name, arguments, body):
|
||||
# node = FunctionDefinitionNode(function.pos)
|
||||
# node.name = name
|
||||
# node.arguments = arguments
|
||||
# node.body = body
|
||||
# return node
|
||||
#
|
||||
# return Parser.allOf(
|
||||
# Parser.terminalParser(TokenType.FUNCTION),
|
||||
# Parser.doAssert(IdentifierNode.identifierParser(), "function name"),
|
||||
# Parser.doAssert(cls._argumentsDeclarationParser(), "arguments list"),
|
||||
# Parser.doAssert(BlockNode.parse, "function body"),
|
||||
# createNode=createNode
|
||||
# )(input)
|
||||
#
|
||||
# @staticmethod
|
||||
# def _argumentsDeclarationParser():
|
||||
# return abstractIterableParser(ArgumentsDeclarationNode, TokenType.OPEN_PAREN, TokenType.CLOSE_PAREN, ArgumentDefinitionNode.parser())
|
||||
@@ -1,11 +1,10 @@
|
||||
from smnp.ast.node.atom import Atom
|
||||
from smnp.ast.node.expression import ExpressionParser
|
||||
from smnp.ast.node.iterable import abstractIterableParser
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.ast.node.none import NoneNode
|
||||
from smnp.ast.node.operator import BinaryOperator, Operator
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.ast.parser import Parsers
|
||||
from smnp.token.type import TokenType
|
||||
from smnp.util.singleton import SingletonParser
|
||||
|
||||
|
||||
class Identifier(Atom):
|
||||
@@ -49,26 +48,28 @@ class Assignment(BinaryOperator):
|
||||
pass
|
||||
|
||||
|
||||
def IdentifierLiteralParser(input):
|
||||
return Parser.terminal(TokenType.IDENTIFIER, createNode=Identifier.withValue)(input)
|
||||
@SingletonParser
|
||||
def IdentifierParser():
|
||||
identifierLiteralParser = Parsers.terminal(TokenType.IDENTIFIER, createNode=Identifier.withValue)
|
||||
|
||||
|
||||
def IdentifierParser(input):
|
||||
functionCallParser = Parser.allOf(
|
||||
IdentifierLiteralParser,
|
||||
abstractIterableParser(ArgumentsList, TokenType.OPEN_PAREN, TokenType.CLOSE_PAREN, Parser.doAssert(ExpressionParser, "expression")),
|
||||
createNode=lambda name, arguments: FunctionCall.withChildren(name, arguments)
|
||||
functionCallParser = Parsers.allOf(
|
||||
identifierLiteralParser,
|
||||
#abstractIterableParser(ArgumentsList, TokenType.OPEN_PAREN, TokenType.CLOSE_PAREN, MaxPrecedenceExpressionParser),
|
||||
createNode=lambda name, arguments: FunctionCall.withChildren(name, arguments),
|
||||
name="functionCall"
|
||||
)
|
||||
|
||||
assignmentParser = Parser.allOf(
|
||||
IdentifierLiteralParser,
|
||||
Parser.terminal(TokenType.ASSIGN, createNode=Operator.withValue),
|
||||
Parser.doAssert(ExpressionParser, "expression"),
|
||||
createNode=lambda identifier, assign, expr: Assignment.withValues(identifier, assign, expr)
|
||||
assignmentParser = Parsers.allOf(
|
||||
identifierLiteralParser,
|
||||
Parsers.terminal(TokenType.ASSIGN, createNode=Operator.withValue),
|
||||
#MaxPrecedenceExpressionParser,
|
||||
createNode=lambda identifier, assign, expr: Assignment.withValues(identifier, assign, expr),
|
||||
name="assignment"
|
||||
)
|
||||
|
||||
return Parser.oneOf(
|
||||
return Parsers.oneOf(
|
||||
assignmentParser,
|
||||
functionCallParser,
|
||||
IdentifierLiteralParser
|
||||
)(input)
|
||||
identifierLiteralParser,
|
||||
name="idExpr"
|
||||
)
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
# from smnp.ast.node.identifier import Identifier
|
||||
# from smnp.ast.node.model import Node
|
||||
# from smnp.ast.node.none import NoneNode
|
||||
# from smnp.ast.node.string import StringLiteralNode
|
||||
# from smnp.ast.node.type import TypeNode
|
||||
# from smnp.ast.parser import Parser
|
||||
# from smnp.token.type import TokenType
|
||||
#
|
||||
from smnp.ast.node.atom import StringParser
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.ast.parser import Parser
|
||||
@@ -26,8 +34,8 @@ class Import(Node):
|
||||
|
||||
def ImportParser(input):
|
||||
return Parser.allOf(
|
||||
Parser.terminal(TokenType.IMPORT),
|
||||
Parser.doAssert(StringParser, "import source as string"),
|
||||
Parser.terminalParser(TokenType.IMPORT),
|
||||
StringParser,
|
||||
createNode=lambda imp, source: Import.withValue(source),
|
||||
name="import"
|
||||
)(input)
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
from smnp.ast.node.ignore import IgnoredNode
|
||||
from smnp.ast.node.model import Node, ParseResult
|
||||
from smnp.ast.node.none import NoneNode
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.ast.parser import Parsers, DecoratorParser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
def abstractIterableParser(iterableNodeType, openTokenType, closeTokenType, itemParser):
|
||||
def abstractIterableParser(iterableNodeType, openTokenType, closeTokenType, itemParser, name):
|
||||
|
||||
class AbstractIterable(Node):
|
||||
def __init__(self, pos):
|
||||
@@ -31,70 +31,74 @@ def abstractIterableParser(iterableNodeType, openTokenType, closeTokenType, item
|
||||
class AbstractIterableTail(AbstractIterable):
|
||||
pass
|
||||
|
||||
def abstractIterableParser(input):
|
||||
return Parser.oneOf(
|
||||
emptyIterable,
|
||||
openIterable
|
||||
)(input)
|
||||
def abstractIterableParser():
|
||||
return Parsers.oneOf(
|
||||
emptyIterable(),
|
||||
openIterable(),
|
||||
name=name
|
||||
)
|
||||
|
||||
def emptyIterable(input):
|
||||
def emptyIterable():
|
||||
def createNode(open, close):
|
||||
node = AbstractIterable(open.pos)
|
||||
node.value = open
|
||||
node.next = close
|
||||
return node
|
||||
|
||||
return Parser.allOf(
|
||||
Parser.terminal(openTokenType),
|
||||
Parser.terminal(closeTokenType),
|
||||
createNode=createNode
|
||||
)(input)
|
||||
return Parsers.allOf(
|
||||
Parsers.terminal(openTokenType),
|
||||
Parsers.terminal(closeTokenType),
|
||||
createNode=createNode,
|
||||
name=name+"Empty"
|
||||
)
|
||||
|
||||
def openIterable(input):
|
||||
def openIterable():
|
||||
def createNode(open, item, tail):
|
||||
node = AbstractIterable(open.pos)
|
||||
node.value = item
|
||||
node.next = tail
|
||||
return node
|
||||
|
||||
return Parser.allOf(
|
||||
Parser.terminal(openTokenType),
|
||||
return Parsers.allOf(
|
||||
Parsers.terminal(openTokenType),
|
||||
itemParser,
|
||||
abstractIterableTailParser,
|
||||
createNode=createNode
|
||||
)(input)
|
||||
abstractIterableTailParser(),
|
||||
createNode=createNode,
|
||||
name=name+"Open"
|
||||
)
|
||||
|
||||
def abstractIterableTailParser(input):
|
||||
return Parser.oneOf(
|
||||
closeIterable,
|
||||
nextItem,
|
||||
)(input)
|
||||
def abstractIterableTailParser():
|
||||
return Parsers.oneOf(
|
||||
closeIterable(),
|
||||
nextItem(),
|
||||
name=name+"Tail"
|
||||
)
|
||||
|
||||
def nextItem(input):
|
||||
def nextItem():
|
||||
def createNode(comma, item, tail):
|
||||
node = AbstractIterableTail(item.pos)
|
||||
node.value = item
|
||||
node.next = tail
|
||||
return node
|
||||
|
||||
return Parser.allOf(
|
||||
Parser.doAssert(Parser.terminal(TokenType.COMMA), f"'{TokenType.COMMA.key}' or '{closeTokenType.key}'"),
|
||||
return Parsers.allOf(
|
||||
Parsers.terminal(TokenType.COMMA, doAssert=True),
|
||||
itemParser,
|
||||
abstractIterableTailParser,
|
||||
abstractIterableTailParser(),
|
||||
name=name+"NextItem",
|
||||
createNode=createNode
|
||||
)(input)
|
||||
)
|
||||
|
||||
def closeIterable(input):
|
||||
return Parser.terminal(closeTokenType)(input)
|
||||
def closeIterable():
|
||||
return Parsers.terminal(closeTokenType)
|
||||
|
||||
|
||||
return toFlatDesiredNode(iterableNodeType, abstractIterableParser)
|
||||
return abstractIterableParser()
|
||||
#return toFlatDesiredNode(iterableNodeType, abstractIterableParser())
|
||||
|
||||
|
||||
def toFlatDesiredNode(iterableNodeType, parser):
|
||||
def parse(input):
|
||||
result = parser(input)
|
||||
|
||||
def wrapper(result):
|
||||
if result.result:
|
||||
value = flattenList(result.node)
|
||||
node = iterableNodeType(result.node.pos)
|
||||
@@ -105,7 +109,7 @@ def toFlatDesiredNode(iterableNodeType, parser):
|
||||
|
||||
return ParseResult.FAIL()
|
||||
|
||||
return Parser(parse, "flat", [parser])
|
||||
return DecoratorParser(wrapper, parser)
|
||||
|
||||
|
||||
def flattenList(node, output=None):
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from smnp.ast.node.atom import AtomParser
|
||||
from smnp.ast.node.iterable import abstractIterableParser
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
@@ -8,12 +8,6 @@ class List(Node):
|
||||
pass
|
||||
|
||||
|
||||
def ListParser(input):
|
||||
from smnp.ast.node.expression import ExpressionParser
|
||||
|
||||
return abstractIterableParser(
|
||||
List,
|
||||
TokenType.OPEN_SQUARE,
|
||||
TokenType.CLOSE_SQUARE,
|
||||
Parser.doAssert(ExpressionParser, "expression")
|
||||
)(input)
|
||||
def ListParser():
|
||||
return abstractIterableParser(List, TokenType.OPEN_SQUARE, TokenType.CLOSE_SQUARE, AtomParser(), name="list")
|
||||
#MaxPrecedenceExpressionParser)(input)
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
from smnp.ast.node.atom import LiteralParser
|
||||
from smnp.ast.node.identifier import IdentifierLiteralParser
|
||||
from smnp.ast.node.iterable import abstractIterableParser
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.ast.node.operator import BinaryOperator, Operator
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.ast.parser import Parsers
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
@@ -31,24 +29,19 @@ class Map(Node):
|
||||
|
||||
|
||||
def MapParser(input):
|
||||
from smnp.ast.node.expression import ExpressionParser
|
||||
keyParser = Parser.oneOf(
|
||||
LiteralParser,
|
||||
IdentifierLiteralParser
|
||||
)
|
||||
valueParser = ExpressionParser
|
||||
from smnp.ast.node.atom import LiteralParser
|
||||
#from smnp.ast.node.expression import MaxPrecedenceExpressionParser
|
||||
keyParser = LiteralParser
|
||||
#valueParser = MaxPrecedenceExpressionParser
|
||||
|
||||
mapEntryParser = Parser.allOf(
|
||||
mapEntryParser = Parsers.allOf(
|
||||
keyParser,
|
||||
Parser.terminal(TokenType.ARROW, createNode=Operator.withValue),
|
||||
Parser.doAssert(valueParser, "expression"),
|
||||
createNode=MapEntry.withValues
|
||||
Parsers.terminal(TokenType.ARROW, createNode=Operator.withValue),
|
||||
#valueParser,
|
||||
createNode=MapEntry.withValues,
|
||||
name="mapEntry"
|
||||
)
|
||||
|
||||
return abstractIterableParser(
|
||||
Map,
|
||||
TokenType.OPEN_CURLY,
|
||||
TokenType.CLOSE_CURLY,
|
||||
mapEntryParser
|
||||
)(input)
|
||||
return abstractIterableParser(Map, TokenType.OPEN_CURLY, TokenType.CLOSE_CURLY, mapEntryParser)
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +28,21 @@ class Node:
|
||||
def pop(self, index):
|
||||
return self.children.pop(index)
|
||||
|
||||
@classmethod
|
||||
def _parse(cls, input):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def parse(cls, input):
|
||||
result = cls._parse(input)
|
||||
if result is None:
|
||||
return ParseResult.FAIL()
|
||||
|
||||
if not isinstance(result, ParseResult):
|
||||
raise RuntimeError(f"_parse() method of '{cls.__name__}' class haven't returned ParseResult object")
|
||||
|
||||
return result
|
||||
|
||||
def print(self):
|
||||
self._print(first=True)
|
||||
|
||||
|
||||
@@ -68,7 +68,68 @@ class BinaryOperator(Node):
|
||||
node.right = right
|
||||
return node
|
||||
|
||||
|
||||
#
|
||||
# class LeftAssociativeOperatorNode(ExpressionNode):
|
||||
# def __init__(self, pos):
|
||||
# super().__init__(pos)
|
||||
# self.children = [NoneNode(), NoneNode(), NoneNode()]
|
||||
#
|
||||
# @property
|
||||
# def left(self):
|
||||
# return self[0]
|
||||
#
|
||||
# @left.setter
|
||||
# def left(self, value):
|
||||
# self[0] = value
|
||||
#
|
||||
# @property
|
||||
# def operator(self):
|
||||
# return self[1]
|
||||
#
|
||||
# @operator.setter
|
||||
# def operator(self, value):
|
||||
# self[1] = value
|
||||
#
|
||||
# @property
|
||||
# def right(self):
|
||||
# return self[2]
|
||||
#
|
||||
# @right.setter
|
||||
# def right(self, value):
|
||||
# self[2] = value
|
||||
#
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# def createNode(left, operator, right):
|
||||
# node = LeftAssociativeOperatorNode(right.pos)
|
||||
# node.left = left
|
||||
# node.operator = operator
|
||||
# node.right = right
|
||||
# return node
|
||||
#
|
||||
# return Parser.leftAssociativeOperatorParser(
|
||||
# cls._lhsParser(),
|
||||
# TokenType.DOT,
|
||||
# cls._rhsParser(),
|
||||
# createNode=createNode
|
||||
# )(input)
|
||||
#
|
||||
# @classmethod
|
||||
# def _lhsParser(cls):
|
||||
# raise RuntimeError(f"LHS parser is not implemented in {cls.__name__}")
|
||||
#
|
||||
# @staticmethod
|
||||
# def _rhsParser():
|
||||
# from smnp.ast.node.identifier import IdentifierNode
|
||||
#
|
||||
# return Parser.oneOf(
|
||||
# # TODO!!!
|
||||
# IdentifierNode._lhsParser(),
|
||||
# IdentifierNode._functionCallParser(),
|
||||
# exception=lambda input: SyntaxException(f"Expected property name or method call, found '{input.current().rawValue}'", input.currentPos())
|
||||
# )
|
||||
#
|
||||
#
|
||||
class Operator(Node):
|
||||
def __init__(self, pos):
|
||||
super().__init__(pos)
|
||||
|
||||
@@ -1,36 +1,44 @@
|
||||
from smnp.ast.node.extend import ExtendParser
|
||||
from smnp.ast.node.function import FunctionDefinitionParser
|
||||
from smnp.ast.node.imports import ImportParser
|
||||
from smnp.ast.node.model import Node, ParseResult
|
||||
from smnp.ast.node.statement import StatementParser
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.error.syntax import SyntaxException
|
||||
|
||||
|
||||
class Program(Node):
|
||||
def __init__(self):
|
||||
super().__init__((-1, -1))
|
||||
|
||||
def ProgramParser(input):
|
||||
def parse(input):
|
||||
root = Program()
|
||||
|
||||
# Start Symbol
|
||||
startSymbolParser = Parser.oneOf(
|
||||
def parse(input):
|
||||
root = Program()
|
||||
while input.hasCurrent():
|
||||
result = Parser.oneOf(
|
||||
# Start Symbol
|
||||
ImportParser,
|
||||
FunctionDefinitionParser,
|
||||
ExtendParser,
|
||||
StatementParser,
|
||||
exception=lambda inp: SyntaxException(f"Invalid statement: {inp.currentToEndOfLine()}", inp.current().pos),
|
||||
name="start symbol"
|
||||
)
|
||||
exception=RuntimeError("Nie znam tego wyrazenia")
|
||||
)(input)
|
||||
|
||||
while input.hasCurrent():
|
||||
result = startSymbolParser(input)
|
||||
if result.result:
|
||||
root.append(result.node)
|
||||
|
||||
if result.result:
|
||||
root.append(result.node)
|
||||
return ParseResult.OK(root)
|
||||
|
||||
return ParseResult.OK(root)
|
||||
|
||||
return Parser(parse, name="program")(input)
|
||||
ProgramParser = Parser(parse, name="program")
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# def parseToken(input):
|
||||
# return Parser.oneOf(
|
||||
# FunctionDefinitionNode.parse,
|
||||
# ExtendNode.parse,
|
||||
# ExpressionNode.parse,
|
||||
# ImportNode.parse,
|
||||
# StatementNode.parse,
|
||||
# exception = SyntaxException(f"Invalid statement: {input.currentToEndOfLine()}", input.current().pos)
|
||||
# )(input)
|
||||
#
|
||||
# root = Program()
|
||||
# while input.hasCurrent():
|
||||
# result = parseToken(input)
|
||||
# if result.result:
|
||||
# root.append(result.node)
|
||||
# return ParseResult.OK(root)
|
||||
@@ -1,17 +1,32 @@
|
||||
from smnp.ast.node.expression import ExpressionParser
|
||||
from smnp.ast.node.valuable import Valuable
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
class Return(Valuable):
|
||||
pass
|
||||
|
||||
|
||||
def ReturnParser(input):
|
||||
return Parser.allOf(
|
||||
Parser.terminal(TokenType.RETURN),
|
||||
Parser.optional(ExpressionParser),
|
||||
createNode=lambda ret, val: Return.withValue(val, ret.pos),
|
||||
name="return"
|
||||
)(input)
|
||||
# from smnp.ast.node.expression import ExpressionNode
|
||||
# from smnp.ast.node.none import NoneNode
|
||||
# from smnp.ast.node.statement import StatementNode
|
||||
# from smnp.ast.parser import Parser
|
||||
# from smnp.token.type import TokenType
|
||||
#
|
||||
#
|
||||
# class ReturnNode(StatementNode):
|
||||
# def __init__(self, pos):
|
||||
# super().__init__(pos)
|
||||
# self.children.append(NoneNode())
|
||||
#
|
||||
# @property
|
||||
# def value(self):
|
||||
# return self[0]
|
||||
#
|
||||
# @value.setter
|
||||
# def value(self, value):
|
||||
# self[0] = value
|
||||
#
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# def createNode(ret, value):
|
||||
# node = ReturnNode(ret.pos)
|
||||
# node.value = value
|
||||
# return node
|
||||
#
|
||||
# return Parser.allOf(
|
||||
# Parser.terminalParser(TokenType.RETURN),
|
||||
# Parser.doAssert(ExpressionNode.parse, "expression"),
|
||||
# createNode=createNode
|
||||
# )(input)
|
||||
@@ -1,6 +1,6 @@
|
||||
from smnp.ast.node.expression import MaxPrecedenceExpressionParser
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
class Statement(Node):
|
||||
@@ -10,28 +10,25 @@ class Statement(Node):
|
||||
def StatementParser(input):
|
||||
from smnp.ast.node.block import BlockParser
|
||||
from smnp.ast.node.condition import IfElseStatementParser
|
||||
from smnp.ast.node.expression import ExpressionParser
|
||||
from smnp.ast.node.ret import ReturnParser
|
||||
from smnp.ast.node.throw import ThrowParser
|
||||
|
||||
return withSemicolon(
|
||||
Parser.oneOf(
|
||||
IfElseStatementParser,
|
||||
ExpressionParser, # Must be above BlockParser because of Map's syntax with curly braces
|
||||
BlockParser,
|
||||
ReturnParser,
|
||||
ThrowParser,
|
||||
name="statement"
|
||||
), optional=True)(input)
|
||||
|
||||
|
||||
def withSemicolon(parser, optional=False, doAssert=False):
|
||||
semicolonParser = Parser.optional(Parser.terminal(TokenType.SEMICOLON)) if optional else Parser.terminal(
|
||||
TokenType.SEMICOLON, doAssert=doAssert)
|
||||
|
||||
return Parser.allOf(
|
||||
parser,
|
||||
semicolonParser,
|
||||
createNode=lambda stmt, semicolon: stmt,
|
||||
name="semicolon" + "?" if optional else ""
|
||||
parser = Parser.oneOf(
|
||||
IfElseStatementParser,
|
||||
BlockParser,
|
||||
MaxPrecedenceExpressionParser
|
||||
)
|
||||
|
||||
return Parser(parser, "statement", parser)(input)
|
||||
|
||||
# class StatementNode(Node):
|
||||
#
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# from smnp.ast.node.block import BlockNode
|
||||
# from smnp.ast.node.expression import ExpressionNode
|
||||
# from smnp.ast.node.ret import ReturnNode
|
||||
#
|
||||
# return Parser.oneOf(
|
||||
# ExpressionNode.parse,
|
||||
# BlockNode.parse,
|
||||
# ReturnNode.parse,
|
||||
# )(input)
|
||||
@@ -1,17 +1,12 @@
|
||||
from smnp.ast.node.factor import FactorParser
|
||||
from smnp.ast.node.operator import BinaryOperator
|
||||
from smnp.ast.node.valuable import Valuable
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
class Product(BinaryOperator):
|
||||
class Term(Valuable):
|
||||
pass
|
||||
|
||||
|
||||
def TermParser(input):
|
||||
return Parser.leftAssociativeOperatorParser(
|
||||
FactorParser,
|
||||
[TokenType.ASTERISK, TokenType.SLASH],
|
||||
FactorParser,
|
||||
lambda left, op, right: Product.withValues(left, op, right)
|
||||
)(input)
|
||||
TermParser = Parser.leftAssociativeOperatorParser(FactorParser, [TokenType.ASTERISK, TokenType.SLASH], FactorParser,
|
||||
lambda left, op, right: Term.withValue(BinaryOperator.withValues(left, op, right)))
|
||||
@@ -1,17 +0,0 @@
|
||||
from smnp.ast.node.expression import ExpressionParser
|
||||
from smnp.ast.node.valuable import Valuable
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
class Throw(Valuable):
|
||||
pass
|
||||
|
||||
|
||||
def ThrowParser(input):
|
||||
return Parser.allOf(
|
||||
Parser.terminal(TokenType.THROW),
|
||||
Parser.doAssert(ExpressionParser, "error message as string"),
|
||||
createNode=lambda throw, message: Throw.withValue(message, throw.pos),
|
||||
name="throw"
|
||||
)(input)
|
||||
@@ -1,67 +1,63 @@
|
||||
from smnp.ast.node.atom import TypeLiteralParser
|
||||
from smnp.ast.node.iterable import abstractIterableParser
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.ast.node.none import NoneNode
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
class Type(Node):
|
||||
def __init__(self, pos):
|
||||
super().__init__(pos)
|
||||
self.children = [NoneNode(), NoneNode()]
|
||||
|
||||
@property
|
||||
def type(self):
|
||||
return self[0]
|
||||
|
||||
@type.setter
|
||||
def type(self, value):
|
||||
self[0] = value
|
||||
|
||||
@property
|
||||
def specifiers(self):
|
||||
return self[1]
|
||||
|
||||
@specifiers.setter
|
||||
def specifiers(self, value):
|
||||
self[1] = value
|
||||
|
||||
@classmethod
|
||||
def withValues(cls, pos, type, specifiers=NoneNode()):
|
||||
node = cls(pos)
|
||||
node.type = type
|
||||
node.specifiers = specifiers
|
||||
return node
|
||||
|
||||
|
||||
class TypesList(Node):
|
||||
pass
|
||||
|
||||
|
||||
def TypesListParser(input):
|
||||
return abstractIterableParser(
|
||||
TypesList,
|
||||
TokenType.OPEN_ANGLE,
|
||||
TokenType.CLOSE_ANGLE,
|
||||
TypeParser
|
||||
)(input)
|
||||
|
||||
|
||||
class TypeSpecifiers(Node):
|
||||
pass
|
||||
|
||||
|
||||
def TypeParser(input):
|
||||
typeWithSpecifier = Parser.allOf(
|
||||
TypeLiteralParser,
|
||||
Parser.many(TypesListParser, createNode=TypeSpecifiers.withChildren),
|
||||
createNode=lambda type, specifiers: Type.withValues(type.pos, type, specifiers),
|
||||
name="type with specifiers?"
|
||||
)
|
||||
|
||||
return Parser.oneOf(
|
||||
typeWithSpecifier,
|
||||
TypesListParser,
|
||||
name="mult. types or type with specifier"
|
||||
)(input)
|
||||
# from smnp.ast.node.iterable import abstractIterableParser
|
||||
# from smnp.ast.node.model import Node
|
||||
# from smnp.ast.node.operator import LeftAssociativeOperatorNode
|
||||
# from smnp.ast.parser import Parser
|
||||
# from smnp.token.type import TokenType
|
||||
# from smnp.type.model import Type
|
||||
#
|
||||
#
|
||||
# class TypeSpecifier(Node):
|
||||
#
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# return abstractIterableParser(TypeSpecifier, TokenType.OPEN_ANGLE, TokenType.CLOSE_ANGLE,
|
||||
# Parser.doAssert(cls._specifierItem(), "type"))(input)
|
||||
#
|
||||
# @classmethod
|
||||
# def _specifierItem(cls):
|
||||
# return Parser.oneOf(
|
||||
# TypeNode.parse,
|
||||
# cls.parse
|
||||
# )
|
||||
#
|
||||
# class TypeSpecifiers(Node):
|
||||
# pass
|
||||
#
|
||||
#
|
||||
# class TypeNode(LeftAssociativeOperatorNode):
|
||||
# def __init__(self, pos):
|
||||
# super().__init__(pos)
|
||||
#
|
||||
# @property
|
||||
# def type(self):
|
||||
# return self[0]
|
||||
#
|
||||
# @type.setter
|
||||
# def type(self, value):
|
||||
# self[0] = value
|
||||
#
|
||||
# @property
|
||||
# def specifiers(self):
|
||||
# return self[1]
|
||||
#
|
||||
# @specifiers.setter
|
||||
# def specifiers(self, value):
|
||||
# self[1] = value
|
||||
#
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# def createNode(type, specifiers):
|
||||
# node = TypeNode(type.pos)
|
||||
# node.type = Type[type.value.upper()]
|
||||
# node.specifiers = specifiers
|
||||
# return node
|
||||
#
|
||||
# return Parser.allOf(
|
||||
# cls._rawTypeParser(),
|
||||
# Parser.many(TypeSpecifier.parse, lambda specifiers, pos: TypeSpecifiers.withChildren(specifiers, pos)),
|
||||
# createNode=createNode
|
||||
# )(input)
|
||||
#
|
||||
# @classmethod
|
||||
# def _rawTypeParser(cls):
|
||||
# return Parser.terminalParser(TokenType.TYPE, lambda val, pos: TypeNode.withValue(val, pos))
|
||||
@@ -1,36 +0,0 @@
|
||||
from smnp.ast.node.atom import AtomParser
|
||||
from smnp.ast.node.operator import BinaryOperator, UnaryOperator, Operator
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
class MinusOperator(UnaryOperator):
|
||||
pass
|
||||
|
||||
|
||||
class Access(BinaryOperator):
|
||||
pass
|
||||
|
||||
|
||||
def UnitParser(input):
|
||||
minusOperator = Parser.allOf(
|
||||
Parser.terminal(TokenType.MINUS, createNode=Operator.withValue),
|
||||
Parser.doAssert(AtomParser, "atom"),
|
||||
createNode=MinusOperator.withValues,
|
||||
name="minus"
|
||||
)
|
||||
|
||||
atom2 = Parser.oneOf(
|
||||
minusOperator,
|
||||
AtomParser,
|
||||
name="atom2"
|
||||
)
|
||||
|
||||
return Parser.leftAssociativeOperatorParser(
|
||||
atom2,
|
||||
[TokenType.DOT],
|
||||
Parser.doAssert(atom2, "atom"),
|
||||
createNode=lambda left, op, right: Access.withValues(left, op, right),
|
||||
name="unit"
|
||||
)(input)
|
||||
|
||||
@@ -16,7 +16,7 @@ class Valuable(Node):
|
||||
self[0] = value
|
||||
|
||||
@classmethod
|
||||
def withValue(cls, value, pos=None):
|
||||
node = cls(value.pos if pos is None else pos)
|
||||
def withValue(cls, value):
|
||||
node = cls(value.pos)
|
||||
node.value = value
|
||||
return node
|
||||
@@ -9,15 +9,8 @@ def parse(input):
|
||||
return ProgramParser(input).node
|
||||
|
||||
|
||||
class Parser:
|
||||
def __init__(self, parse, name=None, parsers=None):
|
||||
if parsers is None:
|
||||
parsers = []
|
||||
self.parsers = parsers
|
||||
|
||||
self._parse = parse
|
||||
if name is None:
|
||||
name = parse.__name__
|
||||
class Parser(object):
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
def parse(self, input):
|
||||
@@ -26,10 +19,27 @@ class Parser:
|
||||
return ParseResult.FAIL()
|
||||
|
||||
if not isinstance(result, ParseResult):
|
||||
raise RuntimeError(f"_parse() method of '{self.__class__.__name__}' class haven't returned ParseResult object")
|
||||
raise RuntimeError(
|
||||
f"_parse() method of '{self.__class__.__name__}' class haven't returned ParseResult object")
|
||||
|
||||
return result
|
||||
|
||||
def _parse(self, input):
|
||||
raise RuntimeError(f"_name method of '{self.__class__.__name__}' class is not implemented")
|
||||
|
||||
def grammar(self):
|
||||
rules = []
|
||||
self._grammarRules(rules)
|
||||
return "\n".join(self._uniq(rules))
|
||||
|
||||
def _uniq(self, seq):
|
||||
seen = set()
|
||||
seen_add = seen.add
|
||||
return [x for x in seq if not (x in seen or seen_add(x))]
|
||||
|
||||
def _grammarRules(self, output):
|
||||
output.append(f"class '{self.__class__.__name__}' does not implement _grammarRules() method")
|
||||
|
||||
def __call__(self, input):
|
||||
return self.parse(input)
|
||||
|
||||
@@ -39,186 +49,242 @@ class Parser:
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
# a -> A
|
||||
|
||||
class Parsers:
|
||||
|
||||
@staticmethod
|
||||
def terminal(expectedType, createNode=None, doAssert=False):
|
||||
def provideNode(value, pos):
|
||||
if createNode is None:
|
||||
return IgnoredNode(pos)
|
||||
return createNode(value, pos)
|
||||
def terminal(expectedType, createNode=lambda val, pos: IgnoredNode(pos), doAssert=False):
|
||||
return TerminalParser(expectedType, createNode, doAssert)
|
||||
|
||||
def parse(input):
|
||||
if input.hasCurrent() and input.current().type == expectedType:
|
||||
token = input.current()
|
||||
input.ahead()
|
||||
return ParseResult.OK(provideNode(token.value, token.pos))
|
||||
elif doAssert:
|
||||
found = f", found '{input.current().rawValue}'" if input.hasCurrent() else ""
|
||||
raise SyntaxException(f"Expected '{expectedType.key}'{found}", input.currentPos())
|
||||
|
||||
return ParseResult.FAIL()
|
||||
|
||||
return Parser(parse, name=expectedType.name.lower())
|
||||
|
||||
# oneOf -> a | b | c | ...
|
||||
@staticmethod
|
||||
def oneOf(*parsers, assertExpected=None, exception=None, name="or"):
|
||||
def combinedParser(input):
|
||||
snap = input.snapshot()
|
||||
for parser in parsers:
|
||||
value = parser(input)
|
||||
if value.result:
|
||||
return value
|
||||
input.reset(snap)
|
||||
def oneOf(*parsers, name, exception=None):
|
||||
return OneOfParser(*parsers, name=name, exception=exception)
|
||||
|
||||
if assertExpected is not None:
|
||||
found = f", found '{input.current().rawValue}'" if input.hasCurrent() else ""
|
||||
raise SyntaxException(f"Expected {assertExpected}{found}", input.currentPos())
|
||||
|
||||
if exception is not None:
|
||||
if callable(exception):
|
||||
raise exception(input)
|
||||
else:
|
||||
raise exception
|
||||
|
||||
input.reset(snap)
|
||||
return ParseResult.FAIL()
|
||||
|
||||
return Parser(combinedParser, name=name, parsers=parsers)
|
||||
|
||||
# allOf -> a b c ...
|
||||
@staticmethod
|
||||
def allOf(*parsers, createNode, exception=None, name="all"):
|
||||
def allOf(*parsers, createNode, exception=None, name):
|
||||
return AllOfParser(*parsers, createNode=createNode, exception=exception, name=name)
|
||||
|
||||
@staticmethod
|
||||
def leftAssociativeOperatorParser(leftParser, operatorTokenTypes, rightParser, createNode, name):
|
||||
return LeftAssociativeOperatorParser(leftParser, operatorTokenTypes, rightParser, createNode, name)
|
||||
|
||||
@staticmethod
|
||||
def many(itemParser, createNode, name):
|
||||
return ManyParser(itemParser, createNode, name)
|
||||
|
||||
@staticmethod
|
||||
def optional(parser, name):
|
||||
return OptionalParser(parser, name)
|
||||
|
||||
@staticmethod
|
||||
def loop(startParser, itemParser, endParser, createNode, name):
|
||||
return LoopParser(startParser, itemParser, endParser, createNode, name)
|
||||
|
||||
|
||||
class DecoratorParser(Parser):
|
||||
def __init__(self, wrapper, parser):
|
||||
super().__init__(parser.name)
|
||||
self.wrapper = wrapper
|
||||
self.parser = parser
|
||||
self._grammarRules = parser._grammarRules
|
||||
|
||||
def _parse(self, input):
|
||||
result = self.parser.parse(input)
|
||||
return self.wrapper(result)
|
||||
|
||||
|
||||
class TerminalParser(Parser):
|
||||
|
||||
def __init__(self, expectedType, createNode=lambda val, pos: IgnoredNode(pos), doAssert=False):
|
||||
super().__init__(expectedType.name.lower())
|
||||
self.expectedType = expectedType
|
||||
self.createNode = createNode
|
||||
self.doAssert = doAssert
|
||||
|
||||
def _grammarRules(self, output):
|
||||
output.append(f"{self.name} -> '{self.expectedType.value}'")
|
||||
|
||||
def _parse(self, input):
|
||||
if input.isCurrent(self.expectedType):
|
||||
token = input.current()
|
||||
input.ahead()
|
||||
return ParseResult.OK(self.createNode(token.value, token.pos))
|
||||
elif self.doAssert:
|
||||
found = f", found '{input.current().rawValue}'" if input.hasCurrent() else ""
|
||||
raise SyntaxException(f"Expected '{self.expectedType.key}'{found}", input.currentPos())
|
||||
|
||||
return ParseResult.FAIL()
|
||||
|
||||
|
||||
|
||||
class OneOfParser(Parser):
|
||||
def __init__(self, *parsers, name, exception=None):
|
||||
super().__init__(name)
|
||||
self.parsers = parsers
|
||||
self.exception = exception
|
||||
|
||||
def _parse(self, input):
|
||||
snap = input.snapshot()
|
||||
for parser in self.parsers:
|
||||
value = parser.parse(input)
|
||||
if value.result:
|
||||
return value
|
||||
input.reset(snap) # TODO sprawdzic, czy koneiczne !!!!!
|
||||
|
||||
if self.exception is not None:
|
||||
if callable(self.exception):
|
||||
raise self.exception(input)
|
||||
else:
|
||||
raise self.exception
|
||||
|
||||
return ParseResult.FAIL()
|
||||
|
||||
def _grammarRules(self, output):
|
||||
output.extend([ f"{self.name} -> {parser.name}" for parser in self.parsers ])
|
||||
[ parser._grammarRules(output) for parser in self.parsers ]
|
||||
|
||||
|
||||
class AllOfParser(Parser):
|
||||
def __init__(self, *parsers, createNode, exception=None, name):
|
||||
super().__init__(name)
|
||||
|
||||
if len(parsers) == 0:
|
||||
raise RuntimeError("Pass one parser at least")
|
||||
|
||||
def extendedParser(input):
|
||||
snap = input.snapshot()
|
||||
self.parsers = parsers
|
||||
self.createNode = createNode
|
||||
self.exception = exception
|
||||
|
||||
results = []
|
||||
def _parse(self, input):
|
||||
snap = input.snapshot()
|
||||
parsedItems = []
|
||||
|
||||
for parser in parsers:
|
||||
result = parser(input)
|
||||
|
||||
if not result.result:
|
||||
if exception is not None:
|
||||
if callable(exception):
|
||||
raise exception(input)
|
||||
else:
|
||||
raise exception
|
||||
|
||||
input.reset(snap)
|
||||
return ParseResult.FAIL()
|
||||
|
||||
results.append(result.node)
|
||||
|
||||
node = createNode(*results)
|
||||
if not isinstance(node, Node):
|
||||
raise RuntimeError("Function 'createNode' haven't returned a Node object. Probably forget to pass 'return'")
|
||||
|
||||
return ParseResult.OK(node)
|
||||
|
||||
|
||||
return Parser(extendedParser, name=name, parsers=parsers)
|
||||
|
||||
|
||||
# leftAssociative -> left | left OP right
|
||||
@staticmethod
|
||||
def leftAssociativeOperatorParser(leftParser, operatorTokenTypes, rightParser, createNode, name="leftAssoc"):
|
||||
from smnp.ast.node.operator import Operator
|
||||
|
||||
def parse(input):
|
||||
operatorParser = Parser.oneOfTerminals(*operatorTokenTypes, createNode=lambda val, pos: Operator.withChildren([val], pos))
|
||||
left = leftParser(input)
|
||||
if left.result:
|
||||
operator = operatorParser(input)
|
||||
while operator.result:
|
||||
right = rightParser(input)
|
||||
left = ParseResult.OK(createNode(left.node, operator.node, right.node))
|
||||
operator = operatorParser(input)
|
||||
return left
|
||||
|
||||
return ParseResult.FAIL()
|
||||
|
||||
return Parser(parse, name=name, parsers=[leftParser, '|'.join([t.value for t in operatorTokenTypes]), rightParser])
|
||||
|
||||
@staticmethod
|
||||
def oneOfTerminals(*tokenTypes, createNode=None):
|
||||
return Parser.oneOf(*[Parser.terminal(expectedType, createNode=createNode) for expectedType in tokenTypes], name='|'.join([t.value for t in tokenTypes]))
|
||||
|
||||
# loop -> start item* end
|
||||
@staticmethod
|
||||
def loop(startParser, itemParser, endParser, createNode, name="loop"):
|
||||
def parse(input):
|
||||
items = []
|
||||
start = startParser(input)
|
||||
if start.result:
|
||||
while True:
|
||||
end = endParser(input)
|
||||
if end.result:
|
||||
return ParseResult.OK(createNode(start.node, items, end.node))
|
||||
item = itemParser(input)
|
||||
if not item.result:
|
||||
return ParseResult.FAIL()
|
||||
items.append(item.node)
|
||||
|
||||
return ParseResult.FAIL()
|
||||
|
||||
return Parser(parse, name, parsers=[startParser, itemParser, endParser])
|
||||
|
||||
@staticmethod
|
||||
def doAssert(parser, expected, name="!!"):
|
||||
def parse(input):
|
||||
result = parser(input)
|
||||
for parser in self.parsers:
|
||||
result = parser.parse(input)
|
||||
|
||||
if not result.result:
|
||||
found = f", found '{input.current().rawValue}'" if input.hasCurrent() else ''
|
||||
if self.exception is not None:
|
||||
if callable(self.exception):
|
||||
raise self.exception(input)
|
||||
else:
|
||||
raise self.exception
|
||||
|
||||
raise SyntaxException(f"Expected {expected}{found}", input.currentPos())
|
||||
input.reset(snap)
|
||||
return ParseResult.FAIL()
|
||||
|
||||
parsedItems.append(result.node)
|
||||
|
||||
node = self.createNode(*parsedItems)
|
||||
if not isinstance(node, Node):
|
||||
raise RuntimeError(f"Method 'createNode' of class '{self.__class__.__name__}' haven't returned a Node object. Probably forget to pass 'return'")
|
||||
|
||||
return ParseResult.OK(node)
|
||||
|
||||
def _grammarRules(self, output):
|
||||
output.append(self.name + ' -> ' + ' '.join([ parser.name for parser in self.parsers ]))
|
||||
[ parser._grammarRules(output) for parser in self.parsers ]
|
||||
|
||||
|
||||
class LeftAssociativeOperatorParser(Parser):
|
||||
def __init__(self, leftParser, operatorTokenTypes, rightParser, createNode, name):
|
||||
from smnp.ast.node.operator import Operator
|
||||
|
||||
super().__init__(name)
|
||||
self.leftParser = leftParser
|
||||
self.rightParser = rightParser
|
||||
self.createNode = createNode
|
||||
self.operators = operatorTokenTypes
|
||||
|
||||
operatorParsers = [ TerminalParser(expectedType, createNode=lambda val, pos: Operator.withValue(val, pos)) for expectedType in operatorTokenTypes ]
|
||||
self.operatorParser = OneOfParser(*operatorParsers, name="not important")
|
||||
|
||||
def _parse(self, input):
|
||||
snap = input.snapshot()
|
||||
left = self.leftParser.parse(input)
|
||||
if left.result:
|
||||
operator = self.operatorParser.parse(input)
|
||||
while operator.result:
|
||||
right = self.rightParser.parse(input)
|
||||
left = ParseResult.OK(self.createNode(left.node, operator.node, right.node))
|
||||
operator = self.operatorParser.parse(input)
|
||||
return left
|
||||
|
||||
return ParseResult.FAIL()
|
||||
|
||||
|
||||
def _grammarRules(self, output):
|
||||
output.append('\n'.join([f"{self.name} -> {self.leftParser.name} {operator.name.lower()} {self.rightParser.name} | {self.leftParser.name}" for operator in self.operators]))
|
||||
self.leftParser._grammarRules(output)
|
||||
self.rightParser._grammarRules(output)
|
||||
|
||||
|
||||
class ManyParser(Parser):
|
||||
def __init__(self, itemParser, createNode, name):
|
||||
super().__init__(name)
|
||||
self.itemParser = itemParser
|
||||
self.createNode = createNode
|
||||
|
||||
def _parse(self, input):
|
||||
snap = input.snapshot()
|
||||
|
||||
parsedItems = []
|
||||
pos = input.currentPos()
|
||||
while True:
|
||||
result = self.itemParser.parse(input)
|
||||
if result.result:
|
||||
parsedItems.append(result.node)
|
||||
snap = input.snapshot()
|
||||
else:
|
||||
input.reset(snap)
|
||||
return ParseResult.OK(self.createNode(parsedItems, pos) if len(parsedItems) > 0 else NoneNode())
|
||||
|
||||
def _grammarRules(self, output):
|
||||
output.append(f"{self.name} -> {self.itemParser.name}*")
|
||||
self.itemParser._grammarRules(output)
|
||||
|
||||
|
||||
class OptionalParser(Parser):
|
||||
def __init__(self, parser, name):
|
||||
super().__init__(name)
|
||||
self.parser = parser
|
||||
|
||||
def _parse(self, input):
|
||||
result = self.parser.parse(input)
|
||||
if result.result:
|
||||
return result
|
||||
|
||||
return Parser(parse, name, parsers=parser)
|
||||
return ParseResult.OK(NoneNode())
|
||||
|
||||
@staticmethod
|
||||
def optional(parser, name="??"):
|
||||
def parse(input):
|
||||
result = parser(input)
|
||||
if result.result:
|
||||
return result
|
||||
def _grammarRules(self, output):
|
||||
output.append(f"{self.name} -> {self.parser.name}?")
|
||||
|
||||
return ParseResult.OK(NoneNode())
|
||||
|
||||
return Parser(parse, name, parsers=[parser])
|
||||
class LoopParser(Parser):
|
||||
def __init__(self, startParser, itemParser, endParser, createNode, name):
|
||||
super().__init__(name)
|
||||
self.startParser = startParser
|
||||
self.itemParser = itemParser
|
||||
self.endParser = endParser
|
||||
self.createNode = createNode
|
||||
|
||||
@staticmethod
|
||||
def epsilon():
|
||||
return lambda *args: ParseResult.OK(NoneNode())
|
||||
|
||||
@staticmethod
|
||||
def many(parser, createNode, name="*"):
|
||||
def parse(input):
|
||||
results = []
|
||||
snap = input.snapshot()
|
||||
pos = input.currentPos()
|
||||
def _parse(self, input):
|
||||
items = []
|
||||
start = self.startParser.parse(input)
|
||||
if start.result:
|
||||
while True:
|
||||
result = parser(input)
|
||||
if result.result:
|
||||
results.append(result.node)
|
||||
snap = input.snapshot()
|
||||
else:
|
||||
input.reset(snap)
|
||||
return ParseResult.OK(createNode(results, pos) if len(results) > 0 else NoneNode())
|
||||
end = self.endParser.parse(input)
|
||||
if end.result:
|
||||
return ParseResult.OK(self.createNode(start.node, items, end.node))
|
||||
item = self.itemParser.parse(input)
|
||||
if not item.result:
|
||||
return ParseResult.FAIL()
|
||||
items.append(item.node)
|
||||
|
||||
return Parser(parse, name, parsers=[parser])
|
||||
return ParseResult.FAIL()
|
||||
|
||||
@staticmethod
|
||||
def wrap(parser, createNode):
|
||||
def parse(input):
|
||||
result = parser(input)
|
||||
if result.result:
|
||||
return ParseResult.OK(createNode(result.node))
|
||||
def _grammarRules(self, output):
|
||||
output.append(f"{self.name} -> {self.startParser.name} {self.itemParser.name}* {self.endParser.name}")
|
||||
self.startParser._grammarRules(output)
|
||||
self.itemParser._grammarRules(output)
|
||||
self.endParser._grammarRules(output)
|
||||
|
||||
return result
|
||||
|
||||
return parse
|
||||
@@ -10,9 +10,6 @@ class Sound:
|
||||
def play(self):
|
||||
sd.play(self.data, self.fs, blocking=True)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.file == other.file and self.data == other.data
|
||||
|
||||
def __str__(self):
|
||||
return f"sound[{self.file}]"
|
||||
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
from smnp.error.function import FunctionNotFoundException, MethodNotFoundException, IllegalFunctionInvocationException
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.function.tools import argsTypesToString
|
||||
from smnp.runtime.evaluators.function import BodyEvaluator, Return
|
||||
from smnp.type.model import Type
|
||||
from smnp.runtime.evaluators.function import BodyEvaluator
|
||||
|
||||
|
||||
class Environment():
|
||||
def __init__(self, scopes, functions, methods, source):
|
||||
def __init__(self, scopes, functions, methods):
|
||||
self.scopes = scopes
|
||||
self.functions = functions
|
||||
self.methods = methods
|
||||
self.customFunctions = []
|
||||
self.customMethods = []
|
||||
self.callStack = []
|
||||
self.source = source
|
||||
|
||||
def invokeMethod(self, object, name, args):
|
||||
builtinMethodResult = self._invokeBuiltinMethod(object, name, args)
|
||||
@@ -40,15 +38,10 @@ class Environment():
|
||||
if method.typeSignature.check([object])[0] and method.name == name: #Todo sprawdzic sygnature typu
|
||||
signatureCheckresult = method.signature.check(args)
|
||||
if signatureCheckresult[0]:
|
||||
self.scopes.append(method.defaultArgs)
|
||||
self.scopes[-1].update({argName: argValue for argName, argValue in zip(method.arguments, list(signatureCheckresult[1:]))})
|
||||
self.scopes.append({argName: argValue for argName, argValue in zip(method.arguments, list(signatureCheckresult[1:]))})
|
||||
self.scopes[-1][method.alias] = object
|
||||
self.callStack.append(CallStackItem(name))
|
||||
result = Type.void()
|
||||
try:
|
||||
BodyEvaluator.evaluate(method.body, self).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
except Return as r:
|
||||
result = r.value
|
||||
result = BodyEvaluator.evaluate(method.body, self).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
self.callStack.pop(-1)
|
||||
self.scopes.pop(-1)
|
||||
return (True, result)
|
||||
@@ -81,27 +74,20 @@ class Environment():
|
||||
if function.name == name:
|
||||
signatureCheckresult = function.signature.check(args)
|
||||
if signatureCheckresult[0]:
|
||||
self.appendScope(function.defaultArgs)
|
||||
appendedScopeIndex = len(self.scopes)-1
|
||||
self.scopes[-1].update({ argName: argValue for argName, argValue in zip(function.arguments, list(signatureCheckresult[1:])) })
|
||||
self.scopes.append({ argName: argValue for argName, argValue in zip(function.arguments, list(signatureCheckresult[1:])) })
|
||||
self.callStack.append(CallStackItem(name))
|
||||
result = Type.void()
|
||||
try:
|
||||
BodyEvaluator.evaluate(function.body, self).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
except Return as r:
|
||||
result = r.value
|
||||
result = BodyEvaluator.evaluate(function.body, self).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
self.callStack.pop(-1)
|
||||
self.popScope(mergeVariables=False)
|
||||
self.removeScopesAfter(appendedScopeIndex)
|
||||
self.scopes.pop(-1)
|
||||
return (True, result)
|
||||
raise IllegalFunctionInvocationException(f"{function.name}{function.signature.string}", f"{name}{argsTypesToString(args)}")
|
||||
return (False, None)
|
||||
|
||||
def addCustomFunction(self, name, signature, arguments, body, defaultArguments):
|
||||
def addCustomFunction(self, name, signature, arguments, body):
|
||||
if len([fun for fun in self.functions + self.customFunctions if fun.name == name]) > 0:
|
||||
raise RuntimeException(f"Cannot redeclare function '{name}'", None)
|
||||
|
||||
self.customFunctions.append(CustomFunction(name, signature, arguments, body, defaultArguments))
|
||||
self.customFunctions.append(CustomFunction(name, signature, arguments, body))
|
||||
|
||||
# TODO:
|
||||
# There is still problem with checking existing of generic types, like lists:
|
||||
@@ -112,14 +98,14 @@ class Environment():
|
||||
# function foo() { return 2 }
|
||||
# }
|
||||
# Then calling [1, 2, 3, 4].foo() will produce 1, when the second method is more suitable
|
||||
def addCustomMethod(self, typeSignature, alias, name, signature, arguments, body, defaultArguments):
|
||||
def addCustomMethod(self, typeSignature, alias, name, signature, arguments, body):
|
||||
if len([m for m in self.methods if m.name == name and m.signature.matchers[0] == typeSignature.matchers[0]]) > 0:
|
||||
raise RuntimeException(f"Cannot redeclare method '{name}' for type '{typeSignature.matchers[0]}'", None)
|
||||
|
||||
if len([m for m in self.customMethods if m.name == name and m.typeSignature.matchers[0] == typeSignature.matchers[0]]) > 0:
|
||||
raise RuntimeException(f"Cannot redeclare method '{name}' for type '{typeSignature.matchers[0]}'", None)
|
||||
|
||||
self.customMethods.append(CustomMethod(typeSignature, alias, name, signature, arguments, body, defaultArguments))
|
||||
self.customMethods.append(CustomMethod(typeSignature, alias, name, signature, arguments, body))
|
||||
|
||||
def findVariable(self, name, type=None, pos=None):
|
||||
for scope in reversed(self.scopes):
|
||||
@@ -142,20 +128,6 @@ class Environment():
|
||||
else:
|
||||
return scope
|
||||
|
||||
def appendScope(self, variables=None):
|
||||
if variables is None:
|
||||
variables = {}
|
||||
|
||||
self.scopes.append(variables)
|
||||
|
||||
def popScope(self, mergeVariables=True):
|
||||
lastScope = self.scopes.pop(-1)
|
||||
if mergeVariables:
|
||||
self.scopes[-1].update(lastScope)
|
||||
|
||||
def removeScopesAfter(self, index):
|
||||
del self.scopes[index:]
|
||||
|
||||
def scopesToString(self):
|
||||
return "Scopes:\n" + ("\n".join([ f" [{i}]: {scope}" for i, scope in enumerate(self.scopes) ]))
|
||||
|
||||
@@ -190,23 +162,22 @@ class Environment():
|
||||
class CallStackItem:
|
||||
def __init__(self, function):
|
||||
self.function = function
|
||||
self.value = None
|
||||
|
||||
|
||||
class CustomFunction:
|
||||
def __init__(self, name, signature, arguments, body, defaultArgs):
|
||||
def __init__(self, name, signature, arguments, body):
|
||||
self.name = name
|
||||
self.signature = signature
|
||||
self.arguments = arguments
|
||||
self.body = body
|
||||
self.defaultArgs = defaultArgs
|
||||
|
||||
|
||||
class CustomMethod:
|
||||
def __init__(self, typeSignature, alias, name, signature, arguments, body, defaultArgs):
|
||||
def __init__(self, typeSignature, alias, name, signature, arguments, body):
|
||||
self.typeSignature = typeSignature
|
||||
self.alias = alias
|
||||
self.name = name
|
||||
self.signature = signature
|
||||
self.arguments = arguments
|
||||
self.body = body
|
||||
self.defaultArgs = defaultArgs
|
||||
@@ -1,3 +1,7 @@
|
||||
def createEnvironment():
|
||||
return
|
||||
from smnp.environment.environment import Environment
|
||||
from smnp.module import functions, methods
|
||||
|
||||
|
||||
def createEnvironment():
|
||||
return Environment([{}], functions, methods)
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ class SmnpException(Exception):
|
||||
def __init__(self, msg, pos):
|
||||
self.msg = msg
|
||||
self.pos = pos
|
||||
self.file = None
|
||||
|
||||
def _title(self):
|
||||
pass
|
||||
@@ -11,10 +10,7 @@ class SmnpException(Exception):
|
||||
return ""
|
||||
|
||||
def _position(self):
|
||||
return "" if self.pos is None else f"[line {self.pos[0]+1}, col {self.pos[1]+1}]"
|
||||
|
||||
def _file(self):
|
||||
return "" if self.file is None else f"File: {self.file}"
|
||||
return "" if self.pos is None else f" [line {self.pos[0]+1}, col {self.pos[1]+1}]"
|
||||
|
||||
def message(self):
|
||||
return f"{self._title()}\n{self._file()} {self._position()}\n\n{self.msg}\n{self._postMessage()}"
|
||||
return f"{self._title()}{self._position()}:\n{self.msg}\n{self._postMessage()}"
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
|
||||
|
||||
class CustomException(RuntimeException):
|
||||
def __init__(self, message, pos):
|
||||
super().__init__(message, pos)
|
||||
|
||||
def _title(self):
|
||||
return "Execution Error"
|
||||
|
||||
def _postMessage(self):
|
||||
return "\n" + self.environment.callStackToString() if len(self.environment.callStack) > 0 else ""
|
||||
@@ -12,9 +12,6 @@ class Signature:
|
||||
|
||||
def varargSignature(varargMatcher, *basicSignature, wrapVarargInValue=False):
|
||||
def check(args):
|
||||
if any([ matcher.optional for matcher in [ varargMatcher, *basicSignature ]]):
|
||||
raise RuntimeError("Vararg signature can't have optional arguments")
|
||||
|
||||
if len(basicSignature) > len(args):
|
||||
return doesNotMatchVararg(basicSignature)
|
||||
|
||||
@@ -41,7 +38,7 @@ def doesNotMatchVararg(basicSignature):
|
||||
|
||||
def signature(*signature):
|
||||
def check(args):
|
||||
if len(args) > len(signature) or len(args) < len([ matcher for matcher in signature if not matcher.optional ]):
|
||||
if len(signature) != len(args):
|
||||
return doesNotMatch(signature)
|
||||
|
||||
for s, a in zip(signature, args):
|
||||
@@ -55,12 +52,6 @@ def signature(*signature):
|
||||
return Signature(check, string, signature)
|
||||
|
||||
|
||||
def optional(matcher):
|
||||
matcher.optional = True
|
||||
matcher.string += "?"
|
||||
return matcher
|
||||
|
||||
|
||||
def doesNotMatch(sign):
|
||||
return (False, *[None for n in sign])
|
||||
|
||||
|
||||
@@ -5,5 +5,7 @@ from smnp.program.interpreter import Interpreter
|
||||
|
||||
def loadStandardLibrary():
|
||||
mainSource = resource_string('smnp.library.code', 'main.mus').decode("utf-8")
|
||||
env = Interpreter.interpretString(mainSource, "<stdlib>")
|
||||
return env
|
||||
boolSource = resource_string('smnp.library.code', 'bool.mus').decode("utf-8")
|
||||
env = Interpreter.interpretString(mainSource)
|
||||
return Interpreter.interpretString(boolSource, baseEnvironment=env)
|
||||
|
||||
|
||||
30
smnp/main.py
30
smnp/main.py
@@ -1,14 +1,32 @@
|
||||
import sys
|
||||
|
||||
from smnp.ast.node.atom import AtomParser
|
||||
from smnp.ast.node.chain import ChainParser
|
||||
from smnp.ast.node.list import ListParser
|
||||
from smnp.ast.node.model import Node
|
||||
from smnp.error.base import SmnpException
|
||||
from smnp.library.loader import loadStandardLibrary
|
||||
from smnp.program.interpreter import Interpreter
|
||||
from smnp.token.tokenizer import tokenize
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
stdLibraryEnv = loadStandardLibrary()
|
||||
Interpreter.interpretFile(sys.argv[1], printTokens=False, printAst=False, execute=True, baseEnvironment=stdLibraryEnv)
|
||||
# stdLibraryEnv = loadStandardLibrary()
|
||||
# Interpreter.interpretFile(sys.argv[1], printTokens=True, printAst=True, execute=False, baseEnvironment=None)
|
||||
# draft()
|
||||
|
||||
class TestNode(Node):
|
||||
def __init__(self, children):
|
||||
super().__init__((-1, -1))
|
||||
self.children = children
|
||||
|
||||
tokens = tokenize(['[1, 2]'])
|
||||
parser = ListParser()
|
||||
#print(parser.grammar())
|
||||
res = parser.parse(tokens)
|
||||
|
||||
print()
|
||||
if res.result:
|
||||
res.node.print()
|
||||
else:
|
||||
print("nie sparsowano")
|
||||
|
||||
except SmnpException as e:
|
||||
print(e.message())
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from smnp.module import system, mic, note, iterable, sound, synth, string, util, integer, float
|
||||
from smnp.module import system, mic, note, iterable, sound, synth, string, util
|
||||
|
||||
functions = [ *system.functions, *mic.functions, *note.functions, *iterable.functions, *sound.functions, *synth.functions, *string.functions, *util.functions, *integer.functions, *float.functions ]
|
||||
methods = [ *system.methods, *mic.methods, *note.methods, *iterable.methods, *sound.methods, *synth.methods, *string.methods, *util.methods, *integer.methods, *float.methods ]
|
||||
functions = [ *system.functions, *mic.functions, *note.functions, *iterable.functions, *sound.functions, *synth.functions, *string.functions, *util.functions ]
|
||||
methods = [ *system.methods, *mic.methods, *note.methods, *iterable.methods, *sound.methods, *synth.methods, *string.functions, *util.methods ]
|
||||
@@ -1,4 +0,0 @@
|
||||
from smnp.module.float.function import float
|
||||
|
||||
functions = [ float.function ]
|
||||
methods = []
|
||||
@@ -1,26 +0,0 @@
|
||||
from smnp.function.model import CombinedFunction, Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature1 = signature(ofType(Type.INTEGER))
|
||||
def _function1(env, value):
|
||||
return Type.float(float(value.value))
|
||||
|
||||
|
||||
_signature2 = signature(ofType(Type.STRING))
|
||||
def _function2(env, value):
|
||||
return Type.float(float(value.value))
|
||||
|
||||
|
||||
_signature3 = signature(ofType(Type.FLOAT))
|
||||
def _function3(env, value):
|
||||
return value
|
||||
|
||||
|
||||
function = CombinedFunction(
|
||||
'Float',
|
||||
Function(_signature1, _function1),
|
||||
Function(_signature2, _function2),
|
||||
Function(_signature3, _function3),
|
||||
)
|
||||
@@ -1,4 +0,0 @@
|
||||
from smnp.module.integer.function import integer
|
||||
|
||||
functions = [ integer.function ]
|
||||
methods = []
|
||||
@@ -1,25 +0,0 @@
|
||||
from smnp.function.model import CombinedFunction, Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature1 = signature(ofType(Type.FLOAT))
|
||||
def _function1(env, value):
|
||||
return Type.integer(int(value.value))
|
||||
|
||||
|
||||
_signature2 = signature(ofType(Type.STRING))
|
||||
def _function2(env, value):
|
||||
return Type.integer(int(value.value))
|
||||
|
||||
|
||||
_signature3 = signature(ofType(Type.INTEGER))
|
||||
def _function3(env, value):
|
||||
return value
|
||||
|
||||
function = CombinedFunction(
|
||||
'Integer',
|
||||
Function(_signature1, _function1),
|
||||
Function(_signature2, _function2),
|
||||
Function(_signature3, _function3),
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
from smnp.module.iterable.function import map, get
|
||||
from smnp.module.iterable.function import combine, flat, map, range, get
|
||||
|
||||
functions = [ map.function ]
|
||||
functions = [ combine.function, flat.function, map.function, range.function ]
|
||||
methods = [ get.function ]
|
||||
18
smnp/module/iterable/function/combine.py
Normal file
18
smnp/module/iterable/function/combine.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from functools import reduce
|
||||
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofTypes
|
||||
|
||||
_signature = varargSignature(ofTypes(Type.LIST))
|
||||
def _function(env, vararg):
|
||||
if len(vararg) == 1:
|
||||
return vararg[0]
|
||||
|
||||
combined = reduce(lambda x, y: x.value + y.value, vararg)
|
||||
return Type.list(combined)
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'combine')
|
||||
|
||||
23
smnp/module/iterable/function/flat.py
Normal file
23
smnp/module/iterable/function/flat.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import allTypes
|
||||
|
||||
_signature = varargSignature(allTypes())
|
||||
def _function(env, vararg):
|
||||
return Type.list(doFlat(vararg, [])).decompose()
|
||||
|
||||
|
||||
def doFlat(input, output=None):
|
||||
if output is None:
|
||||
output = []
|
||||
|
||||
for item in input:
|
||||
if item.type == Type.LIST:
|
||||
doFlat(item.value, output)
|
||||
else:
|
||||
output.append(item)
|
||||
return output
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'flat')
|
||||
36
smnp/module/iterable/function/range.py
Normal file
36
smnp/module/iterable/function/range.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from smnp.function.model import CombinedFunction, Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.note.model import Note
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature1 = signature(ofType(Type.INTEGER))
|
||||
def _function1(env, upper):
|
||||
return Type.list(list(range(upper.value + 1)))
|
||||
|
||||
|
||||
_signature2 = signature(ofType(Type.INTEGER), ofType(Type.INTEGER))
|
||||
def _function2(env, lower, upper):
|
||||
return Type.list(list(range(lower.value, upper.value + 1)))
|
||||
|
||||
|
||||
_signature3 = signature(ofType(Type.INTEGER), ofType(Type.INTEGER), ofType(Type.INTEGER))
|
||||
def _function3(env, lower, upper, step):
|
||||
return Type.list(list(range(lower.value, upper.value + 1, step.value)))
|
||||
|
||||
|
||||
_signature4 = signature(ofType(Type.NOTE), ofType(Type.NOTE))
|
||||
def _function4(env, lower, upper):
|
||||
return Type.list([Type.note(n) for n in Note.range(lower.value, upper.value)])
|
||||
|
||||
|
||||
# TODO
|
||||
# signature5 = range(note lower, note upper, integer step) OR step = "diatonic" | "chromatic" | "augmented" | "diminish"
|
||||
|
||||
function = CombinedFunction(
|
||||
'range',
|
||||
Function(_signature1, _function1),
|
||||
Function(_signature2, _function2),
|
||||
Function(_signature3, _function3),
|
||||
Function(_signature4, _function4),
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
from smnp.module.note.function import note
|
||||
from smnp.module.note.function import tuplet, transpose, semitones, octave, duration, interval
|
||||
|
||||
functions = [ note.function ]
|
||||
methods = []
|
||||
functions = [ semitones.function, interval.function, transpose.function, tuplet.function ]
|
||||
methods = [ duration.function, octave.function ]
|
||||
11
smnp/module/note/function/duration.py
Normal file
11
smnp/module/note/function/duration.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature = signature(ofType(Type.NOTE), ofType(Type.INTEGER))
|
||||
def _function(env, note, duration):
|
||||
return Type.note(note.value.withDuration(duration.value))
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'withDuration')
|
||||
27
smnp/module/note/function/interval.py
Normal file
27
smnp/module/note/function/interval.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from smnp.function.model import Function, CombinedFunction
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.note.interval import intervalToString
|
||||
from smnp.note.model import Note
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.list import listOf
|
||||
from smnp.type.signature.matcher.type import ofTypes
|
||||
|
||||
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
|
||||
def _function1(env, vararg):
|
||||
withoutPauses = [note.value for note in vararg if note.type == Type.NOTE]
|
||||
if len(withoutPauses) < 2:
|
||||
return Type.list([])
|
||||
semitones = [Note.checkInterval(withoutPauses[i-1], withoutPauses[i]) for i in range(1, len(withoutPauses))]
|
||||
return Type.list([Type.string(intervalToString(s)) for s in semitones]).decompose()
|
||||
|
||||
|
||||
_signature2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
|
||||
def _function2(env, vararg):
|
||||
return Type.list([_function1(env, arg.value) for arg in vararg]).decompose()
|
||||
|
||||
|
||||
function = CombinedFunction(
|
||||
'interval',
|
||||
Function(_signature1, _function1),
|
||||
Function(_signature2, _function2)
|
||||
)
|
||||
@@ -1,11 +0,0 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.note.model import Note
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature = signature(ofType(Type.STRING), ofType(Type.INTEGER), ofType(Type.INTEGER), ofType(Type.BOOL))
|
||||
def _function(env, note, octave, duration, dot):
|
||||
return Type.note(Note(note.value, octave.value, duration.value, dot.value))
|
||||
|
||||
function = Function(_signature, _function, 'Note')
|
||||
11
smnp/module/note/function/octave.py
Normal file
11
smnp/module/note/function/octave.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature = signature(ofType(Type.NOTE), ofType(Type.INTEGER))
|
||||
def _function(env, note, octave):
|
||||
return Type.note(note.value.withOctave(octave.value))
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'withOctave')
|
||||
25
smnp/module/note/function/semitones.py
Normal file
25
smnp/module/note/function/semitones.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from smnp.function.model import Function, CombinedFunction
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.note.model import Note
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.list import listOf
|
||||
from smnp.type.signature.matcher.type import ofTypes
|
||||
|
||||
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
|
||||
def _function1(env, vararg):
|
||||
withoutPauses = [note.value for note in vararg if note.type == Type.NOTE]
|
||||
if len(withoutPauses) < 2:
|
||||
return Type.list([])
|
||||
return Type.list([Type.integer(Note.checkInterval(withoutPauses[i-1], withoutPauses[i])) for i in range(1, len(withoutPauses))]).decompose()
|
||||
|
||||
|
||||
_signature2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
|
||||
def _function2(env, vararg):
|
||||
return Type.list([_function1(env, arg.value) for arg in vararg]).decompose()
|
||||
|
||||
|
||||
function = CombinedFunction(
|
||||
"semitones",
|
||||
Function(_signature1, _function1),
|
||||
Function(_signature2, _function2),
|
||||
)
|
||||
22
smnp/module/note/function/transpose.py
Normal file
22
smnp/module/note/function/transpose.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from smnp.function.model import CombinedFunction, Function
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.list import listOf
|
||||
from smnp.type.signature.matcher.type import ofTypes
|
||||
|
||||
_signature1 = varargSignature(ofTypes(Type.INTEGER, Type.NOTE), ofTypes(Type.INTEGER))
|
||||
def _function1(env, value, vararg):
|
||||
transposed = [Type.note(arg.value.transpose(value.value)) if arg.type == Type.NOTE else arg for arg in vararg]
|
||||
return Type.list(transposed).decompose()
|
||||
|
||||
|
||||
_signature2 = varargSignature(listOf(Type.INTEGER, Type.NOTE), ofTypes(Type.INTEGER))
|
||||
def _function2(env, value, vararg):
|
||||
return Type.list([_function1(env, value, arg.value) for arg in vararg]).decompose()
|
||||
|
||||
|
||||
function = CombinedFunction(
|
||||
'transpose',
|
||||
Function(_signature1, _function1),
|
||||
Function(_signature2, _function2)
|
||||
)
|
||||
23
smnp/module/note/function/tuplet.py
Normal file
23
smnp/module/note/function/tuplet.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from smnp.function.model import CombinedFunction, Function
|
||||
from smnp.function.signature import signature, varargSignature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.list import listOf
|
||||
from smnp.type.signature.matcher.type import ofTypes
|
||||
|
||||
_signature1 = varargSignature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER), ofTypes(Type.INTEGER))
|
||||
def _function1(env, n, m, vararg):
|
||||
t = [Type.note(arg.value.withDuration(int(arg.value.duration * n.value / m.value))) for arg in vararg]
|
||||
return Type.list(t).decompose()
|
||||
|
||||
|
||||
|
||||
_signature2 = signature(ofTypes(Type.INTEGER), ofTypes(Type.INTEGER), listOf(Type.NOTE))
|
||||
def _function2(env, n, m, notes):
|
||||
return _function1(env, n, m, notes.value)
|
||||
|
||||
|
||||
function = CombinedFunction(
|
||||
'tuplet',
|
||||
Function(_signature1, _function1),
|
||||
Function(_signature2, _function2)
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
from smnp.module.string.function import stringify
|
||||
from smnp.module.string.function import concat
|
||||
|
||||
functions = []
|
||||
methods = [ stringify.function ]
|
||||
functions = [ concat.function ]
|
||||
methods = []
|
||||
11
smnp/module/string/function/concat.py
Normal file
11
smnp/module/string/function/concat.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature = varargSignature(ofType(Type.STRING))
|
||||
def _function(env, vararg):
|
||||
return Type.string("".join([ arg.value for arg in vararg ]))
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'concat')
|
||||
@@ -1,10 +0,0 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import allTypes
|
||||
|
||||
_signature = signature(allTypes())
|
||||
def _function(env, object):
|
||||
return Type.string(object.stringify())
|
||||
|
||||
function = Function(_signature, _function, 'toString')
|
||||
@@ -1,4 +1,4 @@
|
||||
from smnp.module.synth.function import synth, pause, plot, compile
|
||||
from smnp.module.synth.function import synth, pause
|
||||
|
||||
functions = [ synth.function, pause.function, plot.function, compile.function ]
|
||||
functions = [ synth.function, pause.function ]
|
||||
methods = []
|
||||
@@ -1,141 +0,0 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.function.model import Function, CombinedFunction
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.module.synth.lib.wave import compilePolyphony
|
||||
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.list import listOf
|
||||
from smnp.type.signature.matcher.type import ofTypes, ofType
|
||||
|
||||
|
||||
DEFAULT_BPM = 120
|
||||
DEFAULT_OVERTONES = [0.4, 0.3, 0.1, 0.1, 0.1]
|
||||
DEFAULT_DECAY = 4
|
||||
DEFAULT_ATTACK = 100
|
||||
|
||||
|
||||
def getBpm(config):
|
||||
key = Type.string("bpm")
|
||||
if key in config.value:
|
||||
bpm = config.value[key]
|
||||
if bpm.type != Type.INTEGER or bpm.value <= 0:
|
||||
raise RuntimeException("The 'bpm' property must be positive integer", None)
|
||||
|
||||
return bpm.value
|
||||
|
||||
return DEFAULT_BPM
|
||||
|
||||
|
||||
def getOvertones(config):
|
||||
key = Type.string("overtones")
|
||||
if key in config.value:
|
||||
overtones = config.value[key]
|
||||
rawOvertones = [overtone.value for overtone in overtones.value]
|
||||
if overtones.type != Type.LIST or not all(overtone.type in [Type.FLOAT, Type.INTEGER] for overtone in overtones.value):
|
||||
raise RuntimeException("The 'overtones' property must be list of floats", None)
|
||||
|
||||
if len(rawOvertones) < 1:
|
||||
raise RuntimeException("The 'overtones' property must contain one overtone at least", None)
|
||||
|
||||
if any(overtone < 0 for overtone in rawOvertones):
|
||||
raise RuntimeException("The 'overtones' property mustn't contain negative values", None)
|
||||
|
||||
if sum(rawOvertones) > 1.0:
|
||||
raise RuntimeException("The 'overtones' property must contain overtones which sum is not greater than 1.0", None)
|
||||
|
||||
return rawOvertones
|
||||
|
||||
return DEFAULT_OVERTONES
|
||||
|
||||
|
||||
def getDecay(config):
|
||||
key = Type.string("decay")
|
||||
if key in config.value:
|
||||
decay = config.value[key]
|
||||
if not decay.type in [Type.INTEGER, Type.FLOAT] or decay.value < 0:
|
||||
raise RuntimeException("The 'decay' property must be non-negative integer or float", None)
|
||||
|
||||
return decay.value
|
||||
|
||||
return DEFAULT_DECAY
|
||||
|
||||
|
||||
def getAttack(config):
|
||||
key = Type.string("attack")
|
||||
if key in config.value:
|
||||
attack = config.value[key]
|
||||
if not attack.type in [Type.INTEGER, Type.FLOAT] or attack.value < 0:
|
||||
raise RuntimeException("The 'attack' property must be non-negative integer or float", None)
|
||||
|
||||
return attack.value
|
||||
|
||||
return DEFAULT_ATTACK
|
||||
|
||||
|
||||
class Config:
|
||||
def __init__(self, bpm, overtones, decay, attack):
|
||||
self.bpm = bpm
|
||||
self.overtones = overtones
|
||||
self.decay = decay
|
||||
self.attack = attack
|
||||
|
||||
@staticmethod
|
||||
def default():
|
||||
return Config(DEFAULT_BPM, DEFAULT_OVERTONES, DEFAULT_DECAY, DEFAULT_ATTACK)
|
||||
|
||||
|
||||
_signature1 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
|
||||
def _function1(env, notes):
|
||||
return Type.list([Type.float(float(m)) for m in __function1(notes)])
|
||||
|
||||
|
||||
def __function1(notes):
|
||||
return compilePolyphony([note.value for note in notes], Config.default())
|
||||
|
||||
|
||||
_signature2 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
|
||||
def _function2(env, notes):
|
||||
return Type.list([Type.float(float(m)) for m in __function2(notes)])
|
||||
|
||||
|
||||
def __function2(notes):
|
||||
return compilePolyphony([ notes ], Config.default())
|
||||
|
||||
|
||||
_signature3 = varargSignature(listOf(Type.NOTE, Type.INTEGER), ofType(Type.MAP))
|
||||
def _function3(env, config, notes):
|
||||
return Type.list([ Type.float(float(m)) for m in __function3(config, notes) ])
|
||||
|
||||
|
||||
def __function3(config, notes):
|
||||
rawNotes = [note.value for note in notes]
|
||||
|
||||
bpm = getBpm(config)
|
||||
overtones = getOvertones(config)
|
||||
decay = getDecay(config)
|
||||
attack = getAttack(config)
|
||||
|
||||
return compilePolyphony(rawNotes, Config(bpm, overtones, decay, attack))
|
||||
|
||||
|
||||
_signature4 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER), ofType(Type.MAP))
|
||||
def _function4(env, config, notes):
|
||||
return Type.list([ Type.float(float(m)) for m in __function4(config, notes) ])
|
||||
|
||||
|
||||
def __function4(config, notes):
|
||||
bpm = getBpm(config)
|
||||
overtones = getOvertones(config)
|
||||
decay = getDecay(config)
|
||||
attack = getAttack(config)
|
||||
|
||||
return compilePolyphony([ notes ], Config(bpm, overtones, decay, attack))
|
||||
|
||||
|
||||
function = CombinedFunction(
|
||||
'wave',
|
||||
Function(_signature1, _function1),
|
||||
Function(_signature2, _function2),
|
||||
Function(_signature3, _function3),
|
||||
Function(_signature4, _function4),
|
||||
)
|
||||
@@ -1,13 +1,13 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.module.synth.lib.wave import pause
|
||||
from smnp.module.synth.lib import player
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofTypes
|
||||
|
||||
_signature = signature(ofTypes(Type.INTEGER))
|
||||
def _function(env, value):
|
||||
bpm = env.findVariable('bpm')
|
||||
pause(value.value, bpm.value)
|
||||
player.pause(value.value, bpm.value)
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'pause')
|
||||
@@ -1,13 +0,0 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.module.synth.lib.wave import plot
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.list import listOf
|
||||
|
||||
_signature = signature(listOf(Type.FLOAT))
|
||||
def _function(env, wave):
|
||||
rawWave = [ m.value for m in wave.value ]
|
||||
plot(rawWave)
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'plotWave')
|
||||
@@ -1,47 +1,25 @@
|
||||
from smnp.function.model import Function, CombinedFunction
|
||||
from smnp.function.model import CombinedFunction, Function
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.module.synth.function import compile
|
||||
from smnp.module.synth.lib.wave import play
|
||||
from smnp.module.synth.lib.player import playNotes
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.list import listOf
|
||||
from smnp.type.signature.matcher.type import ofTypes, ofType
|
||||
from smnp.type.signature.matcher.type import ofTypes
|
||||
|
||||
_signature1 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
|
||||
def _function1(env, notes):
|
||||
wave = compile.__function1(notes)
|
||||
play(wave)
|
||||
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
|
||||
def _function1(env, vararg):
|
||||
notes = [arg.value for arg in vararg]
|
||||
bpm = env.findVariable('bpm')
|
||||
playNotes(notes, bpm.value)
|
||||
|
||||
|
||||
_signature2 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
|
||||
def _function2(env, notes):
|
||||
wave = compile.__function2(notes)
|
||||
play(wave)
|
||||
|
||||
|
||||
_signature3 = varargSignature(listOf(Type.NOTE, Type.INTEGER), ofType(Type.MAP))
|
||||
def _function3(env, config, notes):
|
||||
wave = compile.__function3(config, notes)
|
||||
play(wave)
|
||||
|
||||
|
||||
_signature4 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER), ofType(Type.MAP))
|
||||
def _function4(env, config, notes):
|
||||
wave = compile.__function4(config, notes)
|
||||
play(wave)
|
||||
|
||||
|
||||
_signature5 = varargSignature(listOf(Type.FLOAT))
|
||||
def _function5(env, waves):
|
||||
for wave in waves:
|
||||
rawWave = [m.value for m in wave.value]
|
||||
play(rawWave)
|
||||
_signature2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
|
||||
def _function2(env, vararg):
|
||||
for arg in vararg:
|
||||
_function1(env, arg.value)
|
||||
|
||||
|
||||
function = CombinedFunction(
|
||||
'synth',
|
||||
Function(_signature1, _function1),
|
||||
Function(_signature2, _function2),
|
||||
Function(_signature3, _function3),
|
||||
Function(_signature4, _function4),
|
||||
Function(_signature5, _function5)
|
||||
Function(_signature2, _function2)
|
||||
)
|
||||
24
smnp/module/synth/lib/player.py
Normal file
24
smnp/module/synth/lib/player.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import time
|
||||
|
||||
from smnp.module.synth.lib.wave import sine
|
||||
from smnp.note.model import Note
|
||||
|
||||
|
||||
def playNotes(notes, bpm):
|
||||
for note in notes:
|
||||
{
|
||||
Note: play,
|
||||
int: pause
|
||||
}[type(note)](note, bpm)
|
||||
|
||||
|
||||
def play(note, bpm):
|
||||
frequency = note.toFrequency()
|
||||
duration = 60 * 4 / note.duration / bpm
|
||||
duration *= 1.5 if note.dot else 1
|
||||
sine(frequency, duration)
|
||||
|
||||
|
||||
def pause(value, bpm):
|
||||
time.sleep(60 * 4 / value / bpm)
|
||||
|
||||
@@ -1,78 +1,12 @@
|
||||
import time
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import sounddevice as sd
|
||||
|
||||
from smnp.type.model import Type
|
||||
|
||||
FS = 44100
|
||||
|
||||
|
||||
def pause(value, bpm):
|
||||
time.sleep(60 * 4 / value / bpm)
|
||||
|
||||
|
||||
def plot(wave):
|
||||
X = np.arange(len(wave))
|
||||
plt.plot(X, wave)
|
||||
plt.show()
|
||||
|
||||
|
||||
def play(wave):
|
||||
sd.play(wave)
|
||||
time.sleep(len(wave) / FS)
|
||||
|
||||
|
||||
def compilePolyphony(notes, config):
|
||||
compiledLines = [1 / len(notes) * compileNotes(line, config) for line in notes]
|
||||
return sum(adjustSize(compiledLines))
|
||||
|
||||
|
||||
def adjustSize(compiledLines):
|
||||
maxSize = max(len(line) for line in compiledLines)
|
||||
|
||||
return [np.concatenate([line, np.zeros(maxSize - len(line))]) for line in compiledLines]
|
||||
|
||||
|
||||
def compileNotes(notes, config):
|
||||
dispatcher = {
|
||||
Type.NOTE: lambda note, overtones: sineForNote(note.value, config),
|
||||
Type.INTEGER: lambda note, overtones: silenceForPause(note.value, config)
|
||||
}
|
||||
|
||||
return np.concatenate([dispatcher[note.type](note, config) for note in notes])
|
||||
|
||||
|
||||
def sineForNote(note, config):
|
||||
frequency = note.toFrequency()
|
||||
duration = 60 * 4 / note.duration / config.bpm
|
||||
duration *= 1.5 if note.dot else 1
|
||||
return sound(frequency, duration, config)
|
||||
|
||||
|
||||
def sound(frequency, duration, config):
|
||||
return attack(decay(sum(overtone * sine((i+1) * frequency, duration) for i, overtone in enumerate(config.overtones) if overtone > 0), config), config)
|
||||
|
||||
|
||||
def decay(wave, config):
|
||||
magnitude = np.exp(-config.decay/len(wave) * np.arange(len(wave)))
|
||||
|
||||
return magnitude * wave
|
||||
|
||||
|
||||
def attack(wave, config):
|
||||
magnitude = -np.exp(-config.attack / len(wave) * np.arange(len(wave)))+1 \
|
||||
if config.attack > 0 \
|
||||
else np.ones(len(wave))
|
||||
|
||||
return magnitude * wave
|
||||
|
||||
|
||||
def sine(frequency, duration):
|
||||
return (np.sin(2 * np.pi * np.arange(FS * duration) * frequency / FS)).astype(np.float32)
|
||||
|
||||
|
||||
def silenceForPause(value, config):
|
||||
duration = 60 * 4 / value / config.bpm
|
||||
return np.zeros(int(FS * duration))
|
||||
samples = (np.sin(2*np.pi*np.arange(FS*duration)*frequency/FS)).astype(np.float32)
|
||||
sd.play(samples, FS)
|
||||
time.sleep(duration)
|
||||
@@ -1,4 +1,4 @@
|
||||
from smnp.module.system.function import sleep, display, displayln, debug, exit, type, read
|
||||
from smnp.module.system.function import sleep, display, debug, exit, type
|
||||
|
||||
functions = [ debug.function, display.function, displayln.function, exit.function, sleep.function, type.function, read.function ]
|
||||
functions = [ debug.function, display.function, exit.function, sleep.function, type.function ]
|
||||
methods = []
|
||||
@@ -4,7 +4,7 @@ from smnp.type.signature.matcher.type import allTypes
|
||||
|
||||
_signature = varargSignature(allTypes())
|
||||
def _function(env, vararg):
|
||||
print("".join([arg.stringify() for arg in vararg]), end="")
|
||||
print("".join([arg.stringify() for arg in vararg]))
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'print')
|
||||
@@ -1,10 +0,0 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.type.signature.matcher.type import allTypes
|
||||
|
||||
_signature = varargSignature(allTypes())
|
||||
def _function(env, vararg):
|
||||
print("".join([arg.stringify() for arg in vararg]))
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'println')
|
||||
@@ -1,72 +1,3 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.function.model import CombinedFunction, Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.token.tokenizers.bool import boolTokenizer
|
||||
from smnp.token.tokenizers.note import noteTokenizer
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature1 = signature()
|
||||
def _function1(env):
|
||||
value = input()
|
||||
return Type.string(value)
|
||||
|
||||
|
||||
_signature2 = signature(ofType(Type.STRING))
|
||||
def _function2(env, prompt):
|
||||
print(prompt.value, end="")
|
||||
value = input()
|
||||
return Type.string(value)
|
||||
|
||||
|
||||
_signature3 = signature(ofType(Type.TYPE))
|
||||
def _function3(env, type):
|
||||
value = input()
|
||||
return getValueAccordingToType(value, type)
|
||||
|
||||
|
||||
def getValueAccordingToType(value, type):
|
||||
try:
|
||||
if type.value == Type.STRING:
|
||||
return Type.string(value)
|
||||
|
||||
if type.value == Type.INTEGER:
|
||||
return Type.integer(int(value))
|
||||
|
||||
if type.value == Type.BOOL:
|
||||
consumedChars, token = boolTokenizer(value, 0, 0)
|
||||
if consumedChars > 0:
|
||||
return Type.bool(token.value)
|
||||
|
||||
return ValueError()
|
||||
|
||||
if type.value == Type.NOTE:
|
||||
consumedChars, token = noteTokenizer(value, 0, 0)
|
||||
if consumedChars > 0:
|
||||
return Type.note(token.value)
|
||||
|
||||
raise ValueError()
|
||||
|
||||
raise RuntimeException(f"Type {type.value.name.lower()} is not suuported", None)
|
||||
|
||||
except ValueError:
|
||||
raise RuntimeException(f"Invalid value '{value}' for type {type.value.name.lower()}", None)
|
||||
|
||||
|
||||
_signature4 = signature(ofType(Type.STRING), ofType(Type.TYPE))
|
||||
def _function4(env, prompt, type):
|
||||
print(prompt.value, end="")
|
||||
value = input()
|
||||
return getValueAccordingToType(value, type)
|
||||
|
||||
|
||||
function = CombinedFunction(
|
||||
'read',
|
||||
Function(_signature1, _function1),
|
||||
Function(_signature2, _function2),
|
||||
Function(_signature3, _function3),
|
||||
Function(_signature4, _function4)
|
||||
)
|
||||
|
||||
# TODO read function
|
||||
# def read(args, env):
|
||||
|
||||
@@ -1,12 +1,29 @@
|
||||
import random
|
||||
import random as r
|
||||
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.error.function import IllegalArgumentException
|
||||
from smnp.function.model import Function, CombinedFunction
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
from smnp.type.signature.matcher.list import listMatches
|
||||
from smnp.type.signature.matcher.type import ofTypes
|
||||
|
||||
_signature = signature(ofType(Type.INTEGER), ofType(Type.INTEGER))
|
||||
def _function(env, min, max):
|
||||
return Type.integer(random.randint(min.value, max.value))
|
||||
|
||||
function = Function(_signature, _function, 'rand')
|
||||
def forType(t):
|
||||
_signature = varargSignature(listMatches(ofTypes(Type.PERCENT), ofTypes(t)))
|
||||
def _function(env, vararg):
|
||||
choice = r.random()
|
||||
acc = 0
|
||||
if sum(arg.value[0].value for arg in vararg) != 1.0:
|
||||
raise IllegalArgumentException("Sum of all percentage values must be equal 100%")
|
||||
for arg in vararg:
|
||||
percent, item = arg.value
|
||||
acc += percent.value
|
||||
if choice <= acc:
|
||||
return item
|
||||
|
||||
return Function(_signature, _function)
|
||||
|
||||
|
||||
function = CombinedFunction('random', *[ forType(t) for t in Type if t != Type.VOID ])
|
||||
|
||||
#TODO: sample
|
||||
@@ -41,14 +41,10 @@ class NotePitch(Enum):
|
||||
|
||||
@staticmethod
|
||||
def toPitch(string):
|
||||
if string.lower() in stringToPitch:
|
||||
try:
|
||||
return stringToPitch[string.lower()]
|
||||
|
||||
if string.upper() in NotePitch.__members__:
|
||||
return NotePitch[string.upper()]
|
||||
|
||||
raise NoteException(f"Note '{string}' does not exist")
|
||||
|
||||
except KeyError as e:
|
||||
raise NoteException(f"Note '{string}' does not exist")
|
||||
|
||||
stringToPitch = {
|
||||
'cb': NotePitch.H,
|
||||
|
||||
@@ -1,42 +1,25 @@
|
||||
from smnp.ast.parser import parse
|
||||
from smnp.environment.environment import Environment
|
||||
#from smnp.environment.factory import createEnvironment
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.module import functions, methods
|
||||
from smnp.program.FileReader import readLines
|
||||
from smnp.runtime.evaluator import evaluate
|
||||
from smnp.token.tokenizer import tokenize
|
||||
|
||||
|
||||
class Interpreter:
|
||||
|
||||
@staticmethod
|
||||
def interpretString(string, source, printTokens=False, printAst=False, execute=True, baseEnvironment=None):
|
||||
return Interpreter._interpret(
|
||||
string.splitlines(),
|
||||
source,
|
||||
printTokens,
|
||||
printAst,
|
||||
execute,
|
||||
baseEnvironment,
|
||||
)
|
||||
def interpretString(string, printTokens=False, printAst=False, execute=True, baseEnvironment=None):
|
||||
return Interpreter._interpret(string.splitlines(), printTokens, printAst, execute, baseEnvironment)
|
||||
|
||||
@staticmethod
|
||||
def interpretFile(file, printTokens=False, printAst=False, execute=True, baseEnvironment=None, source=None):
|
||||
return Interpreter._interpret(
|
||||
readLines(file),
|
||||
source if source is not None else file,
|
||||
printTokens,
|
||||
printAst,
|
||||
execute,
|
||||
baseEnvironment,
|
||||
)
|
||||
def interpretFile(file, printTokens=False, printAst=False, execute=True, baseEnvironment=None):
|
||||
return Interpreter._interpret(readLines(file), printTokens, printAst, execute, baseEnvironment)
|
||||
|
||||
@staticmethod
|
||||
def _interpret(lines, source, printTokens=False, printAst=False, execute=True, baseEnvironment=None):
|
||||
environment = Environment([{}], functions, methods, source=source)
|
||||
|
||||
if baseEnvironment is not None:
|
||||
environment.extend(baseEnvironment)
|
||||
def _interpret(lines, printTokens=False, printAst=False, execute=True, baseEnvironment=None):
|
||||
#environment = createEnvironment()
|
||||
#if baseEnvironment is not None:
|
||||
# environment.extend(baseEnvironment)
|
||||
|
||||
try:
|
||||
tokens = tokenize(lines)
|
||||
@@ -47,11 +30,10 @@ class Interpreter:
|
||||
if printAst:
|
||||
ast.print()
|
||||
|
||||
if execute:
|
||||
evaluate(ast, environment)
|
||||
#if execute:
|
||||
# evaluate(ast, environment)
|
||||
|
||||
return environment
|
||||
#return environment
|
||||
except RuntimeException as e:
|
||||
e.environment = environment
|
||||
e.file = environment.source
|
||||
#e.environment = environment
|
||||
raise e
|
||||
@@ -1,11 +1,9 @@
|
||||
from smnp.ast.node.block import Block
|
||||
from smnp.ast.node.condition import IfElse
|
||||
from smnp.ast.node.extend import Extend
|
||||
from smnp.ast.node.function import FunctionDefinition
|
||||
from smnp.ast.node.imports import Import
|
||||
from smnp.ast.node.block import BlockNode
|
||||
from smnp.ast.node.extend import ExtendNode
|
||||
from smnp.ast.node.function import FunctionDefinitionNode
|
||||
from smnp.ast.node.imports import ImportNode
|
||||
from smnp.ast.node.program import Program
|
||||
from smnp.ast.node.ret import Return
|
||||
from smnp.ast.node.throw import Throw
|
||||
from smnp.ast.node.ret import ReturnNode
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.type.model import Type
|
||||
|
||||
@@ -71,28 +69,19 @@ class EvaluationResult():
|
||||
def evaluate(node, environment):
|
||||
from smnp.runtime.evaluators.program import ProgramEvaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.runtime.evaluators.condition import IfElseStatementEvaluator
|
||||
|
||||
from smnp.runtime.evaluators.function import FunctionDefinitionEvaluator
|
||||
from smnp.runtime.evaluators.extend import ExtendEvaluator
|
||||
from smnp.runtime.evaluators.block import BlockEvaluator
|
||||
from smnp.runtime.evaluators.imports import ImportEvaluator
|
||||
from smnp.runtime.evaluators.function import FunctionDefinitionEvaluator
|
||||
from smnp.runtime.evaluators.function import ReturnEvaluator
|
||||
from smnp.runtime.evaluators.extend import ExtendEvaluator
|
||||
from smnp.runtime.evaluators.throw import ThrowEvaluator
|
||||
|
||||
result = Evaluator.oneOf(
|
||||
Evaluator.forNodes(ProgramEvaluator.evaluate, Program),
|
||||
Evaluator.forNodes(IfElseStatementEvaluator.evaluate, IfElse),
|
||||
Evaluator.forNodes(BlockEvaluator.evaluate, Block),
|
||||
Evaluator.forNodes(ImportEvaluator.evaluate, Import),
|
||||
Evaluator.forNodes(FunctionDefinitionEvaluator.evaluate, FunctionDefinition),
|
||||
Evaluator.forNodes(ReturnEvaluator.evaluate, Return),
|
||||
Evaluator.forNodes(ExtendEvaluator.evaluate, Extend),
|
||||
Evaluator.forNodes(ThrowEvaluator.evaluate, Throw),
|
||||
#Evaluator.forNodes(ImportEvaluator.evaluate, ImportNode),
|
||||
#Evaluator.forNodes(FunctionDefinitionEvaluator.evaluate, FunctionDefinitionNode),
|
||||
#Evaluator.forNodes(ExtendEvaluator.evaluate, ExtendNode),
|
||||
#Evaluator.forNodes(BlockEvaluator.evaluate, BlockNode),
|
||||
#Evaluator.forNodes(ReturnEvaluator.evaluate, ReturnNode),
|
||||
Evaluator.forNodes(ImportEvaluator.evaluate, ImportNode),
|
||||
Evaluator.forNodes(FunctionDefinitionEvaluator.evaluate, FunctionDefinitionNode),
|
||||
Evaluator.forNodes(ExtendEvaluator.evaluate, ExtendNode),
|
||||
Evaluator.forNodes(BlockEvaluator.evaluate, BlockNode),
|
||||
Evaluator.forNodes(ReturnEvaluator.evaluate, ReturnNode),
|
||||
expressionEvaluator()
|
||||
)(node, environment)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from smnp.ast.node.identifier import Identifier, FunctionCall
|
||||
from smnp.ast.node.identifier import Identifier
|
||||
from smnp.ast.node.invocation import FunctionCallNode
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
@@ -19,9 +20,9 @@ class AccessEvaluator(Evaluator):
|
||||
except KeyError:
|
||||
raise RuntimeException(f"Unknown property '{right.value}' of type '{left.type.name.lower()}'", right.pos)
|
||||
|
||||
if type(right) == FunctionCall:
|
||||
if type(right) == FunctionCallNode:
|
||||
try:
|
||||
arguments = abstractIterableEvaluator(expressionEvaluator(doAssert=True))(right.arguments, environment)
|
||||
arguments = abstractIterableEvaluator(expressionEvaluator(True))(right.arguments, environment)
|
||||
return environment.invokeMethod(left, right.name.value, arguments)
|
||||
except RuntimeException as e:
|
||||
raise updatePos(e, right)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
|
||||
@@ -6,9 +7,16 @@ class AssignmentEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
target = node.left.value
|
||||
value = expressionEvaluator(doAssert=True)(node.right, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
environment.scopes[-1][target] = value
|
||||
target = node.target.value
|
||||
if target.startswith("_"):
|
||||
raise RuntimeException("Declaration and assignation variables with names starting with '_' is not allowed", node.target.pos)
|
||||
|
||||
value = expressionEvaluator(doAssert=True)(node.value, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
scopeOfExistingVariable = environment.findVariableScope(target)
|
||||
if scopeOfExistingVariable is None:
|
||||
environment.scopes[-1][target] = value
|
||||
else:
|
||||
scopeOfExistingVariable[target] = value
|
||||
|
||||
return value
|
||||
|
||||
|
||||
104
smnp/runtime/evaluators/asterisk.py
Normal file
104
smnp/runtime/evaluators/asterisk.py
Normal file
@@ -0,0 +1,104 @@
|
||||
from smnp.ast.node.identifier import Identifier
|
||||
from smnp.runtime.evaluator import evaluate, Evaluator, EvaluationResult
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class AsteriskEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
iterator = expressionEvaluator(doAssert=True)(node.iterator, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
return Evaluator.oneOf(
|
||||
cls._numberIteratorAsteriskEvaluator(iterator),
|
||||
cls._listIteratorAsteriskEvaluator(iterator),
|
||||
cls._mapIteratorAsteriskEvaluator(iterator)
|
||||
)(node, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
|
||||
@classmethod
|
||||
def _numberIteratorAsteriskEvaluator(cls, evaluatedIterator):
|
||||
def evaluator(node, environment):
|
||||
if evaluatedIterator.type == Type.INTEGER:
|
||||
results = []
|
||||
automaticVariable = cls._automaticNamedVariable(node.iterator, environment, "_")
|
||||
for i in range(evaluatedIterator.value):
|
||||
environment.scopes[-1][automaticVariable] = Type.integer(i + 1)
|
||||
result = evaluate(node.statement, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
if result is None or result.type == Type.VOID:
|
||||
results = None
|
||||
|
||||
if results is not None:
|
||||
results.append(result)
|
||||
|
||||
del environment.scopes[-1][automaticVariable]
|
||||
|
||||
return EvaluationResult.OK(Type.list(results).decompose() if results is not None else Type.void())
|
||||
|
||||
return EvaluationResult.FAIL()
|
||||
|
||||
return evaluator
|
||||
|
||||
@classmethod
|
||||
def _automaticNamedVariable(cls, iteratorNode, environment, prefix=''):
|
||||
if type(iteratorNode) == Identifier:
|
||||
return cls._automaticVariableName(environment, prefix, iteratorNode.value, False)
|
||||
else:
|
||||
return cls._automaticVariableName(environment, prefix, '', True)
|
||||
|
||||
@classmethod
|
||||
def _automaticVariableName(cls, environment, prefix='', suffix='', startWithNumber=False):
|
||||
number = 1 if startWithNumber else ''
|
||||
variableName = lambda x: f"{prefix}{x}{suffix}"
|
||||
while environment.findVariableScope(variableName(number)) is not None:
|
||||
if number == '':
|
||||
number = 1
|
||||
else:
|
||||
number += 1
|
||||
|
||||
return variableName(number)
|
||||
|
||||
@classmethod
|
||||
def _listIteratorAsteriskEvaluator(cls, evaluatedIterator):
|
||||
def evaluator(node, environment):
|
||||
if evaluatedIterator.type == Type.LIST:
|
||||
results = []
|
||||
automaticVariableKey = cls._automaticNamedVariable(node.iterator, environment, "_")
|
||||
automaticVariableValue = cls._automaticNamedVariable(node.iterator, environment, "__")
|
||||
for i, v in enumerate(evaluatedIterator.value):
|
||||
environment.scopes[-1][automaticVariableKey] = Type.integer(i + 1)
|
||||
environment.scopes[-1][automaticVariableValue] = v
|
||||
result = evaluate(node.statement, environment).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
if result is not None and result.type != Type.VOID:
|
||||
results.append(result)
|
||||
|
||||
del environment.scopes[-1][automaticVariableKey]
|
||||
del environment.scopes[-1][automaticVariableValue]
|
||||
|
||||
return EvaluationResult.OK(Type.list(results).decompose())
|
||||
|
||||
return EvaluationResult.FAIL()
|
||||
|
||||
return evaluator
|
||||
|
||||
@classmethod
|
||||
def _mapIteratorAsteriskEvaluator(cls, evaluatedIterator):
|
||||
def evaluator(node, environment):
|
||||
if evaluatedIterator.type == Type.MAP:
|
||||
results = []
|
||||
automaticVariableKey = cls._automaticNamedVariable(node.iterator, environment, "_")
|
||||
automaticVariableValue = cls._automaticNamedVariable(node.iterator, environment, "__")
|
||||
for k, v in evaluatedIterator.value.items():
|
||||
environment.scopes[-1][automaticVariableKey] = k
|
||||
environment.scopes[-1][automaticVariableValue] = v
|
||||
result = evaluate(node.statement, environment).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
if result is not None and result.type != Type.VOID:
|
||||
results.append(result)
|
||||
|
||||
del environment.scopes[-1][automaticVariableKey]
|
||||
del environment.scopes[-1][automaticVariableValue]
|
||||
|
||||
return EvaluationResult.OK(Type.list(results).decompose())
|
||||
|
||||
return EvaluationResult.FAIL()
|
||||
|
||||
return evaluator
|
||||
@@ -1,99 +0,0 @@
|
||||
from smnp.ast.node.atom import StringLiteral, IntegerLiteral, NoteLiteral, BoolLiteral, TypeLiteral, FloatLiteral
|
||||
from smnp.ast.node.identifier import Identifier
|
||||
from smnp.ast.node.list import List
|
||||
from smnp.ast.node.map import Map
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator, EvaluationResult
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.runtime.evaluators.float import FloatEvaluator
|
||||
from smnp.runtime.evaluators.iterable import abstractIterableEvaluator
|
||||
from smnp.runtime.tools.error import updatePos
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class IntegerEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
return Type.integer(node.value)
|
||||
|
||||
|
||||
class StringEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
return Type.string(node.value)
|
||||
|
||||
|
||||
class NoteEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
return Type.note(node.value)
|
||||
|
||||
|
||||
class BoolEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
return Type.bool(node.value)
|
||||
|
||||
|
||||
class TypeEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
return Type.type(node.value)
|
||||
|
||||
|
||||
class ListEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
list = abstractIterableEvaluator(expressionEvaluator(doAssert=True))(node, environment)
|
||||
return Type.list(list)
|
||||
|
||||
|
||||
class MapEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
map = {}
|
||||
keyEvaluator = Evaluator.oneOf(
|
||||
Evaluator.forNodes(lambda node, environment: EvaluationResult.OK(Type.string(node.value)), Identifier),
|
||||
expressionEvaluator(doAssert=True)
|
||||
)
|
||||
for entry in node.children:
|
||||
key = keyEvaluator(entry.key, environment).value
|
||||
if key in map:
|
||||
raise RuntimeException(f"Duplicated key '{key.stringify()}' found in map", entry.pos)
|
||||
map[key] = expressionEvaluator(doAssert=True)(entry.value, environment).value
|
||||
|
||||
return Type.map(map)
|
||||
|
||||
|
||||
class IdentifierEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
try:
|
||||
return environment.findVariable(node.value)
|
||||
except RuntimeException as e:
|
||||
raise updatePos(e, node)
|
||||
|
||||
|
||||
class AtomEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
return Evaluator.oneOf(
|
||||
Evaluator.forNodes(StringEvaluator.evaluate, StringLiteral),
|
||||
Evaluator.forNodes(IntegerEvaluator.evaluate, IntegerLiteral),
|
||||
Evaluator.forNodes(FloatEvaluator.evaluate, FloatLiteral),
|
||||
Evaluator.forNodes(NoteEvaluator.evaluate, NoteLiteral),
|
||||
Evaluator.forNodes(BoolEvaluator.evaluate, BoolLiteral),
|
||||
Evaluator.forNodes(TypeEvaluator.evaluate, TypeLiteral),
|
||||
Evaluator.forNodes(IdentifierEvaluator.evaluate, Identifier),
|
||||
Evaluator.forNodes(ListEvaluator.evaluate, List),
|
||||
Evaluator.forNodes(MapEvaluator.evaluate, Map)
|
||||
)(node, environment).value
|
||||
@@ -5,12 +5,12 @@ class BlockEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
environment.appendScope()
|
||||
environment.scopes.append({})
|
||||
|
||||
for child in node.children:
|
||||
evaluate(child, environment)
|
||||
|
||||
environment.popScope()
|
||||
environment.scopes.pop(-1)
|
||||
|
||||
#
|
||||
# def evaluateBlock(block, environment):
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator, evaluate
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class IfElseEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
condition = expressionEvaluator(doAssert=True)(node.condition, environment).value
|
||||
|
||||
if condition.type != Type.BOOL:
|
||||
raise RuntimeException(f"Only {Type.BOOL.name.lower()} types can be used as conditions in conditional expression", node.condition.pos)
|
||||
|
||||
if condition.value:
|
||||
return expressionEvaluator(doAssert=True)(node.ifNode, environment).value
|
||||
else:
|
||||
return expressionEvaluator(doAssert=True)(node.elseNode, environment).value
|
||||
|
||||
|
||||
class IfElseStatementEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
condition = expressionEvaluator(doAssert=True)(node.condition, environment).value
|
||||
|
||||
if condition.type != Type.BOOL:
|
||||
raise RuntimeException(
|
||||
f"Only {Type.BOOL.name.lower()} types can be used as conditions in conditional expression", node.condition.pos)
|
||||
|
||||
if condition.value:
|
||||
evaluate(node.ifNode, environment)
|
||||
else:
|
||||
evaluate(node.elseNode, environment)
|
||||
@@ -1,9 +1,15 @@
|
||||
from smnp.ast.node.condition import IfElse
|
||||
from smnp.ast.node.expression import Sum, Relation, And, Or, Loop
|
||||
from smnp.ast.node.factor import NotOperator, Power
|
||||
from smnp.ast.node.identifier import FunctionCall, Assignment
|
||||
from smnp.ast.node.term import Product
|
||||
from smnp.ast.node.unit import MinusOperator, Access
|
||||
from smnp.ast.node.assignment import AssignmentNode
|
||||
from smnp.ast.node.asterisk import AsteriskNode
|
||||
from smnp.ast.node.bool import BoolLiteralNode
|
||||
from smnp.ast.node.identifier import Identifier
|
||||
from smnp.ast.node.integer import IntegerLiteralNode
|
||||
from smnp.ast.node.invocation import FunctionCallNode
|
||||
from smnp.ast.node.list import List
|
||||
from smnp.ast.node.map import MapNode
|
||||
from smnp.ast.node.note import NoteLiteralNode
|
||||
from smnp.ast.node.operator import LeftAssociativeOperatorNode
|
||||
from smnp.ast.node.string import StringLiteralNode
|
||||
from smnp.ast.node.type import TypeNode
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.type.model import Type
|
||||
@@ -11,54 +17,38 @@ from smnp.type.model import Type
|
||||
|
||||
def expressionEvaluator(doAssert=False):
|
||||
def evaluateExpression(node, environment):
|
||||
from smnp.runtime.evaluators.function import FunctionCallEvaluator
|
||||
from smnp.runtime.evaluators.minus import MinusEvaluator
|
||||
from smnp.runtime.evaluators.atom import AtomEvaluator
|
||||
from smnp.runtime.evaluators.access import AccessEvaluator
|
||||
from smnp.runtime.evaluators.negation import NotEvaluator
|
||||
from smnp.runtime.evaluators.power import PowerEvaluator
|
||||
from smnp.runtime.evaluators.loop import LoopEvaluator
|
||||
from smnp.runtime.evaluators.assignment import AssignmentEvaluator
|
||||
from smnp.runtime.evaluators.product import ProductEvaluator
|
||||
from smnp.runtime.evaluators.string import StringEvaluator
|
||||
from smnp.runtime.evaluators.integer import IntegerEvaluator
|
||||
from smnp.runtime.evaluators.note import NoteEvaluator
|
||||
from smnp.runtime.evaluators.identifier import IdentifierEvaluator
|
||||
from smnp.runtime.evaluators.list import ListEvaluator
|
||||
from smnp.runtime.evaluators.function import FunctionCallEvaluator
|
||||
|
||||
from smnp.runtime.evaluators.sum import SumEvaluator
|
||||
from smnp.runtime.evaluators.relation import RelationEvaluator
|
||||
from smnp.runtime.evaluators.condition import IfElseEvaluator
|
||||
from smnp.runtime.evaluators.logic import AndEvaluator
|
||||
from smnp.runtime.evaluators.logic import OrEvaluator
|
||||
result = Evaluator.oneOf(
|
||||
Evaluator.forNodes(FunctionCallEvaluator.evaluate, FunctionCall),
|
||||
Evaluator.forNodes(MinusEvaluator.evaluate, MinusOperator),
|
||||
Evaluator.forNodes(AccessEvaluator.evaluate, Access),
|
||||
Evaluator.forNodes(NotEvaluator.evaluate, NotOperator),
|
||||
Evaluator.forNodes(PowerEvaluator.evaluate, Power),
|
||||
Evaluator.forNodes(LoopEvaluator.evaluate, Loop),
|
||||
Evaluator.forNodes(AssignmentEvaluator.evaluate, Assignment),
|
||||
Evaluator.forNodes(ProductEvaluator.evaluate, Product),
|
||||
Evaluator.forNodes(SumEvaluator.evaluate, Sum),
|
||||
Evaluator.forNodes(RelationEvaluator.evaluate, Relation),
|
||||
Evaluator.forNodes(IfElseEvaluator.evaluate, IfElse),
|
||||
Evaluator.forNodes(AndEvaluator.evaluate, And),
|
||||
Evaluator.forNodes(OrEvaluator.evaluate, Or),
|
||||
AtomEvaluator.evaluate
|
||||
)(node, environment)
|
||||
from smnp.runtime.evaluators.access import AccessEvaluator
|
||||
from smnp.runtime.evaluators.assignment import AssignmentEvaluator
|
||||
from smnp.runtime.evaluators.asterisk import AsteriskEvaluator
|
||||
from smnp.runtime.evaluators.map import MapEvaluator
|
||||
from smnp.runtime.evaluators.type import TypeEvaluator
|
||||
from smnp.runtime.evaluators.bool import BoolEvaluator
|
||||
result = Evaluator.oneOf(
|
||||
Evaluator.forNodes(FunctionCallEvaluator.evaluate, FunctionCallNode),
|
||||
Evaluator.forNodes(StringEvaluator.evaluate, StringLiteralNode),
|
||||
Evaluator.forNodes(IntegerEvaluator.evaluate, IntegerLiteralNode),
|
||||
Evaluator.forNodes(NoteEvaluator.evaluate, NoteLiteralNode),
|
||||
Evaluator.forNodes(BoolEvaluator.evaluate, BoolLiteralNode),
|
||||
Evaluator.forNodes(TypeEvaluator.evaluate, TypeNode),
|
||||
Evaluator.forNodes(IdentifierEvaluator.evaluate, Identifier),
|
||||
Evaluator.forNodes(ListEvaluator.evaluate, List),
|
||||
Evaluator.forNodes(AccessEvaluator.evaluate, LeftAssociativeOperatorNode),
|
||||
Evaluator.forNodes(AssignmentEvaluator.evaluate, AssignmentNode),
|
||||
Evaluator.forNodes(AsteriskEvaluator.evaluate, AsteriskNode),
|
||||
Evaluator.forNodes(MapEvaluator.evaluate, MapNode)
|
||||
)(node, environment)
|
||||
|
||||
if doAssert and result.result and result.value.type == Type.VOID:
|
||||
raise RuntimeException(f"Expected expression", node.pos)
|
||||
|
||||
return result
|
||||
if doAssert and result.result and result.value.type == Type.VOID:
|
||||
raise RuntimeException(f"Expected expression", node.pos)
|
||||
|
||||
return result
|
||||
|
||||
return evaluateExpression
|
||||
|
||||
|
||||
def expressionEvaluatorWithMatcher(matcher, exceptionProvider, doAssert=True):
|
||||
def evaluate(node, environment):
|
||||
value = expressionEvaluator(doAssert=doAssert)(node, environment).value
|
||||
|
||||
if not matcher.match(value):
|
||||
raise exceptionProvider(value)
|
||||
|
||||
return value
|
||||
|
||||
return evaluate
|
||||
@@ -17,11 +17,11 @@ class ExtendEvaluator(Evaluator):
|
||||
@classmethod
|
||||
def _typeToMethodSignature(cls, node):
|
||||
if type(node.specifiers) == NoneNode:
|
||||
return signature(ofType(node.type.value))
|
||||
return signature(ofType(node.type))
|
||||
|
||||
elif node.type.value == Type.LIST:
|
||||
elif node.type == Type.LIST:
|
||||
return signature(listSpecifier(node.specifiers[0]))
|
||||
elif node.type.value == Type.MAP:
|
||||
elif node.type == Type.MAP:
|
||||
return signature(mapSpecifier(node.specifiers[0], node.specifiers[1]))
|
||||
|
||||
@classmethod
|
||||
@@ -32,8 +32,8 @@ class ExtendEvaluator(Evaluator):
|
||||
@classmethod
|
||||
def _evaluateMethodDefinition(cls, node, environment, type, variable):
|
||||
name = node.name.value
|
||||
defaultArguments, signature = argumentsNodeToMethodSignature(node.arguments, environment)
|
||||
signature = argumentsNodeToMethodSignature(node.arguments)
|
||||
arguments = [arg.variable.value for arg in node.arguments]
|
||||
body = node.body
|
||||
environment.addCustomMethod(type, variable, name, signature, arguments, body, defaultArguments)
|
||||
environment.addCustomMethod(type, variable, name, signature, arguments, body)
|
||||
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class FloatEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
return Type.float(node.value)
|
||||
@@ -4,7 +4,6 @@ from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.runtime.evaluators.iterable import abstractIterableEvaluator
|
||||
from smnp.runtime.tools.error import updatePos
|
||||
from smnp.runtime.tools.signature import argumentsNodeToMethodSignature
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class FunctionCallEvaluator(Evaluator):
|
||||
@@ -25,10 +24,10 @@ class FunctionDefinitionEvaluator(Evaluator):
|
||||
def evaluator(cls, node, environment):
|
||||
try:
|
||||
name = node.name.value
|
||||
defaultArguments, signature = argumentsNodeToMethodSignature(node.arguments, environment)
|
||||
signature = argumentsNodeToMethodSignature(node.arguments)
|
||||
arguments = [ arg.variable.value for arg in node.arguments ]
|
||||
body = node.body
|
||||
environment.addCustomFunction(name, signature, arguments, body, defaultArguments)
|
||||
environment.addCustomFunction(name, signature, arguments, body)
|
||||
except RuntimeException as e:
|
||||
raise updatePos(e, node)
|
||||
|
||||
@@ -39,6 +38,8 @@ class BodyEvaluator(Evaluator):
|
||||
def evaluator(cls, node, environment):
|
||||
for child in node.children:
|
||||
evaluate(child, environment)
|
||||
if environment.callStack[-1].value is not None:
|
||||
return environment.callStack[-1].value
|
||||
|
||||
|
||||
class ReturnEvaluator(Evaluator):
|
||||
@@ -46,21 +47,7 @@ class ReturnEvaluator(Evaluator):
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
if len(environment.callStack) > 0:
|
||||
returnValue = expressionEvaluator()(node.value, environment).value
|
||||
raise Return(returnValue)
|
||||
# Disclaimer
|
||||
# Exception system usage to control program execution flow is really bad idea.
|
||||
# However because of lack of 'goto' instruction equivalent in Python
|
||||
# there is to need to use some mechanism to break function execution on 'return' statement
|
||||
# and immediately go to Environment's method 'invokeFunction()' or 'invokeMethod()',
|
||||
# which can handle value that came with exception and return it to code being executed.
|
||||
returnValue = expressionEvaluator(doAssert=True)(node.value, environment)
|
||||
environment.callStack[-1].value = returnValue.value
|
||||
else:
|
||||
raise RuntimeException("Cannot use 'return' statement outside a function or method", node.pos, environment)
|
||||
|
||||
|
||||
class Return(Exception):
|
||||
def __init__(self, value):
|
||||
if value is None:
|
||||
value = Type.void()
|
||||
|
||||
self.value = value
|
||||
@@ -1,3 +1,4 @@
|
||||
from smnp.ast.node.none import NoneNode
|
||||
from smnp.program.interpreter import Interpreter
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
|
||||
@@ -6,6 +7,13 @@ class ImportEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
if type(node.type) == NoneNode:
|
||||
cls._evaluateCodeImport(node, environment)
|
||||
else:
|
||||
raise RuntimeError("Importing types is not implemented yet")
|
||||
|
||||
@classmethod
|
||||
def _evaluateCodeImport(cls, node, environment):
|
||||
source = node.source
|
||||
newEnvironment = Interpreter.interpretFile(source.value, baseEnvironment=environment)
|
||||
newEnvironment = Interpreter.interpretFile(source.value)
|
||||
environment.extend(newEnvironment)
|
||||
@@ -1,21 +0,0 @@
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class AndEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
left = expressionEvaluator(doAssert=True)(node.left, environment).value
|
||||
right = expressionEvaluator(doAssert=True)(node.right, environment).value
|
||||
return Type.bool(left.value and right.value)
|
||||
|
||||
|
||||
class OrEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
left = expressionEvaluator(doAssert=True)(node.left, environment).value
|
||||
right = expressionEvaluator(doAssert=True)(node.right, environment).value
|
||||
return Type.bool(left.value or right.value)
|
||||
@@ -1,121 +0,0 @@
|
||||
from smnp.ast.node.none import NoneNode
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator, evaluate
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class LoopEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
iterator = expressionEvaluator(doAssert=True)(node.left, environment).value
|
||||
parameters = [ identifier.value for identifier in node.parameters ] if type(node.parameters) != NoneNode() else []
|
||||
|
||||
try:
|
||||
environment.appendScope()
|
||||
|
||||
output = {
|
||||
Type.INTEGER: cls.numberEvaluator,
|
||||
Type.BOOL: cls.boolEvaluator,
|
||||
Type.LIST: cls.listEvaluator,
|
||||
Type.MAP: cls.mapEvaluator
|
||||
}[iterator.type](node, environment, iterator, parameters, node.filter)
|
||||
|
||||
environment.popScope()
|
||||
except KeyError:
|
||||
raise RuntimeException(f"The {iterator.type.name.lower()} type cannot stand as an iterator for loop statement", node.left.pos)
|
||||
|
||||
return Type.list(output)
|
||||
|
||||
@classmethod
|
||||
def numberEvaluator(cls, node, environment, evaluatedIterator, parameters, filter):
|
||||
output = []
|
||||
|
||||
|
||||
if len(parameters) > 1:
|
||||
raise RuntimeException(f"Loop with numeric iterator can handle only one parameter", node.parameters.pos)
|
||||
|
||||
for i in range(evaluatedIterator.value):
|
||||
if len(parameters) > 0:
|
||||
environment.scopes[-1][parameters[0]] = Type.integer(i)
|
||||
|
||||
if cls.doFilter(filter, environment):
|
||||
output.append(evaluate(node.right, environment).value)
|
||||
|
||||
|
||||
|
||||
return output
|
||||
|
||||
@classmethod
|
||||
def doFilter(cls, filter, environment):
|
||||
if type(filter) is not NoneNode:
|
||||
evaluation = expressionEvaluator(doAssert=True)(filter, environment).value
|
||||
if evaluation.type != Type.BOOL:
|
||||
raise RuntimeException(f"Expected {Type.BOOL.name.lower()} as filter expression, found {evaluation.type.name.lower()}", filter.pos)
|
||||
|
||||
return evaluation.value
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
||||
@classmethod
|
||||
def boolEvaluator(cls, node, environment, evaluatedIterator, parameters, filter):
|
||||
output = []
|
||||
|
||||
if len(parameters) > 0:
|
||||
raise RuntimeException(f"Loop with logic iterator can't' handle any parameters", node.parameters.pos)
|
||||
|
||||
condition = evaluatedIterator
|
||||
while condition.value:
|
||||
if cls.doFilter(filter, environment):
|
||||
output.append(evaluate(node.right, environment).value)
|
||||
condition = expressionEvaluator(doAssert=True)(node.left, environment).value
|
||||
|
||||
return output
|
||||
|
||||
@classmethod
|
||||
def listEvaluator(cls, node, environment, evaluatedIterator, parameters, filter):
|
||||
output = []
|
||||
|
||||
if len(parameters) > 2:
|
||||
raise RuntimeException(f"Loop with list iterator can handle only two parameters", node.parameters.pos)
|
||||
|
||||
for i, value in enumerate(evaluatedIterator.value):
|
||||
if len(parameters) == 1:
|
||||
environment.scopes[-1][parameters[0]] = value
|
||||
if len(parameters) == 2:
|
||||
environment.scopes[-1][parameters[0]] = Type.integer(i)
|
||||
environment.scopes[-1][parameters[1]] = value
|
||||
|
||||
if cls.doFilter(filter, environment):
|
||||
output.append(evaluate(node.right, environment).value)
|
||||
|
||||
return output
|
||||
|
||||
|
||||
@classmethod
|
||||
def mapEvaluator(cls, node, environment, evaluatedIterator, parameters, filter):
|
||||
output = []
|
||||
|
||||
if len(parameters) > 3:
|
||||
raise RuntimeException(f"Loop with map iterator can handle only three parameters", node.parameters.pos)
|
||||
|
||||
i = 0
|
||||
for key, value in evaluatedIterator.value.items():
|
||||
if len(parameters) == 1:
|
||||
environment.scopes[-1][parameters[0]] = value
|
||||
if len(parameters) == 2:
|
||||
environment.scopes[-1][parameters[0]] = key
|
||||
environment.scopes[-1][parameters[1]] = value
|
||||
if len(parameters) == 3:
|
||||
environment.scopes[-1][parameters[0]] = Type.integer(i)
|
||||
environment.scopes[-1][parameters[1]] = key
|
||||
environment.scopes[-1][parameters[2]] = value
|
||||
i += 1
|
||||
|
||||
if cls.doFilter(filter, environment):
|
||||
output.append(evaluate(node.right, environment).value)
|
||||
|
||||
return output
|
||||
19
smnp/runtime/evaluators/map.py
Normal file
19
smnp/runtime/evaluators/map.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class MapEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
map = {}
|
||||
exprEvaluator = expressionEvaluator(doAssert=True)
|
||||
for entry in node.children:
|
||||
key = exprEvaluator(entry.key, environment).value
|
||||
if key in map:
|
||||
raise RuntimeException(f"Duplicated key '{key.stringify()}' found in map", entry.pos)
|
||||
map[key] = exprEvaluator(entry.value, environment).value
|
||||
|
||||
return Type.map(map)
|
||||
@@ -1,35 +0,0 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator, evaluate
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class MinusEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
value = evaluate(node.value, environment).value
|
||||
try:
|
||||
return {
|
||||
Type.INTEGER: cls.evaluateForInteger,
|
||||
Type.FLOAT: cls.evaluateForFloat,
|
||||
Type.STRING: cls.evaluateForString,
|
||||
Type.LIST: cls.evaluateForList
|
||||
}[value.type](value.value)
|
||||
except KeyError:
|
||||
raise RuntimeException(f"Type {value.type.name.lower()} does not support '{node.operator.value}' operator", node.pos)
|
||||
|
||||
@classmethod
|
||||
def evaluateForInteger(cls, value):
|
||||
return Type.integer(-value)
|
||||
|
||||
@classmethod
|
||||
def evaluateForFloat(cls, value):
|
||||
return Type.float(-value)
|
||||
|
||||
@classmethod
|
||||
def evaluateForString(cls, value):
|
||||
return Type.string(value[::-1])
|
||||
|
||||
@classmethod
|
||||
def evaluateForList(cls, value):
|
||||
return Type.list(value[::-1])
|
||||
@@ -1,16 +0,0 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class NotEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
value = expressionEvaluator(doAssert=True)(node.value, environment).value
|
||||
|
||||
if value.type != Type.BOOL:
|
||||
raise RuntimeException(f"Operator '{node.operator.value}' is supported only by {Type.BOOL.name.lower()} type", node.value.pos)
|
||||
|
||||
return Type.bool(not value.value)
|
||||
@@ -1,21 +0,0 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class PowerEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
left = expressionEvaluator(doAssert=True)(node.left, environment).value
|
||||
right = expressionEvaluator(doAssert=True)(node.right, environment).value
|
||||
supportedTypes = [Type.INTEGER, Type.FLOAT]
|
||||
|
||||
if not left.type in supportedTypes:
|
||||
raise RuntimeException(f"Operator '{node.operator.value}' is supported only by {Type.INTEGER.name.lower()} type", node.left.pos)
|
||||
|
||||
if not right.type in supportedTypes:
|
||||
raise RuntimeException(f"Operator '{node.operator.value}' is supported only by {[t.name.lower() for t in supportedTypes]} type", node.right.pos)
|
||||
|
||||
return Type.integer(int(left.value ** right.value))
|
||||
@@ -1,45 +0,0 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class ProductEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
left = expressionEvaluator(doAssert=True)(node.left, environment).value
|
||||
right = expressionEvaluator(doAssert=True)(node.right, environment).value
|
||||
supportedTypes = [Type.INTEGER, Type.FLOAT]
|
||||
|
||||
if not left.type in supportedTypes:
|
||||
raise RuntimeException(
|
||||
f"Operator '{node.operator.value}' is supported only by {[t.name.lower() for t in supportedTypes]} type", node.left.pos)
|
||||
|
||||
if not right.type in supportedTypes:
|
||||
raise RuntimeException(
|
||||
f"Operator '{node.operator.value}' is supported only by {[t.name.lower() for t in supportedTypes]} type", node.right.pos)
|
||||
|
||||
if node.operator.value == "*":
|
||||
return getProperTypeProvider(left.value * right.value)
|
||||
|
||||
if node.operator.value == "/":
|
||||
if right.value == 0:
|
||||
raise RuntimeException("Attempt to divide by 0", node.right.pos)
|
||||
|
||||
value = left.value / right.value
|
||||
|
||||
if left.type == right.type == Type.INTEGER and int(value) == value:
|
||||
return Type.integer(int(value))
|
||||
|
||||
return getProperTypeProvider(value)
|
||||
|
||||
raise RuntimeError("This line should never be reached")
|
||||
|
||||
|
||||
def getProperTypeProvider(value):
|
||||
return {
|
||||
int: lambda v: Type.integer(v),
|
||||
float: lambda v: Type.float(v)
|
||||
}[type(value)](value)
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class RelationEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
left = expressionEvaluator(doAssert=True)(node.left, environment).value
|
||||
right = expressionEvaluator(doAssert=True)(node.right, environment).value
|
||||
|
||||
if node.operator.value == "==":
|
||||
return cls.equalOperatorEvaluator(left, node.operator, right)
|
||||
|
||||
if node.operator.value == "!=":
|
||||
return cls.notEqualOperatorEvaluator(left, node.operator, right)
|
||||
|
||||
return cls.otherRelationOperatorsEvaluator(left, node.operator, right)
|
||||
|
||||
@classmethod
|
||||
def equalOperatorEvaluator(cls, left, operator, right):
|
||||
if left.type in [Type.INTEGER, Type.FLOAT] and right.type in [Type.INTEGER, Type.FLOAT]:
|
||||
return Type.bool(left.value == right.value)
|
||||
|
||||
return Type.bool(left.type == right.type and left.value == right.value)
|
||||
|
||||
@classmethod
|
||||
def notEqualOperatorEvaluator(cls, left, operator, right):
|
||||
if left.type in [Type.INTEGER, Type.FLOAT] and right.type in [Type.INTEGER, Type.FLOAT]:
|
||||
return Type.bool(left.value != right.value)
|
||||
|
||||
return Type.bool(left.type != right.type or left.value != right.value)
|
||||
|
||||
@classmethod
|
||||
def otherRelationOperatorsEvaluator(cls, left, operator, right):
|
||||
if left.type in [Type.INTEGER, Type.FLOAT] and right.type in [Type.INTEGER, Type.FLOAT]:
|
||||
if operator.value == ">":
|
||||
return Type.bool(left.value > right.value)
|
||||
|
||||
if operator.value == ">=":
|
||||
return Type.bool(left.value >= right.value)
|
||||
|
||||
if operator.value == "<":
|
||||
return Type.bool(left.value < right.value)
|
||||
|
||||
if operator.value == "<=":
|
||||
return Type.bool(left.value <= right.value)
|
||||
|
||||
raise RuntimeException(f"Operator {operator.value} is not supported by {left.type.name.lower()} and {right.type.name.lower()} types", operator.pos)
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class SumEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
left = expressionEvaluator(doAssert=True)(node.left, environment).value
|
||||
right = expressionEvaluator(doAssert=True)(node.right, environment).value
|
||||
|
||||
if left.type in [Type.INTEGER, Type.FLOAT] and right.type in [Type.INTEGER, Type.FLOAT]:
|
||||
return cls.numberEvaluator(left, node.operator, right)
|
||||
|
||||
if left.type == right.type == Type.STRING:
|
||||
return cls.stringEvaluator(left, node.operator, right)
|
||||
|
||||
if left.type == right.type == Type.LIST:
|
||||
return cls.listEvaluator(left, node.operator, right)
|
||||
|
||||
if left.type == right.type == Type.MAP:
|
||||
return cls.mapEvaluator(left, node.operator, right)
|
||||
|
||||
raise RuntimeException(
|
||||
f"Operator {node.operator.value} is not supported by {left.type.name.lower()} and {right.type.name.lower()} types",
|
||||
node.operator.pos)
|
||||
|
||||
@classmethod
|
||||
def numberEvaluator(cls, left, operator, right):
|
||||
if operator.value == "+":
|
||||
return getProperTypeProvider(left.value + right.value)
|
||||
|
||||
if operator.value == "-":
|
||||
return getProperTypeProvider(left.value - right.value)
|
||||
|
||||
raise RuntimeError("This line should never be reached")
|
||||
|
||||
@classmethod
|
||||
def stringEvaluator(cls, left, operator, right):
|
||||
if operator.value == "+":
|
||||
return Type.string(left.value + right.value)
|
||||
|
||||
if operator.value == "-":
|
||||
raise RuntimeException(f"Operator {operator.value} is not supported by string types", operator.pos)
|
||||
|
||||
raise RuntimeError("This line should never be reached")
|
||||
|
||||
@classmethod
|
||||
def listEvaluator(cls, left, operator, right):
|
||||
if operator.value == "+":
|
||||
return Type.list(left.value + right.value)
|
||||
|
||||
if operator.value == "-":
|
||||
raise RuntimeException(f"Operator {operator.value} is not supported by list types", operator.pos)
|
||||
|
||||
raise RuntimeError("This line should never be reached")
|
||||
|
||||
@classmethod
|
||||
def mapEvaluator(cls, left, operator, right):
|
||||
if operator.value == "+":
|
||||
return Type.map({**left.value, **right.value})
|
||||
|
||||
if operator.value == "-":
|
||||
raise RuntimeException(f"Operator {operator.value} is not supported by map types", operator.pos)
|
||||
|
||||
raise RuntimeError("This line should never be reached")
|
||||
|
||||
|
||||
def getProperTypeProvider(value):
|
||||
return {
|
||||
int: lambda v: Type.integer(v),
|
||||
float: lambda v: Type.float(v)
|
||||
}[type(value)](value)
|
||||
@@ -1,17 +0,0 @@
|
||||
from smnp.error.custom import CustomException
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
class ThrowEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
string = expressionEvaluator(doAssert=True)(node.value, environment).value
|
||||
|
||||
if string.type != Type.STRING:
|
||||
raise RuntimeException(f"Only {Type.STRING.name.lower()} types can be thrown", node.value.pos)
|
||||
|
||||
raise CustomException(string.value, node.pos)
|
||||
@@ -1,34 +0,0 @@
|
||||
from smnp.ast.node.identifier import Identifier, FunctionCall
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.runtime.evaluator import Evaluator
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator
|
||||
from smnp.runtime.evaluators.iterable import abstractIterableEvaluator
|
||||
from smnp.runtime.tools.error import updatePos
|
||||
|
||||
|
||||
class UnitEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
return
|
||||
|
||||
|
||||
class AccessEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def evaluator(cls, node, environment):
|
||||
left = expressionEvaluator(doAssert=True)(node.left, environment).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
||||
right = node.right
|
||||
|
||||
if type(right) == Identifier:
|
||||
try:
|
||||
return left.properties[right.value]
|
||||
except KeyError:
|
||||
raise RuntimeException(f"Unknown property '{right.value}' of type '{left.type.name.lower()}'", right.pos)
|
||||
|
||||
if type(right) == FunctionCall:
|
||||
try:
|
||||
arguments = abstractIterableEvaluator(expressionEvaluator(doAssert=True))(right.arguments, environment)
|
||||
return environment.invokeMethod(left, right.name.value, arguments)
|
||||
except RuntimeException as e:
|
||||
raise updatePos(e, right)
|
||||
@@ -1,9 +1,7 @@
|
||||
from smnp.ast.node import type as ast
|
||||
from smnp.ast.node.none import NoneNode
|
||||
from smnp.ast.node.type import TypesList
|
||||
from smnp.ast.node.type import TypeNode, TypeSpecifier
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.function.signature import varargSignature, signature, optional
|
||||
from smnp.runtime.evaluators.expression import expressionEvaluator, expressionEvaluatorWithMatcher
|
||||
from smnp.function.signature import varargSignature, signature
|
||||
from smnp.runtime.tools.error import updatePos
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.list import listOfMatchers
|
||||
@@ -11,54 +9,31 @@ from smnp.type.signature.matcher.map import mapOfMatchers
|
||||
from smnp.type.signature.matcher.type import allTypes, oneOf, ofType
|
||||
|
||||
|
||||
def evaluateDefaultArguments(node, environment):
|
||||
defaultValues = { arg.variable.value: expressionEvaluator(doAssert=True)(arg.optionalValue, environment).value for arg in node.children if type(arg.optionalValue) != NoneNode }
|
||||
|
||||
return defaultValues
|
||||
|
||||
|
||||
def argumentsNodeToMethodSignature(node, environment):
|
||||
def argumentsNodeToMethodSignature(node):
|
||||
try:
|
||||
sign = []
|
||||
vararg = None
|
||||
argumentsCount = len(node.children)
|
||||
checkPositionOfOptionalArguments(node)
|
||||
defaultArgs = {}
|
||||
for i, child in enumerate(node.children):
|
||||
matchers = {
|
||||
ast.Type: (lambda c: c.type, typeMatcher),
|
||||
TypeNode: (lambda c: c.type, typeMatcher),
|
||||
NoneNode: (lambda c: c.type, lambda c: allTypes()),
|
||||
TypesList: (lambda c: c, multipleTypeMatcher)
|
||||
TypeSpecifier: (lambda c: c, multipleTypeMatcher)
|
||||
}
|
||||
evaluatedMatcher = matchers[type(child.type)][1](matchers[type(child.type)][0](child))
|
||||
|
||||
if child.vararg:
|
||||
if i != argumentsCount - 1:
|
||||
raise RuntimeException("Vararg must be the last argument in signature", child.pos)
|
||||
vararg = evaluatedMatcher
|
||||
else:
|
||||
if type(child.optionalValue) != NoneNode:
|
||||
defaultArgs[child.variable.value] = expressionEvaluatorWithMatcher(
|
||||
evaluatedMatcher,
|
||||
exceptionProvider=lambda value: RuntimeException(
|
||||
f"Value '{value.stringify()}' doesn't match declared type: {evaluatedMatcher.string}", child.optionalValue.pos)
|
||||
)(child.optionalValue, environment)
|
||||
evaluatedMatcher = optional(evaluatedMatcher)
|
||||
sign.append(evaluatedMatcher)
|
||||
|
||||
return defaultArgs, (varargSignature(vararg, *sign, wrapVarargInValue=True) if vararg is not None else signature(*sign))
|
||||
|
||||
return varargSignature(vararg, *sign, wrapVarargInValue=True) if vararg is not None else signature(*sign)
|
||||
except RuntimeException as e:
|
||||
raise updatePos(e, node)
|
||||
|
||||
|
||||
def checkPositionOfOptionalArguments(node):
|
||||
firstOptional = next((i for i, v in enumerate(node.children) if type(v.optionalValue) != NoneNode), None) #next(filter(lambda arg: type(arg.optionalValue) != NoneNode, node.children), None)
|
||||
if firstOptional is not None:
|
||||
regularAfterOptional = next((i for i, v in enumerate(node.children[firstOptional:]) if type(v.optionalValue) == NoneNode), None)
|
||||
if regularAfterOptional is not None:
|
||||
raise RuntimeException(f"Optional arguments should be declared at the end of the arguments list", node.children[regularAfterOptional].pos)
|
||||
|
||||
|
||||
def multipleTypeMatcher(typeNode):
|
||||
subSignature = []
|
||||
|
||||
@@ -74,10 +49,10 @@ def multipleTypeMatcher(typeNode):
|
||||
|
||||
def typeMatcher(typeNode):
|
||||
if type(typeNode.specifiers) == NoneNode:
|
||||
return ofType(typeNode.type.value)
|
||||
elif typeNode.type.value == Type.LIST and len(typeNode.specifiers) == 1:
|
||||
return ofType(typeNode.type)
|
||||
elif typeNode.type == Type.LIST and len(typeNode.specifiers) == 1:
|
||||
return listSpecifier(typeNode.specifiers[0])
|
||||
elif typeNode.type.value == Type.MAP and len(typeNode.specifiers) == 2:
|
||||
elif typeNode.type == Type.MAP and len(typeNode.specifiers) == 2:
|
||||
return mapSpecifier(typeNode.specifiers[0], typeNode.specifiers[1])
|
||||
|
||||
raise RuntimeException("Unknown type", typeNode.pos) # Todo: Improve pointing position
|
||||
|
||||
@@ -2,14 +2,13 @@ from smnp.error.syntax import SyntaxException
|
||||
from smnp.token.model import TokenList
|
||||
from smnp.token.tokenizers.bool import boolTokenizer
|
||||
from smnp.token.tokenizers.comment import commentTokenizer
|
||||
from smnp.token.tokenizers.float import floatTokenizer
|
||||
from smnp.token.tokenizers.identifier import identifierTokenizer
|
||||
from smnp.token.tokenizers.keyword import typeTokenizer
|
||||
from smnp.token.tokenizers.note import noteTokenizer
|
||||
from smnp.token.tokenizers.relation import relationOperatorTokenizer
|
||||
from smnp.token.tokenizers.string import stringTokenizer
|
||||
from smnp.token.tokenizers.whitespace import whitespacesTokenizer
|
||||
from smnp.token.tools import defaultTokenizer, separated, regexPatternTokenizer, mapValue
|
||||
from smnp.token.tools import defaultTokenizer, separated, regexPatternTokenizer
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
tokenizers = (
|
||||
@@ -28,9 +27,7 @@ tokenizers = (
|
||||
defaultTokenizer(TokenType.CLOSE_SQUARE),
|
||||
defaultTokenizer(TokenType.OPEN_ANGLE),
|
||||
defaultTokenizer(TokenType.CLOSE_ANGLE),
|
||||
defaultTokenizer(TokenType.SEMICOLON),
|
||||
defaultTokenizer(TokenType.ASTERISK),
|
||||
defaultTokenizer(TokenType.PERCENT),
|
||||
defaultTokenizer(TokenType.ASSIGN),
|
||||
defaultTokenizer(TokenType.COMMA),
|
||||
defaultTokenizer(TokenType.SLASH),
|
||||
@@ -42,8 +39,7 @@ tokenizers = (
|
||||
defaultTokenizer(TokenType.DOT),
|
||||
|
||||
# Types
|
||||
separated(floatTokenizer),
|
||||
mapValue(separated(regexPatternTokenizer(TokenType.INTEGER, r'\d')), int),
|
||||
separated(regexPatternTokenizer(TokenType.INTEGER, r'\d')),
|
||||
stringTokenizer,
|
||||
noteTokenizer,
|
||||
boolTokenizer,
|
||||
@@ -54,9 +50,7 @@ tokenizers = (
|
||||
separated(defaultTokenizer(TokenType.RETURN)),
|
||||
separated(defaultTokenizer(TokenType.EXTEND)),
|
||||
separated(defaultTokenizer(TokenType.IMPORT)),
|
||||
separated(defaultTokenizer(TokenType.THROW)),
|
||||
separated(defaultTokenizer(TokenType.FROM)),
|
||||
separated(defaultTokenizer(TokenType.WITH)),
|
||||
separated(defaultTokenizer(TokenType.ELSE)),
|
||||
separated(defaultTokenizer(TokenType.AND)),
|
||||
separated(defaultTokenizer(TokenType.NOT)),
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
from smnp.token.model import Token
|
||||
from smnp.token.tools import regexPatternTokenizer, keywordTokenizer, allOf
|
||||
from smnp.token.type import TokenType
|
||||
|
||||
|
||||
def createToken(pos, beforeDot, dot, afterDot):
|
||||
rawValue = f"{beforeDot.value}.{afterDot.value}"
|
||||
value = float(rawValue)
|
||||
return Token(TokenType.FLOAT, value, pos, rawValue)
|
||||
|
||||
|
||||
floatTokenizer = allOf(
|
||||
regexPatternTokenizer(TokenType.INTEGER, r'\d'),
|
||||
keywordTokenizer(None, "."),
|
||||
regexPatternTokenizer(TokenType.INTEGER, r'\d'),
|
||||
createToken=createToken
|
||||
)
|
||||
@@ -3,7 +3,7 @@ from smnp.token.type import TokenType
|
||||
from smnp.type.model import Type
|
||||
|
||||
|
||||
typeTokenizer = separated(keywordsTokenizer(TokenType.TYPE, *[type.name.lower() for type in Type], mapKeyword=lambda value: Type[value.upper()]))
|
||||
typeTokenizer = separated(keywordsTokenizer(TokenType.TYPE, *[type.name.lower() for type in Type]))
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ def noteTokenizer(input, current, line):
|
||||
rawValue = ''
|
||||
if input[current] == '@':
|
||||
rawValue += input[current+consumedChars]
|
||||
consumedChars += 1 # TODO: Check if next item does even exist
|
||||
consumedChars += 1
|
||||
if input[current+consumedChars] in ('C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'A', 'a', 'H', 'h', 'B', 'b'):
|
||||
rawValue += input[current + consumedChars]
|
||||
notePitch = input[current+consumedChars]
|
||||
|
||||
@@ -3,4 +3,4 @@ from smnp.token.type import TokenType
|
||||
|
||||
|
||||
def relationOperatorTokenizer(input, current, line):
|
||||
return keywordsTokenizer(TokenType.RELATION, "==", "!=", ">=", "<=")(input, current, line)
|
||||
return keywordsTokenizer(TokenType.RELATION, "==", "!=", ">=", "<=", ">", "<")(input, current, line)
|
||||
@@ -13,5 +13,5 @@ def stringTokenizer(input, current, line):
|
||||
char = input[current + consumedChars]
|
||||
value += char
|
||||
consumedChars += 1
|
||||
return (consumedChars, Token(TokenType.STRING, value[1:len(value)-1], (line, current), value))
|
||||
return (consumedChars, Token(TokenType.STRING, value[1:len(value)-1], (line, current)))
|
||||
return (0, None)
|
||||
|
||||
@@ -16,10 +16,10 @@ def regexPatternTokenizer(type, pattern):
|
||||
return tokenizer
|
||||
|
||||
|
||||
def keywordsTokenizer(type, *keywords, mapKeyword=lambda x: x):
|
||||
def keywordsTokenizer(type, *keywords):
|
||||
def tokenizer(input, current, line):
|
||||
for keyword in keywords:
|
||||
result = keywordTokenizer(type, keyword, mapKeyword)(input, current, line)
|
||||
result = keywordTokenizer(type, keyword)(input, current, line)
|
||||
if result[0] > 0:
|
||||
return result
|
||||
return (0, None)
|
||||
@@ -27,10 +27,10 @@ def keywordsTokenizer(type, *keywords, mapKeyword=lambda x: x):
|
||||
return tokenizer
|
||||
|
||||
|
||||
def keywordTokenizer(type, keyword, mapKeyword=lambda x: x):
|
||||
def keywordTokenizer(type, keyword):
|
||||
def tokenizer(input, current, line):
|
||||
if len(input) >= current+len(keyword) and input[current:current+len(keyword)] == keyword:
|
||||
return (len(keyword), Token(type, mapKeyword(keyword), (line, current)))
|
||||
return (len(keyword), Token(type, keyword, (line, current)))
|
||||
return (0, None)
|
||||
return tokenizer
|
||||
|
||||
@@ -50,30 +50,3 @@ def separated(tokenizer, end=r"\W"):
|
||||
return (0, None)
|
||||
|
||||
return separated
|
||||
|
||||
|
||||
def mapValue(tokenizer, mapper):
|
||||
def tokenize(input, current, line):
|
||||
consumedChars, token = tokenizer(input, current, line)
|
||||
if consumedChars > 0:
|
||||
return (consumedChars, Token(token.type, mapper(token.value), token.pos))
|
||||
|
||||
return (0, None)
|
||||
|
||||
return tokenize
|
||||
|
||||
def allOf(*tokenizers, createToken):
|
||||
def combinedTokenizer(input, current, line):
|
||||
consumedChars = 0
|
||||
tokens = []
|
||||
for tokenizer in tokenizers:
|
||||
consumed, token = tokenizer(input, current+consumedChars, line)
|
||||
if consumed > 0:
|
||||
consumedChars += consumed
|
||||
tokens.append(token)
|
||||
else:
|
||||
return (0, None)
|
||||
|
||||
return (consumedChars, createToken((current, line), *tokens))
|
||||
|
||||
return combinedTokenizer
|
||||
@@ -2,7 +2,7 @@ from enum import Enum
|
||||
|
||||
|
||||
class TokenType(Enum):
|
||||
RELATION = '==, !=, >=, <='
|
||||
RELATION = 'relation'
|
||||
DOUBLE_ASTERISK = '**'
|
||||
OPEN_CURLY = '{'
|
||||
CLOSE_CURLY = '}'
|
||||
@@ -12,9 +12,7 @@ class TokenType(Enum):
|
||||
CLOSE_SQUARE = ']'
|
||||
OPEN_ANGLE = '<'
|
||||
CLOSE_ANGLE = '>'
|
||||
SEMICOLON = ';'
|
||||
ASTERISK = '*'
|
||||
PERCENT = '%'
|
||||
ASSIGN = '='
|
||||
ARROW = '->'
|
||||
COMMA = ','
|
||||
@@ -30,7 +28,6 @@ class TokenType(Enum):
|
||||
NOT = 'not'
|
||||
INTEGER = 'integer'
|
||||
STRING = 'string'
|
||||
FLOAT = 'float'
|
||||
NOTE = 'note'
|
||||
BOOL = 'bool'
|
||||
TYPE = 'type'
|
||||
@@ -38,14 +35,13 @@ class TokenType(Enum):
|
||||
RETURN = 'return'
|
||||
EXTEND = 'extend'
|
||||
IMPORT = 'import'
|
||||
THROW = 'throw'
|
||||
FROM = 'from'
|
||||
WITH = 'with'
|
||||
ELSE = 'else'
|
||||
IF = 'if'
|
||||
AS = 'as'
|
||||
IDENTIFIER = 'identifier'
|
||||
COMMENT = 'comment'
|
||||
PERCENT = 'percent'
|
||||
|
||||
@property
|
||||
def key(self):
|
||||
|
||||
@@ -8,10 +8,10 @@ from smnp.type.value import Value
|
||||
|
||||
class Type(Enum):
|
||||
INTEGER = (int, lambda x: str(x))
|
||||
FLOAT = (float, lambda x: str(x))
|
||||
STRING = (str, lambda x: x)
|
||||
LIST = (list, lambda x: f"[{', '.join([e.stringify() for e in x])}]")
|
||||
MAP = (dict, lambda x: '{' + ', '.join(f"'{k.stringify()}' -> '{v.stringify()}'" for k, v in x.items()) + '}')
|
||||
PERCENT = (float, lambda x: f"{int(x * 100)}%")
|
||||
NOTE = (Note, lambda x: x.note.name)
|
||||
BOOL = (bool, lambda x: str(x).lower())
|
||||
SOUND = (Sound, lambda x: x.file)
|
||||
@@ -25,10 +25,6 @@ class Type(Enum):
|
||||
def integer(value):
|
||||
return Value(Type.INTEGER, value, {})
|
||||
|
||||
@staticmethod
|
||||
def float(value):
|
||||
return Value(Type.FLOAT, value, {})
|
||||
|
||||
@staticmethod
|
||||
def string(value):
|
||||
return Value(Type.STRING, value, {
|
||||
@@ -55,7 +51,7 @@ class Type(Enum):
|
||||
"pitch": Type.string(str(value.note)),
|
||||
"octave": Type.integer(value.octave),
|
||||
"duration": Type.integer(value.duration),
|
||||
"dot": Type.bool(value.dot)
|
||||
"dot": Type.string('.' if value.dot else '')
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
@@ -77,7 +73,6 @@ class Type(Enum):
|
||||
def void():
|
||||
return Value(Type.VOID, None)
|
||||
|
||||
|
||||
def _failStringify(t):
|
||||
raise RuntimeException(f"Not able to interpret {t.name}'", None)
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user