public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "ArraySliceFull" ) { this.source = c.convertNode( node.ChildNodes[0] ); this.begin = c.convertNode( node.ChildNodes[2] ); this.end = c.convertNode( node.ChildNodes[3] ); return true; } else if( node.Term.Name == "ArraySliceFromStart" ) { this.source = c.convertNode( node.ChildNodes[0] ); this.end = c.convertNode( node.ChildNodes[2] ); return true; } else if( node.Term.Name == "ArraySliceFromEnd" ) { this.source = c.convertNode( node.ChildNodes[0] ); this.begin = c.convertNode( node.ChildNodes[2] ); return true; } return false; }
public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "DictExpr" ) { var elems = node.ChildNodes[1]; if( elems.Term.Name != "DictElements" ) throw new CompilationException( "Expected DictElements", node ); foreach( var child in elems.ChildNodes ) { if( child.Term.Name != "DictElement" ) throw new CompilationException( "Expected DictElement", child ); Pair p = new Pair() { key = c.convertNode( child.ChildNodes[0] ), value = c.convertNode( child.ChildNodes[1] ) }; this.pairs.Add( p ); } return true; } return false; }
public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "ReturnStmt" ) { this.value = c.convertNode( node.ChildNodes[1] ); return true; } return false; }
public override bool convert( ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "AssignmentStmt" ) { this.lhs = c.convertNode( node.ChildNodes[0] ); this.rhs = c.convertNode( node.ChildNodes[2] ); return true; } return false; }
public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "ArrayAccess" ) { this.source = c.convertNode( node.ChildNodes[0] ); this.index = c.convertNode( node.ChildNodes[2] ); return true; } return false; }
public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "FunctionCall" ) { this.source = c.convertNode( node.ChildNodes[0] ); this.parameters = node.ChildNodes[1].ChildNodes.Select( n => c.convertNode( n ) ).ToArray(); return true; } return false; }
public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "IfStmt" ) { var cl = new List<IfClause>(); // Add in the main if clause first. cl.Add( new IfClause( condition: c.convertNode( node.ChildNodes[1] ), block: (AstStatements)c.convertNode( node.ChildNodes[2] ) ) ); // Do we have Elif or Else clauses? for( int i=3; i<node.ChildNodes.Count; ++i ) { if( node.ChildNodes[i].Term.Name == "ElifClauses" ) { foreach( var n in node.ChildNodes[i].ChildNodes ) { // Each of these should be an ElifClause. if( n.Term.Name != "ElifClause" ) throw new CompilationException( "ElifClause expected", n ); cl.Add( new IfClause( condition: c.convertNode( n.ChildNodes[1] ), block: (AstStatements)c.convertNode( n.ChildNodes[2] ) ) ); } } else if( node.ChildNodes[i].Term.Name == "ElseClause" ) { cl.Add( new IfClause( condition: null, block: (AstStatements)c.convertNode( node.ChildNodes[i].ChildNodes[1] ) ) ); } else { throw new CompilationException( "ElifClauses or ElseClause expected", node.ChildNodes[i] ); } } this.clauses = cl.ToArray(); return true; } return false; }
public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "MemberAccess" ) { if( node.ChildNodes[0].Token != null && node.ChildNodes[0].Token.Text == "$" ) this.rvalue = new AstIdentifier( "$" ); else this.rvalue = c.convertNode( node.ChildNodes[0] ); this.member = node.ChildNodes[2].Token.Text; return true; } return false; }
public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name != "StmtList" ) return false; foreach( var child in node.ChildNodes ) { AstNode converted = c.convertNode( child ); converted.statement = true; _children.Add( converted ); } return true; }
public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "FunctionDef" ) { this.name = node.ChildNodes[1].Token.Text; this.parameters = node.ChildNodes[2].ChildNodes.Select( n => n.Token.Text ).ToArray(); this.block = c.convertNode( node.ChildNodes[3] ); this.lambda = false; return true; } // TODO: Lambdas return false; }
/// <summary> /// Compile a code fragment into a CodeFragment, containing a compiled AST. /// </summary> public static CodeFragment Compile( string unitName, string s ) { if( s_parser == null ) s_parser = new Parser( new CoralGrammar() ); ParseTree tree = s_parser.Parse( s ); if( tree.HasErrors() ) return new CodeFragment( tree, unitName ); Compiler c = new Compiler( unitName ); try { AstNode node = c.convertNode( tree.Root ); return new CodeFragment( node ); } catch( CompilationException ex ) { return new CodeFragment( ex ); } }
public override bool convert( ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "BinExpr" ) { this.left = c.convertNode( node.ChildNodes[0] ); this.right = c.convertNode( node.ChildNodes[2] ); this.op = node.ChildNodes[1].Term.Name; return true; } if( node.Term.Name == "UnExpr" ) { this.op = node.ChildNodes[0].Term.Name; this.right = c.convertNode( node.ChildNodes[1] ); return true; } if( node.Term.Name == "NotExpr" ) { this.op = "!"; this.right = c.convertNode( node.ChildNodes[1] ); return true; } return false; }
public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "WhileStmt" ) { // Get our loop variable. this.test = c.convertNode( node.ChildNodes[1] ); // Convert the inner block. this.block = c.convertNode( node.ChildNodes[2] ); return true; } // This is really just a special case of a while loop, so we'll handle it here. if( node.Term.Name == "ForStmt" ) { this.preLoop = c.convertNode( node.ChildNodes[1] ); this.test = c.convertNode( node.ChildNodes[3] ); this.postLoop = c.convertNode( node.ChildNodes[5] ); this.block = c.convertNode( node.ChildNodes[6] ); return true; } return false; }
public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "TryStmt" ) { this.tryBlock = c.convertNode( node.ChildNodes[1] ); if( node.ChildNodes.Count == 2 ) throw new InvalidOperationException( "Can't have a try block without except/finally" ); var except = node.ChildNodes[2]; if( except.ChildNodes.Count == 2 ) this.exceptBlock = c.convertNode( except.ChildNodes[1] ); else { this.exceptIdentifer = except.ChildNodes[1].Token.Text; this.exceptBlock = c.convertNode( except.ChildNodes[2] ); } if( node.ChildNodes.Count > 3 ) { var fin = node.ChildNodes[3]; this.finallyBlock = c.convertNode( fin.ChildNodes[1] ); } return true; } return false; }
public override bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { base.convert( node, c ); if( node.Term.Name == "ForInStmt" && node.ChildNodes.Count == 5 && node.ChildNodes[2].Token.Text == "in" ) { // Get our loop variable. this.loopVariable = node.ChildNodes[1].Token.Text; // Convert the loop-over expression. this.loopOver = c.convertNode( node.ChildNodes[3] ); // Convert the inner block. this.block = c.convertNode( node.ChildNodes[4] ); return true; } return false; }
public virtual bool convert( Irony.Parsing.ParseTreeNode node, Compiler c ) { this.frame = new StackTrace.StackFrame() { line = node.Span.Location.Line + 1, col = node.Span.Location.Column + 1, unitName = c.unitName }; return false; }