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;
        }
        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");
        }
 private void AssertExpectedTokenValue(Token token, String expectedtokenValue, String errorMessage)
 {
     if (token.Value != expectedtokenValue)
         throw new GossipScriptException(errorMessage, token);
 }
        private int ParseApiCalls(CompilationContext compilationContext, string currentTokenValue, int index,
                                  List<Token> tokens, Token currentToken, ref IActionNode currentParent)
        {
            bool match = false;
            foreach (ActionInfo function in m_Engine.GlobalActions.Values)
            {
                if (currentTokenValue == function.TokenName)
                {
                    List<Expression> parametersAsExpressions;
                    int skip = ParseFunctionParameterExpressions(index, tokens, compilationContext,out parametersAsExpressions);

                    currentParent.Next = new ExecuteAction(function, parametersAsExpressions) {Next = new ActionNode()};
                    currentParent = currentParent.Next;
                    match = true;

                    index += skip;
                    currentToken = tokens[index];
                    break;
                }
            }
            if (match == false)
            {
                foreach (HostCall function in m_Engine.HostBridge.HostCallTable.Values)
                {
                    if (function.ReturnType == GossipType.ReferenceObject)
                    {
                        // First check if we are going to encounter an assignment
                        Int32 enounterAssignmentIndex = 0;
                        if (EncounterAssignment(index, tokens, ref enounterAssignmentIndex))
                        {
                            return ParsePropertySet(compilationContext, index, tokens, ref currentParent,
                                                    enounterAssignmentIndex);
                        }

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

                            // Create Action
                            ActionBinding action =
                                compilationContext.TypeBindingTable.GetActionByName(currentToken.Value,
                                                                                    expression.HostReturnType);
                            ICustomActionNode node = action.CreateActionNode();

                            List<Expression> expressions;
                            skip = ParseActionParameters(index + 1, 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, expression);

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

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

                        throw new NotImplementedException(
                            "TODO anything that returns a reference might want to be invoked");
                    }
                }
            }

            if (match == false)
                throw new GossipScriptException("Encountered Unknown Token", currentToken);

            return index;
        }
 private void AssertExpectedTokenValue(Token token, Regex expectedtokenValue, String errorMessage)
 {
     if (expectedtokenValue.Match(token.Value).Success == false)
         throw new GossipScriptException(errorMessage, token);
 }
 private void AssertExpectedTokenType(Token token, TokenType expectedtokenType, String errorMessage)
 {
     if (token.Type != expectedtokenType)
         throw new GossipScriptException(errorMessage, token);
 }
        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;
        }
        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;
        }
 public GossipScriptMissingSemiColonException(String message, Token token)
     : base(message)
 {
     m_Token = token;
 }