public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args) { if (args.Length != 2) throw new ArgumentException("Unproperly formated cast expression"); engine.CompileExpr(il, locals, st, args[0]); Type type = args[1] as Type; if (type == null) throw new ArgumentException("Expecting type value"); Type curr_type = st.Pop(); if (curr_type.IsValueType) { if (type == typeof(System.Object)) il.EmitBoxing(curr_type); else if (curr_type != type) throw new InvalidCastException(); } else { il.Emit(OpCodes.Isinst, type); il.Emit(OpCodes.Dup); Label ok = il.DefineLabel(); il.Emit(OpCodes.Brtrue_S, ok); il.EmitCall(_raiseInvalidCast); il.MarkLabel(ok); if (type.IsValueType) il.EmitUnbox(type); } st.Push(type); }
public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args) { il.Emit(OpCodes.Ldarg_1); il.EmitInt(locals.DefineConstant(args[0])); il.Emit(OpCodes.Ldelem_Ref); st.Push(typeof(System.Object)); }
public override Type Compile(Executive engine, ILGen il, LocalAccess locals, Type[] parameterTypes) { for (int k = 0; k < parameterTypes.Length; k++) il.Emit(OpCodes.Pop); il.EmitFieldGet(typeof(DBNull), "Value"); return typeof(DBNull); }
public CompiledLambda(Executive executive, Executive.Parameter[] parameters) { Engine = executive; Arity = parameters.Length; Parameters = parameters; dynamicMethod = new DynamicMethod("", typeof(System.Object), new Type[] { typeof(CompiledLambda), typeof(object[]), typeof(object[]), typeof(MemoryPool) }, GetType().Module); }
public override object MacroExpand(Executive engine, object[] lval, ref bool proceed) { object[] args = new object[lval.Length]; for (int k = 0; k < args.Length; k++) { args[k] = Lisp.List(engine.MacroExpand(Lisp.First(lval[k]), ref proceed), engine.MacroExpand(Lisp.Second(lval[k]), ref proceed)); } return Lisp.Functor(ID, args); }
public FuncName(Object id, Executive.Parameter[] parameters) { ID = id; int len = parameters.Length; Signature = new Type[len]; for (int k = 0; k < len; k++) Signature[k] = parameters[k].Type; if (len > 0 && parameters[len - 1].VariableParam) VariableLength = true; }
public LambdaExpr(object id, Executive.Parameter[] parameters, Type returnType, object body) { Name = new FuncName(id, parameters); _parameters = parameters; _retType = returnType; _body = body; _compiledBody = new FunctionLink(); _compiled = false; Isolate = true; }
public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args) { if (args.Length != 1) throw new ArgumentException("Unproperly formated expression"); for (int k = 0; k < st.Count; k++) il.Emit(OpCodes.Pop); engine.CompileExpr(il, locals, st, args[0]); st.Pop(); il.Emit(OpCodes.Ret); st.Push(typeof(System.Object)); }
public override void Bind(Executive.Parameter[] parameters, MemoryPool pool) { HashSet<SymbolLink> outerValues = new HashSet<SymbolLink>(QueryContext.Resolver.List()); m_body.Bind(parameters, pool); m_params = new List<SymbolLink>(); m_body.GetValueDependences(null, parameters, false, (SymbolLink value) => { if (!value.IsStatic && outerValues.Contains(value)) m_params.Add(value); }); }
public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args) { if (args.Length != 1 || !Lisp.IsAtom(args[0])) throw new ArgumentException("Unproperly formated expression"); SymbolLink link = engine.Get(args[0]); il.Emit(OpCodes.Ldarg_3); il.Emit(OpCodes.Ldarg_1); il.EmitInt(locals.DefineConstant(link)); il.Emit(OpCodes.Ldelem_Ref); il.EmitCall(m_memoryPoolGetData); st.Push(typeof(System.Object)); }
public override object MacroExpand(Executive engine, object[] lval, ref bool proceed) { object[] arg = Lisp.ToArray(lval[0]); for (int k = 0; k < arg.Length; k++) { object[] pair = Lisp.ToArray(arg[k]); pair[1] = engine.MacroExpand(pair[1], ref proceed); arg[k] = Lisp.List(pair); } return Lisp.List(ID, Lisp.List(arg), engine.MacroExpand(lval[1], ref proceed)); }
public override Type Compile(Executive engine, ILGen il, LocalAccess locals, Type[] parameterTypes) { Label l_true = il.DefineLabel(); Label l_false = il.DefineLabel(); il.Emit(OpCodes.Brfalse_S, l_true); il.EmitNull(); il.Emit(OpCodes.Br_S, l_false); il.MarkLabel(l_true); il.Emit(OpCodes.Ldsfld, typeof(RuntimeOps).GetField("True")); il.MarkLabel(l_false); return typeof(System.Object); }
public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args) { for (int k = 0; k < args.Length; k++) { engine.CompileExpr(il, locals, st, args[k]); if (k > 0) { st.Pop(); il.Emit(OpCodes.Pop); } } }
public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args) { Type resType = null; List<Branch> branches = new List<Branch>(); for (int k = 0; k < args.Length; k++) { Label next = il.DefineLabel(); object cur = args[k]; if (!Lisp.IsCons(cur)) throw new ArgumentException("Unproperly formated expression"); object[] pair = Lisp.ToArray(cur); if (pair.Length != 2) throw new ArgumentException("Unproperly formated expression"); engine.CompileExpr(il, locals, st, pair[0]); if (st.Pop() != typeof(System.Object)) throw new ArgumentException("Expecting boolean value"); il.Emit(OpCodes.Brfalse, next); engine.CompileExpr(il, locals, st, pair[1]); Type type = st.Pop(); Branch branch = GetBranch(branches, il, type); il.Emit(OpCodes.Stloc, branch.localVar); il.Emit(OpCodes.Br, branch.handler); il.MarkLabel(next); if (resType == null) resType = type; else if (resType != type) resType = typeof(System.Object); } il.EmitCall(_raiseError); Label end = il.DefineLabel(); for (int k = 0; k < branches.Count; k++) { Branch b = branches[k]; il.MarkLabel(b.handler); il.Emit(OpCodes.Ldloc, b.localVar); il.FreeLocal(b.localVar); if (resType != b.type) { if (ValueProxy.IsProxyType(b.type)) il.EmitPropertyGet(typeof(ValueProxy), "Value"); else if (b.type.IsValueType) il.Emit(OpCodes.Box, b.type); } if (k < branches.Count -1) il.Emit(OpCodes.Br, end); } il.MarkLabel(end); st.Push(resType); }
public void Set(Row row, Executive exec) { if (!data.IsBinded) exec.DefaultPool.Bind(data); // We are not expecting DBNull and address to itemArray directly exec.DefaultPool.SetData(data, DBNull.Value); Row nested_row = (Row)row.ItemArray[rnum]; if (nested_row != null) { object value = nested_row.ItemArray[fieldType.Ordinal]; if (value != null) exec.DefaultPool.SetData(data, value); } }
public override void Bind(Executive.Parameter[] parameters, MemoryPool pool) { value = QueryContext.Engine.TryGet(name, false, true); if (value == null) { if (parameters != null) { arg_index = 0; foreach (Executive.Parameter p in parameters) { if (p.ID == name) return; arg_index++; } } value = QueryContext.Engine.TryGet(name, true, false); if (value == null) throw new ValueNotDefined(name.ToString()); } }
public override void CreateVariables(Executive engine, object form, ILGen il, LocalAccess locals, object[] args) { if (args.Length == 0) throw new ArgumentException("Unproperly formated expression"); locals.DeclareScope(form); locals.EnterScope(form, _activeScope); foreach (object arg in Lisp.getIterator(args[0])) { object[] pair = Lisp.ToArray(arg); if (pair.Length != 2 || !Lisp.IsAtom(pair[0])) throw new ArgumentException("Unproperly formated expression"); locals.DeclareLocal(pair[0]); engine.CreateVariables(il, locals, pair[1]); } if (!_activeScope) locals.ActivateScope(); for (int k = 1; k < args.Length; k++) engine.CreateVariables(il, locals, args[k]); locals.LeaveScope(); }
public override Type Compile(Executive engine, ILGen il, LocalAccess locals, Type[] parameterTypes) { LocalBuilder[] localVar = new LocalBuilder[Name.Arity]; for (int k = Name.Arity -1; k >= 0; k--) { localVar[k] = il.DeclareLocal(Name.Signature[k]); il.Emit(OpCodes.Stloc, localVar[k]); } il.Emit(OpCodes.Ldarg_1); il.EmitInt(locals.DefineConstant(m_delegate)); il.Emit(OpCodes.Ldelem_Ref); il.Emit(OpCodes.Isinst, m_delegate.GetType()); for (int k = 0; k < Name.Arity; k++) { il.Emit(OpCodes.Ldloc, localVar[k]); il.FreeLocal(localVar[k]); } il.EmitCall(m_delegate.GetType(), "Invoke"); return m_delegate.Method.GetReturnType(); }
public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args) { if (args.Length != 2) throw new ArgumentException("Unproperly formated expression"); engine.CompileExpr(il, locals, st, args[0]); if (st.Pop() != typeof(System.Object)) throw new ArgumentException("Expecting boolean value"); Label l_false = il.DefineLabel(); il.Emit(OpCodes.Brfalse, l_false); engine.CompileExpr(il, locals, st, args[1]); if (st.Pop() != typeof(System.Object)) throw new ArgumentException("Expecting boolean value"); Label end = il.DefineLabel(); il.Emit(OpCodes.Brfalse_S, l_false); il.Emit(OpCodes.Ldsfld, typeof(RuntimeOps).GetField("True")); il.Emit(OpCodes.Br_S, end); il.MarkLabel(l_false); il.EmitNull(); il.MarkLabel(end); st.Push(typeof(System.Object)); }
public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args) { LambdaExpr lambda = new LambdaExpr(null, locals.Parameters, typeof(System.Object), Lisp.Arg1(form)); lambda.Isolate = false; Type[] parameterTypes = new Type[locals.Parameters.Length]; for (int k = 0; k < parameterTypes.Length; k++) { //LocalBuilder local = locals.GetLocal(locals.Parameters[k].ID); //parameterTypes[k] = local.LocalType; //il.Emit(OpCodes.Ldloc, local); parameterTypes[k] = locals.Parameters[k].Type; il.Emit(OpCodes.Ldarg_2); il.EmitInt(k); il.Emit(OpCodes.Ldelem_Ref); il.Emit(OpCodes.Isinst, parameterTypes[k]); if (parameterTypes[k].IsValueType) il.EmitUnbox(parameterTypes[k]); } st.Push(lambda.Compile(engine, il, locals, parameterTypes)); }
public override object Execute(Executive engine, object[] lval, out bool proceed) { DataResolver resolver = (DataResolver)engine.CurrentResolver(); object res = Lisp.Cons(ID.XmlValueConcat); object tail = res; if (lval.Length == 0) { foreach (ColumnBinding b in resolver.Bindings) { Lisp.Rplacd(tail, Lisp.Cons(Lisp.List(ID.XmlValueElem, b.Name, Lisp.List(Funcs.Weak, ATOM.Create(null, new string[] { b.TableName, b.Name }, false)), XmlValueOption.EmptyOnNull))); tail = Lisp.Cdr(tail); } } else foreach (object o in lval) { if (Lisp.IsFunctor(o, DataSelector.Table)) { string tableName = (string)Lisp.Second(o); XmlValueOption option = (XmlValueOption)Lisp.Third(o); foreach (ColumnBinding b in resolver.Bindings) if (b.TableName.Equals(tableName)) { Lisp.Rplacd(tail, Lisp.Cons(Lisp.List(ID.XmlValueElem, b.Name, Lisp.List(Funcs.Weak, ATOM.Create(null, new string[] { b.TableName, b.Name }, false)), option))); tail = Lisp.Cdr(tail); } } else { Lisp.Rplacd(tail, Lisp.Cons(o)); tail = Lisp.Cdr(tail); } } proceed = true; return res; }
public override Type Compile(Executive engine, ILGen il, LocalAccess locals, Type[] parameterTypes) { switch (_case) { case 0: il.Emit(_opcode); break; case 1: il.Emit(_opcode, _type); break; case 2: il.EmitCall(_type, _methodName); break; case 3: il.EmitCall(_type, _methodName, _paramTypes); break; } return _returnType; }
public override void Bind(Executive.Parameter[] parameters, MemoryPool pool) { m_context = new SymbolLink(typeof(IContextProvider)); pool.Bind(m_context); object data = QueryContext.Resolver.GetCurrentStack(); QueryContext.Resolver.SetValue(ID.Context, m_context); m_compiledBody = new FunctionLink[m_expr.Length]; for (int k = 0; k < m_expr.Length; k++) { m_compiledBody[k] = new FunctionLink(); QueryContext.Engine.Compile(parameters, m_expr[k], m_compiledBody[k]); } if (Annotation != null) { m_compiledAnnotation = new FunctionLink[Annotation.Length]; for (int k = 0; k < Annotation.Length; k++) { m_compiledAnnotation[k] = new FunctionLink(); QueryContext.Engine.Compile(parameters, Annotation[k], m_compiledAnnotation[k]); } } QueryContext.Resolver.RevertToStack(data); }
public override void Bind(Executive.Parameter[] parameters, MemoryPool pool) { m_valueExpr = new FunctionLink(); QueryContext.Engine.Compile(parameters, m_expr, m_valueExpr); m_value.IsStatic = true; SymbolLink[] depends = QueryContext.Engine.GetValueDependences(null, parameters, m_expr, m_valueExpr, false); foreach (SymbolLink s in depends) if (!s.IsStatic) { m_value.IsStatic = false; break; } object data = QueryContext.Resolver.GetCurrentStack(); pool.Bind(m_value); QueryContext.Resolver.SetValue(m_var, m_value); if (ConditionExpr != null) { m_conditionExpr = new FunctionLink(); QueryContext.Engine.Compile(parameters, ConditionExpr, m_conditionExpr); } m_bodyExpr.Bind(parameters, pool); QueryContext.Resolver.RevertToStack(data); }
public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args) { locals.EnterScope(form, _activeScope); foreach (object arg in Lisp.getIterator(args[0])) { object[] pair = Lisp.ToArray(arg); engine.CompileExpr(il, locals, st, pair[1]); LocalBuilder localvar = locals.BindLocal(pair[0], st.Pop()); il.Emit(OpCodes.Stloc, localvar); } if (!_activeScope) locals.ActivateScope(); for (int k = 1; k < args.Length; k++) { engine.CompileExpr(il, locals, st, args[k]); if (k < args.Length - 1) { st.Pop(); il.Emit(OpCodes.Pop); } } locals.DestroyScope(); }
public override bool IsContextSensitive(Executive.Parameter[] parameters) { for (int k = 0; k < m_expr.Length; k++) { SymbolLink[] dps = QueryContext.Engine.GetValueDependences(null, parameters, m_expr[k], m_compiledBody[k], false); foreach (SymbolLink s in dps) if (s == m_context) return true; } if (Annotation != null) { for (int k = 0; k < Annotation.Length; k++) { SymbolLink[] dps = QueryContext.Engine.GetValueDependences(null, parameters, Annotation[k], m_compiledAnnotation[k], false); foreach (SymbolLink s in dps) if (s == m_context) return true; } } return false; }
public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args) { if (args.Length < 2) throw new ArgumentException(); engine.CompileExpr(il, locals, st, args[0]); Type inst = st.Pop(); il.Emit(OpCodes.Castclass, inst); String name = args[1].ToString(); MethodInfo methodInfo; if (args.Length > 2) { Type[] parameters = new Type[args.Length - 2]; ParameterModifier modifier = new ParameterModifier(parameters.Length); LocalBuilder[] localVar = new LocalBuilder[parameters.Length]; for (int k = 0; k < args.Length - 2; k++) { engine.CompileExpr(il, locals, st, args[k + 2]); parameters[k] = st.Pop(); localVar[k] = il.DeclareLocal(parameters[k]); il.Emit(OpCodes.Stloc, localVar[k]); } methodInfo = inst.GetMethod(name, parameters, new ParameterModifier[] { modifier }); if (methodInfo == null) throw new ArgumentException("Method not found in class", name); ParameterInfo[] pi = methodInfo.GetParameters(); for (int k = 0; k < pi.Length; k++) { il.Emit(OpCodes.Ldloc, localVar[k]); if (pi[k].ParameterType != parameters[k] && parameters[k].IsValueType) il.Emit(OpCodes.Box, parameters[k]); il.FreeLocal(localVar[k]); } } else { methodInfo = inst.GetMethod(name, null); if (methodInfo == null) throw new ArgumentException("Method not found in class", name); } il.Emit(OpCodes.Callvirt, methodInfo); Type resType = methodInfo.GetReturnType(); if (resType == typeof(System.Boolean)) { // Implict boolean convertion Label l_false = il.DefineLabel(); Label end = il.DefineLabel(); il.Emit(OpCodes.Brfalse_S, l_false); il.Emit(OpCodes.Ldsfld, typeof(RuntimeOps).GetField("True")); il.Emit(OpCodes.Br_S, end); il.MarkLabel(l_false); il.EmitNull(); il.MarkLabel(end); st.Push(typeof(System.Object)); } else if (resType == typeof(void)) { // assume that all procedures returns t il.Emit(OpCodes.Ldsfld, typeof(RuntimeOps).GetField("True")); st.Push(typeof(System.Object)); } else st.Push(resType); }
public virtual bool IsContextSensitive(Executive.Parameter[] parameters) { return false; }
public virtual void GetValueDependences(HashSet<Object> hs, Executive.Parameter[] parameters, bool reviewLambdaExpr, Action<SymbolLink> callback) { foreach (FunctionLink dynFunc in EnumDynamicFuncs()) foreach (SymbolLink value in QueryContext.Engine.GetValueDependences(hs, parameters, null, dynFunc, reviewLambdaExpr)) callback(value); }
public override void Bind(Executive.Parameter[] parameters, MemoryPool pool) { m_compiledExpr = new FunctionLink(); QueryContext.Engine.Compile(parameters, m_expr, m_compiledExpr); m_bodyExpr.Bind(parameters, pool); }