-
Notifications
You must be signed in to change notification settings - Fork 0
/
mfcalc.y
149 lines (137 loc) · 5.35 KB
/
mfcalc.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
%{
#include <stdlib.h> /* malloc. */
#include <string.h> /* strlen. */
#include <stdio.h> /* For printf, etc. */
#include <math.h> /* For pow, used in the grammar. */
extern int yylex(void);
extern void yyterminate();
void yyerror(const char *s);
extern FILE* yyin;
%}
%code top{
void yyerror (char const *s);
}
%code requires {
#include "SymbolTable.h"
#include "errors.h"
}
%define api.value.type union /* Generate YYSTYPE from these types: */
%token <int> END_OF_FILE
%token <int> RESTART_CONTEXT
%token <int> SHOW_CONSTANTS
%token <int> SHOW_FUNCTIONS
%token <int> SHOW_VARIABLES
%token <int> SHOW_SYMBOL_TABLE
%token <int> HELP
%token <int> QUIT
%token <double> NUM /* Simple double precision number. */
%token <symrec*> VAR FNCT /* Symbol table pointer: variable and function. */
%type <double> exp
%precedence '='
%left '-' '+'
%left '*' '/'
%precedence NEG /* negation--unary minus */
%right '^' /* exponentiation */
%% /* The grammar follows. */
input:
%empty
| input line
;
line:
'\n'
| END_OF_FILE
| QUIT '\n' { YYACCEPT;}
| RESTART_CONTEXT '\n' { // destroys the sumbol table and creates a new one
symbolTableDestroy(symbolTable);
symbolTable = symbolTableCreate();
}
| SHOW_CONSTANTS '\n'{ //how availble contants
printf("Available Constants\n");
printf("=========================\n");
symbolTablePrintType(symbolTable,-1);}
| SHOW_FUNCTIONS '\n'{ //shows available functions
printf("Available Functions\n");
printf("=========================\n");
symbolTablePrintType(symbolTable,FNCT);}
| SHOW_VARIABLES '\n'{ //shows available variables
printf("Current Workspace\n");
printf("=========================\n");
symbolTablePrintType(symbolTable,VAR);}
| SHOW_SYMBOL_TABLE '\n'{ //shows full symbol table
printf("Symbol Table\n");
printf("=========================\n");
symbolTablePrint(symbolTable);
}
| HELP '\n' { //shows system help
printf("\n CLIMath v0.1 System Help");
printf("\n===============================");
printf("\nWelcome to the first version of this command line interface");
printf("\n");
printf("\nYou can use \";\" at the end of a command to supress the command output");
printf("\n");
printf("\nAvailable options:");
printf("\n:? --> Shows help menu.");
printf("\n:f --> Shows availble functions.");
printf("\n:v --> Shows variables.");
printf("\n:t --> Shows full symbol table.");
printf("\n:l --> Load scritp.");
printf("\n\t :l pathToFile");
printf("\n:r --> Resets the current workspace.");
printf("\n:q --> Quit.");
printf("\n");
}
| exp ';' '\n' { ; }
| exp '\n' { printf ("%.10g\n", $1); }
| error '\n' { yyerrok; }
;
exp:
NUM { $$ = $1; /* gets thje value of a number */ }
| VAR { //access to a var value
//checks if users tries to check the value of a function
if($1->type == FNCT){
$$ = 0;
showError(ERROR_VALUE_OF_FUNCTION);
}else{
//gets the value of the variable if it's initialized
if(!$1->initialized)
showError(ERROR_NOT_INITIALICED_VARIABLE);
$$ = $1->value.var;
}
}
| VAR '=' exp { //if user is accesing a variable, initialices the value
if($1->type == VAR){
$$ = $3; $1->value.var = $3;
$1->initialized = true;
}else{
showError(ERROR_OVERWITE);
$$ = 0;
}
}
| VAR '(' exp ')' { //invokes a function using a expr as an argument
if($1->type == FNCT)
$$ = (*($1->value.fnctptr))($3);
else{
$$ = 0;
showError(ERROR_NOT_A_FUNCTION);
}
}
//basic arithmetic operations
| exp '+' exp { $$ = $1 + $3; }
| exp '-' exp { $$ = $1 - $3; }
| exp '*' exp { $$ = $1 * $3; }
| exp '/' exp { if($3 == 0) //error if user divides by zero
showError(ERROR_DIVISION_BY_ZERO);
else
$$ = $1 / $3; }
| '-' exp %prec NEG { $$ = -$2; }
| exp '^' exp { $$ = pow ($1, $3); }
| '(' exp ')' { $$ = $2; }
;
/* End of grammar. */
%%
/* Called by yyparse on error. */
void yyerror (char const *s){
printf("\033[0;31m");
printf("%s",s);
printf("\033[0m\n");
}