Mercurial Hosting > luan
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/ExpList.java Wed Nov 14 08:53:25 2012 +0000 @@ -0,0 +1,71 @@ +package luan.interp; + +import java.util.List; +import java.util.ArrayList; +import luan.LuaException; + + +final class ExpList extends Values { + + private interface Adder { + public void addTo(List<Object> list) throws LuaException; + } + + private static class ExprAdder implements Adder { + private final Expr expr; + + ExprAdder(Expr expr) { + this.expr = expr; + } + + public void addTo(List<Object> list) throws LuaException { + list.add( expr.eval() ); + } + + } + + private static class ValuesAdder implements Adder { + private final Values values; + + ValuesAdder(Values values) { + this.values = values; + } + + public void addTo(List<Object> list) throws LuaException { + for( Object val : values.eval() ) { + list.add( val ); + } + } + + } + + static class Builder { + private final List<Adder> adders = new ArrayList<Adder>(); + + void add(Expr expr) { + adders.add( new ExprAdder(expr) ); + } + + void add(Values values) { + adders.add( new ValuesAdder(values) ); + } + + ExpList build() { + return new ExpList( adders.toArray(new Adder[0]) ); + } + } + + private final Adder[] adders; + + private ExpList(Adder[] adders) { + this.adders = adders; + } + + List eval() throws LuaException { + List<Object> list = new ArrayList<Object>(); + for( Adder adder : adders ) { + adder.addTo(list); + } + return list; + } +}