Esempio n. 1
0
        public void Excute(string script)
        {
            var rs = Lexer.Analyzer(script);

            _stack = new VariableStack();

            //初始化全局变量
            foreach (var token in rs.Where(it => it.TokenType == TokenType.AssignmentDefine).Cast <TokenAssignment>())
            {
                ExcuteAssignment(token);
            }

            //获取所有函数定义
            _functions = rs.Where(it => it.TokenType == TokenType.FunctionDefine)
                         .Cast <TokenFunctionDefine>()
                         .ToDictionary(it => it.Name, it => it);

            if (!_functions.ContainsKey("main"))
            {
                throw new Exception("没有找到main方法");
            }

            var main = _functions["main"];

            ExcuteFunction(main);
            _stack.Pop(_stack.Count());
        }
Esempio n. 2
0
        /// <summary>
        ///     执行代码块
        /// </summary>
        /// <param name="block"></param>
        private ScriptValue ExcuteBlock(TokenBlockStement block)
        {
            var size = _stack.Count();

            try
            {
                ScriptValue value = null;
                foreach (var stement in block.Stements)
                {
                    if (stement is TokenFunctionCallStement call)
                    {
                        ExcuteCall(call);
                    }
                    else if (stement is TokenAssignment assignment)
                    {
                        ExcuteAssignment(assignment);
                    }
                    else if (stement is TokenConditionBlock condition)
                    {
                        value = ExcuteCondition(condition);
                        if (value != null)
                        {
                            break;
                        }
                    }
                    else if (stement is TokenReturnStement returnStement)
                    {
                        value = ExcuteReturnStement(returnStement);
                        break;
                    }
                }

                return(value);
            }
            finally
            {
                _stack.Pop(_stack.Count() - size);
            }
        }