Exemplo n.º 1
0
        public string RunFuncBase(string funcName, string[] parameters)
        {
            if (funcName == "")
            {
                return(parameters.Length > 0 ? parameters[0] : "");
            }
            switch (funcName)
            {
            case "strcombine":
            {
                string str = "";
                for (int i = 0; i < parameters.Length; i++)
                {
                    str += parameters[i];
                }
                return(str);
            }

            case "cprint":
            {
                string str = "";
                for (int i = 0; i < parameters.Length; i++)
                {
                    str += parameters[i];
                }
                Console.WriteLine(str);
                return(str);
            }

            case "cread":
            {
                return(Console.ReadLine());
            }

            case "static":
            {
                if (parameters.Length == 1)
                {
                    return(stdval.Get(parameters[0], ValDefault));
                }
                else if (parameters.Length > 1)
                {
                    string str = "";
                    for (int i = 1; i < parameters.Length; i++)
                    {
                        str += parameters[i];
                    }
                    stdval.Set(parameters[0], str);
                    return(str);
                }
                else
                {
                    return(ValError);
                }
            }

            case "stackinfo":
            {
                return(StkName(func_invoke_stack, true, " -> "));
            }

            default:
            {
                FuncStatement fs = funcs.Get(funcName);
                if (fs == null)
                {
                    return(ValError);
                }
                func_invoke_stack.Push(funcName);
                string ret = fs.Eval(parameters);
                func_invoke_stack.Pop();
                return(ret);
            }
            }
        }
Exemplo n.º 2
0
        FuncStatement CompileFuncStatement()
        {
            string        funcname = PeekToBlankSym(':', '{');
            FuncStatement func     = new FuncStatement();

            currentfunc = func;
            func_compile_stack.Push(funcname);
            string fullfuncname = StkName(func_compile_stack, true);

            PeekToWord();
            char t = Peek();

            if (t == ':')
            {
                int i = 0;
                while (true)
                {
                    PeekToWord();
                    char p = Peek();
                    if (p == EOF)
                    {
                        throw new SyntaxException("函数的参数列表直到文件结尾都未结束,请检查函数 " + funcname + " 的定义。", compilingPos);
                    }
                    if (p == '{')
                    {
                        break;
                    }
                    compilingPos--;
                    string varname = PeekToBlankSym();
                    func.AddStatement(new SetValStatement(func, varname, new GetValStatement(func, "pmt" + i)));
                    i++;
                }
                PeekToWord();
            }
            else if (t == '{')
            {
                PeekToWord();
            }
            else
            {
                throw new SyntaxException("错误的函数表达式形式。", compilingPos);
            }

            char ch;

            while (true)
            {
                PeekToWord();
                ch = Peek();
                switch (ch)
                {
                case EOF:
                    throw new SyntaxException("函数 " + funcname + " 直到文件结尾都未结束,请检查大括号是否匹配。", compilingPos);

                case '}':
                    funcs.Add(fullfuncname, func);
                    func_compile_stack.Pop();
                    return(func);

                case '[':
                    func.AddStatement(CompileCallFuncStatement());
                    PeekToWord();
                    if (Peek() != ';')
                    {
                        throw new SyntaxException("函数调用结束后仍然出现语句,请检查是否缺少分号。", compilingPos);
                    }
                    break;

                default:
                {
                    string name = ch + PeekToBlankSym('=');
                    PeekToWord();
                    char p = Peek();
                    if (p == EOF)
                    {
                        throw new SyntaxException("不可分析的文件结尾。", compilingPos);
                    }
                    else if (p == '=')
                    {
                        func.AddStatement(CompileSetValStatement(name));
                    }
                    else
                    {
                        compilingPos--;
                        if (name == "func")
                        {
                            CompileFuncStatement();
                            currentfunc = func;
                        }
                    }
                }
                break;
                }
            }
        }
Exemplo n.º 3
0
 public FuncStatement AddStatement(string name, FuncStatement statement)
 {
     funcs.Add(name, statement);
     return(statement);
 }