From: Mikko Rasa Date: Fri, 5 Mar 2021 20:09:31 +0000 (+0200) Subject: Store actual values for literals X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=fd44325059d59d32d47ef3feb6d41d846f7f36f0;p=libs%2Fgl.git Store actual values for literals --- diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index f5b6b7ad..07181d35 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -99,9 +99,15 @@ void ConstantSpecializer::visit(VariableDeclaration &var) { RefPtr literal = new Literal; if(var.type=="bool") + { literal->token = (i->second ? "true" : "false"); + literal->value = static_cast(i->second); + } else if(var.type=="int") + { literal->token = lexical_cast(i->second); + literal->value = i->second; + } var.init_expression = literal; } } diff --git a/source/glsl/parser.cpp b/source/glsl/parser.cpp index c9a812dd..c924606f 100644 --- a/source/glsl/parser.cpp +++ b/source/glsl/parser.cpp @@ -496,11 +496,7 @@ RefPtr Parser::parse_expression(unsigned precedence) left = parexpr; } else if(isdigit(token[0]) || token=="true" || token=="false") - { - RefPtr literal = new Literal; - literal->token = tokenizer.parse_token(); - left = literal; - } + left = parse_literal(); else if(is_identifier(token)) { RefPtr var = create_node(); @@ -522,6 +518,26 @@ RefPtr Parser::parse_expression(unsigned precedence) } } +RefPtr Parser::parse_literal() +{ + RefPtr literal = create_node(); + 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(literal->token); + else + literal->value = lexical_cast(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 Parser::parse_binary(const RefPtr &left, const Operator &oper) { RefPtr binary = (oper.precedence==16 ? diff --git a/source/glsl/parser.h b/source/glsl/parser.h index 26867ec4..13ec0ab9 100644 --- a/source/glsl/parser.h +++ b/source/glsl/parser.h @@ -71,6 +71,7 @@ private: template void parse_block(Block &, bool, RefPtr (Parser::*)()); RefPtr parse_expression(unsigned = 0); + RefPtr parse_literal(); RefPtr parse_binary(const RefPtr &, const Operator &); RefPtr parse_function_call(const VariableReference &); RefPtr parse_type_declaration(); diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index 0379a911..ae05c02e 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -7,6 +7,7 @@ #include #include #include +#include #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 &);