Esempio n. 1
0
        private ScriptResult _EvaluateExpression(IExpr expr, bool createTempScope, bool hardTerminal = false)
        {
            if (null == expr)
            {
                LogError("Cannot evaluate null expression.");
                return(null);
            }

            StackState stackState = defaultContext.stack.GetState();

            if (createTempScope)
            {
                if (!defaultContext.stack.PushTerminalScope("<Evaluate>", defaultContext, hardTerminal))
                {
                    LogError("_EvaluateExpression: stack overflow");
                    return(null);
                }
            }

            ScriptResult result = new ScriptResult();

            result.value = expr.Evaluate(defaultContext);

            if (defaultContext.IsRuntimeErrorSet())
            {
                defaultContext.stack.RestoreState(stackState);
                if (logCompileErrors)
                {
                    LogError(defaultContext.GetRuntimeErrorString());
                }
                result.runtimeError = defaultContext.control.runtimeError;
                result.success      = false;
                defaultContext.control.Clear();
            }
            else
            {
                result.success = true;
                if (createTempScope)
                {
                    defaultContext.stack.RestoreState(stackState);
                    defaultContext.control.Clear();
                }
            }

            return(result);
        }
Esempio n. 2
0
        // Call this after creating the class and adding members to it.
        public bool FinalizeClass(ExecContext context)
        {
            Pb.Assert(0 == context.control.flags);

            // Only initializing the members that this class has added is a cool idea, but
            // leaves us f****d when it comes to overrridden functions.
            // So, I'm being lazy here and initializing them all.
            for (int ii = 0; ii < _memberFuncs.Count; ++ii)
            {
                ClassMember member = _memberFuncs.Get(ii);

                if (null != member.initializer && (member.initializer is Expr_Literal || member.initializer is Expr_Value))
                {
                    // Make sure vftableVars has a slot for this function.
                    while (vftableVars.Count < ii + 1)
                    {
                        vftableVars.Add(null);
                    }

                    object initValue = member.initializer.Evaluate(context);
                    if (context.IsRuntimeErrorSet())
                    {
                        return(false);
                    }

                    // Create the variable for the function.
                    vftableVars[ii] = new Variable(member.name, member.typeDef, initValue);
                }
            }

            // Initialize the static members. Populates the staticVars list.
            for (int ii = 0; ii < _statics.Count; ++ii)
            {
                ClassMember member    = _statics.Get(ii);
                object      initValue = null;
                if (null != member.initializer)
                {
                    initValue = member.initializer.Evaluate(context);
                    if (context.IsRuntimeErrorSet())
                    {
                        context.engine.LogCompileError(ParseErrorType.StaticMemberEvaluationError, name + "::" + member.name + " - " + context.GetRuntimeErrorString());
                        return(false);
                    }
                }
                staticVars[ii] = new Variable(member.name, member.typeDef, initValue);
            }

            return(true);
        }