示例#1
0
 public void toString(StringBuilder sb)
 {
     if (this.original_type == Token.Token_Type.Token_Id)
     {
         sb.Append(this.value);
     }
     else if (this.original_type == Token.Token_Type.Token_Int)
     {
         sb.Append(this.value);
     }
     else if (this.original_type == Token.Token_Type.Token_Prevent)
     {
         sb.Append("'").Append(this.value);
     }
     else if (this.original_type == Token.Token_Type.Token_String)
     {
         sb.Append(Util.stringToEscape(this.value, '"', '"', null));
     }
     else
     {
         sb.Append(value[0]);
         for (Node <Exp> tmp = children; tmp != null; tmp = tmp.Rest())
         {
             tmp.First().toString(sb);
             sb.Append(" ");
         }
         sb.Append(value[1]);
     }
 }
示例#2
0
 public void toString(StringBuilder sb)
 {
     toString(sb, first,true);
     for (Node<T> tmp = rest; tmp != null; tmp = tmp.Rest())
     {
         sb.Append(" ");
         toString(sb, tmp.First(),true);
     }
 }
示例#3
0
 Object run(Exp exp)
 {
     if (exp.Exp_type() == Exp.Exp_Type.Exp_Small)
     {
         if (exp.Children() != null)
         {
             Exp t = exp.Children().First();
             if (t.Exp_type() == Exp.Exp_Type.Exp_Id && t.Value() == "let")
             {
                 Node <Exp> rst = exp.Children().Rest();
                 while (rst != null)
                 {
                     Exp key = rst.First();
                     rst = rst.Rest();
                     Exp value = rst.First();
                     rst = rst.Rest();
                     Object vas = interpret(value, scope);
                     scope = match(scope, key, vas);
                 }
                 return(null);
             }
             else
             {
                 try
                 {
                     return(interpret(exp, scope));
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine(exp.ToString());
                     throw ex;
                 }
             }
         }
         else
         {
             return(interpret(exp, scope));
         }
     }
     else
     {
         return(interpret(exp, scope));
     }
 }
示例#4
0
        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        public Object exec(Exp exp)
        {
            Object ret = null;

            for (Node <Exp> tmp = exp.Children(); tmp != null; tmp = tmp.Rest())
            {
                ret = run(tmp.First());
            }
            return(ret);
        }
示例#5
0
 public static Node<T> reverse(Node<T> list)
 {
     Node<T> r = null;
     while (list != null)
     {
         r = new Node<T>(list.First(), r);
         list = list.Rest();
     }
     return r;
 }
示例#6
0
 public static Object kvs_find1st(Node<Object> kvs, String k)
 {
     if (kvs == null)
     {
         return null;
     }
     else
     {
         String key = (String)kvs.First();
         kvs = kvs.Rest();
         if (key == k)
         {
             return kvs.First();
         }
         else
         {
             return kvs_find1st(kvs.Rest(), k);
         }
     }
 }
示例#7
0
        String getPath(Node <Object> scope)
        {
            String        path = null;
            Node <Object> tmp  = scope;

            while (tmp != null && path == null)
            {
                String key = tmp.First() as String;
                tmp = tmp.Rest();
                if (key == "pathOf")
                {
                    if (tmp.First() is Function)
                    {
                        Function pathOf = tmp.First() as Function;
                        path = pathOf.exec(null) as String;
                    }
                }
                tmp = tmp.Rest();
            }
            return(path);
        }
示例#8
0
        Node <Object> calNode(Node <Exp> list, Node <Object> scope)
        {
            Node <Object> r = null;

            for (Node <Exp> x = list; x != null; x = x.Rest())
            {
                r = Node <object> .extend(
                    interpret(x.First(), scope),
                    r
                    );
            }
            return(r);
        }
示例#9
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            for (Node <Stack> tmp = stacks; tmp != null; tmp = tmp.Rest())
            {
                Stack stack = tmp.First();
                sb.Append(stack.path).Append("\t").Append(stack.loc.ToString()).Append("\t").Append(stack.exp).Append("\r\n");
            }
            sb.Append(loc.ToString()).Append("\r\n");
            sb.Append(base.ToString()).Append("\r\n");
            return(sb.ToString());
        }
示例#10
0
 Node <Object> when_bracket_match(Node <Object> scope, Node <Exp> keys, Node <Object> values)
 {
     while (keys != null)
     {
         Exp    key   = keys.First();
         Object value = null;
         if (keys.Rest() == null && isWait(key))
         {
             String subvk = key.Value().Substring(3);
             scope = when_normal_match(scope, subvk, values, key.Loc());
         }
         else
         {
             if (values != null)
             {
                 value  = values.First();
                 values = values.Rest();
             }
             scope = match(scope, key, value);
         }
         keys = keys.Rest();
     }
     return(scope);
 }
