Exemple #1
0
        private void Factor()
        {
            if (Stream.Accept(TokenType.Word, "print"))
            {
                TernaryExpression();
                Instructions.Add(new Instruction(Opcode.Print));
            }
            else if (Stream.Accept(TokenType.Word, "len"))
            {
                TernaryExpression();
                Instructions.Add(new Instruction(Opcode.GetVariableLength));
            }
            else if (Stream.Accept(TokenType.Word, "nil"))
            {
                Instructions.Add(new Instruction(Opcode.Push, new Variant()));
            }
            else if (Stream.Accept(TokenType.Delimiter, "("))
            {
                TernaryExpression();
                Stream.Expect(TokenType.Delimiter, ")");
            }
            else if (Stream.Accept(TokenType.Delimiter, "["))
            {
                int varIndex = AddTempVariable();
                ArrayDeclaration(varIndex);
                Instructions.Add(new Instruction(Opcode.GetVariable, new Variant(varIndex)));
            }
            else if (Stream.Pass(TokenType.Word))
            {
                switch (Stream.PeekAhead(1).Value)
                {
                    case "[":
                        {
                            Variant arrayVariable = new Variant(GetVariableIndex(Stream.GetWord()));
                            Stream.Expect(TokenType.Delimiter, "[");
                            Expression();
                            Stream.Expect(TokenType.Delimiter, "]");

                            Instructions.Add(new Instruction(Opcode.GetArrayVariable, arrayVariable));
                            break;
                        }

                    case "(":
                        MethodCall();
                        break;

                    default:
                        Instructions.Add(new Instruction(Opcode.GetVariable, new Variant(GetVariableIndex(Stream.GetWord()))));
                        break;
                }
            }
            else if (Stream.Pass(TokenType.Number) || Stream.Pass(TokenType.String))
            {
                Instructions.Add(new Instruction(Opcode.Push, Stream.GetVariant()));
            }
            else if (!Parser.IsIncrementOperation(Stream.Peek()))
                Stream.Expected("builtin function, variable or literal");

            if (Stream.Accept(TokenType.Delimiter, "++"))
            {
                int variableIndex = GetVariableIndex(Stream.PeekAhead(-2).Value);
                Instructions.Add(new Instruction(Opcode.IncrementVariable, new List<Variant> { new Variant(variableIndex), new Variant(1) }));
            }
            else if (Stream.Accept(TokenType.Delimiter, "--"))
            {
                int variableIndex = GetVariableIndex(Stream.PeekAhead(-2).Value);
                Instructions.Add(new Instruction(Opcode.IncrementVariable, new List<Variant> { new Variant(variableIndex), new Variant(-1) }));
            }
        }
Exemple #2
0
 public Instruction(Opcode opcode, Variant operand)
 {
     Opcode = opcode;
     Operands = new List<Variant> { operand };
 }