From 03deaf70dde4181373c2c3c44e8bbb0ba6718b41 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Tue, 8 Sep 2015 19:14:27 +0200 Subject: make scopes ACTUALLY WORK nicely --- functions.cpp | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'functions.cpp') diff --git a/functions.cpp b/functions.cpp index 9655e5a..8bd4053 100644 --- a/functions.cpp +++ b/functions.cpp @@ -1,11 +1,12 @@ #include #include #include -#include +// #include #include #include #include #include +#include #include #include @@ -18,7 +19,10 @@ extern char **g_argv; #define BUILTIN_GUARD_STACKSIZE(nm,sz) {if(S.size()<(sz))throw string("Builtin '" nm "' needs " #sz " stack item")+(sz==1?"":"s");} -stack> scopestack; +stack>> scopestack; +//Each stack frame contains the variables local to the stack frame. The bool is +//whether it was already present in the previous frame, and the Stackitem +//is the value held by the previous frame, if any. //please extern this shit with const! unordered_map&,unordered_map&)>> builtins={ @@ -213,11 +217,17 @@ unordered_map&,unordered_map &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("gstore",2) @@ -241,17 +251,19 @@ unordered_map&,unordered_map &S,unordered_map &variables){ - //cerr<<"scope enter"< &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("leavescope",0) if(scopestack.size()<1)throw string("Scope stack empty while requesting scope leave"); - //cerr<<"scope leave: "; - for(const string &varname : scopestack.top()){ - variables.erase(varname); - //cerr<> &var : scopestack.top()){ + if(var.second.second){ + variables[var.first]=move(var.second.first); + } else { + variables.erase(var.first); + } } - //cerr<&,unordered_map &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("sleep",1) + if(S.back().isstr)throw string("Argument to 'sleep' not a number"); + usleep(S.back().intval); + S.pop_back(); + }}, {"stackdump",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("stackdump",0) cerr<<"STACKDUMP: "; -- cgit v1.2.3-70-g09d2