コード例 #1
0
        /*
         * O_NEXT_KEY
         * sp:   unchanged
         * in:   S[top-2] = arrayref
         * out:  R = arrayref->cindex
         * op:   if (arrayref->cindex >= arrayref->size) {
         *        R = END-OF-READ;
         *      }
         *      else {
         *        R = arrayref->cindex; arrayref->cindex++;
         *      }
         */

        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // get the array reference
            IValue tableValue = ev.Stack.ReadN(ev.Stack.StackTop - 2);

            // work with arrays only
            if (tableValue.TypeOf() == ValueTypeID.TYPE_TABLEREF)
            {
                ValueTable table = (ValueTable)tableValue.GetObjectValue();

                ValueTableItem vti = table.Next();
                if (vti == null)
                {
                    ev.RegR = new DoneValue();
                }
                else
                {
                    ev.RegR = new StringValue(vti.Key);
                }
            }
            else
            {
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_EXPTYPETABLEREF));
            }
        }
コード例 #2
0
ファイル: IsInOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // second operand (table) is in R
            IValue tableValue = ev.GetVal();

            if (tableValue.TypeOf() != ValueTypeID.TYPE_TABLEREF)
            {
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_EXPTYPETABLEREF));
            }

            // first operand (key)
            IValue key = ev.GetStackTopVal();

            ev.Stack.Pop(); // pop the key

            ValueTable table = (ValueTable)tableValue.GetObjectValue();

            if (table.Search(key.GetStringValue()) != null)
            {
                ev.RegR = new BooleanValue(true);
            }
            else
            {
                ev.RegR = new BooleanValue(false);
            }
        }
コード例 #3
0
ファイル: UnlinkOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // returnning from a function = going one run level up
            ev.DecreaseRunLevel();

            // free all localy created data (==GBC==)
            // -- not needed --

            // restore stack pointer
            ev.Stack.StackTop = ev.RegFP;

            // get old FP value
            IValue oldFPValue = ev.Stack.ReadTop();

            ev.Stack.Pop();

            if (oldFPValue.TypeOf() == ValueTypeID.TYPE_STFP)
            {
                ev.RegFP = oldFPValue.GetIntValue();
            }
            else
            {
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_EXPTYPESTFP));
            }
        }
コード例 #4
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // r = "filename"
            ev.GetVal();
            string fileName = ev.RegR.GetStringValue();

            // store the return value
            OpCodeListItem rtsa = ev.RegPC;

            // load and compile the new source
            ev.State.ImportFile(fileName);

            // execute the subprogram
            ev.SubEval(ev.State);

            // only the END state should remain
            if (ev.ProgramState != ProgramStateID.END)
            {
                ev.ProgramState = ProgramStateID.RUNNING;
            }

            // return from the subprogram to the calling program
            ev.RegPC = rtsa;
        }
コード例 #5
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // second operand (source)
            ev.GetVal();

            // first operand (destination)
            IValue storeValue = ev.Stack.ReadTop();

            ev.Stack.Pop(); // pop destination or index

            if (storeValue.TypeOf() == ValueTypeID.TYPE_STOREREF)
            {
                DoStoreOp((ValueStore)storeValue.GetObjectValue(), ev.RegR, variation);
            }
            else if (storeValue.TypeOf() == ValueTypeID.TYPE_TABLEDATAREF)
            {
                ValueTableItem vti = (ValueTableItem)storeValue.GetObjectValue();

                if (ev.RegR.TypeOf() == ValueTypeID.TYPE_UNDEFINED)
                {
                    ValueTable table = vti.Parent;
                    table.Delete(vti.Key);
                }
                else
                {
                    DoTableOp(vti, ev.RegR, variation);
                }
            }
            else
            {
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_EXPTYPESTOREREF));
            }
        }
