Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added boolean literals to parser.
  • Loading branch information
icculus committed Oct 20, 2010
1 parent d1d5f6f commit db69e64
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 21 additions & 1 deletion mojoshader_compiler.c
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions mojoshader_parser_hlsl.lemon
Expand Up @@ -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 * }
Expand Down

0 comments on commit db69e64

Please sign in to comment.