private ThreeAddressValueType GenVariable(LogicExprNode expr) { if (expr is BooleanNode) { return(new ThreeAddressLogicValue((expr as BooleanNode).Val)); } if (expr is LogicIdNode) { return(new ThreeAddressStringValue((expr as LogicIdNode).Name.Name)); } if (expr is LogicOpNode) { LogicOpNode op = expr as LogicOpNode; string res = GenTempVariable(); ThreeAddressValueType arg1 = GenVariable(op.Left); ThreeAddressValueType arg2 = GenVariable(op.Right); ThreeOperator p = ThreeCode.ParseOperator(op.Operation); AddCode(new ThreeCode(res, p, arg1, arg2)); return(new ThreeAddressStringValue(res)); } if (expr is LogicNotNode) { LogicNotNode lnot = expr as LogicNotNode; string res = GenTempVariable(); ThreeAddressValueType arg1 = GenVariable(lnot.LogExpr); AddCode(new ThreeCode(res, ThreeOperator.Logic_not, arg1)); return(new ThreeAddressStringValue(res)); } throw new Exception("UNKNOW VALUE. Send autors of ThreeAddressCode"); }
private ThreeAddressValueType GenVariable(ExprNode expr) { if (expr is IdNode) { return(new ThreeAddressStringValue((expr as IdNode).Name)); } if (expr is DoubleNumNode) { return(new ThreeAddressDoubleValue((expr as DoubleNumNode).Num)); } if (expr is IntNumNode) { return(new ThreeAddressIntValue((expr as IntNumNode).Num)); } if (expr is BinOpNode) { BinOpNode op = expr as BinOpNode; string res = GenTempVariable(); ThreeAddressValueType arg1 = GenVariable(op.Left); ThreeAddressValueType arg2 = GenVariable(op.Right); ThreeOperator p = ThreeCode.ParseOperator(op.Op); AddCode(new ThreeCode(res, p, arg1, arg2)); return(new ThreeAddressStringValue(res)); } throw new Exception("UNKNOW VALUE. Send autors of ThreeAddressCode"); }
private void GenBinOpCode(ThreeOperator op) { switch (op) { case ThreeOperator.Plus: genc.Emit(OpCodes.Add); break; case ThreeOperator.Minus: genc.Emit(OpCodes.Sub); break; case ThreeOperator.Mult: genc.Emit(OpCodes.Mul); break; case ThreeOperator.Div: genc.Emit(OpCodes.Div); break; case ThreeOperator.Logic_and: genc.Emit(OpCodes.And); break; case ThreeOperator.Logic_or: genc.Emit(OpCodes.Or); break; case ThreeOperator.Logic_equal: genc.Emit(OpCodes.Ceq); break; case ThreeOperator.Logic_greater: genc.Emit(OpCodes.Cgt); break; case ThreeOperator.Logic_less: genc.Emit(OpCodes.Clt); break; case ThreeOperator.Logic_geq: genc.Emit(OpCodes.Clt); genc.Emit(OpCodes.Ldc_I4_1); genc.Emit(OpCodes.Xor); break; case ThreeOperator.Logic_leq: genc.Emit(OpCodes.Cgt); genc.Emit(OpCodes.Ldc_I4_1); genc.Emit(OpCodes.Xor); break; case ThreeOperator.Logic_neq: genc.Emit(OpCodes.Ceq); genc.Emit(OpCodes.Ldc_I4_1); genc.Emit(OpCodes.Xor); break; } }
public ThreeCode(string res, ThreeAddressValueType a1) { label = ""; result = res; arg1 = a1; arg2 = null; operation = ThreeOperator.Assign; }
public ThreeCode(string res, ThreeOperator op, ThreeAddressValueType a1, ThreeAddressValueType a2 = null) { label = ""; result = res; arg1 = a1; arg2 = a2; operation = op; }
private static int EvalConst(int c1, int c2, ThreeOperator op) { switch (op) { case ThreeOperator.Plus: return(c1 + c2); case ThreeOperator.Minus: return(c1 - c2); case ThreeOperator.Mult: return(c1 * c2); case ThreeOperator.Div: return(c1 / c2); default: throw new Exception("Logic error"); } }
public static string GetOperatorString(ThreeOperator op) { switch (op) { case ThreeOperator.Assign: return("="); case ThreeOperator.Div: return("/"); case ThreeOperator.Minus: return("-"); case ThreeOperator.Mult: return("*"); case ThreeOperator.Plus: return("+"); case ThreeOperator.Logic_and: return("&&"); case ThreeOperator.Logic_less: return("<"); case ThreeOperator.Logic_or: return("||"); case ThreeOperator.Logic_equal: return("=="); case ThreeOperator.Logic_greater: return(">"); case ThreeOperator.Logic_geq: return(">="); case ThreeOperator.Logic_leq: return("<="); case ThreeOperator.Logic_neq: return("!="); case ThreeOperator.Logic_not: return("!"); } return("UNKNOWN"); }
public bool CheckToUsefulOperation(ThreeOperator op) { return(!(op == ThreeOperator.Goto || op == ThreeOperator.None || op == ThreeOperator.IfGoto)); }
public static bool IsDefinition(ThreeOperator opType) { return(opType != ThreeOperator.Println && opType != ThreeOperator.Goto && opType != ThreeOperator.IfGoto && opType != ThreeOperator.None); }