Пример #1
0
        public virtual TreeNode OnDefine(TreeNode [] args)
        {
            if(args.Length < 2 || args.Length > 3) {
                throw new ArgumentException("define must have two or three arguments");
            }

            if(!(args[0] is FunctionNode)) {
                throw new ArgumentException("first define argument must be a variable");
            }

            FunctionNode function = new FunctionNode((args[0] as FunctionNode).Function, args[args.Length - 1]);

            if(args.Length == 3 && args[1].HasChildren) {
                foreach(TreeNode function_arg in args[1].Children) {
                    if(!(function_arg is FunctionNode)) {
                        throw new ArgumentException("define function arguments must be variable tokens");
                    }

                    function.RegisterFunction((function_arg as FunctionNode).Function, new VoidLiteral());
                }
            }

            TreeNode parent = args[0].Parent;
            parent = parent.Parent ?? parent;

            parent.RegisterFunction(function.Function, function);

            return new VoidLiteral();
        }
Пример #2
0
 internal void RegisterFunction(string name, FunctionNode function)
 {
     if(functions.ContainsKey(name)) {
         functions[name] = function;
     } else {
         functions.Add(name, function);
     }
 }
Пример #3
0
 internal void RegisterFunction(string name, object value)
 {
     if(functions.ContainsKey(name)) {
         functions[name] = new FunctionNode(name, value);
     } else {
         functions.Add(name, new FunctionNode(name, value));
     }
 }
Пример #4
0
        private TreeNode EvaluateFunction(FunctionNode node)
        {
            object handler = null;

            TreeNode parent = node.Parent;

            TreeNode [] args = null;

            if (!functions.ContainsKey(node.Function))
            {
                handler = ResolveFunction(node);
                if (handler == null)
                {
                    throw new InvalidFunctionException(node.Function);
                }
            }
            else
            {
                handler = functions[node.Function];
            }

            if (parent.Children[0] == node)
            {
                args = new TreeNode[parent.ChildCount - 1];

                for (int i = 0; i < args.Length; i++)
                {
                    args[i] = parent.Children[i + 1];

                    if (handler is MethodInfoContainer && !(handler as MethodInfoContainer).EvaluateVariables)
                    {
                        continue;
                    }
                }
            }

            if (handler is FunctionNode)
            {
                return((handler as FunctionNode).Evaluate(this, args));
            }
            else if (handler is SExpFunctionHandler)
            {
                return(((SExpFunctionHandler)handler)(this, args));
            }
            else if (handler is MethodInfoContainer)
            {
                MethodInfoContainer container = (MethodInfoContainer)handler;
                return((TreeNode)container.MethodInfo.Invoke(container.Object, new object [] { args }));
            }
            else
            {
                throw new InvalidFunctionException(String.Format(
                                                       "Unknown runtime method handler type {1}", handler.GetType()));
            }
        }
Пример #5
0
 internal void RegisterFunction(string name, FunctionNode function)
 {
     if (functions.ContainsKey(name))
     {
         functions[name] = function;
     }
     else
     {
         functions.Add(name, function);
     }
 }
Пример #6
0
 internal void RegisterFunction(string name, object value)
 {
     if (functions.ContainsKey(name))
     {
         functions[name] = new FunctionNode(name, value);
     }
     else
     {
         functions.Add(name, new FunctionNode(name, value));
     }
 }
Пример #7
0
        internal FunctionNode ResolveFunction(FunctionNode node)
        {
            TreeNode shift_node = node;

            do
            {
                if (shift_node.Functions.ContainsKey(node.Function))
                {
                    return(shift_node.Functions[node.Function]);
                }

                shift_node = shift_node.Parent;
            } while(shift_node != null);

            return(null);
        }
        public virtual TreeNode OnForeach(TreeNode [] args)
        {
            TreeNode list = Evaluate(args[1]);

            CheckList(list);
            FunctionNode item_variable = (FunctionNode)args[0];
            TreeNode     function      = args[2];

            TreeNode self = args[0].Parent.Children[0];

            self.Parent.RegisterFunction(item_variable.Function, item_variable);

            foreach (TreeNode child in list.Children)
            {
                item_variable.Body = child;

                try {
                    if (function is FunctionNode)
                    {
                        FunctionNode function_impl = Evaluator.ResolveFunction(function as FunctionNode);
                        function_impl.Evaluate(Evaluator, new TreeNode [] { child });
                    }
                    else
                    {
                        Evaluate(function);
                    }
                } catch (Exception e) {
                    if (ControlFunctionSet.BreakHandler(e))
                    {
                        break;
                    }
                }
            }

            return(new VoidLiteral());
        }
Пример #9
0
        internal FunctionNode ResolveFunction(FunctionNode node)
        {
            TreeNode shift_node = node;

            do {
                if(shift_node.Functions.ContainsKey(node.Function)) {
                    return shift_node.Functions[node.Function];
                }

                shift_node = shift_node.Parent;
            } while(shift_node != null);

            return null;
        }