コード例 #6
0
ファイル: NextOpCode.cs プロジェクト: enif77/SharpNekton
        /*
         * O_NEXT
         * sp:   unchanged
         * in:   S[top-1] = arrayref
         * out:  R = arrayref[arrayref->cindex];
         * op:   if (arrayref->cindex >= arrayref->size) {
         *        R = END-OF-READ;
         *      }
         *      else {
         *        R = arrayref[arrayref->cindex]; arrayref->cindex++;
         *      }
         */

        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // get the array reference
            IValue tableValue = ev.Stack.ReadN(ev.Stack.StackTop - 1);

            // work with arrays only
            if (tableValue.TypeOf() == ValueTypeID.TYPE_TABLEREF)
            {
                ValueTable table = (ValueTable)tableValue.GetObjectValue();

                ValueTableItem vti = table.Next();
                if (vti == null)
                {
                    ev.RegR = new DoneValue();
                }
                else
                {
                    ev.RegR = new TableDataRefValue(vti); // should be TYPE_STOREREF?
                }
            }
            else
            {
                //Console.WriteLine(">> nextop tableval.type = {0}", tableValue.TypeOf().ToString());
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_EXPTYPETABLEREF));
            }
        }
コード例 #7
0
ファイル: PushPOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            ev.Stack.Push(ev.GetVal());
            //ev.RegR = new UndefinedValue();
        }
コード例 #8
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // calling a function = entering a deeper run level
            ev.IncreaseRunLevel();

            // get the number of local variables
            int numlocals = parameter;

            // push(reg_FP)
            ev.Stack.Push(new STFPValue(ev.RegFP));

            // set FP to current stack position
            ev.RegFP = ev.Stack.StackTop;

            // create room for local variables
            ev.Stack.StackTop += numlocals;

            // set locals to undefined
            IValue undefinedValue = new UndefinedValue();
            int    stackTop       = ev.Stack.StackTop;

            for (int i = 0; i < numlocals; i++)
            {
                ev.Stack.WriteN(stackTop - i, undefinedValue);
            }
        }
コード例 #9
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // can be used at local runlevel only!
            if (ev.RunLevel <= 0)
            {
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_NOGLOBARGS));
            }

            // pointer to _argc
            IValue argcValue = ev.Stack.ReadN(ev.RegFP - 2);

            if (argcValue.TypeOf() != ValueTypeID.TYPE_NUMBER)
            {
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADTYPE));
            }

            // get the number of passed params
            int numberOfPassedParams = (int)argcValue.GetNumericValue() + 1; // n + 1 = count with _argc

            // get parameter index
            IValue parameterIndexValue = ev.GetVal();
            int    parameterIndex      = (int)argcValue.GetNumericValue() + 1; // n + 1 = start at offset 1

            // test argno validity
            if (parameterIndex < 1 || parameterIndex >= numberOfPassedParams) // >= numparams => hide _argc to arg()
            {
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADPARAMINDEX));
            }

            // arg[n] -> R
            parameterIndex = -(numberOfPassedParams - parameterIndex + 2);
            ev.RegR        = ev.Stack.ReadN(ev.RegFP + parameterIndex);
        }
コード例 #10
0
ファイル: LoadIOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // index is in R
            IValue keyValue = ev.GetVal();

            // get table (it is on the stack[top])
            IValue tableValue = ev.GetStackTopVal();

            ev.Stack.Pop();

            // for array returns ptr to array[index] as TYPE_TABLEDATAREF
            // else raises an error
            if (tableValue.TypeOf() == ValueTypeID.TYPE_TABLEREF)
            {
                string         key   = keyValue.GetStringValue();
                ValueTable     table = (ValueTable)tableValue.GetObjectValue();
                ValueTableItem vti   = table.Search(key);
                if (vti == null)
                {
                    vti = table.Insert(key, new UndefinedValue());
                }

                ev.RegR = new TableDataRefValue(vti);
            }
            else
            {
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_EXPTYPETABLEREF));
            }
        }
