@@ -60,7 +60,12 @@ pub fn gen_prototype(block: Box<Block>) -> Result<Rc<Prototype>> {
60
60
let last_line = block. last_line ;
61
61
let fn_def = FnDef :: new ( ParList :: default ( ) , block, 0 , last_line) ;
62
62
let mut fn_info = FnInfo :: new ( None , ParList :: default ( ) , 0 , last_line) ;
63
- fn_info. add_local_var ( "_ENV" . to_string ( ) , 0 ) ?;
63
+ // fn_info.add_local_var("_ENV".to_string(), 0)?;
64
+ fn_info. up_values . insert (
65
+ "_ENV" . to_string ( ) ,
66
+ UpValueInfo :: new ( Some ( 0 ) , Some ( 0 ) , 0 ) ,
67
+ ) ;
68
+
64
69
fn_info. codegen_fn_def_exp ( & fn_def, 0 ) ?;
65
70
66
71
Ok ( fn_info. to_prototype ( ) )
@@ -192,7 +197,7 @@ impl FnInfo {
192
197
used_regs : 0 ,
193
198
max_regs : 0 ,
194
199
scope_level : 0 ,
195
- local_vars : Vec :: new ( ) ,
200
+ local_vars : vec ! [ HashMap :: new( ) ] ,
196
201
breaks : Vec :: new ( ) ,
197
202
// parent_index,
198
203
// current_index,
@@ -208,17 +213,6 @@ impl FnInfo {
208
213
}
209
214
}
210
215
211
- fn find_fn_info_by_index ( mut fn_info : Rc < RefCell < FnInfo > > , index : usize ) -> Option < Rc < RefCell < FnInfo > > > {
212
- unimplemented ! ( ) ;
213
- // if fn_info.borrow_mut().current_index == index {
214
- // Some(fn_info)
215
- // } else {
216
- // for sub_fn in fn_info.borrow_mut().sub_fns.iter() {
217
- // return Self::find_fn_info_by_index(sub_fn.0.clone(), index);
218
- // }
219
- // None
220
- // }
221
- }
222
216
223
217
fn constant_index ( & mut self , k : & Constant ) -> usize {
224
218
match self . constants . get ( k) {
@@ -335,10 +329,10 @@ impl FnInfo {
335
329
} ) ) ;
336
330
let slot = new_var. borrow_mut ( ) . slot ;
337
331
if self . local_vars . len ( ) <= self . scope_level {
338
- self . local_vars . resize ( self . local_vars . len ( ) + 2 , HashMap :: new ( ) ) ;
332
+ self . local_vars . resize ( self . local_vars . len ( ) * 2 , HashMap :: new ( ) ) ;
339
333
}
340
- self . local_vars [ self . scope_level ] . insert ( name, new_var) ;
341
334
335
+ self . local_vars [ self . scope_level ] . insert ( name, new_var) ;
342
336
Ok ( slot)
343
337
}
344
338
@@ -455,6 +449,7 @@ impl FnInfo {
455
449
} )
456
450
}
457
451
}
452
+
458
453
res
459
454
}
460
455
@@ -744,7 +739,7 @@ impl FnInfo {
744
739
// clear sBx Op
745
740
ins = ins << 18 >> 18 ;
746
741
// reset sBx op
747
- ins = ins | ( sBx as u32 + MAXARG_SBX as u32 ) << 14 ;
742
+ ins = ins | ( ( sBx + MAXARG_SBX ) as u32 ) << 14 ;
748
743
self . instructions [ pc] = ins;
749
744
}
750
745
}
@@ -984,9 +979,6 @@ impl FnInfo {
984
979
self . exit_scope ( )
985
980
}
986
981
987
- fn remove_tail_nils ( exps : & Vec < Exp > ) -> Result < Vec < Exp > > {
988
- unimplemented ! ( )
989
- }
990
982
991
983
fn codegen_assign_stat ( & mut self , names : & Vec < Exp > , vals : & Vec < Exp > , line : Line ) -> Result < ( ) > {
992
984
let old_regs = self . used_regs ;
@@ -1065,7 +1057,6 @@ impl FnInfo {
1065
1057
self . emit_set_table ( last_line, a as isize , k_regs[ i] , v_regs[ i] ) ;
1066
1058
}
1067
1059
} else {
1068
- dbg ! ( ) ;
1069
1060
let a = self . up_value_index ( & "_ENV" . to_string ( ) )
1070
1061
. ok_or ( Error :: NotUpValue {
1071
1062
line : last_line,
@@ -1125,14 +1116,13 @@ impl FnInfo {
1125
1116
let a = self . alloc_registers ( n) ?;
1126
1117
self . emit_load_nil ( last_line, a as isize , n as isize ) ;
1127
1118
}
1119
+ }
1128
1120
1129
- self . used_regs = old_regs;
1130
- let start_pc = self . pc ( ) + 1 ;
1131
- for name in names {
1132
- self . add_local_var ( name. clone ( ) , start_pc) ?;
1133
- }
1121
+ self . used_regs = old_regs;
1122
+ let start_pc = self . pc ( ) + 1 ;
1123
+ for name in names {
1124
+ self . add_local_var ( name. clone ( ) , start_pc) ?;
1134
1125
}
1135
- dbg ! ( ) ;
1136
1126
1137
1127
Ok ( ( ) )
1138
1128
}
@@ -1172,12 +1162,11 @@ impl FnInfo {
1172
1162
let a = self . alloc_registers ( n) ?;
1173
1163
self . emit_load_nil ( last_line, a as isize , n as isize ) ;
1174
1164
}
1175
-
1176
- self . used_regs = old_regs;
1177
- let start_pc = self . pc ( ) + 1 ;
1178
- for name in names {
1179
- self . add_local_var ( name. clone ( ) , start_pc) ?;
1180
- }
1165
+ }
1166
+ self . used_regs = old_regs;
1167
+ let start_pc = self . pc ( ) + 1 ;
1168
+ for name in names {
1169
+ self . add_local_var ( name. clone ( ) , start_pc) ?;
1181
1170
}
1182
1171
1183
1172
Ok ( ( ) )
@@ -1449,6 +1438,9 @@ fn is_vararg_or_fn_call(exp: &Exp) -> bool {
1449
1438
1450
1439
1451
1440
mod tests {
1441
+ use std:: fs;
1442
+
1443
+ use crate :: binary:: encode;
1452
1444
use crate :: compiler:: lexer:: * ;
1453
1445
use crate :: compiler:: parser:: * ;
1454
1446
@@ -1507,4 +1499,31 @@ mod tests {
1507
1499
let proto = gen_prototype ( Box :: new ( block) ) ;
1508
1500
println ! ( "{:#?}" , proto) ;
1509
1501
}
1502
+
1503
+ #[ test]
1504
+ fn test_codegen2 ( ) {
1505
+ let s = r##"
1506
+ local g = {
1507
+ a = 1,
1508
+ b = {}
1509
+ }
1510
+
1511
+ print(g.a)
1512
+ print(g.b)
1513
+
1514
+ local a = 1
1515
+ while a < 1 do
1516
+ a = a + 1
1517
+ end
1518
+ print(a)
1519
+ "## . to_string ( ) ;
1520
+
1521
+ let mut lexer = Lexer :: from_iter ( s. into_bytes ( ) , "test" . to_string ( ) ) ;
1522
+ let block = parse_block ( & mut lexer) . expect ( "parse error" ) ;
1523
+ let proto = gen_prototype ( Box :: new ( block) ) ;
1524
+
1525
+ let bytes = encode ( proto. unwrap ( ) , Some ( "@hello2.lua" . to_string ( ) ) ) ;
1526
+ fs:: write ( "D:/code/Rust/lua-rs/tests/test.out" , bytes) ;
1527
+ assert_eq ! ( 1 , 2 ) ;
1528
+ }
1510
1529
}
0 commit comments