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); } }
//public abstract Converters.Z3.Library Library { get; } public T Convert(expr e, Func <ident, T> identmap) { this.identmap = identmap; var ret = router.Visit(this, e); if (ret == null) { throw new ArgumentNullException(); } return(ret); }
public static Type TypeCheck(this expr e) { var c = e as constant; if (c != null) { if (c.integer.HasValue) { return(Type.integer); } if (c.boolean.HasValue) { return(Type.boolean); } throw new InvalidOperationException(); } var v = e as variable; if (v != null) { return(v.Untyped.Annotation <DeclAnnotation>().reference.type.ToType()); } var b = e as block; if (b != null) { Type t = Type.error; foreach (expr x in b.expr) { t = x.TypeCheck(); } if (t == Type.error) { throw new InvalidOperationException(); } return(t); } var a = e as assign; if (a != null) { var t = a.Untyped.Annotation <DeclAnnotation>().reference.type.ToType(); if (t != a.rhs.TypeCheck()) { throw new InvalidOperationException(); } return(t); } throw new InvalidOperationException(); }
internal Expr ConvertToExpr(expr e, Dictionary <string, Expr> varInfo) { if (e is boolconst) { return(((boolconst)e).val ? smt.True : smt.False); } else if (e is charconst) { return(smt.MkNumeral(((charconst)e).val, smt.CharSort)); } else if (e is ident) { ident i = (ident)e; if (!varInfo.ContainsKey(i.name)) { throw new BekParseException(i.line, i.pos, string.Format("undefined '{0}'", i.name)); } return(varInfo[i.name]); } else if (e is strconst) { return(smt.MkListFromString(((strconst)e).val, smt.CharSort)); } else { functioncall f = e as functioncall; //must be != null if (f == null) { throw new BekException("Internal error, unexpected bek expression '{0}'"); } try { Expr[] args = Array.ConvertAll(f.args.ToArray(), t => ConvertToExpr(t, varInfo)); var res = smt.Library.ApplyFunction(f.id.name, args); return(res); } catch (BekParseException bek_error) { throw bek_error; } catch (Exception inner) { //add the line info throw new BekParseException(f.id.line, f.id.pos, inner); } } }
Expr MkExpr(BekTypes btype, expr e) { switch (btype) { case BekTypes.BOOL: return(((boolconst)e).val ? stb.Solver.True : stb.Solver.False); case BekTypes.CHAR: return(stb.Solver.MkNumeral(((charconst)e).val, charsort)); case BekTypes.STR: return(stb.Solver.MkListFromString(((strconst)e).val, charsort)); default: throw new BekException(); //TBD: introduce exception kinds } }
public static int Calculate(string exp) //算术表达式求值的算符优先算法。设OPTR和OPND分别为运算符栈和运算数栈 { expr S = new expr(exp); //将传过来的表达式打包成一个类对象,便于计算 string C = string.Empty; int oper1, oper2; string theta = string.Empty; Stack OPTR = new Stack(); OPTR.Push("#"); Stack OPND = new Stack(); C = S.GetS(); //取表达式里的分量 while (C != "#" || OPTR.Peek().ToString() != "#") { if (!In(C)) //不是运算符则进栈 { OPND.Push(C); C = S.GetS(); } else { switch (Precede(OPTR.Peek().ToString(), C)) { case "<": //栈顶元素优先权低 OPTR.Push(C); C = S.GetS(); break; case "=": //脱括号并接收下一个字符 OPTR.Pop(); C = S.GetS(); break; case ">": //退栈并将运算结果入栈 theta = OPTR.Pop().ToString(); oper2 = Convert.ToInt32(OPND.Pop()); oper1 = Convert.ToInt32(OPND.Pop()); OPND.Push(Operate(oper1, theta, oper2).ToString()); break; } //switch } } //while return(Convert.ToInt32(OPND.Peek())); }
public static void Resolve(this expr e, block scope) { if (e is constant) { return; } var v = e as variable; if (v != null) { var d = scope.Lookup(v.id); v.Untyped.AddAnnotation( new DeclAnnotation { reference = d }); return; } var b = e as block; if (b != null) { b.Untyped.AddAnnotation( new BlockAnnotation { reference = scope }); foreach (expr x in b.expr) { x.Resolve(b); } return; } var a = e as assign; if (a != null) { var d = scope.Lookup(a.id); a.Untyped.AddAnnotation( new DeclAnnotation { reference = d }); a.rhs.Resolve(scope); return; } }
internal Raise(RaiseStatement stmt) : this() { if (stmt.ExceptType != null) _type = Convert(stmt.ExceptType); if (stmt.Value != null) _inst = Convert(stmt.Value); if (stmt.Traceback != null) _tback = Convert(stmt.Traceback); }
public Return(ReturnStatement statement) : this() { // statement.Expression is never null //or is it? if (statement.Expression == null) _value = null; else _value = Convert(statement.Expression); }
internal Index(expr expr) : this() { _value = expr; }
internal ListComp(ListComprehension comp) : this() { _elt = Convert(comp.Item); _generators = Convert(comp.Iterators); }
public ListExpr(slice <Expr> ElemList = default, expr expr = default) { this.ElemList = ElemList; this.m_exprRef = new ptr <expr>(expr); }
private Type[] GetExprTypes(expr expr) { if (expr is expr_list) { return (expr as expr_list).list.Select(e => GetExprType(e)).ToArray(); } else if (expr == null) { return null; } else { return new Type[] { GetExprType(expr) }; } }
public static int Mul(expr x, expr y) { return(x.Const * y.Const); }
internal Yield(YieldExpression expr) : this() { // expr.Expression is never null _value = Convert(expr.Expression); }
public STModel Convert(expr e, Func <ident, STModel> strexprmapping, Symtab stab) { this.strexprmapping = strexprmapping; this.stab = stab; return(router.Visit(this, e, false)); }
public static int Add(expr x, expr y) { return(x.Const + y.Const); }
public ParenExpr(Expr X = default, expr expr = default) { this.X = X; this.m_exprRef = new ptr <expr>(expr); }
public (float X, float Y, float Z) EvalVector(InvocationExpression expr, GlobalSymbolTable symbols) => ((BasicIntrinsicType)Type).EvalVector(expr, symbols);
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); } } }
internal Subscript(IndexExpression expr, expr_context ctx) : this() { _value = Convert(expr.Target); AST index = Convert(expr.Index); if (index is expr) if (index is Tuple && ((TupleExpression)expr.Index).IsExpandable) _slice = new ExtSlice(((Tuple)index).elts); else _slice = new Index((expr)index); else if (index is slice) // includes Ellipsis _slice = (slice)index; else throw new ArgumentTypeException("Unexpected index expression: " + expr.Index.GetType()); _ctx = ctx; }
// For 7ø14 public static int Sub(expr x, expr y) { return(x.Const - y.Const); }
internal While(WhileStatement stmt) : this() { _test = Convert(stmt.Test); _body = ConvertStatements(stmt.Body); _orelse = ConvertStatements(stmt.ElseStatement, true); }
// For 7ø15 public static int Div(expr x, expr y) { return(safeDivResult(x.Const, y.Const)); }
internal keyword(IronPython.Compiler.Ast.Arg arg) : this() { _arg = arg.Name; _value = Convert(arg.Expression); }
AddExprStmtToTree(expr, parentItem);
private void GenerateExpr(expr expr, ILGenerator ilGenerator) { if (expr is int_lit) { int_lit e = expr as int_lit; ilGenerator.Emit(OpCodes.Ldc_I4, e.value); } else if (expr is float_lit) { float_lit e = expr as float_lit; ilGenerator.Emit(OpCodes.Ldc_R4, e.value); } else if (expr is bool_lit) { bool_lit e = expr as bool_lit; ilGenerator.Emit(OpCodes.Ldc_I4, (e.value ? 1 : 0)); } else if (expr is string_lit) { string_lit e = expr as string_lit; ilGenerator.Emit(OpCodes.Ldstr, e.value); } else if (expr is byte_lit) { byte_lit e = expr as byte_lit; ilGenerator.Emit(OpCodes.Ldc_I4, e.value); } else if (expr is getvar) { getvar e = expr as getvar; LoadLocal(e.name, ilGenerator); } }
public KeyValueExpr(Expr Key = default, Expr Value = default, expr expr = default) { this.Key = Key; this.Value = Value; this.m_exprRef = new ptr <expr>(expr); }
public InterfaceType(slice <ptr <Field> > MethodList = default, expr expr = default) { this.MethodList = MethodList; this.m_exprRef = new ptr <expr>(expr); }
public AssertExpr(Expr X = default, Expr Type = default, expr expr = default) { this.X = X; this.Type = Type; this.m_exprRef = new ptr <expr>(expr); }
internal Lambda(LambdaExpression lambda) : this() { FunctionDef def = (FunctionDef)Convert(lambda.Function); _args = def.args; Debug.Assert(def.body.Count == 1, "LambdaExpression body should be one Return statement."); _body = ((Return)def.body[0]).value; }
public BasicLit(@string Value = default, LitKind Kind = default, bool Bad = default, expr expr = default) { this.Value = Value; this.Kind = Kind; this.Bad = Bad; this.m_exprRef = new ptr <expr>(expr); }
internal Print(PrintStatement stmt) : this() { if (stmt.Destination != null) _dest = Convert(stmt.Destination); _values = PythonOps.MakeEmptyList(stmt.Expressions.Count); foreach (Compiler.Ast.Expression expr in stmt.Expressions) _values.Add(Convert(expr)); _nl = !stmt.TrailingComma; }
public CallExpr(Expr Fun = default, slice <Expr> ArgList = default, bool HasDots = default, expr expr = default) { this.Fun = Fun; this.ArgList = ArgList; this.HasDots = HasDots; this.m_exprRef = new ptr <expr>(expr); }
internal Repr(BackQuoteExpression expr) : this() { _value = Convert(expr.Expression); }
=> SumOfFractions(expr, leftNum, leftDen, -rightNum, rightDen),
public Slice(SliceExpression expr) : this() { if (expr.SliceStart != null) _lower = Convert(expr.SliceStart); if (expr.SliceStop != null) _upper = Convert(expr.SliceStop); if (expr.StepProvided && expr.SliceStep != null) _step = Convert(expr.SliceStep); }
public TypeSwitchGuard(ref ptr <Name> Lhs = default, Expr X = default, expr expr = default) { this.Lhs = Lhs; this.X = X; this.m_exprRef = new ptr <expr>(expr); }
internal UnaryOp(UnaryExpression expression) : this() { _op = (unaryop)Convert(expression.Op); _operand = Convert(expression.Expression); }
internal Assign(AssignmentStatement stmt) : this() { _targets = PythonOps.MakeEmptyList(stmt.Left.Count); foreach (Compiler.Ast.Expression expr in stmt.Left) _targets.Add(Convert(expr, Store.Instance)); _value = Convert(stmt.Right); }
internal With(WithStatement with) : this() { _context_expr = Convert(with.ContextManager); if (with.Variable != null) _optional_vars = Convert(with.Variable); _body = ConvertStatements(with.Body); }
internal AugAssign(AugmentedAssignStatement stmt) : this() { _target = Convert(stmt.Left, Store.Instance); _value = Convert(stmt.Right); _op = (@operator)Convert(stmt.Operator); }
internal comprehension(ComprehensionFor listFor, ComprehensionIf[] listIfs) : this() { _target = Convert(listFor.Left, Store.Instance); _iter = Convert(listFor.List); _ifs = PythonOps.MakeEmptyList(listIfs.Length); foreach (ComprehensionIf listIf in listIfs) _ifs.Add(Convert(listIf.Test)); }
public SliceExpr(Expr X = default, array <Expr> Index = default, bool Full = default, expr expr = default) { this.X = X; this.Index = Index; this.Full = Full; this.m_exprRef = new ptr <expr>(expr); }
internal Assert(AssertStatement stmt) : this() { _test = Convert(stmt.Test); if (stmt.Message != null) _msg = Convert(stmt.Message); }
public ChanType(ChanDir Dir = default, Expr Elem = default, expr expr = default) { this.Dir = Dir; this.Elem = Elem; this.m_exprRef = new ptr<expr>(expr); }
internal Attribute(MemberExpression attr, expr_context ctx) : this() { _value = Convert(attr.Target); _attr = attr.Name; _ctx = ctx; }
internal BinOp(BinaryExpression expr, @operator op) : this() { _left = Convert(expr.Left); _right = Convert(expr.Right); _op = op; }
public BinOp(expr left, @operator op, expr right, [Optional]int? lineno) : this() { _left = left; _op = op; _right = right; if (lineno != null) this.lineno = lineno.Value; }
public DotsType(Expr Elem = default, expr expr = default) { this.Elem = Elem; this.m_exprRef = new ptr <expr>(expr); }
internal Call(CallExpression call) : this() { _args = PythonOps.MakeEmptyList(call.Args.Count); _keywords = new PythonList(); _func = Convert(call.Target); foreach (IronPython.Compiler.Ast.Arg arg in call.Args) { if (arg.Name == null) _args.Add(Convert(arg.Expression)); else if (arg.Name == "*") _starargs = Convert(arg.Expression); else if (arg.Name == "**") _kwargs = Convert(arg.Expression); else _keywords.Add(new keyword(arg)); } }
internal For(ForStatement stmt) : this() { _target = Convert(stmt.Left, Store.Instance); _iter = Convert(stmt.List); _body = ConvertStatements(stmt.Body); _orelse = ConvertStatements(stmt.Else, true); }
public Name(@string Value = default, expr expr = default) { this.Value = Value; this.m_exprRef = new ptr <expr>(expr); }
internal GeneratorExp(GeneratorExpression expr) : this() { ExtractListComprehensionIterators walker = new ExtractListComprehensionIterators(); expr.Function.Body.Walk(walker); ComprehensionIterator[] iters = walker.Iterators; Debug.Assert(iters.Length != 0, "A generator expression cannot have zero iterators."); iters[0] = new ComprehensionFor(((ComprehensionFor)iters[0]).Left, expr.Iterable); _elt = Convert(walker.Yield.Expression); _generators = Convert(iters); }
private Type GetExprType(expr e) { if (e is int_lit) { return typeof(int); } else if (e is float_lit) { return typeof(float); } else if (e is bool_lit) { return typeof(bool); } else if (e is byte_lit) { return typeof(byte); } else if (e is string_lit) { return typeof(string); } else if (e is getvar) { return GetVarType(e); } else if (e is function_call) { function_call stmt = e as function_call; return GetMethodInfo(stmt.name, null, null, GetMethodTypes(stmt.name, null, GetExprTypes(stmt.arg))).ReturnType; } else if (e == null) { return null; } else { throw new Exception("Cannot find argument type"); } }
internal void Initialize(IfStatementTest ifTest) { _test = Convert(ifTest.Test); _body = ConvertStatements(ifTest.Body); }
private Type GetVarType(expr e) { throw new NotImplementedException(); }
internal IfExp(ConditionalExpression cond) : this() { _test = Convert(cond.Test); _body = Convert(cond.TrueExpression); _orelse = Convert(cond.FalseExpression); }