comparison src/luan/interp/LuaParser.java @ 18:3971113699b8

add NumericForStmt git-svn-id: https://luan-java.googlecode.com/svn/trunk@19 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 02 Dec 2012 07:00:44 +0000
parents 09d41f7490a8
children a7c13c6017f7
comparison
equal deleted inserted replaced
17:09d41f7490a8 18:3971113699b8
43 } 43 }
44 44
45 int index(String name) { 45 int index(String name) {
46 int i = symbols.size(); 46 int i = symbols.size();
47 while( --i >= 0 ) { 47 while( --i >= 0 ) {
48 System.out.println("index ["+name+"] ["+symbols.get(i)+"] "+symbols.get(i).equals(name));
48 if( symbols.get(i).equals(name) ) 49 if( symbols.get(i).equals(name) )
49 return i; 50 return i;
50 } 51 }
51 return -1; 52 return -1;
52 } 53 }
68 ); 69 );
69 } 70 }
70 71
71 Rule Block() { 72 Rule Block() {
72 Var<List<Stmt>> stmts = new Var<List<Stmt>>(new ArrayList<Stmt>()); 73 Var<List<Stmt>> stmts = new Var<List<Stmt>>(new ArrayList<Stmt>());
73 return Sequence( 74 Var<Integer> stackCount = new Var<Integer>(0);
74 push(0), // stackCount 75 return Sequence(
75 Optional( Stmt(stmts) ), 76 Optional( Stmt(stmts,stackCount) ),
76 ZeroOrMore( 77 ZeroOrMore(
77 StmtSep(), 78 StmtSep(),
78 Optional( Stmt(stmts) ) 79 Optional( Stmt(stmts,stackCount) )
79 ), 80 ),
80 push( newBlock(stmts.get()) ) 81 push( newBlock(stmts.get(),stackCount.get()) )
81 ); 82 );
82 } 83 }
83 84
84 Stmt newBlock(List<Stmt> stmts) { 85 Stmt newBlock(List<Stmt> stmts,int stackN) {
85 if( stackSize < symbols.size() ) 86 if( stackSize < symbols.size() )
86 stackSize = symbols.size(); 87 stackSize = symbols.size();
87 int stackN = (Integer)pop();
88 for( int i=0; i<stackN; i++ ) { 88 for( int i=0; i<stackN; i++ ) {
89 symbols.remove(symbols.size()-1); // pop 89 symbols.remove(symbols.size()-1); // pop
90 } 90 }
91 if( stmts.isEmpty() ) 91 if( stmts.isEmpty() )
92 return Stmt.EMPTY; 92 return Stmt.EMPTY;
110 110
111 Rule EndOfLine() { 111 Rule EndOfLine() {
112 return FirstOf("\r\n", '\r', '\n'); 112 return FirstOf("\r\n", '\r', '\n');
113 } 113 }
114 114
115 Rule Stmt(Var<List<Stmt>> stmts) { 115 Rule Stmt(Var<List<Stmt>> stmts,Var<Integer> stackCount) {
116 return FirstOf( 116 return FirstOf(
117 LocalStmt(stmts), 117 LocalStmt(stmts,stackCount),
118 Sequence( 118 Sequence(
119 FirstOf( 119 FirstOf(
120 NumericForStmt(),
120 DoStmt(), 121 DoStmt(),
121 WhileStmt(), 122 WhileStmt(),
122 RepeatStmt(), 123 RepeatStmt(),
123 IfStmt(), 124 IfStmt(),
124 SetStmt(), 125 SetStmt(),
127 stmts.get().add( (Stmt)pop() ) 128 stmts.get().add( (Stmt)pop() )
128 ) 129 )
129 ); 130 );
130 } 131 }
131 132
133 Rule NumericForStmt() {
134 return Sequence(
135 Keyword("for"), Name(), '=', Spaces(), Expr(), ',', Spaces(), Expr(),
136 push( new ConstExpr(new LuaNumber(1)) ), // default step
137 Optional(
138 ',', Spaces(),
139 drop(),
140 Expr()
141 ),
142 symbols.add( (String)pop(3) ), // add "for" var to symbols
143 Keyword("do"), Block(), Keyword("end"),
144 push( new NumericForStmt( symbols.size()-1, expr(pop(3)), expr(pop(2)), expr(pop(1)), (Stmt)pop() ) ),
145 action( symbols.remove(symbols.size()-1) ) // pop
146 );
147 }
148
132 Rule DoStmt() { 149 Rule DoStmt() {
133 return Sequence( 150 return Sequence(
134 Keyword("do"), Block(), Keyword("end") 151 Keyword("do"), Block(), Keyword("end")
135 ); 152 );
136 } 153 }
137 154
138 Rule LocalStmt(Var<List<Stmt>> stmts) { 155 Rule LocalStmt(Var<List<Stmt>> stmts,Var<Integer> stackCount) {
139 Var<List<String>> names = new Var<List<String>>(new ArrayList<String>()); 156 Var<List<String>> names = new Var<List<String>>(new ArrayList<String>());
140 return Sequence( 157 return Sequence(
141 Keyword("local"), 158 Keyword("local"),
142 Name(), 159 Name(),
143 newName(names.get()), 160 newName(names.get(),stackCount),
144 ZeroOrMore( 161 ZeroOrMore(
145 ',', Spaces(), Name(), 162 ',', Spaces(), Name(),
146 newName(names.get()) 163 newName(names.get(),stackCount)
147 ), 164 ),
148 Optional( 165 Optional(
149 '=', Spaces(), 166 '=', Spaces(),
150 ExpList(), 167 ExpList(),
151 stmts.get().add( newSetLocalStmt(names.get()) ) 168 stmts.get().add( newSetLocalStmt(names.get()) )
152 ) 169 )
153 ); 170 );
154 } 171 }
155 172
156 boolean newName(List<String> names) { 173 boolean newName(List<String> names,Var<Integer> stackCount) {
157 String name = (String)pop(); 174 String name = (String)pop();
158 names.add(name); 175 names.add(name);
159 symbols.add(name); 176 symbols.add(name);
160 push( ((Integer)pop()) + 1 ); 177 stackCount.set( stackCount.get() + 1 );
161 return true; 178 return true;
162 } 179 }
163 180
164 SetStmt newSetLocalStmt(List<String> names) { 181 SetStmt newSetLocalStmt(List<String> names) {
165 Expressions values = (Expressions)pop(); 182 Expressions values = (Expressions)pop();
818 835
819 Rule LongBracketsEnd() { 836 Rule LongBracketsEnd() {
820 return Sequence( ']', ZeroOrMore('='), nEquals==matchLength(), ']' ); 837 return Sequence( ']', ZeroOrMore('='), nEquals==matchLength(), ']' );
821 } 838 }
822 839
840 static boolean action(Object obj) {
841 return true;
842 }
843
823 // for debugging 844 // for debugging
824 boolean print(Object o) { 845 boolean print(Object o) {
825 System.out.println(o); 846 System.out.println(o);
826 return true; 847 return true;
827 } 848 }