public ROUTINE(int ps, bool o, string name) : base(name) { PureSafe = ps; isOverride = o; this.name = new IDENTIFIER(name); genericParameters = new List <FORMAL_GENERIC>(); parameters = new List <ENTITY>(); preconditions = new List <EXPRESSION>(); postconditions = new List <EXPRESSION>(); requireElse = false; ensureThen = false; routineBody = new BODY(); }
/// <summary> /// /// </summary> /// <syntax> /// Try : try /// StatementList /// catch ( [ Identifier : ] UnitType ) /// [ StatementList ] /// { catch ( [ Identifier : ] UnitType ) /// [ StatementList ] /// } /// [ else [ StatementsList ] ] /// end /// </syntax> /// <returns></returns> public static void parse(iSCOPE context) { Token token = get(); Token begin = token; if (token.code != TokenCode.Try) // Compiler error { } forget(); TRY result = new TRY(); Context.enter(result); BODY.parse(TokenCode.Catch, TokenCode.ERROR, TokenCode.ERROR, result); while (true) { token = get(); if (token.code != TokenCode.Catch) { break; } CATCH.parse(result); } token = get(); if (token.code == TokenCode.Else) { forget(); BODY.parse(TokenCode.End, TokenCode.ERROR, TokenCode.ERROR, context); } token = get(); if (token.code != TokenCode.End) // Syntax error { } forget(); result.setSpan(begin, token); context.add(result); Context.exit(); }
public LOOP() : base() { invariants = new List <EXPRESSION>(); body = new BODY(); variants = new List <EXPRESSION>(); }
/// <summary> /// /// </summary> /// <syntax> /// Loop : while BooleanExpression /// loop /// [ invariant PredicateList ] /// [ StatementList ] /// [ variant PredicateList ] /// end [ loop ] /// /// | loop /// [ invariant PredicateList ] /// [ StatementList ] /// [ variant PredicateList ] /// [ while BooleanExpression ] /// end [ loop ] /// </syntax> /// <returns></returns> public static void parse(Token id, iSCOPE context) { Debug.Indent(); Debug.WriteLine("Entering LOOP.parse"); // If id != null, then 'id' is a loop label. // ':' after label is already parsed. LOOP loop = new LOOP(); Context.enter(loop); Token token = get(); Token begin = token; if (token.code == TokenCode.While) { forget(); loop.prefix = true; EXPRESSION whileExpr = EXPRESSION.parse(null, context); token = get(); if (token.code == TokenCode.Loop) { forget(); loop.addw(whileExpr); whileExpr.parent = loop; } else // Syntax error { error(token, "no-loop"); } token = get(); if (token.code == TokenCode.Invariant) { forget(); parseInvariant(loop); } BODY body = new BODY(); body.parent = loop; loop.body = body; Context.enter(body); token = get(); BODY.parse(TokenCode.End, TokenCode.Variant, TokenCode.ERROR, body); body.setSpan(token, get()); Context.exit(); token = get(); if (token.code == TokenCode.Variant) { forget(); parseVariant(loop); } } else if (token.code == TokenCode.Loop) { loop.prefix = false; forget(); token = get(); if (token.code == TokenCode.Invariant) { forget(); parseInvariant(loop); } BODY body = new BODY(); body.parent = loop; loop.body = body; Context.enter(body); token = get(); BODY.parse(TokenCode.End, TokenCode.Variant, TokenCode.While, body); body.setSpan(token, get()); Context.exit(); token = get(); if (token.code == TokenCode.Variant) { forget(); parseVariant(loop); } token = get(); if (token.code == TokenCode.While) { forget(); EXPRESSION whileExpr = EXPRESSION.parse(null, context); loop.addw(whileExpr); whileExpr.parent = loop; } } else // Compiler error { error(token, "system-bug"); } token = get(); if (token.code != TokenCode.End) // Syntax error { error(token, "no-end", "loop"); } else { forget(); token = get(); if (token.code == TokenCode.Loop) { forget(); } } if (loop != null) { loop.setSpan(begin, token); } context.add(loop); Context.exit(); Debug.WriteLine("Exiting LOOP.parse"); Debug.Unindent(); }
public static void parse(Token routineName, bool hidden, bool final, bool isOverride, int pure_safe, iSCOPE context) { ROUTINE routine = null; if (routineName.code == TokenCode.Init) { Debug.Indent(); Debug.WriteLine("Entering INITIALIZER.parse"); routine = new INITIALIZER(); goto Init; } Debug.Indent(); Debug.WriteLine("Entering ROUTINE.parse (" + routineName.image + ")"); string image = routineName.image; Token token = get(); // What's after the routine name? switch (routineName.image) { case "and": if (token.code == TokenCode.Then) { forget(); image += " then"; Span span = new Span(routineName.span, token.span); routineName = new Token(span, TokenCode.Identifier, image, new Category(CategoryCode.identifier)); } break; case "or": token = get(); if (token.code == TokenCode.Else) { forget(); image += " else"; Span span = new Span(routineName.span, token.span); routineName = new Token(span, TokenCode.Identifier, image, new Category(CategoryCode.identifier)); } break; } routine = new ROUTINE(pure_safe, isOverride, routineName); Init: routine.setSpecs(hidden, final); Context.enter(routine); if (routineName.code == TokenCode.Init) { goto Init2; } token = get(); if (token.code == TokenCode.Alias) { forget(); token = expect(TokenCode.Identifier); routine.alias = new IDENTIFIER(token.image); } if (token.code == TokenCode.LBracket) { // Generic routine forget(); while (true) { var generic = FORMAL_GENERIC.parse(context); routine.add(generic); token = get(); switch (token.code) { case TokenCode.Comma: case TokenCode.Semicolon: forget(); continue; case TokenCode.RBracket: forget(); goto Finish; default: { /* Syntax error */ break; } } } Finish: ; } Init2: token = get(); if (token.code == TokenCode.LParen) { forget(); token = get(); if (token.code == TokenCode.RParen) { // Empty parameter list forget(); goto Weiter; } while (true) { VARIABLE.parse(false, false, false, false, null, null, routine); token = get(); if (token.code == TokenCode.Comma) { forget(); continue; } if (token.code == TokenCode.Semicolon) { forget(); continue; } break; } expect(TokenCode.RParen); } Weiter: token = get(); if (token.code == TokenCode.Colon) { forget(); bool ref_val, conc; // TEMP SOLUTION Span span2; routine.type = VARIABLE.parseTypeSpecifier(routine, out ref_val, out conc, out span2); if (routine.type != null) { routine.type.parent = routine; routine.type.setSpan(span2); } } token = get(); if (token.code == TokenCode.Require) { forget(); token = get(); if (token.code == TokenCode.Else) { forget(); routine.requireElse = true; } while (true) { EXPRESSION precondition = EXPRESSION.parse(null, routine); routine.addPre(precondition); precondition.parent = routine; token = get(); if (token.code == TokenCode.Is || token.code == TokenCode.Arrow) { break; } } } if (token.code == TokenCode.Arrow) { forget(); BODY body = new BODY(); routine.routineBody = body; body.parent = routine; Context.enter(body); EXPRESSION expression = EXPRESSION.parse(null, body); RETURN ret = new RETURN(expression); expression.parent = ret; ret.setSpan(expression.span); ret.parent = body; body.add(ret); body.setSpan(ret.span); Context.exit(); } else if (token.code == TokenCode.Is) { forget(); token = get(); if (token.code == TokenCode.Abstract) { forget(); routine.isAbstract = true; } else if (token.code == TokenCode.Foreign) { forget(); routine.isForeign = true; } else { BODY body = new BODY(); body.parent = routine; routine.routineBody = body; Context.enter(body); BODY.parse(TokenCode.End, TokenCode.Ensure, TokenCode.ERROR, body); Context.exit(); } token = get(); if (token.code == TokenCode.Ensure) { forget(); token = get(); if (token.code == TokenCode.Then) { forget(); routine.ensureThen = true; } ENTITY.weAreWithinEnsure = true; while (true) { EXPRESSION postcondition = EXPRESSION.parse(null, routine); routine.addPre(postcondition); postcondition.parent = routine; token = get(); if (token.code == TokenCode.End) { forget(); break; } } ENTITY.weAreWithinEnsure = false; } else if (!routine.isAbstract && !routine.isForeign) { expect(TokenCode.End); } } token = get(); if (token.code == TokenCode.Semicolon) { forget(); } Context.exit(); context.add(routine); routine.parent = context.self; routine.setSpan(routineName, token); if (routineName.code == TokenCode.Init) { Debug.WriteLine("Exiting INITIALIZER.parse"); } else { Debug.WriteLine("Exiting ROUTINE.parse"); } Debug.Unindent(); }
public static void parse(iSCOPE context) // REWRITE!! { Token token = get(); Token begin = token; if (token.code != TokenCode.Catch) // Compiler error { } forget(); CATCH handler = new CATCH(); Context.enter(handler); token = get(); if (token.code != TokenCode.LParen) // Syntax error { } else { forget(); } token = get(); if (token.code != TokenCode.Identifier) // Syntax error { } else { Token id = token; forget(); token = get(); if (token.code == TokenCode.Colon) { forget(); // handler.identifier = id.image; token = get(); if (token.code != TokenCode.Identifier) // Syntax error { } forget(); token = id; } UNIT_REF unit_ref = UNIT_REF.parse(null, false, context); // CHECK!! handler.unit_ref = unit_ref; unit_ref.parent = handler; } token = get(); if (token.code != TokenCode.RParen) // Syntax error { } forget(); BODY.parse(TokenCode.Catch, TokenCode.Else, TokenCode.End, handler); token = get(); // just to get the span... handler.setSpan(begin, token); Context.exit(); context.add(handler); }
public CATCH() : base() { body = new BODY(); }
public static void parse(TokenCode stop1, TokenCode stop2, TokenCode stop3, iSCOPE context) { Debug.Indent(); Debug.WriteLine("Entering BODY.parse"); Token token; Token start = get(); while (true) { bool res = STATEMENT.parse(context, stop1, stop2, stop3); if (!res) { // Neither a statement nor a simple declaration. // Perhaps, a nested/local function? token = get(); switch (token.code) { case TokenCode.Routine: case TokenCode.Safe: case TokenCode.Pure: forget(); int pure_safe = 0; switch (token.code) { case TokenCode.Pure: pure_safe = 1; break; case TokenCode.Safe: pure_safe = 2; break; } ROUTINE.parse(null, false, false, false, pure_safe, context); break; case TokenCode.Unit: case TokenCode.Ref: case TokenCode.Val: // A _local_ unit??? UNIT.parse(context); break; default: // What's this? break; } } token = get(); if (token.code == TokenCode.Semicolon /*|| wasEOL*/) { forget(); } if (token.code == stop1) { break; // don't 'forget()' } if (stop2 != TokenCode.ERROR && token.code == stop2) { break; // don't 'forget()' } if (stop3 != TokenCode.ERROR && token.code == stop3) { break; // don't 'forget()' } } BODY body = context as BODY; if (body == null) /* A system error */ } {