You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
6.9 KiB
144 lines
6.9 KiB
%{
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "types.h"
|
|
#include "parser/parser.c"
|
|
#include "functions/functions.c"
|
|
#include "logic/logic.c"
|
|
#include "print/print.c"
|
|
#include "vars/vars.c"
|
|
|
|
int yylex(void);
|
|
void yyerror(char *);
|
|
extern int yylineno;
|
|
%}
|
|
|
|
/* doing these before multiplication and division because they have a lower presidence */
|
|
%left '<' '>'
|
|
|
|
%left '+' '-'
|
|
/* doing this last so they have a higher presidence */
|
|
%left '*' '/' '%'
|
|
|
|
%union {
|
|
char* str_ptr;
|
|
int int_num;
|
|
unsigned uint_num;
|
|
char byte_num;
|
|
double double_num;
|
|
float float_num;
|
|
struct node_info *node_ptr;
|
|
}
|
|
|
|
%token INCREASE PRINT_TOKEN IF END KEYWORD_S KEYWORD_I KEYWORD_C QUOTE ELSE WHILE DEF CALL
|
|
%token LESS_THAN_OR_EQUAL
|
|
%token GREATER_THAN_OR_EQUAL
|
|
%token DECREASE
|
|
|
|
%token <int_num> INTEGER
|
|
%type <int_num> EXPR_I
|
|
|
|
%token <str_ptr> STRING VARNAME
|
|
%token <byte_num> CHAR
|
|
|
|
%token <double_num> DOUBLEVAR
|
|
%type <double_num> EXPR_D
|
|
|
|
%token <float_num> FLOAT_NUM
|
|
%type <node_ptr> STMT PRINT VARIABLE FUNC STMT_LIST LOGIC
|
|
|
|
%%
|
|
|
|
PROGRAM:
|
|
MAIN '\n'
|
|
|
|
|
;
|
|
|
|
MAIN:
|
|
MAIN FUNC ';' { exec_stmt($2); }
|
|
|
|
|
;
|
|
|
|
FUNC:
|
|
DEF VARNAME ':' '{' STMT_LIST '}' { $$ = create_function($2, $5); }
|
|
| CALL VARNAME { $$ = create_function_call($2); }
|
|
;
|
|
|
|
STMT_LIST:
|
|
STMT ';' { $$ = create_compound_statement($1, _temp_statement_head); }
|
|
| STMT_LIST STMT ';' { $$ = add_to_compound_statement($2, $1); }
|
|
;
|
|
|
|
STMT:
|
|
VARIABLE { $$ = $1; }
|
|
| LOGIC { $$ = $1; }
|
|
| FUNC { $$ = $1; }
|
|
| PRINT_TOKEN '[' PRINT ']' { $$ = $3; }
|
|
| '{' STMT_LIST '}' { $$ = $2; }
|
|
;
|
|
|
|
LOGIC:
|
|
IF '[' VARNAME ']' ':' '{' STMT_LIST '}' { $$ = create_logic_node(create_logic(IF_LOGIC, $7, $3)); }
|
|
| WHILE '[' VARNAME ']' ':' '{' STMT_LIST '}' { $$ = create_logic_node(create_logic(WHILE_LOGIC, $7, $3)); }
|
|
;
|
|
|
|
VARIABLE:
|
|
VARNAME '=' STRING { $$ = create_variable($1, (void *) $3, VAR_STRING);}
|
|
| VARNAME '=' EXPR_I { $$ = create_variable($1, alloc_value(&$3, VAR_INT), VAR_INT);}
|
|
| VARNAME '=' EXPR_D { $$ = create_variable($1, alloc_value(&$3, VAR_DOUBLE), VAR_DOUBLE);}
|
|
| VARNAME '=' CHAR { $$ = create_variable($1, alloc_value(&$3, VAR_BYTE), VAR_BYTE);}
|
|
| VARNAME '=' DOUBLEVAR { $$ = create_variable($1, alloc_value(&$3, VAR_DOUBLE), VAR_DOUBLE);}
|
|
| VARNAME '=' '{' STMT_LIST '}' { $$ = create_function($1, $4); }
|
|
| VARNAME '=' VARNAME { $$ = move_value($1, $3); }
|
|
;
|
|
|
|
EXPR_I:
|
|
INTEGER { $$ = $1; }
|
|
| EXPR_I '+' EXPR_I { $$ = $1 + $3; }
|
|
| EXPR_I '-' EXPR_I { $$ = $1 - $3; }
|
|
| EXPR_I '*' EXPR_I { $$ = $1 * $3; }
|
|
| EXPR_I '/' EXPR_I { $$ = $1 / $3; }
|
|
| EXPR_I '%' EXPR_I { $$ = $1 % $3; }
|
|
| EXPR_I LESS_THAN_OR_EQUAL EXPR_I { $$ = $1 <= $3; }
|
|
| EXPR_I GREATER_THAN_OR_EQUAL EXPR_I { $$ = $1 >= $3; }
|
|
| EXPR_I '>' EXPR_I { $$ = $1 > $3; }
|
|
| EXPR_I '<' EXPR_I { $$ = $1 < $3; }
|
|
| INCREASE EXPR_I { $$ = $2 + 1; }
|
|
| EXPR_I INCREASE { $$ = $1 + 1; }
|
|
| DECREASE EXPR_I { $$ = $2 - 1; }
|
|
| EXPR_I DECREASE { $$ = $1 - 1; }
|
|
| '(' EXPR_I ')' { $$ = $2; }
|
|
| EXPR_I { $$ = $1; }
|
|
;
|
|
|
|
EXPR_D:
|
|
DOUBLEVAR
|
|
| EXPR_D '+' EXPR_D { $$ = $1 + $3; }
|
|
| EXPR_D '-' EXPR_D { $$ = $1 - $3; }
|
|
| EXPR_D '*' EXPR_D { $$ = $1 * $3; }
|
|
| EXPR_D '/' EXPR_D { $$ = $1 / $3; }
|
|
| EXPR_D LESS_THAN_OR_EQUAL EXPR_D { $$ = $1 <= $3; }
|
|
| EXPR_D GREATER_THAN_OR_EQUAL EXPR_D { $$ = $1 >= $3; }
|
|
| EXPR_D '>' EXPR_D { $$ = $1 > $3; }
|
|
| EXPR_D '<' EXPR_D { $$ = $1 < $3; }
|
|
| INCREASE EXPR_D { $$ = $2 + 1; }
|
|
| EXPR_D INCREASE { $$ = $1 + 1; }
|
|
| DECREASE EXPR_D { $$ = $2 - 1; }
|
|
| EXPR_D DECREASE { $$ = $1 - 1; }
|
|
| '(' EXPR_D ')' { $$ = $2; }
|
|
| EXPR_D { $$ = $1; }
|
|
;
|
|
|
|
PRINT:
|
|
VARNAME { $$ = create_print_variable($1); }
|
|
| EXPR_I { $$ = create_print_statement(VAR_INT, alloc_value(&$1, VAR_INT)); }
|
|
| STRING { $$ = create_print_statement(VAR_STRING, $1); }
|
|
;
|
|
%%
|
|
|
|
void yyerror(char *s)
|
|
{
|
|
fprintf(stderr, "Error on line %d, %s\n", yylineno, s);
|
|
} |