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
5 years ago
|
%{
|
||
|
#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;
|
||
|
}
|
||
|
|
||
5 years ago
|
%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
|
||
5 years ago
|
|
||
|
%token <int_num> INTEGER
|
||
5 years ago
|
%type <int_num> EXPR_I
|
||
|
|
||
5 years ago
|
%token <str_ptr> STRING VARNAME
|
||
|
%token <byte_num> CHAR
|
||
5 years ago
|
|
||
5 years ago
|
%token <double_num> DOUBLEVAR
|
||
5 years ago
|
%type <double_num> EXPR_D
|
||
|
|
||
5 years ago
|
%token <float_num> FLOAT_NUM
|
||
|
%type <node_ptr> STMT PRINT VARIABLE FUNC STMT_LIST LOGIC
|
||
|
|
||
|
%%
|
||
|
|
||
|
PROGRAM:
|
||
|
MAIN '\n'
|
||
|
|
|
||
|
;
|
||
|
|
||
|
MAIN:
|
||
5 years ago
|
MAIN FUNC ';' { exec_stmt($2); }
|
||
5 years ago
|
|
|
||
|
;
|
||
|
|
||
|
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:
|
||
5 years ago
|
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);}
|
||
5 years ago
|
| VARNAME '=' '{' STMT_LIST '}' { $$ = create_function($1, $4); }
|
||
|
| VARNAME '=' VARNAME { $$ = move_value($1, $3); }
|
||
|
;
|
||
|
|
||
5 years ago
|
EXPR_I:
|
||
5 years ago
|
INTEGER { $$ = $1; }
|
||
5 years ago
|
| 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; }
|
||
5 years ago
|
;
|
||
|
|
||
|
PRINT:
|
||
|
VARNAME { $$ = create_print_variable($1); }
|
||
5 years ago
|
| EXPR_I { $$ = create_print_statement(VAR_INT, alloc_value(&$1, VAR_INT)); }
|
||
5 years ago
|
| STRING { $$ = create_print_statement(VAR_STRING, $1); }
|
||
|
;
|
||
|
%%
|
||
|
|
||
|
void yyerror(char *s)
|
||
|
{
|
||
|
fprintf(stderr, "Error on line %d, %s\n", yylineno, s);
|
||
5 years ago
|
}
|