public BinaryFunc(FuncName name, OpCode opcode, Type returnType) { Name = name; _case = 0; _opcode = opcode; _returnType = returnType; }
public BinaryFunc(FuncName name, Type type, String method_name, Type returnType) { Name = name; _case = 2; _type = type; _methodName = method_name; _returnType = returnType; }
public BinaryFunc(FuncName name, Type type, String method_name, Type[] paramTypes, Type returnType) { Name = name; _case = 3; _type = type; _methodName = method_name; _paramTypes = paramTypes; _returnType = returnType; }
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 InvokeDelegate(Object ID, Delegate del) { ParameterInfo[] parameter_info = del.Method.GetParameters(); Type[] parameter_types = new Type[parameter_info.Length]; foreach(ParameterInfo pi in parameter_info) { if (pi.IsByRefParameter()) throw new ArgumentException("ByRef parameter is not allowed"); parameter_types[pi.Position] = pi.ParameterType; } Name = new FuncName(ID, parameter_types); m_delegate = del; }
public InvokeStatic(Object ID, MethodInfo mi) { _methodInfo = mi; _implictExecutive = false; ParameterInfo[] parameter_info = _methodInfo.GetParameters(); List<Executive.Parameter> parameters = new List<Executive.Parameter>(); foreach (ParameterInfo pi in parameter_info) { if (pi.IsByRefParameter()) throw new ArgumentException("ByRef parameter is not allowed"); Executive.Parameter p = new Executive.Parameter(); p.Type = pi.ParameterType; if (pi.Position == 0 && pi.ParameterType == typeof(Executive)) { object[] attrs = pi.GetCustomAttributes(true); foreach (object atr in attrs) if (atr is ImplictAttribute) { _implictExecutive = true; break; } if (_implictExecutive) continue; } else if (pi.ParameterType.IsArray && pi.Position == parameter_info.Length - 1) { object[] attrs = pi.GetCustomAttributes(true); foreach (object atr in attrs) if (atr is System.ParamArrayAttribute) { p.VariableParam = true; p.Type = pi.ParameterType.GetElementType(); break; } } parameters.Add(p); } Name = new FuncName(ID, parameters.ToArray()); }
internal void CompileExpr(ILGen il, LocalAccess locals, Stack<Type> st, object lval) { if (lval == null) { il.EmitNull(); st.Push(typeof(System.Object)); } else if (Lisp.IsNode(lval)) { if (lval == Lisp.NIL || lval == Lisp.T) { if (lval == Lisp.NIL) il.EmitNull(); else il.Emit(OpCodes.Ldsfld, typeof(RuntimeOps).GetField("True")); st.Push(typeof(System.Object)); } else if (lval == Lisp.INST) { il.Emit(OpCodes.Ldarg_0); il.EmitPropertyGet(typeof(CompiledLambda), "Engine"); st.Push(typeof(Executive)); } else if (lval == Lisp.ARGV) { il.Emit(OpCodes.Ldarg_2); st.Push(typeof(object[])); } else if (lval == Lisp.MPOOL) { il.Emit(OpCodes.Ldarg_3); st.Push(typeof(MemoryPool)); } else { LocalBuilder localVar = locals.GetLocal(lval); if (localVar != null) { il.Emit(OpCodes.Ldloc, localVar); st.Push(localVar.LocalType); } else { Type type = lval.GetType(); if (type == typeof(Integer)) { il.EmitDecimal((Decimal)((Integer)lval)); il.EmitNew(typeof(Integer).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(Decimal) }, null)); } else if (!il.TryEmitConstant(lval, type)) { il.Emit(OpCodes.Ldarg_1); il.EmitInt(locals.DefineConstant(lval)); il.Emit(OpCodes.Ldelem_Ref); if (type.IsValueType) il.EmitUnbox(type); } st.Push(type); } } } else if (Lisp.IsFunctor(lval)) { object head = Lisp.Car(lval); object[] args = Lisp.ToArray(Lisp.Cdr(lval)); ControlFormBase control; if (m_control.TryGetValue(head, out control)) control.Compile(this, lval, il, locals, st, args); else { foreach (object a in args) CompileExpr(il, locals, st, a); Type[] parameterTypes = new Type[args.Length]; for (int k = args.Length - 1; k >= 0; k--) parameterTypes[k] = st.Pop(); FuncName name = new FuncName(head, parameterTypes); FuncBase body = GetFunc(name, false); if (body == null) { bool successed = false; if (parameterTypes.Length == 2) { Type castType = ValueProxy.GetType(parameterTypes[0], parameterTypes[1]); ValueConverter converter1 = FindConverter(parameterTypes[0], castType); ValueConverter converter2 = FindConverter(parameterTypes[1], castType); body = GetFunc(new FuncName(name.ID, new Type[] { castType, castType }), false); if (body != null && converter1 != null && converter2 != null) { LocalBuilder localVar = il.DeclareLocal(parameterTypes[1]); il.Emit(OpCodes.Stloc, localVar); converter1.Compile(this, il, locals, parameterTypes[0]); il.Emit(OpCodes.Ldloc, localVar); il.FreeLocal(localVar); converter2.Compile(this, il, locals, parameterTypes[1]); successed = true; } } if (!successed) { body = GetFunc(name, true); if (body == null) throw new UnknownFuncCall(name.ToString()); else { LocalBuilder[] localVar = new LocalBuilder[parameterTypes.Length]; for (int k = localVar.Length - 1; k >= 0; k--) { localVar[k] = il.DeclareLocal(parameterTypes[k]); il.Emit(OpCodes.Stloc, localVar[k]); } Type[] new_parameter_types = new Type[parameterTypes.Length]; for (int k = 0; k < localVar.Length; k++) { il.Emit(OpCodes.Ldloc, localVar[k]); if (body.Name.GetParameterType(k) != parameterTypes[k]) { if (parameterTypes[k].IsValueType) il.EmitBoxing(parameterTypes[k]); else if (ValueProxy.IsProxyType(parameterTypes[k])) il.EmitPropertyGet(typeof(ValueProxy), "Value"); new_parameter_types[k] = typeof(System.Object); } else new_parameter_types[k] = parameterTypes[k]; il.FreeLocal(localVar[k]); } parameterTypes = new_parameter_types; } } } Type resType = body.Compile(this, il, locals, parameterTypes); 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); } } else if (Lisp.IsFunctor(Lisp.Car(lval), Lisp.LAMBDA)) { object form = Lisp.Car(lval); object body = Lisp.Car(Lisp.Cddr(form)); object tail = Lisp.Cdr(lval); LambdaExpr lambda = new LambdaExpr(null, CreateParameters(Lisp.Arg1(form)), typeof(System.Object), body); List<Type> parameterTypesList = new List<Type>(); foreach (object a in Lisp.getIterator(tail)) { CompileExpr(il, locals, st, a); parameterTypesList.Add(st.Pop()); } Type[] parameterTypes = parameterTypesList.ToArray(); FuncName name = new FuncName(null, parameterTypes); if (!lambda.Name.Match(name, true)) throw new InvalidOperationException("Lambda parameters does not match"); st.Push(lambda.Compile(this, il, locals, parameterTypes)); } else throw new ArgumentException("Unproperly formated expression"); }
private FuncBase GetFunc(FuncName name, bool anyType) { FuncDef def; if (m_func.TryGetValue(name.ID, out def)) return def.FindMatched(name, anyType); else throw new UnknownFuncCall(name.ToString()); }
public bool Match(FuncName name, bool anyType) { if (name.ID == ID) { if (name.Arity == Arity || (VariableLength && Arity < name.Arity)) { for (int k = 0; k < Signature.Length; k++) if (anyType) { if (!Signature[k].IsAssignableFrom(name.Signature[k])) return false; } else { if (Signature[k] != name.Signature[k]) return false; } if (VariableLength && Arity < name.Arity) { int last = Signature.Length - 1; for (int k = Signature.Length; k < name.Arity; k++) if (anyType) { if (!Signature[last].IsAssignableFrom(name.Signature[k])) return false; } else { if (Signature[last] != name.Signature[k]) return false; } } return true; } if (VariableLength && Arity == 1 && name.Arity == 0) return true; } return false; }
public bool Equals(FuncName name) { if (name.ID == ID && name.Arity == Arity && name.VariableLength == VariableLength) { for (int k = 0; k < Signature.Length; k++) if (Signature[k] != name.Signature[k]) return false; return true; } else return false; }
public NotOperator() { Name = new FuncName(Funcs.Not, new Type[] { typeof(System.Object) }); }
public NullFunc(FuncName name) { Name = name; }