Ejemplo n.º 1
0
        private AstItem ReadSingleAstItem(TokenIterator tokens)
        {
            AstItem result;
            var     token = tokens.Current();

            if (token.Kind == TokenKind.Word)
            {
                var next = tokens.Next();
                if (next != null)
                {
                    if (next.Kind == TokenKind.RoundBracketOpen)
                    {
                        var dataType = DataType.FindByName(token.Value);
                        if (dataType != null && dataType.IsVector)
                        {
                            //Vector construction with specific vector type name, i.e. "vec4f(...)"
                            result = ReadVector(tokens);
                            tokens.StepBack();
                        }
                        else
                        {
                            result = ReadFuncCall(tokens);
                            tokens.StepBack();
                        }
                    }
                    else if (next.Kind == TokenKind.SquareBracketOpen)
                    {
                        result = ReadIndexAccess(tokens);
                        tokens.StepBack();
                    }
                    else
                    {
                        //Not sure what identifier is here. Could be type name, variable etc. Will be specified in next steps.
                        result = AstItem.AsIdentifier(token.Value);
                    }
                }
                else
                {
                    //Not sure what identifier is here. Could be type name, variable etc. Will be specified in next steps.
                    result = AstItem.AsIdentifier(token.Value);
                }
            }
            else if (token.Kind == TokenKind.VectorConstructor)
            {
                //Vector construction with the generic "vec(...)"
                result = ReadVector(tokens);
                tokens.StepBack();
            }
            else if (token.Kind == TokenKind.Number)
            {
                result = AstItem.Immediate(token.Value);
            }
            else if (token.Kind == TokenKind.True || token.Kind == TokenKind.False)
            {
                result = AstItem.Immediate(DataType.BOOL, token.Value);
            }
            else if (token.Kind == TokenKind.Char)
            {
                result = AstItem.Immediate(DataType.CHAR8, token.Value);
            }
            else if (token.Kind == TokenKind.String)
            {
                result = AstItem.Immediate(DataType.STRING8, token.Value);
            }
            else if (token.Kind == TokenKind.SquareBracketOpen)
            {
                result = AstItem.NewStackArray(ReadArray(tokens));
            }
            else
            {
                throw new Exception("Unexpected token type in expression: " + token);
            }

            return(result);
        }