private System.Type TypeOfExpr(expr expr) { if (expr is StringLiteral) { return(typeof(string)); //setting the type of expr to type string } else if (expr is IntLiteral) { return(typeof(int)); //type to int } else if (expr is Variable) { Variable var = (Variable)expr; if (this.symbolTable.ContainsKey(var.Ident)) { Emit.LocalBuilder locb = symbolTable[var.Ident]; //loacal variable assigned the value of symbol table at key var.Ident return(locb.LocalType); } else { throw new System.Exception("undeclared variable '" + var.Ident + "'"); } } else if (expr is BinExpr) { BinExpr var = (BinExpr)expr; return(typeof(int)); } else { throw new System.Exception("don't know how to calculate the type of " + expr.GetType().Name); } }
private void GenExpr(expr expr, System.Type expectedType) { System.Type deliveredType; if (expr is StringLiteral) { deliveredType = typeof(string); this.il.Emit(Emit.OpCodes.Ldstr, ((StringLiteral)expr).Value); //pushes the value of passed expression on string type stack // il.EmitWriteLine("hello"); } else if (expr is IntLiteral) { deliveredType = typeof(int); this.il.Emit(Emit.OpCodes.Ldc_I4, ((IntLiteral)expr).Value); //pushes the value of passed expression on integer type stack } else if (expr is Variable) { string ident = ((Variable)expr).Ident; deliveredType = this.TypeOfExpr(expr); if (!this.symbolTable.ContainsKey(ident)) { throw new System.Exception("undeclared variable '" + ident + "'"); } this.il.Emit(Emit.OpCodes.Ldloc, this.symbolTable[ident]); } else if (expr is BinExpr) { BinExpr val = new BinExpr(); val = (BinExpr)expr; deliveredType = typeof(int); whole.Add(val.Left); counter++; //MessageBox.Show(whole.Count().ToString()); whole.Add(val.Op); //counters++; //MessageBox.Show(whole.Count().ToString()); if ((TypeOfExpr((expr)val.Right) == typeof(int) && (!(val.Right is BinExpr))) || val.Right is Variable) { whole.Add(val.Right); //MessageBox.Show(whole.Count().ToString()); ADD_EMIT(); } else { this.GenExpr(val.Right, this.TypeOfExpr(val.Right)); } /*if (val.Left is IntLiteral) * * this.il.Emit(Emit.OpCodes.Ldc_I4, ((IntLiteral)val.Left).Value);//pushes the value of passed expression on integer type stack * else * { * string ident = ((Variable)val.Left).Ident; * Emit.LocalBuilder locb = this.symbolTable[ident]; * this.il.Emit(Emit.OpCodes.Ldloc, locb); * } * * * if ( (TypeOfExpr((expr)val.Right)== typeof(int) && (! (val.Right is BinExpr))) || val.Right is Variable) * { * * * * MessageBox.Show("blah blah"); * * if (val.Right is IntLiteral) * * this.il.Emit(Emit.OpCodes.Ldc_I4, ((IntLiteral)val.Right).Value);//pushes the value of passed expression on integer type stack * else * { * string ident = ((Variable)val.Right).Ident; * Emit.LocalBuilder locb = this.symbolTable[ident]; * this.il.Emit(Emit.OpCodes.Ldloc, locb); * } * * if ((val.Op).Equals(BinOp.add)) * { * this.il.Emit(Emit.OpCodes.Add); * } * else if ((val.Op).Equals(BinOp.minus)) * { * this.il.Emit(Emit.OpCodes.Sub); * } * else if ((val.Op).Equals(BinOp.multiply)) * { * this.il.Emit(Emit.OpCodes.Mul); * } * else if ((val.Op).Equals(BinOp.divide)) * { * this.il.Emit(Emit.OpCodes.Div); * } * * } * else * { * this.GenExpr(val.Right, this.TypeOfExpr(val.Right)); * }*/ } else { throw new System.Exception("don't know how to generate " + expr.GetType().Name); } if (deliveredType != expectedType) //type cheking { if (deliveredType == typeof(int) && expectedType == typeof(string)) { this.il.Emit(Emit.OpCodes.Box, typeof(int)); this.il.Emit(Emit.OpCodes.Callvirt, typeof(object).GetMethod("ToString")); //parsing integer to string //il.EmitCall(Emit.OpCodes.new } else { throw new System.Exception("can't coerce a " + deliveredType.Name + " to a " + expectedType.Name); } } }