Mercurial Hosting > luan
comparison core/src/luan/impl/ExpList.java @ 171:3dcb0f9bee82
add core component
git-svn-id: https://luan-java.googlecode.com/svn/trunk@172 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 22 Jun 2014 05:41:22 +0000 |
parents | src/luan/impl/ExpList.java@4eaee12f6c65 |
children | b48cfa14ba60 |
comparison
equal
deleted
inserted
replaced
170:7c792a328a83 | 171:3dcb0f9bee82 |
---|---|
1 package luan.impl; | |
2 | |
3 import java.util.List; | |
4 import java.util.ArrayList; | |
5 import java.util.Arrays; | |
6 import luan.LuanException; | |
7 import luan.LuanSource; | |
8 import luan.LuanFunction; | |
9 import luan.Luan; | |
10 | |
11 | |
12 final class ExpList { | |
13 | |
14 static final Expressions emptyExpList = new Expressions() { | |
15 | |
16 @Override public Object[] eval(LuanStateImpl luan) { | |
17 return LuanFunction.NOTHING; | |
18 } | |
19 | |
20 @Override public LuanSource.Element se() { | |
21 return null; | |
22 } | |
23 }; | |
24 | |
25 static Expr[] toArray(List<Expressions> list) { | |
26 Expr[] a = new Expr[list.size()]; | |
27 for( int i=0; i<a.length; i++ ) { | |
28 Expressions exprs = list.get(i); | |
29 if( exprs instanceof Expr ) { | |
30 a[i] = (Expr)exprs; | |
31 } else { | |
32 a[i] = new ExpressionsExpr(exprs); | |
33 } | |
34 } | |
35 return a; | |
36 } | |
37 | |
38 static Expressions build(List<Expressions> list) { | |
39 switch(list.size()) { | |
40 case 0: | |
41 return emptyExpList; | |
42 case 1: | |
43 return list.get(0); | |
44 default: | |
45 if( list.get(list.size()-1) instanceof Expr ) { | |
46 return new ExprList1( toArray(list) ); | |
47 } else { | |
48 Expressions last = list.remove(list.size()-1); | |
49 return new ExprList2( toArray(list), last ); | |
50 } | |
51 } | |
52 } | |
53 | |
54 private static class ExprList1 implements Expressions { | |
55 private final Expr[] exprs; | |
56 | |
57 private ExprList1(Expr[] exprs) { | |
58 this.exprs = exprs; | |
59 } | |
60 | |
61 @Override public Object eval(LuanStateImpl luan) throws LuanException { | |
62 Object[] a = new Object[exprs.length]; | |
63 for( int i=0; i<exprs.length; i++ ) { | |
64 a[i] = exprs[i].eval(luan); | |
65 } | |
66 return a; | |
67 } | |
68 | |
69 @Override public LuanSource.Element se() { | |
70 return new LuanSource.Element(exprs[0].se().source,exprs[0].se().start,exprs[exprs.length-1].se().end); | |
71 } | |
72 } | |
73 | |
74 private static class ExprList2 implements Expressions { | |
75 private final Expr[] exprs; | |
76 private final Expressions last; | |
77 | |
78 private ExprList2(Expr[] exprs,Expressions last) { | |
79 this.exprs = exprs; | |
80 this.last = last; | |
81 } | |
82 | |
83 @Override public Object eval(LuanStateImpl luan) throws LuanException { | |
84 List<Object> list = new ArrayList<Object>(); | |
85 for( Expr expr : exprs ) { | |
86 list.add( expr.eval(luan) ); | |
87 } | |
88 list.addAll( Arrays.asList(Luan.array( last.eval(luan) )) ); | |
89 return list.toArray(); | |
90 } | |
91 | |
92 @Override public LuanSource.Element se() { | |
93 return new LuanSource.Element(exprs[0].se().source,exprs[0].se().start,last.se().end); | |
94 } | |
95 } | |
96 } |