Ejemplo n.º 1
0
        protected bool SyntaxCheckFunction()
        {
            PatternFunction lFunction = GetCurrentFunction();

            if (lFunction == null)
            {
                return(SetErrorMessage("Unknown Function: " + CompileErrorLocation()));
            }

            NextToken();
            PatternToken lToken = GetCurrentToken();

            if (lToken != PatternToken.OPEN_PARA)
            {
                return(SetErrorMessage("Expected ')': " + CompileErrorLocation()));
            }

            NextToken();

            if (!SyntaxCheckParameters())
            {
                return(false);
            }

            lToken = GetCurrentToken();

            if (lToken != PatternToken.CLOSE_PARA)
            {
                return(SetErrorMessage("Expected ')': " + CompileErrorLocation()));
            }

            NextToken();

            return(true);
        }
        protected PatternFunction FindFunction(string tokenString, int parameters)
        {
            PatternFunction lFunction = null;

            if (Dictionary.TryGetValue(tokenString + ":" + parameters.ToString(), out lFunction))
            {
                return(lFunction);
            }

            return(null);
        }
Ejemplo n.º 3
0
        protected int EvaulateFunction()
        {
            PatternFunction lFunction = GetCurrentFunction();

            NextToken();
            NextToken();

            PatternFunctionParameters <int> lPatternFunctionParameters = new PatternFunctionParameters <int>();

            while (true)
            {
                PatternToken lToken    = GetCurrentToken();
                int          lInt      = GetTokenInteger();     // in case this is needed later
                char         lVariable = GetTokenVariable();    // in case this is needed later

                NextToken();

                if (lToken == PatternToken.COMMA)
                {
                    continue;
                }

                if (lToken == PatternToken.CLOSE_PARA)
                {
                    break;
                }

                if (lToken == PatternToken.INTEGER)
                {
                    lPatternFunctionParameters.Add(lInt);
                }
                else if (lToken == PatternToken.VARIABLE)
                {
                    Coordinate lCoordinate = OriginPoint + (Pattern.GetLetterLocation(lVariable) - Pattern.Origin).Transform(Transform);

                    lPatternFunctionParameters.Add(Board.Coord.At(lCoordinate.X, lCoordinate.Y));
                }
                else
                {
                    lPatternFunctionParameters.Add(EvaulateExpression());
                }
            }

            // Call function
            return(lFunction(Board, Player, lPatternFunctionParameters));
        }
        public PatternOperand(PatternToken token, string tokenString, int paramCount)
        {
            Token  = token;
            Source = tokenString;             // used for debugging

            if (token == PatternToken.INTEGER)
            {
                Integer = Convert.ToInt32(tokenString);
            }
            else if (token == PatternToken.VARIABLE)
            {
                Variable = tokenString[0];
            }
            else if (token == PatternToken.UNKNOWN)
            {
                return;
            }
            else if (token == PatternToken.FUNCTION)
            {
                Integer  = paramCount;
                Function = PatternFunctions.GetFunction(tokenString, paramCount);
            }
        }
Ejemplo n.º 5
0
        private PatternExpression ParseExpression(bool readBinOp = false)
        {
            PatternExpression ret;
            PatternToken      token = ReadToken();

            switch (token.Type)
            {
            case TokenType.Literal:
                ret = new LiteralExpression(token.Value);
                break;

            case TokenType.LParens:
            {
                ret = ParseExpression(true);
                PatternToken parens = ReadToken();
                if (parens.Type != TokenType.RParens)
                {
                    throw MismatchParens(token.Position.Value);
                }
            }
            break;

            case TokenType.Identifier:
                if (IsOperator(token))
                {
                    // unary operator
                    PatternOperator op = ops[token.Value]();
                    if (!op.IsUnary)
                    {
                        throw UnexpectedToken(token);
                    }
                    op.OperandA = ParseExpression();
                    ret         = op;
                }
                else if (IsFunction(token))
                {
                    // function
                    PatternFunction fn = fns[token.Value]();

                    PatternToken parens = ReadToken();
                    if (parens.Type != TokenType.LParens)
                    {
                        throw UnexpectedToken(parens, '(');
                    }

                    fn.Arguments = new List <PatternExpression>(fn.ArgumentCount);
                    for (int i = 0; i < fn.ArgumentCount; i++)
                    {
                        if (PeekToken() == null)
                        {
                            throw UnexpectedEnd();
                        }
                        if (PeekToken().Value.Type == TokenType.RParens)
                        {
                            throw BadArgCount(token, fn.ArgumentCount);
                        }
                        if (i != 0)
                        {
                            PatternToken comma = ReadToken();
                            if (comma.Type != TokenType.Comma)
                            {
                                throw UnexpectedToken(comma, ',');
                            }
                        }
                        fn.Arguments.Add(ParseExpression());
                    }

                    parens = ReadToken();
                    if (parens.Type == TokenType.Comma)
                    {
                        throw BadArgCount(token, fn.ArgumentCount);
                    }
                    if (parens.Type != TokenType.RParens)
                    {
                        throw MismatchParens(parens.Position.Value);
                    }

                    ret = fn;
                }
                else
                {
                    bool boolValue;
                    if (bool.TryParse(token.Value, out boolValue))
                    {
                        ret = new LiteralExpression(boolValue);
                    }
                    else
                    {
                        throw UnknownToken(token);
                    }
                }

                break;

            default:
                throw UnexpectedToken(token);
            }

            if (!readBinOp)
            {
                return(ret);
            }

            // binary operator
            PatternToken?peek = PeekToken();

            while (peek != null)
            {
                if (peek.Value.Type != TokenType.Identifier)
                {
                    break;
                }
                if (!IsOperator(peek.Value))
                {
                    break;
                }

                PatternToken    binOpToken = ReadToken();
                PatternOperator binOp      = ops[binOpToken.Value]();
                if (binOp.IsUnary)
                {
                    throw UnexpectedToken(binOpToken);
                }
                binOp.OperandA = ret;
                binOp.OperandB = ParseExpression();
                ret            = binOp;

                peek = PeekToken();
            }

            return(ret);
        }
 protected void Add(string functionName, PatternFunction patternFunction, int parameters)
 {
     Dictionary.Add(functionName + ":" + parameters.ToString(), patternFunction);
 }
Ejemplo n.º 7
0
 protected void Add(string functionName, PatternFunction patternFunction, int parameters)
 {
     Dictionary.Add(functionName + ":" + parameters.ToString(), patternFunction);
 }
Ejemplo n.º 8
0
        public PatternOperand(PatternToken token, string tokenString, int paramCount)
        {
            Token = token;
            Source = tokenString; // used for debugging

            if (token == PatternToken.INTEGER)
                Integer = Convert.ToInt32(tokenString);
            else if (token == PatternToken.VARIABLE)
                Variable = tokenString[0];
            else if (token == PatternToken.UNKNOWN)
                return;
            else if (token == PatternToken.FUNCTION)
            {
                Integer = paramCount;
                Function = PatternFunctions.GetFunction(tokenString, paramCount);
            }
        }