コード例 #11
0
        /*
         * O_FSTORE
         * sin:  S[top] = varref; S[top-1] = arrayref
         * sout: S[top] = arrayref
         * in:   R = index; S[top] = varref; S[top-1] = arrayref
         * out:  R = arrayref[index]; S[top] = arrayref;
         * op:   R = arrayref[index]; store;
         */

        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // get the array reference
            IValue tableValue = ev.Stack.ReadN(ev.Stack.StackTop - 1);

            // work with arrays only
            if (tableValue.TypeOf() == ValueTypeID.TYPE_TABLEREF)
            {
                ValueTable table = (ValueTable)tableValue.GetObjectValue();

                // if this creates a new array item, foreach fall into a endless loop!
                ValueTableItem vti = table.Search(ev.RegR.GetStringValue());
                if (vti != null)
                {
                    ev.RegR = new TableDataRefValue(vti);
                }
                else
                {
                    throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADKEYINFOREACH));
                }
            }
            else
            {
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_EXPTYPETABLEREF));
            }

            // store the R to the var
            // TODO: remove the new opcode creation here
            AOpCode storeOpCode = new StoreOpCode(this.line, this.linePosition, TokenID.T_ASSIGN_OP);

            storeOpCode.Eval(ev);
        }
コード例 #12
0
        public override void Eval(EvaluatorState ev)
        {
            int numberOfDefinedParams = 0, parameterIndex = -1;

            //Console.WriteLine(this.ToString());

            // recalculate offsets only for function parameters and not for
            // _argc and local vars
            int offset = this.parameter;

            if (offset < -2)
            {
                // pointer to _argc
                IValue argcValue = ev.Stack.ReadN(ev.RegFP - 2);
                if (argcValue.TypeOf() != ValueTypeID.TYPE_NUMBER)
                {
                    Console.WriteLine(">> exp. argc\n");
                    throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADTYPE));
                }

                // get number of passed params
                int numberOfPassedParams = (int)argcValue.GetNumericValue() + 1; // n + 1 = count with _argc

                // get defined number of params
                IValue functinRefValue = ev.Stack.ReadN(ev.RegFP + -(numberOfPassedParams + 2));

                switch (functinRefValue.TypeOf())
                {
                case ValueTypeID.TYPE_FUNCTIONREF:
                    // get the defined parameter count
                    numberOfDefinedParams = ((FunctionRef)functinRefValue.GetObjectValue()).NumberOfDefinedParameters;
                    break;

                // TODO: implement cfunction LOADFP
                //case N_TYPE_CFUNCTIONREF :
                //  // get the defined parameter count
                //  numdefparams = t->value.cfunction.nparams;
                //  break;

                default:
                    throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_EXPTYPEFUNCREF));
                }

                // calculate number of fixed params
                if (numberOfDefinedParams < 0)
                {
                    numberOfDefinedParams = -numberOfDefinedParams - 1;
                }

                // calculate position of the parameter in the parameters list
                parameterIndex = (numberOfDefinedParams + (offset + 2)) + 1;

                // calculate the new offset
                offset = -(numberOfPassedParams - parameterIndex + 2);
            }

            // return a pointer to a parameter or local var
            ev.RegR = new StoreRefValue(ev.Stack.NRef(ev.RegFP + offset));
        }
コード例 #13
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // create a new table
            ev.RegR = new TableRefValue();
            ev.Stack.Push(ev.RegR);
        }
コード例 #14
0
ファイル: TypeOfOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // operand
            IValue a = ev.GetVal();

            ev.RegR = new StringValue(Tools.GetDescription(a.TypeOf()));
        }
コード例 #15
0
ファイル: NotOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // second operand
            IValue a = ev.GetVal();

            ev.RegR = new BooleanValue(!a.GetBooleanValue());
        }
コード例 #16
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // move the tableref to R and pop it from the stack
            // TODO: add some test, that stack top contains a table ref.
            ev.RegR = ev.Stack.ReadTop();
            ev.Stack.Pop(); // pop the table
        }
コード例 #17
0
ファイル: SizeOfOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // operand
            IValue a = ev.GetVal();

            ev.RegR = new NumericValue(a.SizeOf());
        }
