Example #1
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;
        }
Example #2
0
 public JumpOpCode(int line, int linePosition, OpCodeListItem destination) : base(line, linePosition, destination)
 {
     opCodeID = OpCodeID.O_JUMP;
 }
Example #3
0
 public APointerOpCode(int line, int linePosition, OpCodeListItem parameter) : base(line, linePosition)
 {
     this.parameter = parameter;
 }
Example #4
0
 public RTSAValue(OpCodeListItem rtsaRef)
 {
     valueType = ValueTypeID.TYPE_RTSA;
     val       = rtsaRef;
 }
Example #5
0
 public JumpIfTrueOpCode(int line, int linePosition, OpCodeListItem destination) : base(line, linePosition, destination)
 {
     opCodeID = OpCodeID.O_JUMP_IF_TRUE;
 }
Example #6
0
        public override void Eval(EvaluatorState ev)
        {
            //Console.WriteLine(this.ToString());

            // pointer to _argc
            IValue argcValue = ev.Stack.ReadTop();

            if (argcValue.TypeOf() != ValueTypeID.TYPE_NUMBER)
            {
                Console.WriteLine(">> exp. argc");
                // TODO: add more specific error code here
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADTYPE));
            }

            // get number of passed params
            int numberOfPassedParams = (int)argcValue.GetNumericValue();

            // functionref is stored on the stack[numparams] by pushp instuction,
            // so GetVal() is not neccessary
            IValue functionRefValue = ev.Stack.ReadN(ev.Stack.StackTop - (numberOfPassedParams + 1)); // n + 1 = count with _argc

            switch (functionRefValue.TypeOf())
            {
            case ValueTypeID.TYPE_FUNCTIONREF:
                // store the return address on the stack
                ev.Stack.Push(new RTSAValue(ev.RegPC));

                // check parameter counts
                FunctionRef functionRef = (FunctionRef)functionRefValue.GetObjectValue();
                if (CheckParams(functionRef.NumberOfDefinedParameters, numberOfPassedParams) == false)
                {
                    throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADPARAMCOUNT));
                }

                // set PC to new address and test it
                OpCodeListItem functionStartAddress = functionRef.CodePart.First();
                if (functionStartAddress != null)
                {
                    ev.RegPC = functionStartAddress;
                }
                else
                {
                    // TODO: add more specific error code here
                    throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADJMPTARGET));
                }
                break;

            case ValueTypeID.TYPE_CFUNCTIONREF:
                // check parameter counts
                ExternalFunctionRef extFunctionRef = (ExternalFunctionRef)functionRefValue.GetObjectValue();
                if (CheckParams(extFunctionRef.NumberOfDefinedParameters, numberOfPassedParams) == false)
                {
                    throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_BADPARAMCOUNT));
                }

                // call the function
                extFunctionRef.FunctionRef(numberOfPassedParams);
                break;

            default:
                //Console.WriteLine(">> type is {0}", functionRefValue.TypeOf().ToString());
                throw new SharpNektonException(new SharpNektonError(SharpNektonErrorID.E_EXPTYPEFUNCREF));
            }
        }