public static void Main(string[] args) { IExeStack <IStatement> stack = new ExeStack <IStatement>(); Utils.IDictionary <string, int> dict = new MyDictionary <string, int>(); IMyList <int> output = new MyList <int>(); FileTable <int, FileData> fileTable = new FileTable <int, FileData>(); IStatement s1 = new OpenRFile("var_f", "C:\\Users\\Emy\\RiderProjects\\lab7\\text1.txt"); IStatement s2 = new ReadFile(new VarExp("var_f"), "var_c"); IStatement thenS1 = new ReadFile(new VarExp("var_f"), "var_c"); IStatement thenS2 = new PrintStmt(new VarExp("var_c")); IStatement thenS = new CompStmt(thenS1, thenS2); IStatement elseS = new PrintStmt(new ConstExp(0)); IStatement s3 = new IfStmt(new VarExp("var_c"), thenS, elseS); IStatement s4 = new CloseRFile(new VarExp("var_f")); IStatement s5 = new CompStmt(s1, s2); IStatement s6 = new CompStmt(s3, s4); IStatement s7 = new CompStmt(s5, s6); stack.push(s7); PrgState state = new PrgState(dict, stack, output, fileTable); Controller.Controller ctrl = new Controller.Controller(state); TextMenu menu = new TextMenu(); menu.addCommand(new ExitCommand("0", "exit")); menu.addCommand(new RunExample("1", "example_1", ctrl)); menu.show(); }
public void Visit(PrintStmt printStmt) { TreeLevel++; PrintNode("PrintStm"); printStmt.PrintExpr.Accept(this); TreeLevel--; }
public virtual void Visit(PrintStmt printStatement) { VisitNullableAttributes(printStatement.Attributes); foreach (var argument in printStatement.Args) { Visit(argument); } }
public override void Visit(PrintStmt stmt) { if (isNewLine) { isNewLine = false; AddStr(WS()); } AddStr("print "); stmt.expr.Accept(this); AddStr(";"); NewLine(); }
public string GenerateString(PrintStmt statement, int tabs) { StringBuilder bob = new StringBuilder(); ApplyIndentation(bob, tabs); bob.Append(statement.Tok.val + " "); // append "print " foreach (Expression arg in statement.Args) { bob.Append(GenerateString(arg)); bob.Append(", "); } bob.Remove(bob.Length - 2, 2); bob.Append(statement.EndTok.val); return(bob.ToString()); }
public void Init() { IList <int> ot = new ArrayList <int>(); IDict <string, int> symtbl = new ArrayDict <string, int>(); IDict <int, int> heap = new ArrayDict <int, int>(); IStack <IStmt> stk = new ArrayStack <IStmt>(); IStmt stmt = new PrintStmt(new ArithExpr(new ConstExpr(3), "+", new ConstExpr(4))); ot.Add(11); ot.Add(12); symtbl.Add("bb", 11); symtbl.Add("bc", 12); stk.Push(stmt); prg = new ProgState(ot, symtbl, stk, heap); Assert.AreEqual("[ print( 3 + 4 ) ]", prg.ToStrExe()); Assert.AreEqual("[ 11, 12 ]", prg.ToStrOut()); Assert.AreEqual("[ bb:11, bc:12 ]", prg.ToStrSym()); }
static void Main(string[] args) { IStmt ex1 = new CompStmt(new AssignStmt("a", new ArithExp(new ConstExp(1), new ConstExp(2), "+")), new IfStmt(new VarExp("a"), new PrintStmt(new VarExp("a")), new AssignStmt("b", new ConstExp(3)))); PrgState prg1 = new PrgState(new MyStack <IStmt>(), new MyDictionary <string, int>(), new MyList <int>(), new MyMap <ITuple <string, StreamReader> >(), ex1); IRepository repo1 = new Repository("D:\\VSProjects\\Interpreter\\log1.txt"); repo1.AddProgram(prg1); Controller ctr1 = new Controller(repo1); IStmt ex2 = new PrintStmt(new VarExp("a")); PrgState prg2 = new PrgState(new MyStack <IStmt>(), new MyDictionary <string, int>(), new MyList <int>(), new MyMap <ITuple <string, StreamReader> >(), ex2); IRepository repo2 = new Repository("D:\\VSProjects\\Interpreter\\log2.txt"); repo2.AddProgram(prg2); Controller ctr2 = new Controller(repo2); IStmt ex3 = new PrintStmt(new ArithExp(new ConstExp(1), new ConstExp(0), "/")); PrgState prg3 = new PrgState(new MyStack <IStmt>(), new MyDictionary <string, int>(), new MyList <int>(), new MyMap <ITuple <string, StreamReader> >(), ex3); IRepository repo3 = new Repository("D:\\VSProjects\\Interpreter\\log3.txt"); repo3.AddProgram(prg3); Controller ctr3 = new Controller(repo3); IStmt ex4 = new CompStmt(new OpenRFileStmt("var_f", "D:\\VSProjects\\Interpreter\\file1.txt"), new CompStmt(new ReadFileStmt(new VarExp("var_f"), "var_c"), new CompStmt(new PrintStmt(new VarExp( "var_c")), new CompStmt(new IfStmt(new VarExp("var_c"), new CompStmt(new ReadFileStmt(new VarExp("var_f"), "var_c"), new PrintStmt(new VarExp("var_c"))), new PrintStmt(new ConstExp(0))), new CloseRFileStmt(new VarExp("var_f")))))); PrgState prg4 = new PrgState(new MyStack <IStmt>(), new MyDictionary <string, int>(), new MyList <int>(), new MyMap <ITuple <string, StreamReader> >(), ex4); IRepository repo4 = new Repository("D:\\VSProjects\\Interpreter\\log4.txt"); repo4.AddProgram(prg4); Controller ctr4 = new Controller(repo4); IStmt ex5 = new CompStmt(new OpenRFileStmt("var_f", "D:\\VSProjects\\Interpreter\\file2.txt"), new CompStmt(new ReadFileStmt(new VarExp("var_f"), "var_c"), new CompStmt(new PrintStmt(new VarExp( "var_c")), new CompStmt(new IfStmt(new VarExp("var_c"), new CompStmt(new ReadFileStmt(new VarExp("var_f"), "var_c"), new PrintStmt(new VarExp("var_c"))), new PrintStmt(new ConstExp(0))), new CloseRFileStmt(new VarExp("var_f")))))); PrgState prg5 = new PrgState(new MyStack <IStmt>(), new MyDictionary <string, int>(), new MyList <int>(), new MyMap <ITuple <string, StreamReader> >(), ex5); IRepository repo5 = new Repository("D:\\VSProjects\\Interpreter\\log5.txt"); repo5.AddProgram(prg5); Controller ctr5 = new Controller(repo5); IStmt ex6 = new CompStmt(new OpenRFileStmt("var_f", "D:\\VSProjects\\Interpreter\\file1.txt"), new OpenRFileStmt("var_f", "D:\\VSProjects\\Interpreter\\file1.txt")); PrgState prg6 = new PrgState(new MyStack <IStmt>(), new MyDictionary <string, int>(), new MyList <int>(), new MyMap <ITuple <string, StreamReader> >(), ex6); IRepository repo6 = new Repository("D:\\VSProjects\\Interpreter\\log6.txt"); repo6.AddProgram(prg6); Controller ctr6 = new Controller(repo6); IStmt ex7 = new OpenRFileStmt("var_f", "D:\\VSProjects\\Interpreter\\file0.txt"); PrgState prg7 = new PrgState(new MyStack <IStmt>(), new MyDictionary <string, int>(), new MyList <int>(), new MyMap <ITuple <string, StreamReader> >(), ex7); IRepository repo7 = new Repository("D:\\VSProjects\\Interpreter\\log7.txt"); repo7.AddProgram(prg7); Controller ctr7 = new Controller(repo7); IStmt ex8 = new ReadFileStmt(new ConstExp(1), "var_c"); PrgState prg8 = new PrgState(new MyStack <IStmt>(), new MyDictionary <string, int>(), new MyList <int>(), new MyMap <ITuple <string, StreamReader> >(), ex8); IRepository repo8 = new Repository("D:\\VSProjects\\Interpreter\\log8.txt"); repo8.AddProgram(prg8); Controller ctr8 = new Controller(repo8); TextMenu menu = new TextMenu(); menu.AddCommand(new ExitCommand("0", "exit")); menu.AddCommand(new RunExample("1", ex1.ToString(), ctr1)); menu.AddCommand(new RunExample("2", ex2.ToString(), ctr2)); menu.AddCommand(new RunExample("3", ex3.ToString(), ctr3)); menu.AddCommand(new RunExample("4", ex4.ToString(), ctr4)); menu.AddCommand(new RunExample("5", ex5.ToString(), ctr5)); menu.AddCommand(new RunExample("6", ex6.ToString(), ctr6)); menu.AddCommand(new RunExample("7", ex7.ToString(), ctr7)); menu.AddCommand(new RunExample("8", ex8.ToString(), ctr8)); menu.Show(); }
void PrintStmt(out Statement s) { Contract.Ensures(Contract.ValueAtReturn(out s) != null); IToken x; Expression e; var args = new List<Expression>(); Expect(102); x = t; Expression(out e, false, true); args.Add(e); while (la.kind == 22) { Get(); Expression(out e, false, true); args.Add(e); } Expect(28); s = new PrintStmt(x, t, args); }
public virtual Statement CloneStmt(Statement stmt) { if (stmt == null) { return null; } Statement r; if (stmt is AssertStmt) { var s = (AssertStmt)stmt; r = new AssertStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null); } else if (stmt is AssumeStmt) { var s = (AssumeStmt)stmt; r = new AssumeStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null); } else if (stmt is TacticInvariantStmt) { var s = (TacticInvariantStmt)stmt; r = new TacticInvariantStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null, s.IsObjectLevel); } else if (stmt is TacticAssertStmt) { var s = (TacticAssertStmt)stmt; r = new TacticAssertStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null, s.IsObjectLevel); } else if (stmt is PrintStmt) { var s = (PrintStmt)stmt; r = new PrintStmt(Tok(s.Tok), Tok(s.EndTok), s.Args.ConvertAll(CloneExpr)); } else if (stmt is BreakStmt) { var s = (BreakStmt)stmt; if (s.TargetLabel != null) { r = new BreakStmt(Tok(s.Tok), Tok(s.EndTok), s.TargetLabel); } else { r = new BreakStmt(Tok(s.Tok), Tok(s.EndTok), s.BreakCount); } } else if (stmt is ReturnStmt) { var s = (ReturnStmt)stmt; r = new ReturnStmt(Tok(s.Tok), Tok(s.EndTok), s.rhss == null ? null : s.rhss.ConvertAll(CloneRHS)); } else if (stmt is YieldStmt) { var s = (YieldStmt)stmt; r = new YieldStmt(Tok(s.Tok), Tok(s.EndTok), s.rhss == null ? null : s.rhss.ConvertAll(CloneRHS)); } else if (stmt is AssignStmt) { var s = (AssignStmt)stmt; r = new AssignStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Lhs), CloneRHS(s.Rhs)); } else if (stmt is BlockStmt) { r = CloneBlockStmt((BlockStmt)stmt); } else if (stmt is IfStmt) { var s = (IfStmt)stmt; r = new IfStmt(Tok(s.Tok), Tok(s.EndTok), s.IsExistentialGuard, CloneExpr(s.Guard), CloneBlockStmt(s.Thn), CloneStmt(s.Els)); } else if (stmt is AlternativeStmt) { var s = (AlternativeStmt)stmt; r = new AlternativeStmt(Tok(s.Tok), Tok(s.EndTok), s.Alternatives.ConvertAll(CloneGuardedAlternative)); } else if (stmt is WhileStmt) { var s = (WhileStmt)stmt; r = new WhileStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Guard), s.Invariants.ConvertAll(CloneMayBeFreeExpr), CloneSpecExpr(s.Decreases), CloneSpecFrameExpr(s.Mod), CloneBlockStmt(s.Body)); ((WhileStmt)r).TacAps = s.TacAps; } else if (stmt is AlternativeLoopStmt) { var s = (AlternativeLoopStmt)stmt; r = new AlternativeLoopStmt(Tok(s.Tok), Tok(s.EndTok), s.Invariants.ConvertAll(CloneMayBeFreeExpr), CloneSpecExpr(s.Decreases), CloneSpecFrameExpr(s.Mod), s.Alternatives.ConvertAll(CloneGuardedAlternative)); } else if (stmt is ForallStmt) { var s = (ForallStmt)stmt; r = new ForallStmt(Tok(s.Tok), Tok(s.EndTok), s.BoundVars.ConvertAll(CloneBoundVar), null, CloneExpr(s.Range), s.Ens.ConvertAll(CloneMayBeFreeExpr), CloneStmt(s.Body)); if (s.ForallExpressions != null) { ((ForallStmt)r).ForallExpressions = s.ForallExpressions.ConvertAll(CloneExpr); } } else if (stmt is CalcStmt) { var s = (CalcStmt)stmt; // calc statements have the unusual property that the last line is duplicated. If that is the case (which // we expect it to be here), we share the clone of that line as well. var lineCount = s.Lines.Count; var lines = new List<Expression>(lineCount); for (int i = 0; i < lineCount; i++) { lines.Add(i == lineCount - 1 && 2 <= lineCount && s.Lines[i] == s.Lines[i - 1] ? lines[i - 1] : CloneExpr(s.Lines[i])); } Contract.Assert(lines.Count == lineCount); r = new CalcStmt(Tok(s.Tok), Tok(s.EndTok), CloneCalcOp(s.Op), lines, s.Hints.ConvertAll(CloneBlockStmt), s.StepOps.ConvertAll(CloneCalcOp), CloneCalcOp(s.ResultOp), CloneAttributes(s.Attributes)); } else if (stmt is MatchStmt) { var s = (MatchStmt)stmt; r = new MatchStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Source), s.Cases.ConvertAll(CloneMatchCaseStmt), s.UsesOptionalBraces); } else if (stmt is AssignSuchThatStmt) { var s = (AssignSuchThatStmt)stmt; r = new AssignSuchThatStmt(Tok(s.Tok), Tok(s.EndTok), s.Lhss.ConvertAll(CloneExpr), CloneExpr(s.Expr), s.AssumeToken == null ? null : Tok(s.AssumeToken), null); } else if (stmt is UpdateStmt) { var s = (UpdateStmt)stmt; r = new UpdateStmt(Tok(s.Tok), Tok(s.EndTok), s.Lhss.ConvertAll(CloneExpr), s.Rhss.ConvertAll(CloneRHS), s.CanMutateKnownState); } else if (stmt is VarDeclStmt) { var s = (VarDeclStmt)stmt; var lhss = s.Locals.ConvertAll(c => new LocalVariable(Tok(c.Tok), Tok(c.EndTok), c.Name, CloneType(c.OptionalType), c.IsGhost)); r = new VarDeclStmt(Tok(s.Tok), Tok(s.EndTok), lhss, (ConcreteUpdateStatement)CloneStmt(s.Update)); } else if (stmt is LetStmt) { var s = (LetStmt)stmt; r = new LetStmt(Tok(s.Tok), Tok(s.EndTok), s.LHSs.ConvertAll(CloneCasePattern), s.RHSs.ConvertAll(CloneExpr)); } else if (stmt is ModifyStmt) { var s = (ModifyStmt)stmt; var mod = CloneSpecFrameExpr(s.Mod); var body = s.Body == null ? null : CloneBlockStmt(s.Body); r = new ModifyStmt(Tok(s.Tok), Tok(s.EndTok), mod.Expressions, mod.Attributes, body); } else if (stmt is TacnyCasesBlockStmt) { var s = (TacnyCasesBlockStmt)stmt; var guard = CloneExpr(s.Guard); var body = s.Body == null ? null : CloneBlockStmt(s.Body); r = new TacnyCasesBlockStmt(Tok(s.Tok), Tok(s.EndTok), guard, body); } else if (stmt is TacnyChangedBlockStmt) { var s = (TacnyChangedBlockStmt)stmt; var body = s.Body == null ? null : CloneBlockStmt(s.Body); r = new TacnyChangedBlockStmt(Tok(s.Tok), Tok(s.EndTok), body); } else if (stmt is TacnySolvedBlockStmt) { var s = (TacnySolvedBlockStmt)stmt; var body = s.Body == null ? null : CloneBlockStmt(s.Body); r = new TacnySolvedBlockStmt(Tok(s.Tok), Tok(s.EndTok), body); } else if (stmt is TacnyTryCatchBlockStmt) { var s = (TacnyTryCatchBlockStmt)stmt; var body = s.Body == null ? null : CloneBlockStmt(s.Body); var c = s.Ctch == null ? null : CloneBlockStmt(s.Ctch); r = new TacnyTryCatchBlockStmt(Tok(s.Tok), Tok(s.EndTok), body, c); } else if (stmt is TacticVarDeclStmt) { var s = (TacticVarDeclStmt)stmt; var lhss = s.Locals.ConvertAll(c => new LocalVariable(Tok(c.Tok), Tok(c.EndTok), c.Name, CloneType(c.OptionalType), c.IsGhost)); r = new TacticVarDeclStmt(Tok(s.Tok), Tok(s.EndTok), lhss, (ConcreteUpdateStatement)CloneStmt(s.Update)); } else { Contract.Assert(false); throw new cce.UnreachableException(); // unexpected statement } // add labels to the cloned statement AddStmtLabels(r, stmt.Labels); r.Attributes = CloneAttributes(stmt.Attributes); return r; }
protected abstract void MatchPrintStmt(PrintStmt printStmt);
protected override void MatchPrintStmt(PrintStmt stmt) => Resolve(stmt.Expression);
public virtual void Visit(PrintStmt stmt) { }
public override void Visit(PrintStmt stmt) { stmt.expr.Accept(this); chunk.WriteOpCode(OpCode.PRINT); }
public void FinalizeStatement() { Statement = new PrintStmt(Exp.GetExp()); }