Exemple #1
0
 public static ConsNode NewCons(RuntimeStack stack)
 {
     var resNode = new ConsNode();
     resNode.HideFromCallstack = true;
     resNode.Stack = stack;
     return resNode;
 }
Exemple #2
0
        public static void SetupMacroArgs(ConsNode definedArgs, FunctionInvocation call, Scope newScope)
        {
            int i = 1;

            //todo: verify arg list, only identifiers allowed

            //setup local args
            for (int argIndex = 0; argIndex < definedArgs.Args.Count; argIndex++)
            {
                var arg = definedArgs.Args[argIndex] as SymbolNode;

                string argName = arg.Name;

                if (argName == "&optional") {}
                else if (argName == "&rest")
                {
                    argIndex++;
                    var restArg = definedArgs.Args[argIndex] as SymbolNode;
                    string restName = restArg.Name;

                    ConsNode restCons = NewCons(call.StackFrame.Root);
                    restCons.Args = call.Args.Skip(i).ToList();

                    newScope.PushSymbol(restName).Value = restCons;
                }
                else
                {
                    Symbol symbol = newScope.PushSymbol(argName);
                    object bodyArg = call.Args[i];
                    symbol.Value = bodyArg;
                }
                i++;
            }
        }
        public StackFrame(StackFrame previousStackFrame, ConsNode cons)
        {
            PreviousStackFrame = previousStackFrame;
            Cons = cons;

            if (previousStackFrame != null)
            {
                Root = previousStackFrame.Root;
                Scope = previousStackFrame.Scope;
            }
        }
Exemple #4
0
 public void Parse(string code)
 {
     try
     {
         ParseInfo info = SetupParseInfo(code, "user", false);
         root = Parser.Parse(info);
         //     Cons rootCons = Parser.MakeSExpression(root);
     }
     catch
     {
         throw;
     }
 }
Exemple #5
0
        private ConsNode ParseFunction(ParseInfo info, ref int index)
        {
            var funcNode = new ConsNode();
            SetupNode(info, funcNode);
            funcNode.CodeStartIndex = index;

            EnsureToken(info, ref index, "(");
            List<object> args = ParseArgs(info, ref index);
            funcNode.Args = args;
            index++;
            funcNode.CodeLength = index - funcNode.CodeStartIndex;

            return funcNode;
        }
        public override object Clone(CloneInfo info)
        {
            if (info.BackQuote)
            {
                var clone = new ConsNode();
                BaseClone(clone);
                clone.Args = new List<object>(10);
                foreach (object arg in Args)
                {
                    object cloneArg = Utils.Clone(info, arg);
                    if (cloneArg is Splice)
                    {
                        var splice = cloneArg as Splice;
                        foreach (object spliceArg in splice.List)
                        {
                            object cloneSpliceArg = Utils.Clone(info, spliceArg);
                            clone.Args.Add(cloneSpliceArg);
                        }
                    }
                    else
                    {
                        clone.Args.Add(cloneArg);
                    }
                }

                return clone;
            }
            else
            {
                var clone = new ConsNode();
                BaseClone(clone);
                clone.Args = new List<object>(10);
                foreach (object arg in Args)
                {
                    object cloneArg = Utils.Clone(info, arg);
                    clone.Args.Add(cloneArg);
                }

                return clone;
            }
        }
Exemple #7
0
        public override object Clone(CloneInfo info)
        {
            if (info.BackQuote)
            {
                var clone = new ConsNode();
                BaseClone(clone);
                clone.Args = new List <object>(10);
                foreach (object arg in Args)
                {
                    object cloneArg = Utils.Clone(info, arg);
                    if (cloneArg is Splice)
                    {
                        var splice = cloneArg as Splice;
                        foreach (object spliceArg in splice.List)
                        {
                            object cloneSpliceArg = Utils.Clone(info, spliceArg);
                            clone.Args.Add(cloneSpliceArg);
                        }
                    }
                    else
                    {
                        clone.Args.Add(cloneArg);
                    }
                }

                return(clone);
            }
            else
            {
                var clone = new ConsNode();
                BaseClone(clone);
                clone.Args = new List <object>(10);
                foreach (object arg in Args)
                {
                    object cloneArg = Utils.Clone(info, arg);
                    clone.Args.Add(cloneArg);
                }

                return(clone);
            }
        }
