public static Block InstrToBlock(InstrNode instr) { if (instr.Value is Block) { return((Block)instr.Value); } else { return(new Block(new InstrNode[] { instr })); } }
public static void CheckVarDecl(InstrNode node, ref Environment env) { VarDecl variable = (VarDecl)node.Value; TypeDesc varType = TypeResolver.ResolveType(variable.Value, env, variable.Value.Position); if (variable.Type == null && !(varType is NullType)) { variable.Type = new TypeNode(varType, variable.Value.Position); } env.AddEntry(variable.Name, varType, node.Position); }
public static ExprNode GetBlockLast(Block block) { if (block.Instructions.Count() > 0) { InstrNode last = block.Instructions.Last(); if (last.Value is ExprInstr) { return(new ExprNode(((ExprInstr)last.Value).Expression, last.Position)); } else { throw new ParserError( new InvalidElementException("Invalid last instruction for block"), last.Position ); } } else { throw new ArgumentException(); } }
private static InstrNode[] ParseStatementSeq(Parser parser, bool topLevel, bool intoNamespace, bool intoClass) { var statements = new List <InstrNode>(); bool isLastEOL = true; while (true) { InstrNode statement = null; bool eolFailed = false; try { parser.Eat(TokenInfo.TokenType.EOL); } catch (ParserError) { eolFailed = true; } if (eolFailed) { if (!isLastEOL) { TokenInfo.TokenType nextTokenType; try { nextTokenType = parser.LookAhead().Type; } catch (ParserError) { break; } if (nextTokenType == TokenInfo.TokenType.R_CURLY_BRACKET) { break; } throw new ParserError( new UnexpectedTokenException(nextTokenType), parser.LookAhead().Pos ); } try { if (topLevel) { statement = parser.TryConsumer((Parser p) => new InstrNode(p.TryConsumer(NamespaceDecl.Consume), p.Cursor)); } else if (intoNamespace) { statement = parser.TryManyConsumers(new Func <Parser, InstrNode>[] { (Parser p) => new InstrNode(p.TryConsumer((Parser p) => VarDecl.Consume(p, true)), p.Cursor), (Parser p) => new InstrNode(p.TryConsumer((Parser p) => FuncDecl.Consume(p)), p.Cursor), (Parser p) => new InstrNode(p.TryConsumer(ClassDecl.Consume), p.Cursor) }); } else if (intoClass) { statement = parser.TryManyConsumers(new Func <Parser, InstrNode>[] { (Parser p) => new InstrNode(p.TryConsumer((Parser p) => VarDecl.Consume(p, true, true)), p.Cursor), (Parser p) => new InstrNode(p.TryConsumer((Parser p) => FuncDecl.Consume(p, true)), p.Cursor), (Parser p) => new InstrNode(p.TryConsumer(ConstructorDecl.Consume), p.Cursor) }); } else { statement = parser.TryConsumer(InstrNode.Consume); } } catch (ParserError ex) { if (!ex.IsExceptionFictive()) { throw ex; } if (parser.LookAhead().Type != TokenInfo.TokenType.R_CURLY_BRACKET) { throw new ParserError( new UnexpectedTokenException(parser.LookAhead().Type), parser.Cursor ); } break; } statements.Add(statement); isLastEOL = false; } else { isLastEOL = true; } } return(statements.ToArray()); }
public override string Pretty() { return($"Block({InstrNode.PrettyMultiple(Instructions)})"); }