示例#11
0
        public static Exp Parse(Node <Token> tokens)
        {
            Location   root_loc = new Location(0, 0, 0);
            Exp        exp      = new Exp(Exp_Type.Exp_Large, "{}", root_loc, Token.Token_Type.Token_BracketRight, null, null);
            Node <Exp> caches   = Node <Exp> .extend(exp, null);

            Node <Token> xs       = tokens;
            Node <Exp>   children = null;

            while (xs != null)
            {
                Token x = xs.First();
                xs = xs.Rest();
                if (x.Token_type() == Token.Token_Type.Token_BracketRight)
                {
                    Exp.Exp_Type tp;
                    String       v = "";
                    if (x.Value() == ")")
                    {
                        tp = Exp_Type.Exp_Small;
                        v  = "()";
                    }
                    else if (x.Value() == "]")
                    {
                        tp = Exp_Type.Exp_Medium;
                        v  = "[]";
                    }
                    else
                    {
                        tp = Exp_Type.Exp_Large;
                        v  = "{}";
                    }
                    Exp cache = new Exp(tp, v, x.Loc(), x.Token_type(), children, null);
                    caches = Node <Exp> .extend(cache, caches);

                    children = null;
                }
                else if (x.Token_type() == Token.Token_Type.Token_BracketLeft)
                {
                    Exp        cache      = caches.First();
                    Node <Exp> r_children = null;
                    if (cache.Exp_type() != Exp_Type.Exp_Large)
                    {
                        r_children = Node <Exp> .reverse(children);
                    }

                    children = Node <Exp> .extend(
                        new Exp(
                            cache.Exp_type(),
                            cache.Value(),
                            cache.Loc(),
                            x.Token_type(),
                            children,
                            r_children
                            ),
                        cache.Children()
                        );

                    caches = caches.Rest();
                }
                else
                {
                    Exp_Type tp   = 0;
                    bool     deal = true;
                    if (x.Token_type() == Token.Token_Type.Token_String)
                    {
                        tp = Exp_Type.Exp_String;
                    }
                    else if (x.Token_type() == Token.Token_Type.Token_Int)
                    {
                        tp = Exp_Type.Exp_Int;
                    }
                    else
                    {
                        Exp parent = caches.First();
                        if (parent.Exp_type() == Exp_Type.Exp_Medium)
                        {
                            if (x.Token_type() == Token.Token_Type.Token_Prevent)
                            {
                                tp = Exp_Type.Exp_Id;
                            }
                            else if (x.Token_type() == Token.Token_Type.Token_Id)
                            {
                                tp = Exp_Type.Exp_String;
                            }
                            else
                            {
                                deal = false;
                            }
                        }
                        else
                        {
                            if (x.Token_type() == Token.Token_Type.Token_Prevent)
                            {
                                tp = Exp_Type.Exp_String;
                            }
                            else if (x.Token_type() == Token.Token_Type.Token_Id)
                            {
                                tp = Exp_Type.Exp_Id;
                            }
                            else
                            {
                                deal = false;
                            }
                        }
                    }

                    if (deal)
                    {
                        Node <String> kvs_path = null;
                        if (tp == Exp_Type.Exp_Id)
                        {
                            kvs_path = isKVS_path(x.Value(), x.Loc());
                        }
                        children = Node <Exp> .extend(
                            new Exp(tp, x.Value(), x.Loc(), x.Token_type(), kvs_path),
                            children
                            );
                    }
                }
            }
            return(new Exp(Exp_Type.Exp_Large, "{}", root_loc, Token.Token_Type.Token_BracketLeft, children, null));
        }
示例#12
0
        Object interpret(Exp exp, Node <Object> scope)
        {
            if (exp.Exp_type() == Exp.Exp_Type.Exp_Small)
            {
                Node <Object> children = calNode(exp.R_children(), scope);
                Object        o        = children.First();

                if (o is Function)
                {
                    try
                    {
                        return(((Function)o).exec(children.Rest()));
                    }
                    catch (LocationException lex)
                    {
                        lex.addStack(getPath(scope), exp.Loc(), exp.ToString());
                        throw lex;
                    }
                    catch (Exception ex)
                    {
                        throw error_throw("函数执行内部错误" + ex.Message, exp, scope, children);
                    }
                }
                else
                {
                    if (o == null)
                    {
                        throw error_throw("未找到函数定义", exp, scope, children);
                    }
                    else
                    {
                        throw error_throw("不是函数", exp, scope, children);
                    }
                }
            }
            else if (exp.Exp_type() == Exp.Exp_Type.Exp_Medium)
            {
                return(calNode(exp.R_children(), scope));
            }
            else if (exp.Exp_type() == Exp.Exp_Type.Exp_Large)
            {
                return(new UserFunction(exp, scope));
            }
            else if (exp.Exp_type() == Exp.Exp_Type.Exp_String)
            {
                return(exp.Value());
            }
            else if (exp.Exp_type() == Exp.Exp_Type.Exp_Int)
            {
                return(int.Parse(exp.Value()));
            }
            else if (exp.Exp_type() == Exp.Exp_Type.Exp_Id)
            {
                Node <String> paths = exp.KVS_paths();
                if (paths == null)
                {
                    throw match_Exception(scope, exp.Value() + "不是合法的ID类型:\t" + exp.ToString(), exp.Loc());
                }
                else
                {
                    Node <Object> c_scope = scope;
                    Object        value   = null;
                    while (paths != null)
                    {
                        String key = paths.First();
                        value = Node <Object> .kvs_find1st(c_scope, key);

                        paths = paths.Rest();
                        if (paths != null)
                        {
                            if (value == null || value is Node <Object> )
                            {
                                c_scope = value as Node <Object>;
                            }
                            else
                            {
                                throw match_Exception(scope, "计算" + paths.ToString() + ",其中" + value + "不是kvs类型:\t" + exp.ToString(), exp.Loc());
                            }
                        }
                    }
                    return(value);
                }
            }
            else
            {
                return(null);
            }
        }
示例#13
0
        public override object exec(Node <object> args)
        {
            String path = args.First() as String;

            return(calculate(path));
        }