comparison src/luan/interp/LuaParser.java @ 11:b7d7069fee58

add assignment statement and CmdLine git-svn-id: https://luan-java.googlecode.com/svn/trunk@12 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 22 Nov 2012 10:51:56 +0000
parents 600676034a1a
children 9cea1aea5eef
comparison
equal deleted inserted replaced
10:8217d8485715 11:b7d7069fee58
14 import luan.LuaState; 14 import luan.LuaState;
15 15
16 16
17 public class LuaParser extends BaseParser<Object> { 17 public class LuaParser extends BaseParser<Object> {
18 18
19 Rule Target() { 19 public Rule Target() {
20 return Sequence(Spaces(), Expr(), EOI); 20 return Sequence(
21 Spaces(),
22 FirstOf(
23 Sequence( Expressions(), EOI ),
24 Sequence( Stmt(), EOI )
25 )
26 );
27 }
28
29 Rule Stmt() {
30 return SetStmt();
31 }
32
33 Rule SetStmt() {
34 return Sequence(
35 VarList(),
36 '=', Spaces(),
37 ExpList(),
38 push( newSetStmt() )
39 );
40 }
41
42 SetStmt newSetStmt() {
43 Expressions values = (Expressions)pop();
44 @SuppressWarnings("unchecked")
45 List<SetStmt.Var> vars = (List<SetStmt.Var>)pop();
46 return new SetStmt( vars.toArray(new SetStmt.Var[0]), values );
47 }
48
49 Rule VarList() {
50 return Sequence(
51 push(new ArrayList<SetStmt.Var>()),
52 Var(),
53 addToVarList(),
54 ZeroOrMore(
55 ',', Spaces(), Var(),
56 addToVarList()
57 )
58 );
59 }
60
61 boolean addToVarList() {
62 Object obj = pop();
63 if( obj==null )
64 return false;
65 Expr key = expr(obj);
66 Expr table = expr(pop());
67 @SuppressWarnings("unchecked")
68 List<SetStmt.Var> vars = (List<SetStmt.Var>)peek();
69 vars.add( new SetStmt.Var(table,key) );
70 return true;
21 } 71 }
22 72
23 Rule Expr() { 73 Rule Expr() {
24 return OrExpr(); 74 return OrExpr();
25 } 75 }
26 76
27 Rule OrExpr() { 77 Rule OrExpr() {
28 return Sequence( 78 return Sequence(
29 AndExpr(), 79 AndExpr(),
30 ZeroOrMore( "or", Spaces(), AndExpr(), push( new OrExpr((Expr)pop(1),(Expr)pop()) ) ) 80 ZeroOrMore( "or", Spaces(), AndExpr(), push( new OrExpr(expr(pop(1)),expr(pop())) ) )
31 ); 81 );
32 } 82 }
33 83
34 Rule AndExpr() { 84 Rule AndExpr() {
35 return Sequence( 85 return Sequence(
36 RelExpr(), 86 RelExpr(),
37 ZeroOrMore( "and", Spaces(), RelExpr(), push( new AndExpr((Expr)pop(1),(Expr)pop()) ) ) 87 ZeroOrMore( "and", Spaces(), RelExpr(), push( new AndExpr(expr(pop(1)),expr(pop())) ) )
38 ); 88 );
39 } 89 }
40 90
41 Rule RelExpr() { 91 Rule RelExpr() {
42 return Sequence( 92 return Sequence(
43 ConcatExpr(), 93 ConcatExpr(),
44 ZeroOrMore( 94 ZeroOrMore(
45 FirstOf( 95 FirstOf(
46 Sequence( "==", Spaces(), ConcatExpr(), push( new EqExpr((Expr)pop(1),(Expr)pop()) ) ), 96 Sequence( "==", Spaces(), ConcatExpr(), push( new EqExpr(expr(pop(1)),expr(pop())) ) ),
47 Sequence( "~=", Spaces(), ConcatExpr(), push( new NotExpr(new EqExpr((Expr)pop(1),(Expr)pop())) ) ), 97 Sequence( "~=", Spaces(), ConcatExpr(), push( new NotExpr(new EqExpr(expr(pop(1)),expr(pop()))) ) ),
48 Sequence( "<=", Spaces(), ConcatExpr(), push( new LeExpr((Expr)pop(1),(Expr)pop()) ) ), 98 Sequence( "<=", Spaces(), ConcatExpr(), push( new LeExpr(expr(pop(1)),expr(pop())) ) ),
49 Sequence( ">=", Spaces(), ConcatExpr(), push( new LeExpr((Expr)pop(),(Expr)pop()) ) ), 99 Sequence( ">=", Spaces(), ConcatExpr(), push( new LeExpr(expr(pop()),expr(pop())) ) ),
50 Sequence( "<", Spaces(), ConcatExpr(), push( new LtExpr((Expr)pop(1),(Expr)pop()) ) ), 100 Sequence( "<", Spaces(), ConcatExpr(), push( new LtExpr(expr(pop(1)),expr(pop())) ) ),
51 Sequence( ">", Spaces(), ConcatExpr(), push( new LtExpr((Expr)pop(),(Expr)pop()) ) ) 101 Sequence( ">", Spaces(), ConcatExpr(), push( new LtExpr(expr(pop()),expr(pop())) ) )
52 ) 102 )
53 ) 103 )
54 ); 104 );
55 } 105 }
56 106
57 Rule ConcatExpr() { 107 Rule ConcatExpr() {
58 return Sequence( 108 return Sequence(
59 SumExpr(), 109 SumExpr(),
60 Optional( "..", Spaces(), ConcatExpr(), push( new ConcatExpr((Expr)pop(1),(Expr)pop()) ) ) 110 Optional( "..", Spaces(), ConcatExpr(), push( new ConcatExpr(expr(pop(1)),expr(pop())) ) )
61 ); 111 );
62 } 112 }
63 113
64 Rule SumExpr() { 114 Rule SumExpr() {
65 return Sequence( 115 return Sequence(
66 TermExpr(), 116 TermExpr(),
67 ZeroOrMore( 117 ZeroOrMore(
68 FirstOf( 118 FirstOf(
69 Sequence( '+', Spaces(), TermExpr(), push( new AddExpr((Expr)pop(1),(Expr)pop()) ) ), 119 Sequence( '+', Spaces(), TermExpr(), push( new AddExpr(expr(pop(1)),expr(pop())) ) ),
70 Sequence( '-', Spaces(), TermExpr(), push( new SubExpr((Expr)pop(1),(Expr)pop()) ) ) 120 Sequence( '-', Spaces(), TermExpr(), push( new SubExpr(expr(pop(1)),expr(pop())) ) )
71 ) 121 )
72 ) 122 )
73 ); 123 );
74 } 124 }
75 125
76 Rule TermExpr() { 126 Rule TermExpr() {
77 return Sequence( 127 return Sequence(
78 UnaryExpr(), 128 UnaryExpr(),
79 ZeroOrMore( 129 ZeroOrMore(
80 FirstOf( 130 FirstOf(
81 Sequence( '*', Spaces(), UnaryExpr(), push( new MulExpr((Expr)pop(1),(Expr)pop()) ) ), 131 Sequence( '*', Spaces(), UnaryExpr(), push( new MulExpr(expr(pop(1)),expr(pop())) ) ),
82 Sequence( '/', Spaces(), UnaryExpr(), push( new DivExpr((Expr)pop(1),(Expr)pop()) ) ), 132 Sequence( '/', Spaces(), UnaryExpr(), push( new DivExpr(expr(pop(1)),expr(pop())) ) ),
83 Sequence( '%', Spaces(), UnaryExpr(), push( new ModExpr((Expr)pop(1),(Expr)pop()) ) ) 133 Sequence( '%', Spaces(), UnaryExpr(), push( new ModExpr(expr(pop(1)),expr(pop())) ) )
84 ) 134 )
85 ) 135 )
86 ); 136 );
87 } 137 }
88 138
89 Rule UnaryExpr() { 139 Rule UnaryExpr() {
90 return FirstOf( 140 return FirstOf(
91 Sequence( '#', Spaces(), PowExpr(), push( new LenExpr((Expr)pop()) ) ), 141 Sequence( '#', Spaces(), PowExpr(), push( new LenExpr(expr(pop())) ) ),
92 Sequence( '-', Spaces(), PowExpr(), push( new UnmExpr((Expr)pop()) ) ), 142 Sequence( '-', Spaces(), PowExpr(), push( new UnmExpr(expr(pop())) ) ),
93 Sequence( "not", Spaces(), PowExpr(), push( new NotExpr((Expr)pop()) ) ), 143 Sequence( "not", Spaces(), PowExpr(), push( new NotExpr(expr(pop())) ) ),
94 PowExpr() 144 PowExpr()
95 ); 145 );
96 } 146 }
97 147
98 Rule PowExpr() { 148 Rule PowExpr() {
99 return Sequence( 149 return Sequence(
100 SingleExpr(), 150 SingleExpr(),
101 Optional( '^', Spaces(), PowExpr(), push( new PowExpr((Expr)pop(1),(Expr)pop()) ) ) 151 Optional( '^', Spaces(), PowExpr(), push( new PowExpr(expr(pop(1)),expr(pop())) ) )
102 ); 152 );
103 } 153 }
104 154
105 Rule SingleExpr() { 155 Rule SingleExpr() {
106 return FirstOf( 156 return FirstOf(
107 TableExpr(), 157 TableExpr(),
108 PrefixExpr(), 158 VarExp(),
109 LiteralExpr() 159 LiteralExpr()
110 ); 160 );
111 } 161 }
112 162
113 Rule TableExpr() { 163 Rule TableExpr() {
152 ) 202 )
153 ); 203 );
154 } 204 }
155 205
156 boolean addField() { 206 boolean addField() {
157 TableExpr.Field field = new TableExpr.Field( (Expr)pop(1), (Expr)pop() ); 207 TableExpr.Field field = new TableExpr.Field( expr(pop(1)), expr(pop()) );
158 @SuppressWarnings("unchecked") 208 @SuppressWarnings("unchecked")
159 List<TableExpr.Field> list = (List<TableExpr.Field>)peek(1); 209 List<TableExpr.Field> list = (List<TableExpr.Field>)peek(1);
160 list.add(field); 210 list.add(field);
161 return true; 211 return true;
162 } 212 }
163 213
164 boolean addIndexedField() { 214 boolean addIndexedField() {
165 Expr val = (Expr)pop(); 215 Expr val = expr(pop());
166 double i = (Double)pop(); 216 double i = (Double)pop();
167 TableExpr.Field field = new TableExpr.Field( new ConstExpr(new LuaNumber(i)), val ); 217 TableExpr.Field field = new TableExpr.Field( new ConstExpr(new LuaNumber(i)), val );
168 push( i + 1 ); 218 push( i + 1 );
169 @SuppressWarnings("unchecked") 219 @SuppressWarnings("unchecked")
170 List<TableExpr.Field> list = (List<TableExpr.Field>)peek(1); 220 List<TableExpr.Field> list = (List<TableExpr.Field>)peek(1);
171 list.add(field); 221 list.add(field);
172 return true; 222 return true;
173 } 223 }
174 224
175 Rule PrefixExpr() { 225 static Expr expr(Object obj) {
226 if( obj instanceof Expressions )
227 return new ExpressionsExpr((Expressions)obj);
228 return (Expr)obj;
229 }
230
231 Rule VarExp() {
232 return Sequence(
233 Var(),
234 makeVarExp()
235 );
236 }
237
238 Rule Var() {
176 return Sequence( 239 return Sequence(
177 FirstOf( 240 FirstOf(
178 Sequence( '(', Spaces(), Expr(), ')', Spaces() ), 241 Sequence(
179 Var() 242 '(', Spaces(), Expr(), ')', Spaces(),
180 ), 243 push(expr(pop())),
181 ZeroOrMore( 244 push(null) // marker
245 ),
246 Sequence(
247 push(EnvExpr.INSTANCE),
248 Name()
249 )
250 ),
251 ZeroOrMore(
252 makeVarExp(),
182 FirstOf( 253 FirstOf(
183 SubExpr(), 254 SubExpr(),
184 Sequence( '.', Spaces(), Name() ) 255 Sequence( '.', Spaces(), Name() ),
256 Sequence(
257 Args(),
258 push(null) // marker
259 )
260 )
261 )
262 );
263 }
264
265 boolean makeVarExp() {
266 Object obj = pop();
267 if( obj==null )
268 return true;
269 return push( new GetExpr( expr(pop()), expr(obj) ) );
270 }
271
272 // function should be on top of the stack
273 Rule Args() {
274 return Sequence(
275 FirstOf(
276 Sequence(
277 '(', Spaces(), Expressions(), ')', Spaces()
185 ), 278 ),
186 push( new GetExpr( (Expr)pop(1), (Expr)pop() ) ) 279 Sequence(
187 ) 280 TableExpr(),
188 ); 281 push( new ExpList.SingleExpList(expr(pop())) )
282 ),
283 Sequence(
284 StringLiteral(), Spaces(),
285 push( new ExpList.SingleExpList(new ConstExpr(pop())) )
286 )
287 ),
288 push( new FnCall( expr(pop(1)), (Expressions)pop() ) )
289 );
290 }
291
292 Rule Expressions() {
293 return FirstOf(
294 ExpList(),
295 push( ExpList.emptyExpList )
296 );
297 }
298
299 Rule ExpList() {
300 return Sequence(
301 push(new ExpList.Builder()),
302 Expr(),
303 addToExpList(),
304 ZeroOrMore(
305 ',', Spaces(), Expr(),
306 addToExpList()
307 ),
308 push( ((ExpList.Builder)pop()).build() )
309 );
310 }
311
312 boolean addToExpList() {
313 Object obj = pop();
314 ExpList.Builder bld = (ExpList.Builder)peek();
315 if( obj instanceof Expressions ) {
316 bld.add( (Expressions)obj );
317 } else {
318 bld.add( (Expr)obj );
319 }
320 return true;
189 } 321 }
190 322
191 Rule SubExpr() { 323 Rule SubExpr() {
192 return Sequence( '[', Spaces(), Expr(), ']', Spaces() ); 324 return Sequence( '[', Spaces(), Expr(), ']', Spaces() );
193 }
194
195 Rule Var() {
196 return Sequence(
197 Name(),
198 push( new GetExpr( EnvExpr.INSTANCE, (Expr)pop() ) )
199 );
200 } 325 }
201 326
202 Rule Name() { 327 Rule Name() {
203 return Sequence( 328 return Sequence(
204 Sequence( 329 Sequence(
271 Rule DecNumber() { 396 Rule DecNumber() {
272 return FirstOf( 397 return FirstOf(
273 Sequence( 398 Sequence(
274 Int(), 399 Int(),
275 Optional( '.', Optional(Int()) ), 400 Optional( '.', Optional(Int()) ),
276 NumberExp() 401 Exponent()
277 ), 402 ),
278 Sequence( '.', Int(), NumberExp() ) 403 Sequence( '.', Int(), Exponent() )
279 ); 404 );
280 } 405 }
281 406
282 Rule NumberExp() { 407 Rule Exponent() {
283 return Optional( 408 return Optional(
284 IgnoreCase('e'), 409 IgnoreCase('e'),
285 Optional(AnyOf("+-")), 410 Optional(AnyOf("+-")),
286 Int() 411 Int()
287 ); 412 );
379 String input = new Scanner(System.in).nextLine(); 504 String input = new Scanner(System.in).nextLine();
380 ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(input); 505 ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(input);
381 if( result.hasErrors() ) { 506 if( result.hasErrors() ) {
382 System.out.println("Parse Errors:\n" + ErrorUtils.printParseErrors(result)); 507 System.out.println("Parse Errors:\n" + ErrorUtils.printParseErrors(result));
383 } else { 508 } else {
384 Expr expr = (Expr)result.resultValue; 509 Expr expr = expr(result.resultValue);
385 LuaState lua = new LuaState(); 510 LuaState lua = new LuaState();
386 Object val = expr.eval(lua); 511 Object val = expr.eval(lua);
387 System.out.println("Result: "+Lua.toString(val)); 512 System.out.println("Result: "+Lua.toString(val));
388 } 513 }
389 } 514 }