From db69e64d6373e0535be01fdd789830684015cf3f Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 20 Oct 2010 02:19:34 -0400 Subject: [PATCH] Added boolean literals to parser. --- mojoshader_compiler.c | 22 +++++++++++++++++++++- mojoshader_parser_hlsl.lemon | 2 ++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/mojoshader_compiler.c b/mojoshader_compiler.c index 0307db4b..2f22d3e5 100644 --- a/mojoshader_compiler.c +++ b/mojoshader_compiler.c @@ -102,6 +102,7 @@ typedef enum ASTNodeType AST_OP_INT_LITERAL, AST_OP_FLOAT_LITERAL, AST_OP_STRING_LITERAL, + AST_OP_BOOLEAN_LITERAL, AST_OP_END_RANGE_DATA, AST_OP_START_RANGE_MISC, @@ -259,6 +260,12 @@ typedef struct ExpressionStringLiteral const char *string; } ExpressionStringLiteral; +typedef struct ExpressionBooleanLiteral +{ + ASTNode ast; // Always AST_OP_BOOLEAN_LITERAL + int value; // Always 1 or 0. +} ExpressionBooleanLiteral; + typedef struct ExpressionConstructor { ASTNode ast; // Always AST_OP_CONSTRUCTOR @@ -800,7 +807,14 @@ static Expression *new_literal_string_expr(Context *ctx, const char *string) NEW_AST_NODE(retval, ExpressionStringLiteral, AST_OP_STRING_LITERAL); retval->string = string; // cached; don't copy string. return (Expression *) retval; -} // new_string_literal_expr +} // new_literal_string_expr + +static Expression *new_literal_boolean_expr(Context *ctx, const int value) +{ + NEW_AST_NODE(retval, ExpressionBooleanLiteral, AST_OP_BOOLEAN_LITERAL); + retval->value = value; + return (Expression *) retval; +} // new_literal_boolean_expr static void delete_expr(Context *ctx, Expression *expr) { @@ -1759,6 +1773,10 @@ static void print_ast(const int substmt, void *ast) printf("\"%s\"", ((ExpressionStringLiteral *) ast)->string); break; + case AST_OP_BOOLEAN_LITERAL: + printf("%s", ((ExpressionBooleanLiteral *) ast)->value ? "true" : "false"); + break; + case AST_OP_CONSTRUCTOR: printf("%s(", ((ExpressionConstructor *) ast)->datatype); print_ast(0, ((ExpressionConstructor *) ast)->args); @@ -2416,6 +2434,8 @@ static int convert_to_lemon_token(Context *ctx, const char *token, if (tokencmp("samplerCUBE")) return TOKEN_HLSL_SAMPLERCUBE; if (tokencmp("sampler_state")) return TOKEN_HLSL_SAMPLER_STATE; if (tokencmp("SamplerState")) return TOKEN_HLSL_SAMPLERSTATE; + if (tokencmp("true")) return TOKEN_HLSL_TRUE; + if (tokencmp("false")) return TOKEN_HLSL_FALSE; if (tokencmp("SamplerComparisonState")) return TOKEN_HLSL_SAMPLERCOMPARISONSTATE; if (tokencmp("isolate")) return TOKEN_HLSL_ISOLATE; if (tokencmp("maxInstructionCount")) return TOKEN_HLSL_MAXINSTRUCTIONCOUNT; diff --git a/mojoshader_parser_hlsl.lemon b/mojoshader_parser_hlsl.lemon index 5582adfb..9f420101 100644 --- a/mojoshader_parser_hlsl.lemon +++ b/mojoshader_parser_hlsl.lemon @@ -460,6 +460,8 @@ primary_expr(A) ::= IDENTIFIER(B). { A = new_identifier_expr(ctx, B.string); } primary_expr(A) ::= INT_CONSTANT(B). { A = new_literal_int_expr(ctx, B.i64); } primary_expr(A) ::= FLOAT_CONSTANT(B). { A = new_literal_float_expr(ctx, B.dbl); } primary_expr(A) ::= STRING_LITERAL(B). { A = new_literal_string_expr(ctx, B.string); } +primary_expr(A) ::= TRUE. { A = new_literal_boolean_expr(ctx, 1); } +primary_expr(A) ::= FALSE. { A = new_literal_boolean_expr(ctx, 0); } primary_expr(A) ::= LPAREN expression(B) RPAREN. { A = B; } %type postfix_expr { Expression * }