Ejemplo n.º 1
0
        void GenerateCode(Node node, Parser.AssignmentStatement statement)
        {
            // Is it a straight assignment?
            if (statement.operation == TokenType.EqualToOrAssign)
            {
                // Evaluate the expression, which will result in a value
                // on the stack
                GenerateCode(node, statement.valueExpression);

                // Stack now contains [destinationValue]
            }
            else
            {
                // It's a combined operation-plus-assignment

                // Get the current value of the variable
                Emit(node, ByteCode.PushVariable, statement.destinationVariableName);

                // Evaluate the expression, which will result in a value
                // on the stack
                GenerateCode(node, statement.valueExpression);

                // Stack now contains [currentValue, expressionValue]

                switch (statement.operation)
                {
                case TokenType.AddAssign:
                    Emit(node, ByteCode.CallFunc, TokenType.Add.ToString());
                    break;

                case TokenType.MinusAssign:
                    Emit(node, ByteCode.CallFunc, TokenType.Minus.ToString());
                    break;

                case TokenType.MultiplyAssign:
                    Emit(node, ByteCode.CallFunc, TokenType.Multiply.ToString());
                    break;

                case TokenType.DivideAssign:
                    Emit(node, ByteCode.CallFunc, TokenType.Divide.ToString());
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                // Stack now contains [destinationValue]
            }

            // Store the top of the stack in the variable
            Emit(node, ByteCode.StoreVariable, statement.destinationVariableName);

            // Clean up the stack
            Emit(node, ByteCode.Pop);
        }
Ejemplo n.º 2
0
        // Assigns a value to a variable.
        private void RunAssignmentStatement(Parser.AssignmentStatement assignment)
        {
            // The place where we're stickin' this value.
            var variableName = assignment.destinationVariableName;

            // The value that's going into this variable.
            // TODO: Currently this coerces this into a float. Add support for
            // storing different types of data in the client.
            var computedValue = EvaluateExpression(assignment.valueExpression).AsNumber;

            // The current value of this variable.
            float originalValue = dialogue.continuity.GetNumber(variableName);

            // What shall we do with it?

            // TODO: Hmm this should be a function, like the other operators
            float finalValue = 0.0f;

            switch (assignment.operation)
            {
            case TokenType.EqualToOrAssign:
                finalValue = computedValue;
                break;

            case TokenType.AddAssign:
                finalValue = originalValue + computedValue;
                break;

            case TokenType.MinusAssign:
                finalValue = originalValue - computedValue;
                break;

            case TokenType.MultiplyAssign:
                finalValue = originalValue * computedValue;
                break;

            case TokenType.DivideAssign:
                finalValue = originalValue / computedValue;
                break;
            }

            dialogue.LogDebugMessage(string.Format("Set {0} to {1}", variableName, finalValue));
            dialogue.continuity.SetNumber(variableName, finalValue);
        }
Ejemplo n.º 3
0
        // Assigns a value to a variable.
        private void RunAssignmentStatement(Parser.AssignmentStatement assignment)
        {
            // The place where we're stickin' this value.
            var variableName = assignment.destinationVariableName;

            // The value that's going into this variable.
            var computedValue = EvaluateExpression(assignment.valueExpression);

            // The current value of this variable.
            Value originalValue = dialogue.continuity.GetValue(variableName);

            // What shall we do with it?

            Value finalValue = Value.NULL;

            switch (assignment.operation)
            {
            case TokenType.EqualToOrAssign:
                finalValue = computedValue;
                break;

            case TokenType.AddAssign:
                finalValue = originalValue + computedValue;
                break;

            case TokenType.MinusAssign:
                finalValue = originalValue - computedValue;
                break;

            case TokenType.MultiplyAssign:
                finalValue = originalValue * computedValue;
                break;

            case TokenType.DivideAssign:
                finalValue = originalValue / computedValue;
                break;
            }

            dialogue.LogDebugMessage(string.Format("Set {0} to {1}", variableName, finalValue));
            dialogue.continuity.SetValue(variableName, finalValue);
        }