From ed0409a2a7450905bdc70015ffe537171684dddf Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Tue, 8 Sep 2015 20:40:20 +0200 Subject: @includeonce :tada: --- runtime.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/runtime.cpp b/runtime.cpp index 3cf607d..03c87b4 100644 --- a/runtime.cpp +++ b/runtime.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -232,7 +233,9 @@ void run(vector T){ unordered_map variables; unordered_map jumpmap; vector S; + unordered_set includeonces; unsigned int cursor=0; + bool skipincluding; //first pass, handle functions and includes while(cursor T){ if(depth==0)break; } if(depth!=0)throw string("Non-terminated @defun statement at end of file"); + auto it=functions.find(name); + if(it!=functions.end()){ + throw string("Re-definition of function '"+name+"'!"); + } vector &functoks=functions[name].first; functoks.resize(end-start); for(unsigned int i=start;i=T.size())throw string("Unterminated @include statement at end of file"); string name=T[cursor+1]; if(name[0]!='\'')throw string("@include expected a string as file, but got '")+name+"'"; name.erase(0,1); - ifstream file(name); - if(!file)throw string("Could not open file '")+name+"' specified by @include statement"; - vector included=tokenise(file); - file.close(); + skipincluding=false; + if(word=="@includeonce"){ + if(includeonces.find(name)==includeonces.end()) + includeonces.insert(name); + else + skipincluding=true; + } T.erase(T.begin()+cursor,T.begin()+cursor+2); - T.insert(T.begin()+cursor,included.begin(),included.end()); + if(!skipincluding){ + ifstream file(name); + if(!file)throw "Could not open file '"+name+"' specified by "+word+" statement"; + vector included=tokenise(file); + file.close(); + T.insert(T.begin()+cursor,included.begin(),included.end()); + } } else { cursor++; } -- cgit v1.2.3-70-g09d2