]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/parser.cpp
Store actual values for literals
[libs/gl.git] / source / glsl / parser.cpp
index c9a812ddbd62f8c45184d565186404b33541f410..c924606f09c018c33388b020ec23100aa5a291f4 100644 (file)
@@ -496,11 +496,7 @@ RefPtr<Expression> Parser::parse_expression(unsigned precedence)
                                left = parexpr;
                        }
                        else if(isdigit(token[0]) || token=="true" || token=="false")
-                       {
-                               RefPtr<Literal> literal = new Literal;
-                               literal->token = tokenizer.parse_token();
-                               left = literal;
-                       }
+                               left = parse_literal();
                        else if(is_identifier(token))
                        {
                                RefPtr<VariableReference> var = create_node<VariableReference>();
@@ -522,6 +518,26 @@ RefPtr<Expression> Parser::parse_expression(unsigned precedence)
        }
 }
 
+RefPtr<Literal> Parser::parse_literal()
+{
+       RefPtr<Literal> literal = create_node<Literal>();
+       literal->token = tokenizer.parse_token();
+       if(isdigit(literal->token[0]))
+       {
+               // TODO have the tokenizer return the type of the token
+               if(isnumrc(literal->token))
+                       literal->value = lexical_cast<int>(literal->token);
+               else
+                       literal->value = lexical_cast<float>(literal->token);
+       }
+       else if(literal->token=="true" || literal->token=="false")
+               literal->value = (literal->token=="true");
+       else
+               throw parse_error(tokenizer.get_location(), literal->token, "a literal");
+
+       return literal;
+}
+
 RefPtr<BinaryExpression> Parser::parse_binary(const RefPtr<Expression> &left, const Operator &oper)
 {
        RefPtr<BinaryExpression> binary = (oper.precedence==16 ?