Exemple #8
0
        public static void TearDownArgs(ConsNode definedArgs, FunctionInvocation call)
        {
            //int i = 1;
            ////tear down args
            //foreach (IdentifierNode arg in definedArgs.Args)
            //{
            //    string argName = arg.Name;

            //    if (argName.StartsWith("*"))
            //    {
            //        argName = argName.Substring(1);
            //        call.StackFrame.PopSymbol(argName);
            //    }
            //    else if (argName.StartsWith("!"))
            //    {
            //        argName = argName.Substring(1);
            //        call.StackFrame.PopSymbol(argName);
            //    }
            //    else if (argName.StartsWith("#"))
            //    {
            //        argName = argName.Substring(1);
            //        call.StackFrame.PopSymbol(argName);
            //    }
            //    else if (argName.StartsWith("@"))
            //    {
            //        argName = argName.Substring(1);
            //        call.StackFrame.PopSymbol(argName);
            //    }
            //    else if (argName.StartsWith(":"))
            //    {

            //    }
            //    else
            //    {
            //        call.StackFrame.PopSymbol(argName);
            //    }


            //    i++;
            //}

            ////for (int j = i; j < call.Args.Count; j++)
            ////{
            ////    ValueNode node = call.Args[j];
            ////    string argName = string.Format("arg{0}", j);
            ////    stack.PopSymbol(argName);
            ////}
        }
Exemple #9
0
        public static void SetupArgs(ConsNode definedArgs, FunctionInvocation call, Scope newScope)
        {
            int i = 1;

            //todo: verify arg list, only identifiers allowed

            //setup local args
            for (int argIndex = 0; argIndex < definedArgs.Args.Count; argIndex ++)
            {
                var arg = definedArgs.Args[argIndex] as SymbolNode;

                string argName = arg.Name;

                if (argName == "&optional") {}
                else if (argName == "&rest")
                {
                    argIndex ++;
                    var restArg = definedArgs.Args[argIndex] as SymbolNode;
                    string restName = restArg.Name;

                    ConsNode restCons = NewCons(call.StackFrame.Root);
                    restCons.Args = call.EvalArgs(i);

                    newScope.PushSymbol(restName).Value = restCons;
                }
                else if (argName[0] == '*')
                {
                    //ref symbol
                    var argId = call.Args[i] as SymbolNode;
                    argName = argName.Substring(1);
                    if (argId == null)
                    {
                        throw new Exception(
                            string.Format("Argument '{0}' is defined as a pointer, but passed as a value", argName));
                    }

                    Symbol sourceVar = argId.GetSymbol(call.StackFrame);

                    newScope.DeclareRef(argName, sourceVar);
                }
                else if (argName[0] == '!')
                {
                    argName = argName.Substring(1);
                    newScope.PushSymbol(argName);
                    var bodyArg = call.Args[i] as ValueNode;
                    newScope.SetSymbolValue(argName, bodyCall => bodyArg.Eval(call.StackFrame));
                }
                else if (argName[0] == '#')
                {
                    argName = argName.Substring(1);
                    Symbol symbol = newScope.PushSymbol(argName);
                    object bodyArg = call.Args[i];
                    symbol.Value = bodyArg;
                }
                else if (argName[0] == ':')
                {
                    var argId = call.Args[i] as SymbolNode;
                    argName = argName.Substring(1);
                    if (argId == null)
                    {
                        throw new Exception(
                            string.Format("Argument '{0}' is defined as a verbatim, but passed as a value", argName));
                    }
                    if (argId.Name != argName)
                    {
                        throw new Exception(string.Format("Argument '{0}' is defined as a verbatim, but passed as {1}",
                                                          argName, argId.Name));
                    }
                }
                else if (argName[0] == '@')
                {
                    var argId = call.Args[i] as SymbolNode;
                    object argValue = argId.Name;
                    argName = argName.Substring(1);
                    newScope.PushSymbol(argName).Value = argValue;
                }
                else
                {
                    //normal var
                    object argValue = Eval(call.StackFrame, call.Args[i]);
                    newScope.PushSymbol(argName).Value = argValue;
                }
                i++;
            }

            //for (int j = i; j < call.Args.Count; j++)
            //{
            //    ValueNode node = call.Args[j];
            //    string argName = string.Format("arg{0}", j);
            //    object argValue = node.GetValue(stack);
            //    stack.PushSymbol(argName);
            //    stack.Scope.SetSymbolValue(argName, argValue);
            //}
        }
Exemple #10
0
 protected internal virtual void OnEndNotifyCall(StackFrame stackFrame, ConsNode cons, LispFunc func,
                                                 string funcName, object res)
 {
     if (EndNotifyCall != null)
         EndNotifyCall(stackFrame, cons, func, funcName, res);
 }
Exemple #11
0
 protected internal virtual void OnBeginNotifyCall(StackFrame stackFrame, ConsNode cons, LispFunc func,
                                                   string funcName)
 {
     if (BeginNotifyCall != null)
         BeginNotifyCall(stackFrame, cons, func, funcName);
 }
        private ValueNode ParseQuote(ParseInfo info, ref int index)
        {
            var node = new ConsNode();
            node.Args = new List<object>();
            var idNode = new SymbolNode();
            idNode.Name = "quote";
            idNode.CodeStartIndex = index;
            idNode.CodeLength = 1;
            SetupNode(info, idNode);
            node.Args.Add(idNode);

            node.CodeStartIndex = index;
            index++;
            object expression = ParseArg(info, ref index);

            node.Args.Add(expression);

            node.CodeLength = index - node.CodeStartIndex;
            SetupNode(info, node);

            return node;
        }