Пример #1
0
        Instruction AnalyseAssignement(Tokenizer.Token[] Tokens, int index, out int tokenCount)
        {
            InstructionAssignment instructionAssignment = new InstructionAssignment();

            instructionAssignment.VariableName = Tokens[index].Value;

            tokenCount = 1;

            int tc = 0;

            instructionAssignment.Path = AnalysePath(Tokens, index + tokenCount, out tc);
            tokenCount += tc;

            instructionAssignment.Type = TokenNameToAssignmentType[Tokens[index + tokenCount].TokenName];
            tokenCount++;

            int tokenCount_rightValue = 0;

            Tokenizer.Token[] instructionTokens = GetTokensTo(Tokens, index + tokenCount, (t) => t.TokenName == Tokenizer.TokenName.LineEnd);
            instructionAssignment.Instruction = AnalyseInstruction(instructionTokens, 0, out tokenCount_rightValue);

            tokenCount += instructionTokens.Length + 1;

            return(instructionAssignment);
        }
Пример #2
0
        void AnalyseVariableDeclation(Tokenizer.Token[] Tokens, int index, out int tokenCount)
        {
            List <Variable> variables = new List <Variable>();

            tokenCount = 0;
            do
            {
                tokenCount += 1;

                Variable variable = new Variable();
                if (Tokens[index + tokenCount].TokenName == Tokenizer.TokenName.Reference)
                {
                    variable.Name      = Tokens[index + tokenCount + 1].Value;
                    variable.Reference = true;
                    tokenCount        += 2;
                }
                else
                {
                    variable.Name      = Tokens[index + tokenCount].Value;
                    variable.Reference = false;
                    tokenCount        += 1;
                }

                if (_currentFunction != null)
                {
                    variable.Index  = _currentFunction.Variables.Count;
                    variable.Global = false;
                    _currentFunction.Variables.Add(variable);
                }
                else
                {
                    variable.Index  = _program.Variables.Count;
                    variable.Global = true;
                    _program.Variables.Add(variable);
                }

                variables.Add(variable);
            } while (Tokens[index + tokenCount].TokenName == Tokenizer.TokenName.ParameterSeparation);

            if (Tokens[index + tokenCount].TokenName == Tokenizer.TokenName.Assignment)
            {
                tokenCount++;

                Tokenizer.Token[] instructionTokens = GetTokensTo(Tokens, index + tokenCount, (t) => t.TokenName == Tokenizer.TokenName.LineEnd);

                foreach (Variable variable in variables)
                {
                    InstructionAssignment instructionAssignment = new InstructionAssignment();
                    instructionAssignment.VariableName = variable.Name;
                    instructionAssignment.Path         = new List <PathParameter>();

                    int tc = 0;
                    instructionAssignment.Instruction = AnalyseInstruction(instructionTokens, 0, out tc);

                    if (_currentFunction != null)
                    {
                        _currentFunction.Instructions.Add(instructionAssignment);
                    }
                    else
                    {
                        _program.Instructions.Add(instructionAssignment);
                    }
                }

                tokenCount += instructionTokens.Length;
            }

            tokenCount++;
        }
Пример #3
0
        InstructionResult ExecuteInstructionAssignment(Instruction instruction)
        {
            InstructionAssignment instructionAssignment = instruction as InstructionAssignment;

            Variable variable = GetVariableByName(instructionAssignment.VariableName);

            ThrowExceptionOnCondition(variable == null, instructionAssignment, 30001, new string[] { instructionAssignment.VariableName });
            Value value = ExecuteInstruction(instructionAssignment.Instruction).Value;

            Value variableValue = GetVariableValue(variable);

            foreach (PathParameter parameter in instructionAssignment.Path)
            {
                if (variableValue.Type == Value.DataType.Array)
                {
                    Value indexValue = ExecuteInstruction(parameter.IndexInstruction).Value;
                    variableValue = variableValue.Array[(int)indexValue.NumericValue];
                }
                else if (variableValue.Type == Value.DataType.Map)
                {
                    Value keyValue = ExecuteInstruction(parameter.IndexInstruction).Value;
                    if (variableValue.Map.ContainsKey(keyValue.ToString()))
                    {
                        variableValue = variableValue.Map[keyValue.ToString()];
                    }
                    else
                    {
                        Value newVariableValue = new Value();
                        variableValue.Map.Add(keyValue.ToString(), newVariableValue);
                        variableValue = newVariableValue;
                    }
                }
                else if (variableValue.Type == Value.DataType.Structure)
                {
                    if (variableValue.Structure.ContainsKey(parameter.Member))
                    {
                        variableValue = variableValue.Structure[parameter.Member];
                    }
                    else
                    {
                        Value newVariableValue = new Value();
                        variableValue.Structure.Add(parameter.Member, newVariableValue);
                        variableValue = newVariableValue;
                    }
                }
            }

            switch (instructionAssignment.Type)
            {
            case InstructionAssignment.AssignmentType.Base:
                CopyValue(ref variableValue, value);
                break;

            case InstructionAssignment.AssignmentType.Addition:
                variableValue.NumericValue += value.NumericValue;
                break;

            case InstructionAssignment.AssignmentType.Multiplication:
                variableValue.NumericValue *= value.NumericValue;
                break;

            case InstructionAssignment.AssignmentType.Substraction:
                variableValue.NumericValue -= value.NumericValue;
                break;

            case InstructionAssignment.AssignmentType.Division:
                variableValue.NumericValue /= value.NumericValue;
                break;
            }

            return(null);
        }