]> git.tdb.fi Git - libs/gl.git/commitdiff
Store actual values for literals
authorMikko Rasa <tdb@tdb.fi>
Fri, 5 Mar 2021 20:09:31 +0000 (22:09 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 5 Mar 2021 23:00:31 +0000 (01:00 +0200)
source/glsl/generate.cpp
source/glsl/parser.cpp
source/glsl/parser.h
source/glsl/syntax.h

index f5b6b7adb91769277f95de93d56d605e38cc88f7..07181d3583e69fbc1067292bbaddc7855c58a5ac 100644 (file)
@@ -99,9 +99,15 @@ void ConstantSpecializer::visit(VariableDeclaration &var)
                {
                        RefPtr<Literal> literal = new Literal;
                        if(var.type=="bool")
+                       {
                                literal->token = (i->second ? "true" : "false");
+                               literal->value = static_cast<bool>(i->second);
+                       }
                        else if(var.type=="int")
+                       {
                                literal->token = lexical_cast<string>(i->second);
+                               literal->value = i->second;
+                       }
                        var.init_expression = literal;
                }
        }
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 ?
index 26867ec4dcd881230229b253927457ba1f1a72d7..13ec0ab9a1f8de2f0acdd279e3a4bef1ed35403e 100644 (file)
@@ -71,6 +71,7 @@ private:
        template<typename T>
        void parse_block(Block &, bool, RefPtr<T> (Parser::*)());
        RefPtr<Expression> parse_expression(unsigned = 0);
+       RefPtr<Literal> parse_literal();
        RefPtr<BinaryExpression> parse_binary(const RefPtr<Expression> &, const Operator &);
        RefPtr<FunctionCall> parse_function_call(const VariableReference &);
        RefPtr<TypeDeclaration> parse_type_declaration();
index 0379a91146c17c3cbb02680717bea4036bcfb83b..ae05c02e4379c429207418fdadac16ba81b3d011 100644 (file)
@@ -7,6 +7,7 @@
 #include <string>
 #include <vector>
 #include <msp/core/refptr.h>
+#include <msp/core/variant.h>
 #include "features.h"
 #include "glsl_error.h"
 #include "sourcemap.h"
@@ -138,6 +139,7 @@ struct Expression: Node
 struct Literal: Expression
 {
        std::string token;
+       Variant value;
 
        virtual Literal *clone() const { return new Literal(*this); }
        virtual void visit(NodeVisitor &);