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.
122 lines
4.8 KiB
122 lines
4.8 KiB
5 years ago
|
%{
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include "parser/parser.c"
|
||
|
int yylex(void);
|
||
|
void yyerror(char *);
|
||
|
extern int yylineno;
|
||
|
extern int _DEBUG;
|
||
|
|
||
|
%}
|
||
|
|
||
|
/* 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 nodeInfo *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 '}' { $$ = createLogic(1, $3, $7, NULL); } //examples of logic in this language
|
||
|
| WHILE '[' expr ']' ':' '{' statement_list '}' { $$ = createLogic(2, $3, $7, NULL); }
|
||
|
| PRINTF'['print']' { $$ = $3; }
|
||
|
| '{' statement_list '}' { $$ = $2; }
|
||
|
;
|
||
|
|
||
|
statement_list:
|
||
|
|
||
|
statement ';' { $$ = createCompoundStatement($1); }
|
||
|
| statement_list statement ';' { $$ = addToCompoundStatement($2); }
|
||
|
;
|
||
|
|
||
|
FUNC:
|
||
|
|
||
|
DEF VARNAME ':' '{' statement_list '}' { $$ = createFunction($2, $5); } // defining functions
|
||
|
| CALL VARNAME { $$ = createFunctionCall($2); } // calling functions
|
||
|
;
|
||
|
|
||
|
VARIABLE:
|
||
|
|
||
|
VARNAME '=' STRING { $$ = createVarAssignmentString($1, $3);}
|
||
|
| VARNAME '=' expr { $$ = createVarAssignmentInt($1, $3);}
|
||
|
| VARNAME '=' CHAR { $$ = createVarAssignmentChar($1, $3);}
|
||
|
| VARNAME '=' DOUBLEVAR { $$ = createVarAssignmentDouble($1, $3);}
|
||
|
| VARNAME '=' '{' statement_list '}' { $$ = createFunction($1, $4); } // you can also define functions like this
|
||
|
;
|
||
|
|
||
|
|
||
|
expr:
|
||
|
INTEGER { $$ = $1; }
|
||
|
| '{' KEYWORD_I '}' VARNAME { $$ = getValue($4)->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; }
|
||
|
;
|
||
|
|
||
|
|
||
|
print:
|
||
|
|
||
|
VARNAME { $$ = createPrintVarNode($1); }
|
||
|
| expr { $$ = createPrintStatement(1, $1, NULL); }
|
||
|
| STRING { $$ = createPrintStatement(2, 0, $1); }
|
||
|
| ',' print { $$ = $2; }
|
||
|
;
|
||
|
|
||
|
%%
|
||
|
|
||
|
void yyerror(char *s) {
|
||
|
|
||
|
fprintf(stderr, "Error on line %d, %s\n", yylineno, s);
|
||
|
|
||
|
}
|
||
|
|