comparison src/luan/interp/ExpList.java @ 2:4da26b11d12a

start interp git-svn-id: https://luan-java.googlecode.com/svn/trunk@3 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 14 Nov 2012 08:53:25 +0000
parents
children 7a2cdbc5767f
comparison
equal deleted inserted replaced
1:2df768b926aa 2:4da26b11d12a
1 package luan.interp;
2
3 import java.util.List;
4 import java.util.ArrayList;
5 import luan.LuaException;
6
7
8 final class ExpList extends Values {
9
10 private interface Adder {
11 public void addTo(List<Object> list) throws LuaException;
12 }
13
14 private static class ExprAdder implements Adder {
15 private final Expr expr;
16
17 ExprAdder(Expr expr) {
18 this.expr = expr;
19 }
20
21 public void addTo(List<Object> list) throws LuaException {
22 list.add( expr.eval() );
23 }
24
25 }
26
27 private static class ValuesAdder implements Adder {
28 private final Values values;
29
30 ValuesAdder(Values values) {
31 this.values = values;
32 }
33
34 public void addTo(List<Object> list) throws LuaException {
35 for( Object val : values.eval() ) {
36 list.add( val );
37 }
38 }
39
40 }
41
42 static class Builder {
43 private final List<Adder> adders = new ArrayList<Adder>();
44
45 void add(Expr expr) {
46 adders.add( new ExprAdder(expr) );
47 }
48
49 void add(Values values) {
50 adders.add( new ValuesAdder(values) );
51 }
52
53 ExpList build() {
54 return new ExpList( adders.toArray(new Adder[0]) );
55 }
56 }
57
58 private final Adder[] adders;
59
60 private ExpList(Adder[] adders) {
61 this.adders = adders;
62 }
63
64 List eval() throws LuaException {
65 List<Object> list = new ArrayList<Object>();
66 for( Adder adder : adders ) {
67 adder.addTo(list);
68 }
69 return list;
70 }
71 }