public IrBinop(AstBinop.OP b, IrExp l, IrExp r) { left = l; right = r; switch (b) { case AstBinop.OP.ADD: op = OP.ADD; break; case AstBinop.OP.SUB: op = OP.SUB; break; case AstBinop.OP.MUL: op = OP.MUL; break; case AstBinop.OP.DIV: op = OP.DIV; break; case AstBinop.OP.AND: op = OP.AND; break; case AstBinop.OP.OR: op = OP.OR; break; default: Debug.Assert(false, "Encountered unknown binary operator: value = " + b.ToString()); break; } }
// Binop --- // int op; // Exp e1, e2; public IrExp visit(AstBinop n) { IrExp l = n.e1.accept(this); IrExp r = n.e2.accept(this); return new IrBinop(n.op, l, r); }
public AstType visit(AstBinop n) { return null; }
/* throws ParseException */ /* final */ public static AstExp astExp() { AstType t; AstToken n; AstRelop.OP relop; AstBinop.OP binop; AstUnop.OP unop; AstExp e, e1, e2; AstExpList el; jj_consume_token(AstRegExpId.kw56); switch ((jj_ntk == AstRegExpId.UNDEFINED)?jj_ntk_fn():jj_ntk) { case AstRegExpId.kwArrayElm: jj_consume_token(AstRegExpId.kwArrayElm); e1 = astExp(); e2 = astExp(); e = new AstArrayElm(e1,e2); break; case AstRegExpId.kwArrayLen: jj_consume_token(AstRegExpId.kwArrayLen); e1 = astExp(); e = new AstArrayLen(e1); break; case AstRegExpId.kwBinop: jj_consume_token(AstRegExpId.kwBinop); binop = binOp(); e1 = astExp(); e2 = astExp(); e = new AstBinop(binop,e1,e2); break; case AstRegExpId.kwBoolVal: jj_consume_token(AstRegExpId.kwBoolVal); switch ((jj_ntk == AstRegExpId.UNDEFINED)?jj_ntk_fn():jj_ntk) { case AstRegExpId.kwTrue: jj_consume_token(AstRegExpId.kwTrue); e = new AstBoolVal(true); break; case AstRegExpId.kwFalse: jj_consume_token(AstRegExpId.kwFalse); e = new AstBoolVal(false); break; default: jj_la1[12] = jj_gen; jj_consume_token(AstRegExpId.UNDEFINED); throw new AstParseException(); } break; case AstRegExpId.kwCall: jj_consume_token(AstRegExpId.kwCall); e1 = astExp(); e2 = astExp(); el = astExpList(); e = new AstCall(e1,(AstId)e2,el); break; case AstRegExpId.kwId: jj_consume_token(AstRegExpId.kwId); n = jj_consume_token(AstRegExpId.ID); e = new AstId(n.image); break; case AstRegExpId.kwIntVal: jj_consume_token(AstRegExpId.kwIntVal); n = jj_consume_token(AstRegExpId.INTVAL); e = new AstIntVal(int.Parse(n.image)); break; case AstRegExpId.kwField: jj_consume_token(AstRegExpId.kwField); e1 = astExp(); e2 = astExp(); e = new AstField(e1,(AstId)e2); break; case AstRegExpId.kwNewArray: jj_consume_token(AstRegExpId.kwNewArray); t = astType(); n = jj_consume_token(AstRegExpId.INTVAL); e = new AstNewArray((AstType)t, int.Parse(n.image)); break; case AstRegExpId.kwNewObj: jj_consume_token(AstRegExpId.kwNewObj); e1 = astExp(); el = astExpList(); e = new AstNewObj((AstId)e1,el); break; case AstRegExpId.kwRelop: jj_consume_token(AstRegExpId.kwRelop); relop = relOp(); e1 = astExp(); e2 = astExp(); e = new AstRelop(relop, e1, e2); break; case AstRegExpId.kwStrVal: jj_consume_token(AstRegExpId.kwStrVal); n = jj_consume_token(AstRegExpId.STRVAL); String s = n.image; e = new AstStrVal(s.Substring(1, s.Length-2)); break; case AstRegExpId.kwThis: jj_consume_token(AstRegExpId.kwThis); e = new AstThis(); break; case AstRegExpId.kwUnop: jj_consume_token(AstRegExpId.kwUnop); unop = unOp(); e1 = astExp(); e = new AstUnop(unop,e1); break; case AstRegExpId.kwNullExp: jj_consume_token(AstRegExpId.kwNullExp); e = null; break; default: jj_la1[13] = jj_gen; jj_consume_token(AstRegExpId.UNDEFINED); throw new AstParseException(); } jj_consume_token(AstRegExpId.kw57); {if (true) return e;} throw new Error("Missing return statement in function"); }
private bool isArithOp(AstBinop.OP op) { return (AstBinop.OP.ADD <= op) && (op <= AstBinop.OP.DIV); }
/* throws Exception */ // Binop --- // int op; // Exp e1, e2; public AstType visit(AstBinop n) { AstType t1 = n.e1.accept(this); AstType t2 = n.e2.accept(this); if (isArithOp(n.op)) { if (isIntType(t1) && isIntType(t2)) return IntType; } else if (isBoolType(t1) && isBoolType(t2)) { return BoolType; } throw new TypeException("Binop operands' type mismatch: " + t1 + n.opName(n.op) + t2); }
/* throws ParseException */ // ---------------------------------------------------------------- // ArithTail -> [ ( "+" | "-" ) Term ArithTail ] // ---------------------------------------------------------------- /* final */ public static AstExp ArithTail(AstExp e0) { AstExp e1 = null; AstExp e2 = null; DemiTasse.ast.AstBinop.OP op; switch ((jj_ntk == MpRegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk) { case MpRegExpId.ADD: switch ((jj_ntk == MpRegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk) { case MpRegExpId.ADD: jj_consume_token(MpRegExpId.ADD); op = DemiTasse.ast.AstBinop.OP.ADD; break; case MpRegExpId.SUB: jj_consume_token(MpRegExpId.SUB); op = DemiTasse.ast.AstBinop.OP.SUB; break; default: jj_la1[30] = jj_gen; jj_consume_token(MpRegExpId.UNDEFINED); throw new MiniParseException(); } e1 = Term(); e1 = new AstBinop(op, e0, e1); e2 = ArithTail(e1); break; case MpRegExpId.SUB: switch ((jj_ntk == MpRegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk) { case MpRegExpId.ADD: jj_consume_token(MpRegExpId.ADD); op = DemiTasse.ast.AstBinop.OP.ADD; break; case MpRegExpId.SUB: jj_consume_token(MpRegExpId.SUB); op = DemiTasse.ast.AstBinop.OP.SUB; break; default: jj_la1[30] = jj_gen; jj_consume_token(MpRegExpId.UNDEFINED); throw new MiniParseException(); } e1 = Term(); e1 = new AstBinop( op, e0, e1 ); e2 = ArithTail(e1); break; default: jj_la1[31] = jj_gen; break; } return e2 == null ? e1 : e2; }
/* throws ParseException */ // ---------------------------------------------------------------- // TermTail -> ("*" | "/" ) Factor TermTail // ---------------------------------------------------------------- /* final */ public static AstExp TermTail(AstExp e0) { AstExp e1 = null; AstExp e2 = null; DemiTasse.ast.AstBinop.OP op; switch ((jj_ntk == MpRegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk) { case MpRegExpId.MUL: switch ((jj_ntk == MpRegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk) { case MpRegExpId.MUL: jj_consume_token(MpRegExpId.MUL); op = DemiTasse.ast.AstBinop.OP.MUL; break; case MpRegExpId.DIV: jj_consume_token(MpRegExpId.DIV); op = DemiTasse.ast.AstBinop.OP.DIV; break; default: jj_la1[32] = jj_gen; jj_consume_token(MpRegExpId.UNDEFINED); throw new MiniParseException(); } e1 = Factor(); e1 = new AstBinop(op, e0, e1); e2 = TermTail(e1); break; case MpRegExpId.DIV: switch ((jj_ntk == MpRegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk) { case MpRegExpId.MUL: jj_consume_token(MpRegExpId.MUL); op = DemiTasse.ast.AstBinop.OP.MUL; break; case MpRegExpId.DIV: jj_consume_token(MpRegExpId.DIV); op = DemiTasse.ast.AstBinop.OP.DIV; break; default: jj_la1[32] = jj_gen; jj_consume_token(MpRegExpId.UNDEFINED); throw new MiniParseException(); } e1 = Factor(); e1 = new AstBinop( op, e0, e1 ); e2 = TermTail(e1); break; default: jj_la1[33] = jj_gen; break; } return e2 == null ? e1 : e2; }
/* throws ParseException */ // ---------------------------------------------------------------- // AndTail -> [ "&&" RelExpr AndTail ] // ---------------------------------------------------------------- /* final */ public static AstExp AndTail(AstExp e0) { AstExp e1 = null; AstExp e2 = null; switch ((jj_ntk == MpRegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk) { case MpRegExpId.AND: jj_consume_token(MpRegExpId.AND); e1 = RelExpr(); e1 = new AstBinop(DemiTasse.ast.AstBinop.OP.AND, e0, e1); e2 = AndTail(e1); break; default: jj_la1[28] = jj_gen; break; } return e2 == null ? e1 : e2; }