Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include "adreno/vm/vm.h"
- #include "adreno/vm/emit.h"
- extern char ras_text[];
- static AdrenoScript *emits;
- static AdrenoFunction *emitf;
- %}
- %token <string> IDENTIFIER STRING_LITERAL
- %token <integer> CONSTANT
- %token INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
- %token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
- %token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
- %token XOR_ASSIGN OR_ASSIGN
- %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
- %expect 1
- %start translation_unit
- %union {
- char *string;
- unsigned int integer;
- }
- %%
- threat_as_ref
- : { ras_set_prefix(P_REF); }
- ;
- func_call_start
- :
- ;
- func_call_end
- :
- ;
- variable
- : IDENTIFIER { ras_parse_variable_psh($1); aFree($1); }
- ;
- variable_ref
- : threat_as_ref variable
- ;
- primary_expression
- : variable
- | CONSTANT { ras_emit_op2(OP_LDC_I4, $1); }
- | STRING_LITERAL { ras_emit_op2(OP_LDSTR, ras_add_str($1)); aFree($1); }
- | '(' expression ')'
- ;
- postfix_expression
- : primary_expression
- | variable_ref '[' expression ']' { ras_emit_op(OP_LDELEM); }
- | func_call_start IDENTIFIER { ras_parse_variable_psh($2); aFree($2); } '(' ')' func_call_end
- | func_call_start IDENTIFIER { ras_parse_variable_psh($2); aFree($2); } '(' argument_expression_list ')' func_call_end
- | IDENTIFIER INC_OP { int id = ras_parse_variable($1); if (id != -1) ras_load_add(id); aFree($1); }
- | IDENTIFIER DEC_OP { int id = ras_parse_variable($1); if (id != -1) ras_load_sub(id); aFree($1); }
- ;
- argument_expression_list
- : assignment_expression
- | argument_expression_list ',' assignment_expression
- ;
- unary_expression
- : postfix_expression
- | INC_OP IDENTIFIER { int id = ras_parse_variable($2); if (id != -1) ras_add_load(id); aFree($2); }
- | DEC_OP IDENTIFIER { int id = ras_parse_variable($2); if (id != -1) ras_sub_load(id); aFree($2); }
- | '-' multiplicative_expression { ras_emit_op(OP_NEG); }
- | '~' multiplicative_expression { ras_emit_op(OP_NOT); }
- | '!' multiplicative_expression { ras_emit_op(OP_LNOT); }
- ;
- multiplicative_expression
- : unary_expression
- | multiplicative_expression '*' unary_expression { ras_emit_op(OP_MUL); }
- | multiplicative_expression '/' unary_expression { ras_emit_op(OP_DIV); }
- | multiplicative_expression '%' unary_expression { ras_emit_op(OP_REM); }
- ;
- additive_expression
- : multiplicative_expression
- | additive_expression '+' multiplicative_expression { ras_emit_op(OP_ADD); }
- | additive_expression '-' multiplicative_expression { ras_emit_op(OP_SUB); }
- ;
- shift_expression
- : additive_expression
- | shift_expression LEFT_OP additive_expression { ras_emit_op(OP_SHL); }
- | shift_expression RIGHT_OP additive_expression { ras_emit_op(OP_SHR); }
- ;
- relational_expression
- : shift_expression
- | relational_expression '<' shift_expression { ras_emit_op(OP_LT); }
- | relational_expression '>' shift_expression { ras_emit_op(OP_GT); }
- | relational_expression LE_OP shift_expression { ras_emit_op(OP_LET); }
- | relational_expression GE_OP shift_expression { ras_emit_op(OP_GET); }
- ;
- equality_expression
- : relational_expression
- | equality_expression EQ_OP relational_expression { ras_emit_op(OP_EQ); }
- | equality_expression NE_OP relational_expression { ras_emit_op(OP_NE); }
- ;
- and_expression
- : equality_expression
- | and_expression '&' equality_expression { ras_emit_op(OP_AND); }
- ;
- exclusive_or_expression
- : and_expression
- | exclusive_or_expression '^' and_expression { ras_emit_op(OP_XOR); }
- ;
- inclusive_or_expression
- : exclusive_or_expression
- | inclusive_or_expression '|' exclusive_or_expression { ras_emit_op(OP_OR); }
- ;
- logical_and_expression
- : inclusive_or_expression
- | logical_and_expression AND_OP inclusive_or_expression { ras_emit_op(OP_LAND); }
- ;
- logical_or_expression
- : logical_and_expression
- | logical_or_expression OR_OP logical_and_expression { ras_emit_op(OP_LOR); }
- ;
- conditional_expression
- : logical_or_expression
- | logical_or_expression '?' expression ':' conditional_expression
- ;
- assignment_expression
- : conditional_expression
- | variable_ref '=' assignment_expression { ras_assign(); }
- | variable_ref MUL_ASSIGN assignment_expression { ras_assign_op(OP_MUL); }
- | variable_ref DIV_ASSIGN assignment_expression { ras_assign_op(OP_DIV); }
- | variable_ref MOD_ASSIGN assignment_expression { ras_assign_op(OP_REM); }
- | variable_ref ADD_ASSIGN assignment_expression { ras_assign_op(OP_ADD); }
- | variable_ref SUB_ASSIGN assignment_expression { ras_assign_op(OP_SUB); }
- | variable_ref LEFT_ASSIGN assignment_expression { ras_assign_op(OP_SHL); }
- | variable_ref RIGHT_ASSIGN assignment_expression { ras_assign_op(OP_SHR); }
- | variable_ref AND_ASSIGN assignment_expression { ras_assign_op(OP_AND); }
- | variable_ref XOR_ASSIGN assignment_expression { ras_assign_op(OP_XOR); }
- | variable_ref OR_ASSIGN assignment_expression { ras_assign_op(OP_OR); }
- ;
- expression
- : assignment_expression
- ;
- constant_expression
- : conditional_expression
- ;
- labeled_statement
- : IDENTIFIER ':' statement { ras_start_function($1); aFree($1); }
- | CASE constant_expression ':' statement
- | DEFAULT ':' statement
- ;
- expression_statement
- : ';'
- | expression ';'
- | func_call_start IDENTIFIER { ras_parse_variable_psh($2); aFree($2); } func_call_end ';'
- | func_call_start IDENTIFIER { ras_parse_variable_psh($2); aFree($2); } argument_expression_list func_call_end ';'
- ;
- selection_statement
- : IF '(' expression ')' statement
- | IF '(' expression ')' statement ELSE statement
- | SWITCH '(' expression ')' statement
- ;
- iteration_statement
- : WHILE '(' expression ')' statement
- | DO statement WHILE '(' expression ')' ';'
- | FOR '(' expression_statement expression_statement ')' statement
- | FOR '(' expression_statement expression_statement expression ')' statement
- ;
- jump_statement
- : GOTO IDENTIFIER ';'
- | CONTINUE ';'
- | BREAK ';'
- | RETURN ';'
- | RETURN expression ';'
- ;
- statement
- : labeled_statement
- | compound_statement
- | expression_statement
- | selection_statement
- | iteration_statement
- | jump_statement
- ;
- statement_list
- : statement
- | statement_list statement
- ;
- compound_statement
- : '{' '}'
- | '{' statement_list '}'
- ;
- translation_unit
- : { ras_emit_op(OP_ENTER); } compound_statement { ras_emit_op(OP_END); }
- ;
- %%
- void yyerror(char *s)
- {
- fflush(stdout);
- printf("%s\n", s);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement