annotate core/src/luan/impl/ExpList.java @ 653:538b0ae08faa

compile IfStmt and BreakStmt
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Apr 2016 13:01:04 -0600
parents 859c0dedc8b6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
166
4eaee12f6c65 move luan/interp to impl
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 154
diff changeset
1 package luan.impl;
2
4da26b11d12a start interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
2
4da26b11d12a start interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
3 import java.util.List;
4da26b11d12a start interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
4 import java.util.ArrayList;
154
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
5 import java.util.Arrays;
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
6 import luan.LuanException;
154
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
7 import luan.LuanFunction;
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
8 import luan.Luan;
2
4da26b11d12a start interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
9
4da26b11d12a start interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
10
154
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
11 final class ExpList {
13
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 11
diff changeset
12
11
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 3
diff changeset
13 static final Expressions emptyExpList = new Expressions() {
40
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 31
diff changeset
14
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
15 @Override public Object[] eval(LuanStateImpl luan) {
154
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
16 return LuanFunction.NOTHING;
3
7a2cdbc5767f add LuaState
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
17 }
7a2cdbc5767f add LuaState
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
18 };
7a2cdbc5767f add LuaState
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
19
653
538b0ae08faa compile IfStmt and BreakStmt
Franklin Schmidt <fschmidt@gmail.com>
parents: 645
diff changeset
20 private static Expr[] toArray(List<Expressions> list) {
154
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
21 Expr[] a = new Expr[list.size()];
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
22 for( int i=0; i<a.length; i++ ) {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
23 Expressions exprs = list.get(i);
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
24 if( exprs instanceof Expr ) {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
25 a[i] = (Expr)exprs;
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
26 } else {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
27 a[i] = new ExpressionsExpr(exprs);
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
28 }
3
7a2cdbc5767f add LuaState
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
29 }
154
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
30 return a;
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
31 }
3
7a2cdbc5767f add LuaState
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
32
154
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
33 static Expressions build(List<Expressions> list) {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
34 switch(list.size()) {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
35 case 0:
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
36 return emptyExpList;
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
37 case 1:
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
38 return list.get(0);
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
39 default:
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
40 if( list.get(list.size()-1) instanceof Expr ) {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
41 return new ExprList1( toArray(list) );
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
42 } else {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
43 Expressions last = list.remove(list.size()-1);
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
44 return new ExprList2( toArray(list), last );
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
45 }
132
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 111
diff changeset
46 }
3
7a2cdbc5767f add LuaState
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
47 }
7a2cdbc5767f add LuaState
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
48
154
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
49 private static class ExprList1 implements Expressions {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
50 private final Expr[] exprs;
2
4da26b11d12a start interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
51
154
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
52 private ExprList1(Expr[] exprs) {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
53 this.exprs = exprs;
2
4da26b11d12a start interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
54 }
154
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
55
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
56 @Override public Object eval(LuanStateImpl luan) throws LuanException {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
57 Object[] a = new Object[exprs.length];
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
58 for( int i=0; i<exprs.length; i++ ) {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
59 a[i] = exprs[i].eval(luan);
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
60 }
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
61 return a;
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
62 }
2
4da26b11d12a start interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
63 }
40
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 31
diff changeset
64
154
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
65 private static class ExprList2 implements Expressions {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
66 private final Expr[] exprs;
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
67 private final Expressions last;
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
68
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
69 private ExprList2(Expr[] exprs,Expressions last) {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
70 this.exprs = exprs;
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
71 this.last = last;
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
72 }
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
73
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
74 @Override public Object eval(LuanStateImpl luan) throws LuanException {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
75 List<Object> list = new ArrayList<Object>();
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
76 for( Expr expr : exprs ) {
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
77 list.add( expr.eval(luan) );
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
78 }
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
79 list.addAll( Arrays.asList(Luan.array( last.eval(luan) )) );
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
80 return list.toArray();
c2e5101682ae Expr extends Expressions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 132
diff changeset
81 }
40
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 31
diff changeset
82 }
2
4da26b11d12a start interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
83 }