コード例 #18
0
ファイル: IsSameOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            bool result = false;

            // second operand
            IValue b = ev.GetVal();

            // first operand
            IValue a = ev.GetStackTopVal();

            ev.Stack.Pop(); // pop a

            if (a.TypeOf() == b.TypeOf())
            {
                switch (a.TypeOf())
                {
                case ValueTypeID.TYPE_UNDEFINED:
                    result = true;
                    break;

                case ValueTypeID.TYPE_NULL:
                    result = true;
                    break;

                case ValueTypeID.TYPE_BOOLEAN:
                    result = a.GetBooleanValue() == b.GetBooleanValue();
                    break;

                case ValueTypeID.TYPE_NUMBER:
                    result = a.GetNumericValue() == b.GetNumericValue();
                    break;

                case ValueTypeID.TYPE_STRING:
                    result = String.Compare(a.GetStringValue(), b.GetStringValue()) == 0;
                    break;

                case ValueTypeID.TYPE_TABLEREF:
                    result = a.GetObjectValue() == b.GetObjectValue();
                    break;

                default:
                    throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADTYPE));
                }
            }
            else
            {
                result = false;
            }

            ev.RegR = new BooleanValue(result);
        }
コード例 #19
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // value is in R
            IValue a = ev.GetVal();

            if (a.GetBooleanValue() == false)
            {
                ev.RegPC = parameter;
            }
        }
コード例 #20
0
ファイル: ToNumberOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // operand
            IValue a = ev.GetVal();

            if (a.TypeOf() != ValueTypeID.TYPE_NUMBER)
            {
                ev.RegR = new NumericValue(a.GetNumericValue());
            }
        }
コード例 #21
0
ファイル: ToObjectOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // operand
            IValue a = ev.GetVal();

            if (a.TypeOf() != ValueTypeID.TYPE_OBJECTREF)
            {
                ev.RegR = new ObjectValue(a.GetObjectValue());
            }
        }
コード例 #22
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // value is in R
            IValue a = ev.GetVal();

            if (a.TypeOf() == ValueTypeID.TYPE_DONE)
            {
                ev.RegPC = parameter;
            }
        }
コード例 #23
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // operand
            IValue a = ev.GetVal();

            if (a.TypeOf() != ValueTypeID.TYPE_BOOLEAN)
            {
                ev.RegR = new BooleanValue(a.GetBooleanValue());
            }
        }
コード例 #24
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // value is in R
            IValue a = ev.GetVal();

            if (a.GetBooleanValue() == true)
            {
                ev.RegPC = parameter;
                //Console.WriteLine(">> jtrue: {0}", parameter.GetType().ToString());
                //Console.WriteLine(">> jtrue.Data: {0}", parameter.Data.GetType().ToString());
            }
        }
コード例 #25
0
ファイル: OrOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // second operand is in R
            IValue b = ev.GetVal();

            // first operand
            IValue a = ev.GetStackTopVal();

            ev.Stack.Pop(); // pop a

            ev.RegR = new BooleanValue(a.GetBooleanValue() || b.GetBooleanValue());
        }
コード例 #26
0
ファイル: ScriptState.cs プロジェクト: enif77/SharpNekton
        public ScriptState()
        {
            _parser       = new ParserState();
            _evaluator    = new EvaluatorState(this);
            _fileSources  = new Dictionary <string, string>();
            _sourceLoaded = false;
            _libraries    = new Dictionary <string, ALibrary>();

            _printFCallBack = DefaultPrintF;

            // register all basic _libraries
            RegisterLibrary(new BaseLib(this));
            RegisterLibrary(new MathLib(this));
        }
コード例 #27
0
ファイル: PowOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // second operand
            IValue b = ev.GetVal();

            // first operand
            IValue a = ev.GetStackTopVal();

            ev.Stack.Pop(); // pop a

            ev.RegR = new NumericValue(Math.Pow(a.GetNumericValue(), b.GetNumericValue()));
        }
コード例 #28
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // R -> value
            ev.GetVal();

            // if it is an table, make a copy of it...
            if (ev.RegR.TypeOf() == ValueTypeID.TYPE_TABLEREF)
            {
                ValueTable table = (ValueTable)ev.RegR.GetObjectValue();
                ev.RegR = new TableRefValue(table.Copy());
            }
        }
