Added boolean literals to parser.
authorRyan C. Gordon <icculus@icculus.org>
Wed, 20 Oct 2010 02:19:34 -0400
changeset 924 c99418745e1b
parent 923 859fc7151d43
child 925 2a7cae5f1d68
Added boolean literals to parser.
mojoshader_compiler.c
mojoshader_parser_hlsl.lemon
--- a/mojoshader_compiler.c	Tue Oct 19 17:48:24 2010 -0400
+++ b/mojoshader_compiler.c	Wed Oct 20 02:19:34 2010 -0400
@@ -102,6 +102,7 @@
     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 @@
     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 @@
     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 @@
             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 @@
             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;
--- a/mojoshader_parser_hlsl.lemon	Tue Oct 19 17:48:24 2010 -0400
+++ b/mojoshader_parser_hlsl.lemon	Wed Oct 20 02:19:34 2010 -0400
@@ -460,6 +460,8 @@
 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 * }