def to_source(self, printer: CodePrinter): with printer.block('@[', ']'): for i in range(len(self.values)): if i > 0: printer.add_token('; ') self.values[i].to_source(printer)
def to_source(self, printer: CodePrinter): self.left.to_source(printer) printer.add_token( f" { self.operator if self.operator is not None else '' }= ") self.right.to_source(printer)
def to_source(self, printer: CodePrinter): for i in range(len(self.statements)): if i > 0: printer.add_token(';') printer.begin_line() self.statements[i].to_source(printer)
def to_source(self, printer: CodePrinter): from .string_literal_node import StringLiteralNode self.expression.to_source(printer) if self.as_attr and isinstance(self.name, StringLiteralNode): printer.add_token('::' + self.name.value) else: with printer.block('::[', ']'): self.name.to_source(printer)
def to_source(self, printer: CodePrinter): printer.add_token('for ') with printer.block('(', ')'): printer.add_token( ", ".join(["$" + name for name in self.variables]) + " in ") self.it.to_source(printer) self.body.to_source(printer)
def to_source ( self, printer : CodePrinter ): from .variable_expression_node import VariableExpressionNode if isinstance( self.expression, VariableExpressionNode ): printer.add_token( self.expression.name ) else: self.expression.to_source( printer ) with printer.block( '(', ')' ): for i in range( len( self.parameters ) ): if i > 0: printer.add_token( '; ' ) self.parameters[ i ].to_source( printer ) not_first = bool( self.parameters ) for key, node in self.named_parameters.items(): if not_first: printer.add_token( '; ' ) printer.add_token( key + ' = ' ) node.to_source( printer ) not_first = True
def to_source(self, printer: CodePrinter): with printer.block('@{', '}'): for i in range(len(self.values)): if i > 0: printer.add_token('; ') key, node = self.values[i] printer.add_token(str(key) + ' = ') node.to_source(printer)
def to_string( self, events_per_voice: List[Tuple[Voice, int, List[MusicEvent]]] ) -> Union[str, ast.Node]: if self.ast: return self.to_ast(events_per_voice) else: ast = self.to_ast(events_per_voice) printer = CodePrinter() return printer.print(ast)
def to_source(self, printer: CodePrinter): for i, op in enumerate(self.to_source): if i > 0: printer.add_token(", ") op.to_source(printer) printer.add_token( f" { self.operator if self.operator is not None else '' }= ") self.right.to_source(printer)
def to_source(self, printer: CodePrinter): if self.is_expression: printer.add_token('@py') with printer.block(): printer.add_token(self.code) else: printer.add_token('@python ') printer.add_token(self.code)
def save_all(context: Context, file: str, buffers): from musikla.parser.printer import CodePrinter from musikla.libraries.music import function_to_mkl import musikla.parser.abstract_syntax_tree as ast import musikla.parser.abstract_syntax_tree.statements as ast_st import musikla.parser.abstract_syntax_tree.expressions as ast_ex body = ast_st.StatementsListNode([]) body.statements.append( ast_st.VariableDeclarationStatementNode( 'buffers', ast_ex.ObjectLiteralNode([]))) for key, buffer in buffers.items(): set_fn = ast_ex.PropertyAccessorNode( ast_ex.VariableExpressionNode('buffers'), ast_ex.StringLiteralNode('set'), True) key_p = ast_ex.NumberLiteralNode(key) music_p = function_to_mkl( context, buffer.to_music(), ast = True ) \ if buffer else \ ast_ex.NoneLiteralNode() body.statements.append( ast_ex.FunctionExpressionNode(set_fn, [key_p, music_p])) file_str = CodePrinter().print(body) with open(file, 'w+') as f: f.write(file_str)
def to_source ( self, printer : CodePrinter ): printer.add_token( 'import ' ) if self.local: printer.add_token( f'"{self.module_name}"' ) else: printer.add_token( self.module_name )
def __eval__ ( self, context ): value = self.expression.eval( context ) if value == None: if isinstance( self.expression, VariableExpressionNode ): raise BaseException( f"Calling undefined function { cast( VariableExpressionNode, self.expression ).name }" ) elif isinstance( self.expression, PropertyAccessorNode ): method_name = cast( PropertyAccessorNode, self.expression ).name.eval( context ) object_name = CodePrinter().print( cast( PropertyAccessorNode, self.expression ).expression ) raise BaseException( f"Calling undefined function '{ method_name }' in '{ object_name }'" ) else: raise BaseException( "Calling undefined function" ) return CallablePythonValue.call( value, context, self.parameters, self.named_parameters )
def to_source(self, printer: CodePrinter): from ..expressions.group_node import GroupNode from ..expressions.block_node import BlockNode printer.add_token('if ') self.condition.to_source(printer) if not isinstance(self.condition, GroupNode) and not isinstance( self.body, BlockNode): printer.add_token(" then ") else: printer.add_token(" ") self.body.to_source(printer) if self.else_body is not None: printer.add_token(" else ") self.else_body.to_source(printer)
def to_source(self, printer: CodePrinter): for i in range(len(self.expressions)): if i > 0: printer.add_token(' ') self.expressions[i].to_source(printer)
def function_ast_to_code(ast: Node, ident=4) -> str: printer = CodePrinter(ident=ident) return printer.print(ast)
def to_source(self, printer: CodePrinter): with printer.block('(', ')'): if self.expression is not None: self.expression.to_source(printer)
def to_source ( self, printer : CodePrinter ): printer.add_token( 'T' + str( self.tempo ) )
def to_source(self, printer: CodePrinter): printer.add_token('fun ') if self.name is not None: printer.add_token(self.name + ' ') printer.begin_block('(', ')') for i in range(len(self.arguments)): if i > 0: printer.add_token(', ') name, mod, expr = self.arguments[i] if mod is not None: printer.add_token(mod + ' ') printer.add_token('$' + name) if expr is not None: printer.add_token(' = ') expr.to_source(printer) printer.end_block() printer.begin_block() self.body.to_source(printer) printer.end_block()
def to_source(self, printer: CodePrinter): self.left.to_source(printer) printer.add_token(' ' + self.operator + ' ') self.right.to_source(printer)
def to_source(self, printer: CodePrinter): printer.add_token('L' + str(self.length))
def to_source(self, printer: CodePrinter): with printer.block(): self.body.to_source(printer)
def to_source ( self, printer : CodePrinter ): printer.add_token( "return " ) if self.expression is not None: self.expression.to_source( printer )
def to_source(self, printer: CodePrinter): printer.add_token(self.note.to_string(base_octave=0))
def to_source(self, printer: CodePrinter): printer.add_token(str(self.value))
def to_source ( self, printer : CodePrinter ): printer.add_token( "none" )
def to_source(self, printer: CodePrinter): printer.add_token('$' + self.name)
def to_source(self, printer: CodePrinter): printer.add_token('true' if self.value else 'false')
def to_source(self, printer: CodePrinter): self.left.to_source(printer) printer.add_token(' / ') self.right.to_source(printer)
def to_source(self, printer: CodePrinter): printer.add_token("not ") self.node.to_source(printer)