From b50d606bdd0fbda3451e9fcb07e8096fa5bee55b Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 6 Sep 2015 22:18:30 +0200 Subject: Add scopes! --- functions.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'functions.cpp') diff --git a/functions.cpp b/functions.cpp index 4795c93..9655e5a 100644 --- a/functions.cpp +++ b/functions.cpp @@ -1,7 +1,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -16,6 +18,8 @@ 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; + //please extern this shit with const! unordered_map&,unordered_map&)>> builtins={ {"+",[](vector &S,unordered_map &variables){ @@ -207,6 +211,18 @@ unordered_map&,unordered_map &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("gstore",2) + Stackitem name=move(S.back()); S.pop_back(); + if(!name.isstr)throw string("Second argument to 'store' not a string"); variables[name.strval]=move(S.back()); S.pop_back(); }}, @@ -224,6 +240,20 @@ unordered_map&,unordered_map &S,unordered_map &variables){ + //cerr<<"scope enter"< &S,unordered_map &variables){ + 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< &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("roll",1) -- cgit v1.2.3-70-g09d2