Exemplo n.º 1
0
        /// <summary>
        /// Executes a single line of code
        /// </summary>
        /// <param name="codeLine"></param>
        /// <returns></returns>
        public TFunctionData Execute(Line codeLine)
        {
            TFunctionData data = new TFunctionData(this);

            Execute(data, codeLine);
            return(data);
        }
Exemplo n.º 2
0
        private void HandleError(Line current, TFunctionData stackFrame, Exception ex)
        {
            TbasicException cEx = ex as TbasicException;

            if (cEx != null)
            {
                int    status = cEx.Status;
                string msg    = stackFrame.Data as string;
                if (string.IsNullOrEmpty(msg))
                {
                    msg = cEx.Message;
                }
                stackFrame.Status = status;
                stackFrame.Data   = msg;
                stackFrame.Context.SetReturns(stackFrame);
                if (ThrowError)
                {
                    throw new LineException(current.LineNumber, current.VisibleName, cEx);
                }
            }
            else
            {
                throw new LineException(current.LineNumber, current.VisibleName, ex);
            }
        }
Exemplo n.º 3
0
        internal TFunctionData Execute(LineCollection lines)
        {
            TFunctionData stackFrame = new TFunctionData(this);

            for (int index = 0; index < lines.Count; index++)
            {
                if (BreakRequest)
                {
                    break;
                }
                Line current = lines[index];
                CurrentLine = current.LineNumber;
                try {
                    ObjectContext blockContext = Context.FindBlockContext(current.Name);
                    if (blockContext == null)
                    {
                        Execute(stackFrame, current);
                    }
                    else
                    {
                        CodeBlock block = blockContext.GetBlock(current.Name).Invoke(index, lines);
                        Context = Context.CreateSubContext();
                        block.Execute(this);
                        Context = Context.Collect();
                        index  += block.Length - 1; // skip the length of the executed block
                    }
                }
                catch (Exception ex) {
                    HandleError(current, stackFrame, ex);
                }
            }
            return(stackFrame);
        }
Exemplo n.º 4
0
        private object ExecuteFunction(StringSegment _name, IList <object> l_params)
        {
            string name = _name.Trim().ToString();

            object[] a_evaluated = null;
            if (l_params != null)
            {
                a_evaluated = new object[l_params.Count];
                l_params.CopyTo(a_evaluated, 0);
                for (int i = 0; i < a_evaluated.Length; ++i)
                {
                    IEvaluator expr = a_evaluated[i] as IEvaluator;
                    if (expr != null)
                    {
                        a_evaluated[i] = expr.Evaluate();
                    }
                }
            }
            ObjectContext context = CurrentContext.FindFunctionContext(name);

            if (context == null)
            {
                throw ThrowHelper.UndefinedFunction(name);
            }
            else
            {
                TFunctionData _sframe = new TFunctionData(CurrentExecution);
                _sframe.SetAll(a_evaluated);
                _sframe.Name = name;
                context.GetFunction(name).Invoke(_sframe);
                CurrentContext.SetReturns(_sframe);
                return(_sframe.Data);
            }
        }
Exemplo n.º 5
0
 internal void SetReturns(TFunctionData _sframe)
 {
     SetVariable("@lasterror", _sframe.Status);
     SetVariable("@lasterr", _sframe.Status);
     SetVariable("@err", _sframe.Status);
     SetVariable("@error", _sframe.Status);
 }
Exemplo n.º 6
0
        internal void PersistReturns(TFunctionData _sframe)
        {
            ObjectContext context = FindVariableContext("@error");

            if (context != null)
            {
                _sframe.Status = (int)context.GetVariable("@error");
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Copies all properties of another StackFrame into this one
        /// </summary>
        /// <param name="other"></param>
        public void CopyFrom(TFunctionData other)
        {
            TFunctionData clone = other.Clone();

            StackExecuter = clone.StackExecuter;
            Text          = clone.Text;
            _params       = clone._params;
            Status        = clone.Status;
            Data          = clone.Data;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Clones this StackFrame
        /// </summary>
        /// <returns>A new StackFrame object with the same data</returns>
        public TFunctionData Clone()
        {
            TFunctionData clone = new TFunctionData(StackExecuter);

            clone.Text = Text;
            if (_params == null)
            {
                clone._params = new List <object>();
            }
            else
            {
                clone._params.AddRange(_params);
            }
            clone.Status = Status;
            clone.Data   = Data;
            return(clone);
        }
Exemplo n.º 9
0
        internal static void Execute(TFunctionData stackFrame, Line codeLine)
        {
            ObjectContext context = stackFrame.Context.FindCommandContext(codeLine.Name);

            if (context == null)
            {
                Evaluator eval   = new Evaluator(new StringSegment(codeLine.Text), stackFrame.StackExecuter);
                object    result = eval.Evaluate();
                stackFrame.Context.PersistReturns(stackFrame);
                stackFrame.Data = result;
            }
            else if (codeLine.IsFunction)
            {
                throw ThrowHelper.ExpectedSpaceAfterCommand();
            }
            else
            {
                stackFrame.SetAll(codeLine.Text);
                context.GetCommand(codeLine.Name).Invoke(stackFrame);
            }
            stackFrame.Context.SetReturns(stackFrame);
        }