-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniScheme5.jj
225 lines (202 loc) · 5.53 KB
/
MiniScheme5.jj
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
220
221
222
223
224
225
/**
* JavaCC template file created by SF JavaCC plugin 1.5.28+ wizard for JavaCC 1.5.0+
*/
options
{
static = true;
}
PARSER_BEGIN(MiniScheme5)
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class MiniScheme5
{
public static Scanner in;
public static void main(String args[]) throws ParseException
{
try {
MiniScheme5 parser = null;
in = new Scanner(System.in);
if (args.length > 0)
parser = new MiniScheme5(new FileInputStream(args[0]));
else
{
System.out.print("Program filename: ");
parser = new MiniScheme5(new FileInputStream(in.nextLine()));
}
Program p = parser.program();
p.eval(new Env());
//p.print();
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
catch (RuntimeException e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
PARSER_END(MiniScheme5)
SKIP :
{
" "
| "\r"
| "\t"
| "\n"
}
TOKEN : /* OPERATORS */
{
< PLUS : "+" >
| < MINUS : "-" >
| < MULTIPLY : "*" >
| < QUOTIENT : "quotient" >
}
TOKEN: /* NUMBERS */
{
< INTEGER : ("+" | "-")? (< DIGIT >)+>
| < #DIGIT : [ "0"-"9" ] >
}
TOKEN: /* BOOLEANS */
{
< BOOLEAN : ("#f" | "#t") >
}
TOKEN: /* KEYWORDS */
{
< READ : "read" >
| < WRITE : "write" >
| < NEWLINE : "newline" >
| < DEFINE : "define" >
| < LET : "let" >
| < SET : "set!" >
| < IF : "if" >
| < AND : "and" >
| < OR : "or" >
| < NOT : "not" >
| < EQV_CHECK : "eqv?" >
| < LESS_THAN : "<" >
| < INT_CHECK : "integer?" >
| < BOOL_CHECK : "boolean?" >
| < LAMBDA : "lambda" >
| < PAIR_CHECK : "pair?" >
| < CONS_PAIR : "cons" >
| < CONS_LIST : "list" >
| < NULL_CHECK : "null?" >
| < CAR : "car" >
| < CDR : "cdr" >
| < SET_CAR : "set-car!" >
}
TOKEN: /* IDENTIFIERS */
{
< IDENTIFIER : < INITIAL > (< SUBSEQUENT >)* >
| < INITIAL : ["a"-"z", "!", "$", "%", "&", "*", "/", ":", "<", "=", ">", "?", "^", "_", "~"] >
| < SUBSEQUENT: (< INITIAL > | < DIGIT > | ["+", "-", ".", "@"]) >
}
Program program() :
{ Program p = new Program(); Exp e = null; Definition d = null; }
{
( LOOKAHEAD(def()) d=def() { p.lineList.add(d); } | e=exp() { p.lineList.add(e); })*
{ return p; }
}
Definition def() :
{ Definition d = null; Variable var = null; Exp e = null; }
{
"(" < DEFINE > var=variable() e=exp() ")" { d = new Definition(var, e); }
{ return d; }
}
Exp exp() :
{ Exp e = null; }
{
(
e=variable()
| e=literal()
| "(" (e=cond() | e=assign() | e=derivedExpression() | e=lambdaExp() | e=procCall()) ")"
)
{ return e; }
}
Variable variable() :
{ Variable var = null; String id = null; }
{
< IDENTIFIER > { id = token.image; var = new Variable(id); }
{ return var; }
}
Literal literal() :
{ Literal l = null; String s = null; }
{
(
< INTEGER > { s = token.image; l = new Int(s); }
| < BOOLEAN > { s = token.image; l = new Bool(s); }
)
{ return l; }
}
Cond cond() :
{ Cond c = null; Exp e1 = null; Exp e2 = null; Exp e3 = null; }
{
< IF > e1=exp() e2=exp() (e3=exp())?
{ c = new Cond(e1, e2, e3); return c; }
}
Assignment assign() :
{ Assignment a = null; Variable var = null; Exp e = null; }
{
< SET > var=variable() e=exp() { a = new Assignment(var, e); }
{ return a; }
}
Exp derivedExpression() :
{ Token t = null; Exp e1 = null; Exp e2 = null;
WriteExpression we = null; ArithExp ae = null;
ReadExpression re = null; NewlineExpression nl = null;
LetExp le = null; BindSpec bi = null; Body bod = null;
AndExp andE = null; OrExp orE = null; List lst = null;}
{
(
(< READ > { re = new ReadExpression(in); e1 = re; })
| (< WRITE > e1=exp() { we = new WriteExpression(e1); e1 = we; })
| (< NEWLINE > { nl = new NewlineExpression(); e1 = nl; })
| ((< PLUS > | < MINUS > | < MULTIPLY > | < QUOTIENT >) { t = token; } e1=exp() e2=exp() { ae = new ArithExp(t.image, e1, e2); e1 = ae; })
| (< LET > { le = new LetExp(); }"(" ( bi=bindSpec() { le.binds.add(bi); })* ")" bod=body() { le.body = bod; e1=le; })
| (< AND > { andE = new AndExp(); } (e1=exp() { andE.expList.add(e1); })* { e1 = andE; })
| (< OR > { orE = new OrExp(); } (e1=exp() { orE.expList.add(e1); })* { e1 = orE; })
| (< NOT > e1=exp() { e1 = new NotExp(e1); })
| (< EQV_CHECK > e1=exp() e2=exp() { e1 = new EqvExp(e1, e2); })
| (< LESS_THAN > e1=exp() e2=exp() { e1 = new LessExp(e1, e2); })
| (< INT_CHECK > e1=exp() { e1 = new IntCheckExp(e1); })
| (< BOOL_CHECK > e1=exp() { e1 = new BoolCheckExp(e1); })
| (< PAIR_CHECK > e1=exp() { e1 = new PairCheckExp(e1); })
| (< CONS_PAIR > e1=exp() e2=exp() {e1 = new Pair(e1, e2); })
| (< CONS_LIST > { lst = new List(); } (e1=exp() { lst.addList(e1); })* {e1=lst;})
| (< NULL_CHECK > e1=exp() { e1 = new NullCheckExp(e1); })
| (< CAR > e1=exp() { e1 = new CarExp(e1); })
| (< CDR > e1=exp() { e1 = new CdrExp(e1); })
| (< SET_CAR > e1=exp() e2=exp(){ e1 = new SetCarExp(e1, e2);})
)
{ return e1; }
}
BindSpec bindSpec() :
{ Variable var = null; Exp e = null; }
{
"(" var=variable() e=exp() ")"
{ return new BindSpec(var, e); }
}
Body body() :
{ Body bod = new Body(); Definition def = null; Exp exp = null; }
{
(
(LOOKAHEAD(def()) def=def() { bod.lines.add(def); })*
(exp=exp() { bod.lines.add(exp); })+
)
{ return bod; }
}
LambdaExp lambdaExp() :
{ LambdaExp l = new LambdaExp(); Variable v = null; Body b = null;}
{
< LAMBDA > "(" (v=variable() { l.varList.add(v); })* ")" b=body() { l.body = b; }
{ return l; }
}
ProcCall procCall() :
{ ProcCall p = new ProcCall(); Exp e = null; }
{
(e=exp() { p.expList.add(e); })+
{ return p; }
}