Example #1
0
        /// <summary>
        /// Creates a rule - metadata that allows invocation of a function on
        /// </summary>
        private void RuleDeclaration()
        {
            if (_EnclosingCompiler != null)
            {
                throw new CompilerException(Tokens.Peek(), "Rules must be globally scoped.");
            }
            Token triggerName = Tokens.Consume(IDENTIFIER, "Rule declarations must begin with a named trigger.");
            List <RuleCondition> conditions = new List <RuleCondition>();

            // rule conditions
            while (!Tokens.Match(RIGHT_BRACKET))
            {
                Token contextVariableName  = Tokens.Consume(IDENTIFIER, "Rule must contain list of comparison expressions (missing identifier).");
                Token comparisonOperation  = Tokens.Advance(); // we will check validity of this token after consuming the value
                Token contextVariableValue = Tokens.Consume(NUMBER, "Rule must contain list of comparison expressions (missing value)");
                switch (comparisonOperation.Type)
                {
                case BANG_EQUAL:
                    // conditions.Add(RuleCondition.ConditionNotEquals(contextVariableName.Lexeme, contextVariableValue.LiteralAsNumber));
                    throw new CompilerException(Tokens.Peek(), "Rule must contain list of comparison expressions (can't use != operator).");

                case EQUAL:
                case EQUAL_EQUAL:
                    conditions.Add(RuleCondition.ConditionEquals(contextVariableName.Lexeme, contextVariableValue.LiteralAsNumber));
                    break;

                case GREATER:
                    conditions.Add(RuleCondition.ConditionGreaterThan(contextVariableName.Lexeme, contextVariableValue.LiteralAsNumber));
                    break;

                case GREATER_EQUAL:
                    conditions.Add(RuleCondition.ConditionGreaterThanOrEqual(contextVariableName.Lexeme, contextVariableValue.LiteralAsNumber));
                    break;

                case LESS:
                    conditions.Add(RuleCondition.ConditionLessThan(contextVariableName.Lexeme, contextVariableValue.LiteralAsNumber));
                    break;

                case LESS_EQUAL:
                    conditions.Add(RuleCondition.ConditionLessThanOrEqual(contextVariableName.Lexeme, contextVariableValue.LiteralAsNumber));
                    break;

                default:
                    throw new CompilerException(Tokens.Peek(), "Rule must contain list of comparison expressions (missing operator).");
                }
            }
            if (Tokens.Peek().Type == FUNCTION && Tokens.Peek(1).Type == IDENTIFIER)
            {
                string functionName = Tokens.Peek(1).Lexeme;
                _Rules.Add(new Rule(BitString.GetBitStr(triggerName.Lexeme), BitString.GetBitStr(functionName), conditions.ToArray()));
            }
            else
            {
                throw new CompilerException(Tokens.Peek(), "Rule declaration must be followed by function.");
            }
        }