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.
131 lines
5.1 KiB
131 lines
5.1 KiB
%{
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "parser/parser.c"
|
|
#include "functions/functions.c"
|
|
#include "logic/logic.c"
|
|
#include "print/print.c"
|
|
#include "includes/include.h"
|
|
|
|
int yylex(void);
|
|
void yyerror(char *);
|
|
|
|
extern int yylineno;
|
|
extern int _DEBUG;
|
|
|
|
//hashmaps/link list heads
|
|
map_void_t *_var_map = NULL;
|
|
node_info *_temp_statement_head = NULL;
|
|
//struct variable *_var_head = NULL;
|
|
map_void_t *_function_list = NULL;
|
|
|
|
%}
|
|
|
|
/* 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;
|
|
int num;
|
|
char character;
|
|
double Double;
|
|
struct node_info *nPtr;
|
|
|
|
}
|
|
|
|
|
|
%token INCREASE DECREASE PRINTF IF END KEYWORD_S KEYWORD_I KEYWORD_C QUOTE ELSE WHILE DEF CALL
|
|
%token <num> INTEGER
|
|
%token <str> STRING VARNAME
|
|
%token <character> CHAR
|
|
%token <Double> DOUBLEVAR
|
|
%type <num> expr
|
|
%type <nPtr> statement print VARIABLE FUNC statement_list
|
|
|
|
%%
|
|
|
|
program:
|
|
|
|
main '\n'
|
|
|
|
|
;
|
|
|
|
main:
|
|
|
|
main FUNC ';' { if (_DEBUG == 1 ) { printf(">>> "); ex($2); } else { ex($2); } }
|
|
| FUNC ';' { if (_DEBUG == 1 ) { printf(">>> "); ex($1); } else { ex($1); } }
|
|
| statement ';' { if (_DEBUG == 1 ) { printf(">>> "); ex($1); } else { ex($1); } }
|
|
;
|
|
|
|
statement:
|
|
|
|
VARIABLE { $$ = $1; } //this is a real var dec.
|
|
| FUNC { $$ = $1; }
|
|
| IF '[' expr ']' ':' '{' statement_list '}' { $$ = create_logic(1, $3, $7, NULL); } //examples of logic in this language
|
|
| WHILE '[' expr ']' ':' '{' statement_list '}' { $$ = create_logic(2, $3, $7, NULL); }
|
|
| PRINTF'['print']' { $$ = $3; }
|
|
| '{' statement_list '}' { $$ = $2; }
|
|
;
|
|
|
|
statement_list:
|
|
|
|
statement ';' { $$ = create_compound_statement($1, _temp_statement_head); }
|
|
| statement_list statement ';' { $$ = add_to_compound_statement($2, $1); }
|
|
;
|
|
|
|
FUNC:
|
|
|
|
DEF VARNAME ':' '{' statement_list '}' { $$ = create_function($2, $5); } // defining functions
|
|
| CALL VARNAME { $$ = create_function_call($2); } // calling functions
|
|
;
|
|
|
|
VARIABLE:
|
|
|
|
VARNAME '=' STRING { $$ = create_var_assignment_string($1, $3);}
|
|
| VARNAME '=' expr { $$ = create_var_assignment_int($1, $3);}
|
|
| VARNAME '=' CHAR { $$ = create_var_assignment_char($1, $3);}
|
|
| VARNAME '=' DOUBLEVAR { $$ = create_var_assignment_double($1, $3);}
|
|
| VARNAME '=' '{' statement_list '}' { $$ = create_function($1, $4); } // you can also define functions like this
|
|
;
|
|
|
|
|
|
expr:
|
|
INTEGER { $$ = $1; }
|
|
| '{' KEYWORD_I '}' VARNAME { $$ = get_value($4, _var_map)->_int; }
|
|
| expr '+' expr { $$ = $1 + $3; } /* addition */
|
|
| expr '-' expr { $$ = $1 - $3; } /* subtration */
|
|
| expr '*' expr { $$ = $1 * $3; } /* multiplication */
|
|
| expr '/' expr { $$ = $1 / $3; } /* division */
|
|
| expr '%' expr { $$ = $1 % $3; }
|
|
| expr '>' expr { if ($1 > $3) { $$ = 1; } else { $$ = 0;}}
|
|
| expr '<' expr { if ($1 < $3) { $$ = 1; } else { $$ = 0;}}
|
|
| '(' expr ')' { $$ = $2; }
|
|
;
|
|
|
|
//TODO update this.
|
|
print:
|
|
|
|
VARNAME { $$ = create_print_var_node($1); }
|
|
| expr { $$ = create_print_statement(1, $1, NULL); }
|
|
| STRING { $$ = create_print_statement(2, 0, $1); }
|
|
;
|
|
|
|
%%
|
|
|
|
void yyerror(char *s) {
|
|
fprintf(stderr, "Error on line %d, %s\n", yylineno, s);
|
|
}
|
|
|