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.

120 lines
5.1 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;
}
%token INCREASE DECREASE PRINT_TOKEN IF END KEYWORD_S KEYWORD_I KEYWORD_C QUOTE ELSE WHILE DEF CALL
%token <int_num> INTEGER
%token <str_ptr> STRING VARNAME
%token <byte_num> CHAR
%token <double_num> DOUBLEVAR
%token <float_num> FLOAT_NUM
%type <int_num> EXPR
%type <node_ptr> STMT PRINT VARIABLE FUNC STMT_LIST LOGIC
%%
PROGRAM:
MAIN '\n'
|
;
MAIN:
MAIN FUNC ';' { if (_DEON_DEBUG == true ) { printf(">>> "); exec_stmt($2); } else { 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_const($1, (void *) $3, VAR_STRING);}
| VARNAME '=' EXPR { $$ = create_variable_const($1, alloc_value(&$3, VAR_INT), VAR_INT);}
| VARNAME '=' CHAR { $$ = create_variable_const($1, alloc_value(&$3, VAR_BYTE), VAR_BYTE);}
| VARNAME '=' DOUBLEVAR { $$ = create_variable_const($1, alloc_value(&$3, VAR_DOUBLE), VAR_DOUBLE);}
| VARNAME '=' '{' STMT_LIST '}' { $$ = create_function($1, $4); }
| VARNAME '=' VARNAME { $$ = move_value($1, $3); }
;
EXPR:
INTEGER { $$ = $1; }
| EXPR '+' EXPR { $$ = $1 + $3; }
| EXPR '-' EXPR { $$ = $1 - $3; }
| EXPR '*' EXPR { $$ = $1 * $3; }
| EXPR '/' EXPR { $$ = $1 / $3; }
| EXPR '%' EXPR { $$ = $1 % $3; }
| EXPR '>' EXPR { if ($1 > $3) { $$ = 1; } else { $$ = 0;}}
| EXPR '<' EXPR { if ($1 < $3) { $$ = 1; } else { $$ = 0;}}
| INCREASE EXPR { $$ = $2 + 1; }
| EXPR INCREASE { $$ = $1 + 1; }
| DECREASE EXPR { $$ = $2 - 1; }
| EXPR DECREASE { $$ = $1 - 1; }
| '(' EXPR ')' { $$ = $2; }
;
PRINT:
VARNAME { $$ = create_print_variable($1); }
| EXPR { $$ = 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);
}