Esempio n. 1
0
        private int ParseReferenceObjectAccess(CompilationContext compilationContext, Token currentToken,
                                               List<Token> tokens, int index, ref IActionNode currentParent)
        {
            int varIndex = currentToken.GetAccessIndex(m_Engine.HostBridge.EnumDefineTable);

            // Map enums to their index counterparts
            if (currentToken.Type == TokenType.ReferenceObjectAccessEnum)
                currentToken.Type = TokenType.ReferenceObjectAccess;

            currentToken = tokens[++index]; // Assignment or Property set or Action only

            Token operand = currentToken;

            Expression expression;

            if (currentToken.Type == TokenType.ReferencePropertySet)
            {
                currentToken = tokens[++index];
                AssertExpectedTokenType(currentToken, TokenType.Assignment, "Unexpected token");
                currentToken = tokens[++index];
                int skip = ParseExpression(index, tokens, compilationContext, out expression);
                index += skip;
                currentToken = tokens[index];
                ReferenceAssignment assignment = compilationContext.ReferenceAssignmentTable.GetAssignment(varIndex);

                if (!m_Engine.HostBridge.TypeBindingTable.SetPropertyExists(operand.Value, assignment.Type))
                    throw new GossipScriptException(String.Format("Property {0} not found or is read only.",
                                                                  operand.Value));

                PropertySetBinding setter = m_Engine.HostBridge.TypeBindingTable.GetPropertySetByName(operand.Value,
                                                                                                      assignment.Type);

                if (setter.ParameterType != expression.ReturnType)
                    throw new GossipScriptException("Incompatile property set parameter");

                currentParent.Next = new AssignReferenceProperty(varIndex, setter.Id, expression);
                currentParent = currentParent.Next;
                return index;
            }

            if (currentToken.Type == TokenType.Assignment)
            {
                currentToken = tokens[++index];
                int skip = ParseExpression(index, tokens, compilationContext, out expression);
                index += skip;
                currentToken = tokens[index];

                compilationContext.ReferenceAssignmentTable.TrackReference(varIndex, expression.HostReturnType);

                if (expression.ReturnType != GossipType.ReferenceObject)
                    throw new GossipScriptException("Expected return type reference object");

                // Create the assignment command
                currentParent.Next = new AssignReference(varIndex, expression);
                currentParent = currentParent.Next;

                return index;
            }

            // Parse Action
            {
                int skip = ParseExpression(index - 1, tokens, compilationContext, out expression);
                index += (skip);
                currentToken = tokens[index - 1];
                if (currentToken.Type == TokenType.ReferenceAction)
                {
                    // Create an action
                    ReferenceAssignment actionType = compilationContext.ReferenceAssignmentTable.GetAssignment(varIndex);
                    ActionBinding action = compilationContext.TypeBindingTable.GetActionByName(currentToken.Value,
                                                                                               actionType.Type);
                    ICustomActionNode node = action.CreateActionNode();

                    int refIndex = varIndex; // Unless the expression result is
                    List<Expression> expressions;
                    skip = ParseActionParameters(index, tokens, compilationContext, out expressions);

                    if (action.NumParameters != expressions.Count)
                        throw new GossipScriptParameterException(
                            String.Format("Invalid number of parameters for action:{0} found:{1} expected:{2}",
                                          action.Name, expressions.Count, action.NumParameters));

                    var customActionContext = new CustomActionContext(expressions, refIndex);

                    currentParent.Next = new ExecuteCustomActionNode(node, customActionContext);
                    currentParent = currentParent.Next;

                    index += (skip + 1);
                    currentToken = tokens[index];
                    return index;
                }
            }

            throw new GossipScriptException("Unhandled operand");
        }
