-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
141 lines (113 loc) · 3.21 KB
/
parser.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
#include "parser.h"
static char getsymbol() {
char c;
while (isspace(c = getchar()));
if (c == COMMENT_SYMBOL) {
while (getchar() != '\n');
return getsymbol();
}
return c;
}
static const Expr* getExprList(const Expr *vars[], int * const parenLevel) {
int currentLevel = *parenLevel;
const Expr *expr, *arg;
Expr *app;
expr = getExpr(vars, parenLevel);
if (!expr) {
if (*parenLevel < currentLevel) RAISE(NULL, "empty expression\n");
return NULL;
}
if (*parenLevel < currentLevel) return expr;
while (1) {
arg = getExpr(vars, parenLevel);
if (!arg)
if (*parenLevel < currentLevel)
return expr;
else
return NULL;
MALLOC(app)
app->type = APP;
app->data.app.func = expr;
app->data.app.arg = arg;
expr = app;
if (*parenLevel < currentLevel) return expr;
}
}
Expr* parse() {
int parenLevel = 0;
const Expr *vars[256];
for (int i = 0; i < 256; ++i)
vars[i] = NULL;
const Expr *root = getExprList(&vars[0], &parenLevel);
if (!root && parenLevel > -1)
return NULL;
return (Expr*) root;
}
static const Expr* getFunction(const Expr *vars[], int * const parenLevel) {
char c;
const Expr *prev, *body;
Expr *func;
c = getsymbol();
if (c == EOF) RAISE(NULL, "unexpected EOF in function\n")
if (!isalpha(c)) RAISE(NULL, "invalid parameter: %c\n", c)
if (getsymbol() != DOT_SYMBOL) RAISE(NULL, "expected %c\n", DOT_SYMBOL)
MALLOC(func)
prev = vars[c];
vars[c] = func;
body = getExprList(vars, parenLevel);
if (!body) return NULL;
vars[c] = prev;
func->type = FUNC;
func->data.func = (Expr*) body;
return func;
}
static const Expr* getVar(const Expr *vars[], char c) {
Expr *var;
MALLOC(var)
if (!isalpha(c)) RAISE(NULL, "invalid character: %c\n", c)
if (!vars[c]) {
var->type = FREE;
var->data.freeVar = c;
} else {
var->type = BOUND;
var->data.func = (Expr*) vars[c];
}
return var;
}
static const Expr* getExpr(const Expr *vars[], int * const parenLevel) {
char c = getsymbol();
switch (c) {
case LAMBDA_SYMBOL:
return getFunction(vars, parenLevel);
case EOF:
if (*parenLevel > 0) RAISE(NULL, "expected %c\n", CLOSEPAREN_SYMBOL)
--(*parenLevel);
return NULL;
case OPENPAREN_SYMBOL:
++(*parenLevel);
return getExprList(vars, parenLevel);
case CLOSEPAREN_SYMBOL:
if (*parenLevel == 0) RAISE(NULL, "unexpected %c\n", CLOSEPAREN_SYMBOL)
--(*parenLevel);
return NULL;
default:
return getVar(vars, c);
}
}
void freeExpr(const Expr * const expr) {
switch (expr->type) {
case FUNC:
freeExpr(expr->data.func);
break;
case APP:
freeExpr(expr->data.app.func);
freeExpr(expr->data.app.arg);
case BOUND:
case FREE:
break;
case NULL_T:
RAISE(, "cant free null\n")
}
P_DEBUG("freeing %p\n", (void*) expr)
free((void*) expr);
}