Пример #10
0
        private TreeNode EvaluateFunction(FunctionNode node)
        {
            object handler = null;

            TreeNode parent = node.Parent;
            TreeNode [] args = null;

            if(!functions.ContainsKey(node.Function)) {
                handler = ResolveFunction(node);
                if(handler == null) {
                    throw new InvalidFunctionException(node.Function);
                }
            } else {
                handler = functions[node.Function];
            }

            if(parent.Children[0] == node) {
                args = new TreeNode[parent.ChildCount - 1];

                for(int i = 0; i < args.Length; i++) {
                    args[i] = parent.Children[i + 1];

                    if(handler is MethodInfoContainer && !(handler as MethodInfoContainer).EvaluateVariables) {
                        continue;
                    }
                }
            }

            if(handler is FunctionNode) {
                return (handler as FunctionNode).Evaluate(this, args);
            } else if(handler is SExpFunctionHandler) {
                return ((SExpFunctionHandler)handler)(this, args);
            } else if(handler is MethodInfoContainer) {
                MethodInfoContainer container = (MethodInfoContainer)handler;
                return (TreeNode)container.MethodInfo.Invoke(container.Object, new object [] { args });
            } else {
                throw new InvalidFunctionException(String.Format(
                    "Unknown runtime method handler type {1}", handler.GetType()));
            }
        }
Пример #11
0
        void TokenPush(bool as_string)
        {
            if (current_token.Length == 0 && !as_string)
            {
                return;
            }

            string token = current_token.ToString();

            if (Debug)
            {
                Console.Write("{3}[{0}] TOKEN({4},{5}): [{2}{1}{2}]", scope, token,
                              as_string ? "\"" : string.Empty, string.Empty.PadLeft(scope - 1, ' '),
                              line, column - current_token.Length);
            }


            TreeNode node;

            if (as_string)
            {
                node = new StringLiteral(token);
            }
            else if (token == "#t")
            {
                node = new BooleanLiteral(true);
            }
            else if (token == "#f")
            {
                node = new BooleanLiteral(false);
            }
            else if (token.Length > 0 && token != "." && token != "-" &&
                     token != "+" && number_regex.IsMatch(token))
            {
                try {
                    if (token.StartsWith("0x") || token.StartsWith("-0x"))
                    {
                        int offset = token[0] == '-' ? 3 : 2;
                        int value  = int.Parse(token.Substring(offset),
                                               NumberStyles.HexNumber, culture_info.NumberFormat);
                        node = new IntLiteral(value * (offset == 3 ? -1 : 1));
                    }
                    else if (token.Contains("."))
                    {
                        node = new DoubleLiteral(double.Parse(token,
                                                              NumberStyles.Float, culture_info.NumberFormat));
                    }
                    else
                    {
                        node = new IntLiteral(int.Parse(token,
                                                        NumberStyles.Integer, culture_info.NumberFormat));
                    }
                } catch {
                    throw new FormatException("Invalid number format: " + token);
                }
            }
            else
            {
                node = new FunctionNode(token);
            }

            if (Debug)
            {
                Console.WriteLine(" => [{0}]", node);
            }

            node.Line   = line;
            node.Column = column;
            current_parent.AddChild(node);

            current_token.Remove(0, current_token.Length);
        }
Пример #12
0
        private void TokenPush(bool as_string)
        {
            if(current_token.Length == 0 && !as_string) {
                return;
            }

            TreeNode node = null;
            string token = current_token.ToString();

            if(Debug) {
                Console.Write("{3}[{0}] TOKEN({4},{5}): [{2}{1}{2}]", scope, token,
                    as_string ? "\"" : String.Empty, String.Empty.PadLeft(scope - 1, ' '),
                    line, column - current_token.Length);
            }

            if(as_string) {
                node = new StringLiteral(token);
            } else if(token == "#t") {
                node = new BooleanLiteral(true);
            } else if(token == "#f") {
                node = new BooleanLiteral(false);
            } else if(token.Length > 0 && token != "." && token != "-" &&
                token != "+" && number_regex.IsMatch(token)) {
                try {
                    if(token.StartsWith("0x") || token.StartsWith("-0x")) {
                        int offset = token[0] == '-' ? 3 : 2;
                        int value = Int32.Parse(token.Substring(offset),
                            NumberStyles.HexNumber, culture_info.NumberFormat);
                        node = new IntLiteral(value * (offset == 3 ? -1 : 1));
                    } else if(token.Contains(".")) {
                        node = new DoubleLiteral(Double.Parse(token,
                            NumberStyles.Float, culture_info.NumberFormat));
                    } else {
                        node = new IntLiteral(Int32.Parse(token,
                            NumberStyles.Integer, culture_info.NumberFormat));
                    }
                } catch {
                    throw new FormatException("Invalid number format: " + token);
                }
            } else {
                node = new FunctionNode(token);
            }

            if(Debug) {
                Console.WriteLine(" => [{0}]", node);
            }

            node.Line = line;
            node.Column = column;
            current_parent.AddChild(node);

            current_token.Remove(0, current_token.Length);
        }