} // NT_Stat private static void NT_IncDecAssignOrCallStat(out Stat s) { int spix = 0; Symbol sy = null; Expr lhs = null, e = null; SrcPos sp = null; s = null; for (;;) { switch (Syn.Interpret()) { case 0: return; case 1: Lex.GETidentAttr(out spix); break; case 2: // SEM sy = SymTab.SymbolOf(spix, Symbol.Kind.varKind, Symbol.Kind.parKind, Symbol.Kind.funcKind); break; case 3: // SEM s = new IncStat(sp, new VarOperand(sy)); break; case 4: // SEM s = new DecStat(sp, new VarOperand(sy)); break; case 5: // SEM lhs = new VarOperand(sy); break; case 6: // SEM sp = new SrcPos(); break; case 7: NT_Expr(out e); break; case 8: // SEM lhs = new ArrIdxOperator(sp, new VarOperand(sy), e); break; case 9: // SEM sp = new SrcPos(); break; case 10: NT_Expr(out e); break; case 11: // SEM s = new AssignStat(sp, lhs, e); break; case 12: // SEM sp = new SrcPos(); break; case 13: NT_ActParList(out e); break; case 14: // SEM s = new CallStat(sp, sy, e); break; } // switch } // for } // NT_IncDecAssignOrCallStat
} // DumpSymTab // === generate source text from symbol table and AST === public static void WriteStat(Stat stat) { switch (stat.kind) { case Stat.Kind.emptyStatKind: genMcpp.WriteLine(Indent() + ";"); break; case Stat.Kind.blockStatKind: BlockStat b_s = (BlockStat)stat; genMcpp.WriteLine(Indent() + "{"); IncIndent(); WriteStatList(b_s.statList); DecIndent(); genMcpp.WriteLine(Indent() + "}"); break; case Stat.Kind.incStatKind: IncStat i_s = (IncStat)stat; genMcpp.WriteLine(Indent() + i_s.vo.sy + "++;"); break; case Stat.Kind.decStatKind: DecStat d_s = (DecStat)stat; genMcpp.WriteLine(Indent() + d_s.vo.sy + "--;"); break; case Stat.Kind.assignStatKind: AssignStat a_s = (AssignStat)stat; genMcpp.WriteLine(Indent() + a_s.lhs + " = " + a_s.rhs + ";"); break; case Stat.Kind.callStatKind: CallStat c_s = (CallStat)stat; genMcpp.WriteLine(Indent() + c_s.func + "(" + c_s.apl + ");"); break; case Stat.Kind.ifStatKind: IfStat if_s = (IfStat)stat; genMcpp.WriteLine(Indent() + "if (" + if_s.cond + ")"); IncIndent(); WriteStatList(if_s.thenStat); DecIndent(); if (if_s.elseStat != null) { genMcpp.WriteLine(Indent() + "else "); IncIndent(); WriteStatList(if_s.elseStat); DecIndent(); } // if break; case Stat.Kind.whileStatKind: WhileStat w_s = (WhileStat)stat; genMcpp.WriteLine(Indent() + "while (" + w_s.cond + ")"); IncIndent(); WriteStatList(w_s.body); DecIndent(); break; case Stat.Kind.breakStatKind: genMcpp.WriteLine(Indent() + "break;"); break; case Stat.Kind.inputStatKind: InputStat in_s = (InputStat)stat; genMcpp.WriteLine(Indent() + "cin >> " + in_s.vo.sy + ";"); break; case Stat.Kind.outputStatKind: OutputStat out_s = (OutputStat)stat; genMcpp.Write(Indent() + "cout"); foreach (Object o in out_s.values) { genMcpp.Write(" << "); if (o is Expr) { genMcpp.Write(o); } else if (o is String) { String s = o as String; if (s == "\n") { genMcpp.Write("endl"); } else { genMcpp.Write('"' + s + '"'); } } else { throw new Exception("invalid value"); } } // foreach genMcpp.WriteLine(";"); break; case Stat.Kind.deleteStatKind: DeleteStat del_s = (DeleteStat)stat; genMcpp.WriteLine(Indent() + "delete[] " + NameList.NameOf(del_s.vo.sy.spix) + ";"); break; case Stat.Kind.returnStatKind: ReturnStat r_s = (ReturnStat)stat; genMcpp.Write(Indent() + "return"); if (r_s.e != null) { genMcpp.Write(" " + r_s.e); } genMcpp.WriteLine(";"); break; default: throw new Exception("invalid statement kind"); } // switch } // WriteStatList