@@ -11,7 +11,7 @@ use structs::{
1111use ustructs:: { UserStructDef , UserStructInst } ;
1212
1313use crate :: {
14- errors, lexer:: TokenType , utils, vars:: { self , VarMap , VARMAP }
14+ errors, lexer:: TokenType , utils, vars:: { self , VarMap }
1515} ;
1616
1717impl < Visitor , T > Accept < Visitor > for LiteralExpr
@@ -313,20 +313,36 @@ where
313313 }
314314}
315315
316- pub fn __execute_block ( stmt_eval : & StmtEvaluator , statements : & Vec < Statement > ) -> ControlFlowType {
316+ pub fn __execute_block_without_cleanup ( stmt_eval : & StmtEvaluator , statements : & Vec < Statement > , env : & mut VarMap ) -> ControlFlowType {
317+ vars:: set_environment ( env. clone ( ) ) ;
318+
317319 for stat in statements {
318320 let result = stat. accept ( stmt_eval) ;
319321
320322 match result {
321323 // in case it's a break or continue we have to propagate upwards
322324 ControlFlowType :: None => { }
323- out => return out,
325+ out => {
326+ return out
327+ } ,
324328 }
325329 }
326330
327331 ControlFlowType :: None
328332}
329333
334+ pub fn __execute_block ( stmt_eval : & StmtEvaluator , statements : & Vec < Statement > , env : & mut VarMap ) -> ControlFlowType {
335+ let old_env = vars:: clone_environment ( ) ;
336+
337+ let result = __execute_block_without_cleanup ( stmt_eval, statements, env) ;
338+
339+ * env = vars:: clone_environment ( ) ;
340+
341+ vars:: set_environment ( old_env) ;
342+
343+ result
344+ }
345+
330346pub struct StmtEvaluator ;
331347
332348impl StmtVisitor for StmtEvaluator {
@@ -353,15 +369,15 @@ impl StmtVisitor for StmtEvaluator {
353369
354370 let as_copy: Value = match expr_value {
355371 Value :: Array ( arr) => arr. lock ( ) . unwrap ( ) . clone ( ) . into ( ) ,
356- Value :: StructInst ( inst) => {
357- let env = ( * inst. env . lock ( ) . unwrap ( ) ) . clone ( ) ;
372+ Value :: StructInst ( ref inst) => {
373+ let env = inst. 0 . lock ( ) . unwrap ( ) . clone ( ) ;
358374
359375 UserStructInst :: new ( env) . into ( )
360376 }
361377 _ => expr_value,
362378 } ;
363379
364- VARMAP . lock ( ) . unwrap ( ) . insert ( name. clone ( ) , as_copy) ;
380+ vars :: insert ( name. clone ( ) , as_copy) ;
365381
366382 ControlFlowType :: None
367383 }
@@ -373,10 +389,9 @@ impl StmtVisitor for StmtEvaluator {
373389 . as_identifier ( )
374390 . expect ( "Unexpected lexing of the name of the function" ) ;
375391
376- VARMAP . lock ( ) . unwrap ( ) . insert (
377- name. clone ( ) ,
378- UserFunction :: new ( expr. params . clone ( ) , expr. body . clone ( ) ) . into ( ) ,
379- ) ;
392+ let func: Value = UserFunction :: new ( expr. clone ( ) , vars:: clone_environment ( ) ) . into ( ) ;
393+
394+ vars:: insert ( name. clone ( ) , func) ;
380395
381396 ControlFlowType :: None
382397 }
@@ -390,23 +405,21 @@ impl StmtVisitor for StmtEvaluator {
390405
391406 let strct = UserStructDef :: new ( self , & expr. methods ) ;
392407
393- VARMAP . lock ( ) . unwrap ( ) . insert ( name. clone ( ) , strct. into ( ) ) ;
408+ vars :: insert ( name. clone ( ) , strct. into ( ) ) ;
394409
395410 ControlFlowType :: None
396411 }
397412
398413 fn visit_block_stmt ( & self , expr : & BlockStmt ) -> Self :: Output {
399- if expr. is_standalone {
400- vars:: create_inner ( false ) ;
401- }
402-
403- let result = __execute_block ( self , & expr. statements ) ;
414+ let mut vmap = vars:: clone_environment ( ) ;
404415
405- if expr. is_standalone {
406- vars :: delete_inner ( ) ;
416+ if ! expr. is_standalone {
417+ return __execute_block_without_cleanup ( self , & expr . statements , & mut vmap ) ;
407418 }
408419
409- result
420+ let mut map = VarMap :: new ( Some ( Box :: new ( vmap) ) ) ;
421+
422+ __execute_block ( self , & expr. statements , & mut map)
410423 }
411424
412425 fn visit_if_stmt ( & self , expr : & IfStmt ) -> Self :: Output {
@@ -460,6 +473,7 @@ impl StmtVisitor for StmtEvaluator {
460473 }
461474
462475 continue ;
476+
463477 }
464478 ControlFlowType :: Return ( ret) => {
465479 return ControlFlowType :: Return ( ret) ;
@@ -506,22 +520,20 @@ impl StmtVisitor for StmtEvaluator {
506520 fn visit_set_stmt ( & self , expr : & SetStmt ) -> Self :: Output {
507521 let eval = ExprEvaluator ;
508522
509- let caller = expr. callee . accept ( & eval) ;
510-
511- let insert_into_env = |env : & Mutex < VarMap > | {
512- let mut guard = env. lock ( ) . unwrap ( ) ;
523+ let mut caller = expr. callee . accept ( & eval) ;
513524
525+ let insert_into_env = |env : & mut UserStructInst | {
514526 let name = expr. name . token_type . as_identifier ( ) . unwrap ( ) . clone ( ) ;
515527
516528 let rvalue = expr. rvalue . accept ( & eval) ;
517529
518530 if expr. op . token_type . is_equal ( ) {
519- guard . insert ( name, rvalue) ;
531+ env . set ( name, rvalue) ;
520532 return ;
521533 }
522534
523535 //is of type
524- let lvalue = match guard . get ( & name) . cloned ( ) {
536+ let lvalue = match env . get ( & name, true ) {
525537 Some ( val) => val,
526538 None => Value :: Null ,
527539 } ;
@@ -535,15 +547,12 @@ impl StmtVisitor for StmtEvaluator {
535547 _ => unreachable ! ( ) ,
536548 } ;
537549
538- guard . insert ( name, result) ;
550+ env . set ( name, result) ;
539551 } ;
540552
541553 match caller {
542- Value :: __StructEnv ( ref inst) => {
554+ Value :: StructInst ( ref mut inst) => {
543555 insert_into_env ( inst) ;
544- }
545- Value :: StructInst ( ref inst) => {
546- insert_into_env ( & inst. env ) ;
547556 } //we don't allow static set stmts
548557 _ => {
549558 errors:: LIST
@@ -615,9 +624,11 @@ impl ExprEvaluator {
615624 [ ( * left) . clone ( ) , ( * right) . clone ( ) ] . concat ( ) . into ( )
616625 }
617626 ( Value :: Array ( l) , r) => self . push_to_arr ( & l, r) . into ( ) ,
618- ( Value :: StructInst ( l) , r) => {
627+ ( Value :: StructInst ( mut l) , r) => {
619628 let func = match l. get_with_this_as_func ( "_add_" ) {
620- Some ( v) => v,
629+ Some ( v) => {
630+ v
631+ } ,
621632 None => {
622633 errors:: LIST
623634 . lock ( )
@@ -632,7 +643,7 @@ impl ExprEvaluator {
632643 _ => errors:: LIST
633644 . lock ( )
634645 . unwrap ( )
635- . push ( AstError :: InvalidUseOfAddOperator , None ) ,
646+ . push ( AstError :: InvalidUseOfAddOperator , None )
636647 }
637648 }
638649
@@ -666,8 +677,6 @@ impl ExprEvaluator {
666677 }
667678 }
668679
669-
670-
671680 fn divide ( & self , left : Value , right : Value ) -> Value {
672681 match ( left, right) {
673682 ( Value :: Number ( l) , Value :: Number ( r) ) => {
@@ -723,7 +732,7 @@ impl ExprEvaluator {
723732
724733 fn equal ( & self , left : Value , right : Value , not : bool ) -> Value {
725734 match ( left, right) {
726- ( Value :: StructInst ( l) , r) => {
735+ ( Value :: StructInst ( mut l) , r) => {
727736 let func = match l. get_with_this_as_func ( "_eq_" ) {
728737 Some ( v) => v,
729738 None => {
@@ -893,11 +902,7 @@ impl ExprVisitor for ExprEvaluator {
893902 . as_identifier ( )
894903 . expect ( "Failure converting to identifier at the parser" ) ;
895904
896- VARMAP
897- . lock ( )
898- . unwrap ( )
899- . get ( name)
900- . cloned ( )
905+ vars:: get ( name)
901906 . unwrap_or ( Value :: Null )
902907 }
903908
@@ -937,7 +942,7 @@ impl ExprVisitor for ExprEvaluator {
937942
938943 let caller = expr. callee . accept ( self ) ;
939944
940- if !caller. is_function ( ) && !caller. is_struct_def ( ) {
945+ if !caller. is_function ( ) && !caller. is_global_function ( ) && !caller . is_struct_def ( ) {
941946 errors:: LIST
942947 . lock ( )
943948 . unwrap ( )
@@ -954,6 +959,7 @@ impl ExprVisitor for ExprEvaluator {
954959
955960 match caller {
956961 Value :: Function ( ref func) => func. call ( & eval, self , args) ,
962+ Value :: GlobalFunction ( ref gfunc) => gfunc. call ( & eval, self , args) ,
957963 Value :: StructDef ( ref def) => def. call ( & eval, self , args) ,
958964 _ => unreachable ! ( ) ,
959965 }
@@ -970,7 +976,7 @@ impl ExprVisitor for ExprEvaluator {
970976 . to_string ( ) ;
971977
972978 match caller {
973- Value :: StructInst ( ref inst) => {
979+ Value :: StructInst ( mut inst) => {
974980 if expr. is_static {
975981 errors:: LIST
976982 . lock ( )
@@ -981,22 +987,6 @@ impl ExprVisitor for ExprEvaluator {
981987
982988 inst. get_with_this ( argument) . unwrap_or ( Value :: Null )
983989 }
984- Value :: __StructEnv( ref env) => {
985- //a 'this' expression
986- if expr. is_static {
987- errors:: LIST
988- . lock ( )
989- . unwrap ( )
990- . push ( AstError :: BadGetExpression , None ) ;
991- return Value :: Null ;
992- }
993-
994- env. lock ( )
995- . unwrap ( )
996- . get ( argument)
997- . unwrap_or ( & Value :: Null )
998- . clone ( )
999- }
1000990 Value :: StructDef ( ref def) => {
1001991 if !expr. is_static {
1002992 errors:: LIST
@@ -1007,11 +997,8 @@ impl ExprVisitor for ExprEvaluator {
1007997 }
1008998
1009999 def. env
1010- . lock ( )
1011- . unwrap ( )
10121000 . get ( argument)
1013- . unwrap_or ( & Value :: Null )
1014- . clone ( )
1001+ . unwrap_or ( Value :: Null )
10151002 }
10161003 Value :: Array ( ref arr) => arr. get_internal_var ( argument) ,
10171004 _ => {
@@ -1026,10 +1013,8 @@ impl ExprVisitor for ExprEvaluator {
10261013 }
10271014
10281015 fn visit_this_expr ( & self , expr : & ThisExpr ) -> Self :: Output {
1029- let guard = VARMAP . lock ( ) . unwrap ( ) ;
1030-
1031- match guard. get ( "this" ) {
1032- Some ( val) => val. clone ( ) ,
1016+ match vars:: get ( "this" ) {
1017+ Some ( val) => val,
10331018 None => {
10341019 errors:: LIST
10351020 . lock ( )
@@ -1054,10 +1039,8 @@ impl Interpreter {
10541039 let funcs: Vec < Box < dyn Callable > > =
10551040 vec ! [ Box :: new( Clock ) , Box :: new( Print ) , Box :: new( PrintLn ) , Box :: new( TypeOf ) , Box :: new( _TypeOf) ] ;
10561041
1057- let mut var_map = VARMAP . lock ( ) . unwrap ( ) ;
1058-
10591042 for func in funcs{
1060- var_map . insert ( func. name ( ) , Value :: Function ( func) ) ; // map the global functions
1043+ vars :: insert ( func. name ( ) , Value :: GlobalFunction ( func) ) ; //map the global functions
10611044 }
10621045
10631046 Interpreter
0 commit comments