コード例 #29
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // value is in R
            IValue val = ev.GetVal();

            // get tableptr
            // TODO: add a test, if is the value at the stack top a table
            ValueTable table = (ValueTable)ev.Stack.ReadTop().GetObjectValue();

            // get the key and assign the value to the array
            // table[key] = value
            table.Insert(table.AssignAutoKey().ToString(), val);
        }
コード例 #30
0
ファイル: RewindOpCode.cs プロジェクト: enif77/SharpNekton
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // second operand
            IValue a = ev.GetVal();

            // work with arrays only
            if (ev.RegR.TypeOf() == ValueTypeID.TYPE_TABLEREF)
            {
                ValueTable table = (ValueTable)ev.RegR.GetObjectValue();
                table.Rewind();
            }
            else
            {
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_EXPTYPETABLEREF));
            }
        }
コード例 #31
0
ファイル: Evaluator.cs プロジェクト: wj32/Lambda
        private static Expression Evaluate(Expression expression, int depthLimit, EvaluatorState state, bool stopOnAbstraction = false)
        {
            if (depthLimit == 0)
                throw new Exception("Depth limit exceeded");

            depthLimit--;

            if (state.ResolveFunction != null)
                expression = state.ResolveFunction(expression);

            if (expression is AbstractionExpression)
            {
                var abstraction = (AbstractionExpression)expression;

                if (stopOnAbstraction)
                    return expression;

                var right = Evaluate(abstraction.Right, depthLimit, state);

                if ((state.Flags & EvaluatorFlags.NoEta) == 0)
                {
                    // Eta-conversion
                    if (right is ApplicationExpression)
                    {
                        var application = (ApplicationExpression)right;

                        if (application.Right is BoundSymbolExpression)
                        {
                            var boundSymbol = (BoundSymbolExpression)application.Right;

                            if (boundSymbol.Symbol == abstraction.Left && !ContainsBoundSymbol(application.Left, abstraction.Left))
                            {
                                return application.Left;
                            }
                        }
                    }
                }

                return new AbstractionExpression { Left = abstraction.Left, Right = right };
            }
            else if (expression is ApplicationExpression)
            {
                var application = (ApplicationExpression)expression;

                if ((state.Flags & EvaluatorFlags.NoBeta) == 0)
                {
                    var left = Evaluate(application.Left, depthLimit, state, true);

                    // Beta-reduction
                    if (left is AbstractionExpression)
                    {
                        var abstraction = (AbstractionExpression)left;
                        Expression right;

                        if ((state.Flags & EvaluatorFlags.Lazy) != 0)
                            right = state.RegisterLazyExpression(application.Right);
                        else
                            right = application.Right;

                        return Evaluate(Substitute(abstraction.Right, abstraction.Left, right), depthLimit, state, stopOnAbstraction);
                    }

                    return new ApplicationExpression { Left = left, Right = Evaluate(application.Right, depthLimit, state) };
                }
                else
                {
                    return new ApplicationExpression { Left = Evaluate(application.Left, depthLimit, state), Right = Evaluate(application.Right, depthLimit, state) };
                }
            }
            else if (expression is BuiltinExpression)
            {
                var builtin = (BuiltinExpression)expression;

                if ((state.Flags & EvaluatorFlags.NoBuiltin) != 0 || builtin.Evaluate == null)
                    return builtin;

                BuiltinExpression temp = new BuiltinExpression { Left = builtin.Left, Right = builtin.Right, Display = builtin.Display, Evaluate = builtin.Evaluate };

                if (temp.Left != null)
                    temp.Left = Evaluate(temp.Left, depthLimit, state, stopOnAbstraction);
                if (temp.Right != null)
                    temp.Right = Evaluate(temp.Right, depthLimit, state, stopOnAbstraction);

                return temp.Evaluate(temp);
            }
            else if (expression is LazyExpression)
            {
                return state.EvaluateLazyExpression((LazyExpression)expression, depthLimit, stopOnAbstraction);
            }
            else
            {
                return expression;
            }
        }