private BinaryOperatorInstruction _binaryOperator(InstructionsEnum binaryOperator)
        {
            BinaryOperatorInstruction instruction = new BinaryOperatorInstruction(binaryOperator);

            _instructions.Add(instruction);
            return(instruction);
        }
Esempio n. 2
0
        private void _defFunction(List <byte> bytes, ref int i)
        {
            byte id = Pop();

            BytecodeFunction function = new BytecodeFunction();

            byte functionCount = 1;

            bool shouldEscapeWhileLoop = false;

            while (i < bytes.Count)
            {
                // Read the next byte from the bytecode.
                byte             value       = bytes[++i];
                InstructionsEnum instruction = (InstructionsEnum)value;

                //skip all instructions until we hit end function (still shift i for literals though)
                switch (instruction)
                {
                case InstructionsEnum.DefFunction:
                    functionCount++;
                    //TODO: consider order and things
                    function.Instructions.Add(value);
                    break;

                case InstructionsEnum.EndDefFunction:
                    functionCount--;
                    if (functionCount == 0)
                    {
                        shouldEscapeWhileLoop = true;
                    }
                    else
                    {
                        function.Instructions.Add(value);
                    }
                    break;

                case InstructionsEnum.Literal:
                    //TODO: consider order and things
                    function.Instructions.Add(value);
                    function.Instructions.Add(bytes[++i]);
                    break;

                default:
                    //TODO: consider order and things
                    function.Instructions.Add(value);
                    break;
                }

                if (shouldEscapeWhileLoop)
                {
                    break;
                }
            }

            Console.WriteLine($"Defining function_{id}");
            _functions[id] = function;
        }
        public void Init()
        {
            _val1 = 3;
            _val2 = 5;

            _binaryOperator = InstructionsEnum.Add;

            _expected = new List <byte>();

            _expected.AddRange <byte>(
                //set literal to 3 so we can confirm value later
                (byte)InstructionsEnum.Literal,
                _val1,

                //set literal to 5 so we can confirm value later
                (byte)InstructionsEnum.Literal,
                _val2,

                //add 3 + 5
                (byte)_binaryOperator
                );
        }
Esempio n. 4
0
        private byte[] _interpretFunction(BytecodeFunction function)
        {
            Console.WriteLine(this);
            int size = function.Instructions.Count;

            for (int i = 0; i < size; i++)
            {
                InstructionsEnum instruction = (InstructionsEnum)function.Instructions[i];

                //if we're in an if and the status is false
                if (ifs.Count > 0 && ifs.Peek() == false)
                {
                    //skip all instructions until we hit end if (still shift i for literals though)
                    switch (instruction)
                    {
                    case InstructionsEnum.If:
                        ifs.Push(false);     //push a false so we can handle nested ifs
                        continue;

                    case InstructionsEnum.Literal:
                        i++;
                        continue;

                    case InstructionsEnum.EndIf:
                        break;

                    default:
                        continue;
                    }
                }

                switch (instruction)
                {
                case InstructionsEnum.Literal:
                    _literal(function.Instructions, ref i);
                    break;

                case InstructionsEnum.ReturnSignature:
                    _returnSignature(function);
                    break;

                case InstructionsEnum.Add:     //should be preceded by two values to add
                    _add();
                    break;

                case InstructionsEnum.Subtract:     //should be preceded by two values to subtract
                    _subtract();
                    break;

                case InstructionsEnum.Multiply:     //should be preceded by two values to multiply
                    _multiply();
                    break;

                case InstructionsEnum.Divide:     //should be preceded by two values to divide
                    _divide();
                    break;

                case InstructionsEnum.LessThan:
                    _lessThan();
                    break;

                case InstructionsEnum.GreaterThan:
                    _greaterThan();
                    break;

                case InstructionsEnum.EqualTo:
                    _equalTo();
                    break;

                case InstructionsEnum.GreaterThanOrEqualTo:
                    _greaterThanOrEqualTo();
                    break;

                case InstructionsEnum.LessThanOrEqualTo:
                    _lessThanOrEqualTo();
                    break;

                case InstructionsEnum.If:
                    _if();
                    break;

                case InstructionsEnum.EndIf:
                    _endIf();
                    break;

                case InstructionsEnum.For:
                    _for(i);
                    break;

                case InstructionsEnum.EndFor:
                    _endFor(ref i);
                    break;

                case InstructionsEnum.DefArray:
                    _defArray();
                    break;

                case InstructionsEnum.GetArray:
                    _getArray();
                    break;

                case InstructionsEnum.GetArrayLength:
                    _getArrayLength();
                    break;

                case InstructionsEnum.GetArrayValueAtIndex:
                    _getArrayValueAtIndex();
                    break;

                case InstructionsEnum.SetArrayValueAtIndex:
                    _setArrayValueAtIndex();
                    break;

                case InstructionsEnum.DefType:
                    _defType();
                    break;

                case InstructionsEnum.DefVar:
                    _defVar();
                    break;

                case InstructionsEnum.GetVar:
                    _getVar();
                    break;

                case InstructionsEnum.SetVar:
                    _setVar();
                    break;

                case InstructionsEnum.DefFunction:
                    _defFunction(function.Instructions, ref i);
                    break;

                case InstructionsEnum.Function:
                    _function();
                    break;

                case InstructionsEnum.CustomFunction:
                    _customFunction();
                    break;

                case InstructionsEnum.Return:
                    return(_return(function));
                }

                Console.WriteLine(this);
            }

            if (function.ReturnType > 0)
            {
                throw new Exception("Missing return statement.");
            }

            return(null);
        }
Esempio n. 5
0
 public InputParsedEventArgs(int commandTarget, InstructionsEnum instructions, string argument)
 {
     CommandTarget = commandTarget;
     Instructions  = instructions;
     Argument      = argument;
 }
Esempio n. 6
0
 public BinaryOperatorInstruction(InstructionsEnum mathInstruction)
 {
     _mathInstruction = mathInstruction;
 }