예제 #1
0
파일: Utils.cs 프로젝트: attila3453/alsing
 public static object Eval(StackFrame stackFrame, object arg)
 {
     if (arg is ValueNode)
         return ((ValueNode) arg).Eval(stackFrame);
     else
         return arg;
 }
예제 #2
0
 public override object Eval(StackFrame stackFrame)
 {
     object res = null;
     foreach (object arg in Args)
     {
         res = Utils.Eval(stackFrame, arg);
     }
     return res;
 }
예제 #3
0
        public object HandleCall(FunctionInvocation call, string methodName)
        {
            var var = call.Args[1] as ValueNode;
            object target = var.Eval(call.StackFrame);
            int paramCount = call.Args.Count - 3;

            MethodInfo[] methods =
                (from mi in
                     target.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public |
                                                 BindingFlags.FlattenHierarchy)
                 where mi.Name == methodName && mi.GetParameters().Length == paramCount
                 select mi).ToArray();

            MethodInfo method = methods[0];

            var methodArgs = new object[paramCount];
            for (int i = 0; i < paramCount; i++)
            {
                object arg = Utils.Eval(call.StackFrame, call.Args[i + 3]);
                if (arg is LispFunc)
                {
                    var func = arg as LispFunc;
                    Type del = method.GetParameters()[i].ParameterType;
                    MethodInfo invoke = del.GetMethod("Invoke");
                    ParameterInfo[] invokeParams = invoke.GetParameters();


                    EventHandler eh = (s, e) =>
                                      {
                                          var args = new List<object>(10);

                                          args.Add(new SymbolNode());
                                          args.Add(s);
                                          args.Add(e);

                                          var newStackFrame = new StackFrame(StackFrame, null);
                                          newStackFrame.Function = func;
                                          newStackFrame.FunctionName = "{native call}";

                                          var kall = new FunctionInvocation(func, args, newStackFrame);
                                          //foreach (ParameterInfo invokeParam in invokeParams)
                                          //{
                                          //    kall.Args.Add(new PrimitiveValueNode<object>(null));
                                          //}
                                          func(kall);
                                      };
                    arg = eh;
                }
                methodArgs[i] = arg;
            }


            object res = method.Invoke(target, methodArgs);
            return res;
        }
예제 #4
0
        public StackFrame(StackFrame previousStackFrame, ConsNode cons)
        {
            PreviousStackFrame = previousStackFrame;
            Cons = cons;

            if (previousStackFrame != null)
            {
                Root = previousStackFrame.Root;
                Scope = previousStackFrame.Scope;
            }
        }
예제 #5
0
        public RuntimeStack()
        {
            AllNodes = new List<ValueNode>();
            OperatorPriority = new Dictionary<LispFunc, int>();
            //  FunctionMeta = new Dictionary<LispFunc, FunctionMeta>();

            StackFrame = new StackFrame(null, null);
            StackFrame.Root = this;
            StackFrame.Scope = new Scope(null);
            StackFrame.Scope.Symbols = new Dictionary<string, Symbol>(1000);
        }
예제 #6
0
 public abstract object Eval(StackFrame stackFrame);
예제 #7
0
 public override object Eval(StackFrame stackFrame)
 {
     throw stackFrame.CreateException("CommaNodes should not be evaluated");
 }
예제 #8
0
        public override object Eval(StackFrame stackFrame)
        {
            if (Args.Count == 0)
                return null;

            object res = null;
            object first = Args[0];
            var func = Utils.Eval(stackFrame, first) as LispFunc;
            if (func != null)
            {
                string name = "";
                if (first is SymbolNode)
                {
                    name = ((SymbolNode) first).Name;
                }

                var newStackFrame = new StackFrame(stackFrame, this);
                newStackFrame.Function = func;
                newStackFrame.FunctionName = name;
                Stack.Engine.OnBeginNotifyCall(newStackFrame, this, func, name);

                //function
                var invocation = new FunctionInvocation(func, Args, newStackFrame);

                try
                {
                    res = func(invocation);
                }
                catch (Exception x)
                {
                    throw newStackFrame.CreateException(x.Message);
                }

                Stack.Engine.OnEndNotifyCall(newStackFrame, this, func, name, res);

                //int recursions = 0;
                //while (res is TailRecursionValue)
                //{
                //    TailRecursionValue tail = res as TailRecursionValue;

                //    if (!tail.ReadyForEval)
                //    {
                //        tail.ReadyForEval = true;
                //        break;
                //    }
                //    else
                //    {
                //        recursions++;

                //        ConsNode tailCons = tail.Expression as ConsNode;

                //        newStackFrame = new StackFrame(stackFrame, this);
                //        newStackFrame.Function = func;
                //        newStackFrame.FunctionName = name;
                //        Stack.Engine.OnBeginNotifyCall(newStackFrame, this, func, name);
                //        invocation.Args = tailCons.Args;
                //        try
                //        {
                //            res = func(invocation);
                //        }
                //        catch (Exception x)
                //        {
                //            throw newStackFrame.CreateException(x.Message);
                //        }

                //        Stack.Engine.OnEndNotifyCall(newStackFrame, this, func, name, res);
                //    }
                //}

                return res;
            }

            throw stackFrame.CreateException(string.Format("Invalid function : {0}", this));
        }
예제 #9
0
파일: Engine.cs 프로젝트: attila3453/alsing
 protected internal virtual void OnEndNotifyCall(StackFrame stackFrame, ConsNode cons, LispFunc func,
                                                 string funcName, object res)
 {
     if (EndNotifyCall != null)
         EndNotifyCall(stackFrame, cons, func, funcName, res);
 }
예제 #10
0
파일: Engine.cs 프로젝트: attila3453/alsing
 protected internal virtual void OnBeginNotifyCall(StackFrame stackFrame, ConsNode cons, LispFunc func,
                                                   string funcName)
 {
     if (BeginNotifyCall != null)
         BeginNotifyCall(stackFrame, cons, func, funcName);
 }
예제 #11
0
 public override object Eval(StackFrame stackFrame)
 {
     return staticSymbol.Value;
 }
 public FunctionInvocation(LispFunc func, List<object> args, StackFrame stackFrame)
 {
     Func = func;
     Args = args;
     StackFrame = stackFrame;
 }
예제 #13
0
 public override object Eval(StackFrame stackFrame)
 {
     return GetSymbol(stackFrame).Value;
 }
예제 #14
0
 public Symbol GetSymbol(StackFrame stackFrame)
 {
     return stackFrame.Scope.GetSymbol(Name);
 }