-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortlisp.c
201 lines (165 loc) · 4.21 KB
/
shortlisp.c
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "mpc.h"
#include <stdio.h>
#include <stdlib.h>
#include <editline/history.h>
#include <editline/readline.h>
// create enumeration of possible error types
enum { LERR_DIV_ZERO, LERR_BAD_OP, LERR_BAD_NUM };
// create enumeration of possible lval types
enum { LVAL_NUM, LVAL_ERR };
// declare new lval struct
typedef struct
{
int type;
long num;
int err;
} lval;
// create a new number type lval
lval lval_num(long x)
{
lval v;
v.type = LVAL_NUM;
v.num = x;
return v;
}
// create a new error type lval
lval lval_err(int x)
{
lval v;
v.type = LVAL_ERR;
v.err = x;
return v;
}
// print an lval
void lval_print(lval v)
{
switch (v.type)
{
// in the case the type is a number print it
// then break out of the switch
case LVAL_NUM: printf("%li", v.num); break;
// in the case the type is an error
case LVAL_ERR:
// check what type of error it is and print it
if (v.err == LERR_DIV_ZERO)
{
printf("Error: Division by Zero");
}
if (v.err == LERR_BAD_OP)
{
printf("Error: Invalid Operator");
}
if (v.err == LERR_BAD_NUM)
{
printf("Error: Invalid Number");
}
break;
}
}
// print an lval followed by a newline
void lval_println(lval l) { lval_print(l); putchar('\n'); }
// use operator string to see which operator to perform
lval eval_op(lval x, char* op, lval y)
{
// if either value is an error return it
if (x.type == LVAL_ERR) { return x; }
if (y.type == LVAL_ERR) { return y; }
// otherwise do maths on the number values
if (strcmp(op, "+") == 0) { return lval_num(x.num + y.num); }
if (strcmp(op, "-") == 0) { return lval_num(x.num - y.num); }
if (strcmp(op, "*") == 0) { return lval_num(x.num * y.num); }
if (strcmp(op, "/") == 0)
{
return y.num == 0
? lval_err(LERR_DIV_ZERO)
: lval_num(x.num / y.num);
}
if (strcmp(op, "%") == 0) { return lval_num(x.num % y.num); }
if (strcmp(op, "^") == 0)
{
int result = 1;
while (y.num)
{
if (y.num&1)
{
result *= x.num;
}
y.num >>=1 ;
x.num *= x.num;
}
return lval_num(result);
}
if (strcmp(op, "min") == 0)
{
if (x.num < y.num) { return lval_num(x.num); }
return lval_num(y.num);
}
if (strcmp(op, "max") == 0)
{
if (x.num > y.num) { return lval_num(x.num); }
return lval_num(y.num);
}
return lval_err(LERR_BAD_OP);
}
lval eval(mpc_ast_t* t) {
// if tagged as number return it directly
if (strstr(t->tag, "number"))
{
// check if there is some error in conversion
errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno != ERANGE ? lval_num(x) : lval_err(LERR_BAD_NUM);
}
// the operator is always the second child
char* op = t->children[1]->contents;
// store the third child in 'x'
lval x = eval(t->children[2]);
// iterate the remaining children and combining
int i = 3;
while (strstr(t->children[i]->tag, "expr"))
{
x = eval_op(x, op, eval(t->children[i]));
i++;
}
return x;
}
int main(int argc, char** argv)
{
// create and name rules
mpc_parser_t* Number = mpc_new("number");
mpc_parser_t* Operator = mpc_new("operator");
mpc_parser_t* Expr = mpc_new("expr");
mpc_parser_t* ShortLisp = mpc_new("shortlisp");
// define rules
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?[0-9]+/ ; \
operator : '+' | '-' | '*' | '/' | '%' | '^' | \"min\" | \"max\" ; \
expr : <number> | '(' <operator> <expr>+ ')' ; \
shortlisp : /^/ <operator> <expr>+ /$/ ; \
",
Number, Operator, Expr, ShortLisp);
// repl
puts("ShortLisp Version 0.2");
puts("Press Ctrl+c to Exit\n");
while(1)
{
char* input = readline("shortlisp> ");
add_history(input);
// parse input
mpc_result_t r;
if (mpc_parse("<stdin>", input, ShortLisp, &r))
{
lval result = eval(r.output);
lval_println(result);
mpc_ast_delete(r.output);
} else {
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
free(input);
}
// undefine and delete parsers
mpc_cleanup(4, Number, Operator, Expr, ShortLisp);
return 0;
}