-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc.cpp
More file actions
219 lines (190 loc) · 4.77 KB
/
src.cpp
File metadata and controls
219 lines (190 loc) · 4.77 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <deque>
#include <iostream>
#include <unordered_map>
using std::string;
// instead of 'λ'.
const char lambda='%';
// error handling.
void myassert(bool pred, const char prob[]="")
{
if(pred) return;
std::cerr<<"parse failed.\n";
if(prob!="") std::cerr<<"problem: "<<prob<<"\n";
exit(1);
}
// type of lambda calculus.
struct judged_type : std::deque<char>
{
bool ismap=false;
void apply(judged_type rhs)
{
if(rhs.empty()) return;
if(front()=='(')
{
pop_front();
while(!rhs.empty())
{
myassert(rhs.front()==front(), "applying type doesn't match.");
rhs.pop_front();
pop_front();
}
myassert(front()==')', "bracket not closed.");
pop_front();
}
else
{
while(!rhs.empty())
{
myassert(front()!='-' and front()!='>',"applying type doesn't match.");
myassert(rhs.front()==front(),"applying type doesn't match.");
rhs.pop_front();
pop_front();
}
}
myassert(front()=='-',"applying type doesn't match.");
pop_front();
myassert(front()=='>',"applying type doesn't match.");
pop_front();
}
void mapto(judged_type rhs)
{
if(ismap)
{
push_front('(');
push_back(')');
}
push_back('-');
push_back('>');
while(!rhs.empty())
{
push_back(rhs.front());
rhs.pop_front();
}
ismap=true;
}
void append(char c)
{
if(c=='>') ismap=true;
push_back(c);
}
// for result output.
friend std::ostream &operator<<(std::ostream& os, judged_type jty)
{
std::string str;
while(!jty.empty())
{
str+=jty.front();
jty.pop_front();
if(str.back()==')') // remove unnecessary bracket.
{
auto itr=str.end();
do { --itr; } while(*itr!='>' and *itr!='(');
if(*itr=='(')
{
str.erase(itr);
str.pop_back();
}
}
}
return os<<str;
}
}; // struct judged_type
std::unordered_map<string, judged_type> type_of;
bool normal(char chr)
{
const char lst[]="() .,:\n"; // end with a null character.
for(char c: lst) if(chr == c) return false;
return true;
}
void skip_space(char *&ptr) { while(*ptr==' ') ++ptr; }
judged_type parse(char *&ptr)
{
skip_space(ptr);
if(*ptr=='(')
{
++ptr;
auto lhs=parse(ptr);
lhs.apply(parse(ptr));
skip_space(ptr);
myassert(*ptr==')',"bracket not closed.");
++ptr;
return lhs;
}
if(*ptr==')') return judged_type();
if(*ptr==lambda)
{
++ptr;
string var;
while(normal(*ptr))
{
var+=*ptr;
++ptr;
}
myassert(!type_of.count(var),"multiple definition of variable.");
skip_space(ptr);
myassert(*ptr==':',"missing a \':\' in declaration.");
++ptr;
skip_space(ptr);
judged_type lhs;
while(normal(*ptr) or *ptr=='(' or *ptr==')')
{
if(*ptr!=' ') lhs.append(*ptr);
++ptr;
}
type_of[var]=lhs;
skip_space(ptr);
myassert(*ptr=='.',"missing a \'.\' in declaration.");
++ptr;
lhs.mapto(parse(ptr));
return lhs;
}
if(*ptr!='\0' or *ptr!='\n')
{
string var;
while(normal(*ptr))
{
var+=*ptr;
++ptr;
}
myassert(type_of.count(var),"variable not declared."); // should be already declared.
return type_of[var];
}
return judged_type();
} // judged_type parse(char*&)
signed main(int argc, char *args[])
{
if(argc==1)
{
std::cerr<<"args empty.\n";
return 0;
}
char *ptr=args[1];
if(argc>2)
{
skip_space(ptr);
while(*ptr!='\0')
{
string var;
while(*ptr!=':')
{
var+=*ptr;
++ptr;
}
myassert(!type_of.count(var),"multiple definition of variable.");
skip_space(ptr);
myassert(*ptr==':',"syntax error in env.");
++ptr;
skip_space(ptr);
judged_type &ty=type_of[var];
while(normal(*ptr) or *ptr=='(' or *ptr==')')
{
ty.append(*ptr);
++ptr;
}
skip_space(ptr);
if(*ptr==',') skip_space(++ptr);
}
ptr=args[2];
}
std::cout<<parse(ptr)<<"\n";
}