%{ #include "mbasic.h" #include "mbasic.c" %} %union { double number; char *string; int boolean; }; %token UNREC %token STRLIT %token SVAR %type sexpr %token NUMLIT %token NVAR %type nexpr %token BOOLIT %token BVAR %type bexpr %token EOL %token CLS %token INPUT %token PRINT %token DIM %token AS %token NUMBER %token STRING %token BOOLEAN %token IF THEN ELSE %token AND OR NOT %token EQ NE GT GE LT LE %token '=' %left '-' '+' %left '*' '/' %right '^' %% input: /* empty */ | input line ; line: EOL | stmt EOL | error EOL { yyerrok; } ; stmt: CLS { system("clear"); } | PRINT nexpr { printf("%f\n",$2); } | PRINT sexpr { printf("%s\n",$2); } | PRINT { printf("\n"); }; | INPUT UNREC { yyerror("Variable not defined"); } | INPUT NVAR { InputNumber(stdin,$2,0); } | INPUT STRLIT ';' NVAR { printf("%s",$2); InputNumber(stdin,$4,0); } | INPUT NVAR '(' nexpr ')' { InputNumber(stdin,$2,$4); } | INPUT STRLIT ';' NVAR '(' nexpr ')' { printf("%s",$2); InputNumber(stdin,$4,$6); } | INPUT SVAR { InputString(stdin,$2,0); } | INPUT STRLIT ';' SVAR { printf("%s",$2); InputString(stdin,$4,0); } | INPUT SVAR '(' nexpr ')' { InputString(stdin,$2,$4); } | INPUT STRLIT ';' SVAR '(' nexpr ')' { printf("%s",$2); InputString(stdin,$4,$6); } | DIM UNREC AS NUMBER { Dim($2,0,TP_NUMBER); } | DIM UNREC '(' nexpr ')' AS NUMBER { Dim($2,$4,TP_NUMBER); } | UNREC '=' nexpr { yyerror("Variable not defined"); } | NVAR '=' nexpr { PutNumber($1,0,$3); } | NVAR '(' nexpr ')' '=' nexpr { PutNumber($1,$3,$6); } | DIM UNREC AS STRING { Dim($2,0,TP_STRING); } | DIM UNREC '(' nexpr ')' AS STRING { Dim($2,$4,TP_STRING); } | UNREC '=' sexpr { yyerror("Variable not defined"); } | SVAR '=' sexpr { PutString($1,0,$3); } | SVAR '(' nexpr ')' '=' sexpr { PutString($1,$3,$6); } | DIM UNREC AS BOOLEAN { Dim($2,0,TP_BOOLEAN); } | DIM UNREC '(' nexpr ')' AS BOOLEAN { Dim($2,$4,TP_BOOLEAN); } | BVAR '=' UNREC { yyerror("Variable not defined"); } | BVAR '=' bexpr { PutBoolean($1,0,$3); } | BVAR '(' nexpr ')' '=' bexpr { PutBoolean($1,$3,$6); } | IF bexpr { printf("Answer: %i\n",$2); } ; nexpr: NUMLIT { $$ = $1; } | NVAR { $$ = GetNumber($1,0); } | NVAR '(' nexpr ')' { $$ = GetNumber($1,$3); } | '-' nexpr { $$ = 0-$2; } | nexpr '+' nexpr { $$ = $1 + $3; } | nexpr '-' nexpr { $$ = $1 - $3; } | nexpr '*' nexpr { $$ = $1 * $3; } | nexpr '/' nexpr { $$ = $1 / $3; } | '(' nexpr ')' { $$ = $2; } ; sexpr: STRLIT { $$ = $1; } | SVAR { $$ = GetString($1,0); } | SVAR '(' nexpr ')' { $$ = GetString($1,$3); } | sexpr '+' sexpr { $$ = ConcatString($1,$3); } | sexpr '+' nexpr { $$ = ConcatString($1,Str($3)); } ; bexpr: BOOLIT { $$ = $1; } | nexpr EQ nexpr { if($1 == $3) { $$ = 1; } else { $$ = 0; } } | nexpr NE nexpr { if($1 != $3) { $$ = 1; } else { $$ = 0; } } | nexpr GT nexpr { if($1 > $3) { $$ = 1; } else { $$ = 0; } } | nexpr GE nexpr { if($1 >= $3) { $$ = 1; } else { $$ = 0; } } | nexpr LT nexpr { if($1 < $3) { $$ = 1; } else { $$ = 0; } } | nexpr LE nexpr { if($1 <= $3) { $$ = 1; } else { $$ = 0; } } | bexpr AND bexpr { if($1 && $3) { $$ = 1; } else { $$ = 0; } } ; %% yyerror(char *s) { fprintf(stderr,"%s\n",s); }