Mercurial Hosting > luan
comparison src/luan/interp/LuanParser.java @ 86:6db8f286fa6c
_ENV is per module, not global
git-svn-id: https://luan-java.googlecode.com/svn/trunk@87 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 27 Feb 2013 08:03:51 +0000 |
parents | b84f66704026 |
children | d03022acea47 |
comparison
equal
deleted
inserted
replaced
85:b2551f00bc51 | 86:6db8f286fa6c |
---|---|
22 import luan.LuanSource; | 22 import luan.LuanSource; |
23 | 23 |
24 | 24 |
25 class LuanParser extends BaseParser<Object> { | 25 class LuanParser extends BaseParser<Object> { |
26 | 26 |
27 LuanSource source; | |
28 | |
29 LuanSource.Element se(int start) { | |
30 return new LuanSource.Element(source,start,currentIndex()); | |
31 } | |
32 | |
33 static final String _ENV = "_ENV"; | |
34 | |
35 static final class Frame { | 27 static final class Frame { |
36 final Frame parent; | 28 final Frame parent; |
37 final List<String> symbols = new ArrayList<String>(); | 29 final List<String> symbols = new ArrayList<String>(); |
38 int stackSize = 0; | 30 int stackSize = 0; |
39 int loops = 0; | 31 int loops = 0; |
40 boolean isVarArg = false; | 32 boolean isVarArg = false; |
41 final List<String> upValueSymbols = new ArrayList<String>(); | 33 final List<String> upValueSymbols = new ArrayList<String>(); |
42 final List<UpValue.Getter> upValueGetters = new ArrayList<UpValue.Getter>(); | 34 final List<UpValue.Getter> upValueGetters = new ArrayList<UpValue.Getter>(); |
43 | 35 |
44 Frame() { | 36 Frame(UpValue.Getter envGetter) { |
45 this.parent = null; | 37 this.parent = null; |
46 upValueSymbols.add(_ENV); | 38 upValueSymbols.add(_ENV); |
47 upValueGetters.add(UpValue.globalGetter); | 39 upValueGetters.add(envGetter); |
48 } | 40 } |
49 | 41 |
50 Frame(Frame parent) { | 42 Frame(Frame parent) { |
51 this.parent = parent; | 43 this.parent = parent; |
44 if( upValueIndex(_ENV) != 0 ) | |
45 throw new RuntimeException(); | |
52 } | 46 } |
53 | 47 |
54 int stackIndex(String name) { | 48 int stackIndex(String name) { |
55 int i = symbols.size(); | 49 int i = symbols.size(); |
56 while( --i >= 0 ) { | 50 while( --i >= 0 ) { |
80 upValueSymbols.add(name); | 74 upValueSymbols.add(name); |
81 return upValueSymbols.size() - 1; | 75 return upValueSymbols.size() - 1; |
82 } | 76 } |
83 } | 77 } |
84 | 78 |
79 static final String _ENV = "_ENV"; | |
85 static final UpValue.Getter[] NO_UP_VALUE_GETTERS = new UpValue.Getter[0]; | 80 static final UpValue.Getter[] NO_UP_VALUE_GETTERS = new UpValue.Getter[0]; |
86 | 81 |
82 // UpValue.Getter envGetter = new UpValue.EnvGetter(); | |
83 final LuanSource source; | |
84 Frame frame; | |
87 int nEquals; | 85 int nEquals; |
88 Frame frame = new Frame(); | 86 |
87 LuanParser(LuanSource source,UpValue.Getter envGetter) { | |
88 this.source = source; | |
89 this.frame = new Frame(envGetter); | |
90 } | |
91 | |
92 LuanSource.Element se(int start) { | |
93 return new LuanSource.Element(source,start,currentIndex()); | |
94 } | |
89 | 95 |
90 boolean nEquals(int n) { | 96 boolean nEquals(int n) { |
91 nEquals = n; | 97 nEquals = n; |
92 return true; | 98 return true; |
93 } | 99 } |
138 boolean decLoops() { | 144 boolean decLoops() { |
139 frame.loops--; | 145 frame.loops--; |
140 return true; | 146 return true; |
141 } | 147 } |
142 | 148 |
143 Chunk newChunk(int start) { | 149 FnDef newFnDef(int start) { |
144 return new Chunk( se(start), (Stmt)pop(), frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); | 150 return new FnDef( se(start), (Stmt)pop(), frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); |
145 } | 151 } |
146 | 152 |
147 Rule Target() { | 153 Rule Target() { |
148 Var<Integer> start = new Var<Integer>(); | 154 Var<Integer> start = new Var<Integer>(); |
149 return Sequence( | 155 return Sequence( |
151 start.set(currentIndex()), | 157 start.set(currentIndex()), |
152 FirstOf( | 158 FirstOf( |
153 Sequence( | 159 Sequence( |
154 ExpList(false), | 160 ExpList(false), |
155 push( new ReturnStmt( se(start.get()), (Expressions)pop() ) ), | 161 push( new ReturnStmt( se(start.get()), (Expressions)pop() ) ), |
156 push( newChunk(start.get()) ), | 162 push( newFnDef(start.get()) ), |
157 EOI | 163 EOI |
158 ), | 164 ), |
159 Sequence( | 165 Sequence( |
160 action( frame.isVarArg = true ), | 166 action( frame.isVarArg = true ), |
161 Block(), | 167 Block(), |
162 push( newChunk(start.get()) ), | 168 push( newFnDef(start.get()) ), |
163 EOI | 169 EOI |
164 ) | 170 ) |
165 ) | 171 ) |
166 ); | 172 ); |
167 } | 173 } |
646 ), | 652 ), |
647 VarArgName() | 653 VarArgName() |
648 ) | 654 ) |
649 ), | 655 ), |
650 ')', Spaces(inParens), Block(), Keyword("end",inParens), | 656 ')', Spaces(inParens), Block(), Keyword("end",inParens), |
651 push( newChunk(start.get()) ), | 657 push( newFnDef(start.get()) ), |
652 action( frame = frame.parent ) | 658 action( frame = frame.parent ) |
653 ); | 659 ); |
654 } | 660 } |
655 | 661 |
656 Rule VarArgName() { | 662 Rule VarArgName() { |