Esempio n. 2
0
        private int ParseVariableAccess(CompilationContext compilationContext, Token currentToken, List<Token> tokens,
                                        int index, ref IActionNode currentParent)
        {
            int varIndex = currentToken.GetAccessIndex(m_Engine.HostBridge.EnumDefineTable);

            // Map enums to their index counterparts
            if (currentToken.Type == TokenType.LocalVariableAccessEnum)
                currentToken.Type = TokenType.LocalVariableAccess;
            if (currentToken.Type == TokenType.GlobalVariableAccessEnum)
                currentToken.Type = TokenType.GlobalVariableAccess;

            // Global or local?
            var op = TokenType.GlobalVariableAccess;
            if (currentToken.Type == TokenType.LocalVariableAccess)
                op = TokenType.LocalVariableAccess;

            currentToken = tokens[++index]; // Assignment or increment or decrement
            if (currentToken.Value != "=" && currentToken.Value != "++" && currentToken.Value != "--" &&
                currentToken.Value != "+=" && currentToken.Value != "-=")
                throw new GossipScriptException("Unexpected Token following variable access", currentToken);

            if (currentToken.Value == "++")
            {
                currentParent.Next = new ModifyVariable(varIndex, op, TokenType.Increment, 1);
                currentParent = currentParent.Next;
                return index;
            }

            if (currentToken.Value == "--")
            {
                currentParent.Next = new ModifyVariable(varIndex, op, TokenType.Decrement, 1);
                currentParent = currentParent.Next;
                return index;
            }

            if (currentToken.Type == TokenType.Assignment || currentToken.Type == TokenType.IncrementAndAssign ||
                currentToken.Type == TokenType.DecrementAndAssign)
            {
                TokenType tokenType = currentToken.Type;
                currentToken = tokens[++index]; // First token after assignment

                Expression expression;
                int skip = ParseExpression(index, tokens, compilationContext, out expression);
                if (expression.ReturnType != GossipType.Number)
                    throw new GossipScriptException(String.Format("Invalid return type:{0}, Expected Number",
                                                                  expression.ReturnType));

                index += skip;
                currentToken = tokens[index];

                // Specal case
                if (op == TokenType.LocalVariableAccess && currentToken.Type == TokenType.Assignment &&
                    expression.Instructions.Count == 1)
                {
                    currentParent.Next = new AssignLocalVariableWithLiteral(varIndex,
                                                                            Convert.ToDouble(
                                                                                expression.Instructions[0].Data));
                    currentParent = currentParent.Next;

                    return index;
                }

                // Generic Case
                currentParent.Next = new AssignVariable(varIndex, op, tokenType, expression);
                currentParent = currentParent.Next;

                return index;
            }
            return index;
        }
Esempio n. 3
0
        private int ParseFlagAccess(CompilationContext compilationContext, Token currentToken, List<Token> tokens,
                                    int index, ref IActionNode currentParent)
        {
            int varIndex = currentToken.GetAccessIndex(m_Engine.HostBridge.EnumDefineTable);

            // Map enums to their index counterparts
            if (currentToken.Type == TokenType.LocalFlagAccessEnum)
                currentToken.Type = TokenType.LocalFlagAccess;
            if (currentToken.Type == TokenType.GlobalFlagAccessEnum)
                currentToken.Type = TokenType.GlobalFlagAccess;

            // Global or local?
            var op = TokenType.GlobalFlagAccess;
            if (currentToken.Type == TokenType.LocalFlagAccess)
                op = TokenType.LocalFlagAccess;

            currentToken = tokens[++index]; // Assignment
            AssertExpectedTokenType(currentToken, TokenType.Assignment, "Expected Assignment");

            currentToken = tokens[++index];

            Expression expression;
            int skip = ParseExpression(index, tokens, compilationContext, out expression);

            index += skip;
            currentToken = tokens[index];

            // Create the assignment command
            currentParent.Next = new ModifyFlag(varIndex, op, expression);
            currentParent = currentParent.Next;

            return index;
        }
Esempio n. 4
0
        private int ParseStringAccess(CompilationContext compilationContext, Token currentToken, List<Token> tokens,
                                      int index, ref IActionNode currentParent)
        {
            int destIndex = currentToken.GetAccessIndex(m_Engine.HostBridge.EnumDefineTable);

            // Map enums to their index counterparts
            if (currentToken.Type == TokenType.LocalStringAccessEnum)
                currentToken.Type = TokenType.LocalStringAccess;
            if (currentToken.Type == TokenType.GlobalFlagAccessEnum)
                currentToken.Type = TokenType.GlobalFlagAccess;

            // Global or local?
            var destScope = VariableScope.Global;

            if (currentToken.Type == TokenType.LocalStringAccess)
                destScope = VariableScope.Local;

            currentToken = tokens[++index];
            if (currentToken.Type != TokenType.Assignment)
                throw new GossipScriptException("Unexpected Token following string access", currentToken);

            Token op = currentToken;
            currentToken = tokens[++index];

            if (currentToken.Type == TokenType.StringLiteral)
            {
                string stringValue = currentToken.Value.Substring(1, currentToken.Value.Length - 2);
                currentParent.Next = new AssignStringLiteral(destIndex, destScope, stringValue);
                currentParent = currentParent.Next;
                return index;
            }

            // If none of the above it must be an expression
            Expression expression;
            int skipTokens = ParseExpression(index, tokens, compilationContext, out expression);
            index += skipTokens;
            currentToken = tokens[index];
            currentParent.Next = new AssignString(destIndex, destScope, expression);
            currentParent = currentParent.Next;
            return index;
        }