From 0711be4f145b3d74bc90658dd71d186716e6a72c Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 9 Sep 2015 22:06:32 +0200 Subject: Add arrays to Postrun --- functions.cpp | 197 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 135 insertions(+), 62 deletions(-) (limited to 'functions.cpp') diff --git a/functions.cpp b/functions.cpp index 7a6ce36..dacd194 100644 --- a/functions.cpp +++ b/functions.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -30,81 +31,107 @@ unordered_map&,unordered_map at least one is string and other is int + if(a.type!=SIT_STR){ + S.emplace_back(to_string(a.intval)+b.strval); + } else if(b.type!=SIT_STR){ + S.emplace_back(a.strval+to_string(b.intval)); + } else { + S.emplace_back(a.strval+b.strval); } - S.emplace_back(a.strval+b.strval); }}, {"-",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("-",2) Stackitem b=move(S.back()); S.pop_back(); Stackitem a=move(S.back()); S.pop_back(); - if(a.isstr||b.isstr)throw string("Builtin '-' cannot accept a string argument"); + if(a.type!=SIT_INT||b.type!=SIT_INT) + throw string("Builtin '-' expects two number arguments"); S.emplace_back(a.intval-b.intval); }}, {"*",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("*",2) Stackitem b=move(S.back()); S.pop_back(); Stackitem a=move(S.back()); S.pop_back(); - if(a.isstr||b.isstr)throw string("Builtin '*' cannot accept a string argument"); + if(a.type!=SIT_INT||b.type!=SIT_INT) + throw string("Builtin '*' expects two number arguments"); S.emplace_back(a.intval*b.intval); }}, {"/",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("/",2) Stackitem b=move(S.back()); S.pop_back(); Stackitem a=move(S.back()); S.pop_back(); - if(a.isstr||b.isstr)throw string("Builtin '/' cannot accept a string argument"); + if(a.type!=SIT_INT||b.type!=SIT_INT) + throw string("Builtin '/' expects two number arguments"); S.emplace_back(a.intval/b.intval); }}, {"%",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("%",2) Stackitem b=move(S.back()); S.pop_back(); Stackitem a=move(S.back()); S.pop_back(); - if(a.isstr||b.isstr)throw string("Builtin '%' cannot accept a string argument"); + if(a.type!=SIT_INT||b.type!=SIT_INT) + throw string("Builtin '%' expects two number arguments"); S.emplace_back(a.intval%b.intval); }}, {"&",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("&",2) Stackitem b=move(S.back()); S.pop_back(); Stackitem a=move(S.back()); S.pop_back(); - if(a.isstr||b.isstr)throw string("Builtin '&' cannot accept a string argument"); + if(a.type!=SIT_INT||b.type!=SIT_INT) + throw string("Builtin '&' expects two number arguments"); S.emplace_back(a.intval&b.intval); }}, {"|",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("|",2) Stackitem b=move(S.back()); S.pop_back(); Stackitem a=move(S.back()); S.pop_back(); - if(a.isstr||b.isstr)throw string("Builtin '|' cannot accept a string argument"); + if(a.type!=SIT_INT||b.type!=SIT_INT) + throw string("Builtin '|' expects two number arguments"); S.emplace_back(a.intval|b.intval); }}, {"~",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("~",1) Stackitem &back=S.back(); - if(back.isstr)throw string("Builtin '~' cannot accept a string argument"); + if(back.type!=SIT_INT)throw string("Builtin '~' expects a number argument"); else back.intval=~back.intval; }}, {"^",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("^",2) Stackitem b=move(S.back()); S.pop_back(); Stackitem a=move(S.back()); S.pop_back(); - if(a.isstr||b.isstr)throw string("Builtin '^' cannot accept a string argument"); + if(a.type!=SIT_INT||b.type!=SIT_INT) + throw string("Builtin '^' expects two number arguments"); S.emplace_back((int)pow(a.intval,b.intval)); }}, {"!",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("!",1) Stackitem &back=S.back(); - if(back.isstr){ - back.isstr=false; + if(back.type==SIT_ARR){ + back.type=SIT_INT; + back.intval=!back.arrval.size(); + back.arrval.clear(); + } else if(back.type==SIT_STR){ + back.type=SIT_INT; back.intval=!back.strval.size(); + back.strval.clear(); } else back.intval=!back.intval; }}, {"=",[](vector &S,unordered_map &variables){ @@ -117,25 +144,30 @@ unordered_map&,unordered_map",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE(">",2) Stackitem b=move(S.back()); S.pop_back(); Stackitem a=move(S.back()); S.pop_back(); - if(a.isstr&&b.isstr)S.emplace_back(a.strval>b.strval); - else if(!a.isstr&&!b.isstr)S.emplace_back(a.intval>b.intval); - else if(a.isstr)S.emplace_back(a.strval>to_string(b.intval)); + if(a.type==SIT_ARR&&b.type==SIT_ARR)S.emplace_back(a.arrval.size()>b.arrval.size()); + else if(a.type==SIT_ARR||b.type==SIT_ARR){ + throw string("Builtin '>' cannot accept an array and a non-array argument"); + } else if(a.type==SIT_STR&&b.type==SIT_STR)S.emplace_back(a.strval>b.strval); + else if(a.type==SIT_INT&&b.type==SIT_INT)S.emplace_back(a.intval>b.intval); + else if(a.type==SIT_STR)S.emplace_back(a.strval>to_string(b.intval)); else S.emplace_back(to_string(a.intval)>b.strval); }}, {"print",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("print",1) - Stackitem v=move(S.back()); S.pop_back(); - if(v.isstr)cout< &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("lf",0) @@ -146,7 +178,7 @@ unordered_map&,unordered_map &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("getc",0) @@ -158,23 +190,23 @@ unordered_map&,unordered_map &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("flf",1) Stackitem file=move(S.back()); S.pop_back(); - if(file.isstr)throw string("Argument to 'flf' not a number"); + if(file.type!=SIT_INT)throw string("Argument to 'flf' not a number"); write(file.intval,"\n",1); }}, {"fgetc",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("fgetc",1) Stackitem file=move(S.back()); S.pop_back(); - if(file.isstr)throw string("Argument to 'fgetc' not a number"); + if(file.type!=SIT_INT)throw string("Argument to 'fgetc' not a number"); char c; int ret=read(file.intval,&c,1); if(ret==-1)perror("read"); @@ -185,8 +217,8 @@ unordered_map&,unordered_map&,unordered_map &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("argvget",1) Stackitem idx=move(S.back()); S.pop_back(); - if(idx.isstr)throw string("Argument to 'argvget' not a number"); + if(idx.type!=SIT_INT)throw string("Argument to 'argvget' not a number"); if(idx.intval<0||idx.intval>=g_argc)throw string("Argument to 'argvget' out of bounds"); S.emplace_back(g_argv[idx.intval]); }}, @@ -223,13 +255,13 @@ unordered_map&,unordered_map &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("store",2) Stackitem name=move(S.back()); S.pop_back(); - if(!name.isstr)throw string("Second argument to 'store' not a string"); + if(name.type!=SIT_STR)throw string("Second argument to 'store' not a string"); auto it=variables.find(name.strval); bool exists=it!=variables.end(); if(scopestack.size()&&scopestack.top().find(name.strval)==scopestack.top().end()){ @@ -247,23 +279,31 @@ 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"); + if(name.type!=SIT_STR)throw string("Second argument to 'store' not a string"); variables[name.strval]=move(S.back()); S.pop_back(); }}, {"get",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("get",1) Stackitem name=move(S.back()); S.pop_back(); - if(!name.isstr)throw string("Argument to 'get' not a string"); + if(name.type!=SIT_STR)throw string("Argument to 'get' not a string"); S.push_back(variables[name.strval]); }}, //leaves variable with value 0 {"swapoutvar",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("swapoutvar",1) Stackitem name=move(S.back()); S.pop_back(); - if(!name.isstr)throw string("Second argument to 'swapoutvar' not a string"); - S.push_back(move(variables[name.strval])); - variables[name.strval].intval=0; + if(name.type!=SIT_STR)throw string("Second argument to 'swapoutvar' not a string"); + auto it=variables.find(name.strval); + if(it==variables.end()){ + variables.emplace(piecewise_construct, + forward_as_tuple(name.strval), + forward_as_tuple()); + it=variables.find(name.strval); + } + S.push_back(move(it->second)); + it->second.intval=0; + it->second.type=SIT_INT; }}, {"enterscope",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("enterscope",0) @@ -285,7 +325,7 @@ unordered_map&,unordered_map &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("roll",1) Stackitem ns=move(S.back()); S.pop_back(); - if(ns.isstr)throw string("Argument to 'roll' not a number"); + if(ns.type!=SIT_INT)throw string("Argument to 'roll' not a number"); int n=ns.intval; if(S.size()<2)return; bool negative=n<0; @@ -314,45 +354,78 @@ unordered_map&,unordered_map=(int)str.strval.size())throw string("Index argument to 'stridx' out of range"); + if(str.type!=SIT_STR)throw string("Top of stack is not a string in builtin 'stridx'"); + if(idx.type!=SIT_INT)throw string("Argument to 'stridx' not a number"); + if(idx.intval<0||idx.intval>=(int)str.strval.size())throw string("Argument to 'stridx' out of range"); S.emplace_back(string(1,str.strval[idx.intval])); }}, //leaves the string on the stack {"strlen",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("strlen",1) const Stackitem &str=S.back(); - if(!str.isstr)throw string("Argument to 'strlen' not a string"); + if(str.type!=SIT_STR)throw string("Argument to 'strlen' not a string"); S.emplace_back(str.strval.size()); }}, - //leaves the string on the stack + //leaves the string on the stack; `to` is exclusive {"substr",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("substr",3) const Stackitem to=move(S.back()); S.pop_back(); const Stackitem from=move(S.back()); S.pop_back(); const Stackitem &str=S.back(); - if(!str.isstr)throw string("First argument to 'substr' not a string"); - if(from.isstr)throw string("Second argument to 'substr' not a number"); - if(to.isstr)throw string("Third argument to 'substr' not a number"); + if(str.type!=SIT_STR)throw string("Top of stack is not a string in builtin 'substr'"); + if(from.type!=SIT_INT)throw string("First argument to 'substr' not a number"); + if(to.type!=SIT_INT)throw string("Second argument to 'substr' not a number"); S.emplace_back(str.strval.substr(from.intval,to.intval-from.intval)); }}, {"chr",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("chr",1) - Stackitem ascii=move(S.back()); S.pop_back(); - if(ascii.isstr)throw string("Argument to 'chr' not a number"); - S.emplace_back(string(1,ascii.intval)); + if(S.back().type!=SIT_INT)throw string("Argument to 'chr' not a number"); + S.back().type=SIT_STR; + S.back().strval=string(1,S.back().intval); }}, {"ord",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("ord",1) - if(!S.back().isstr)throw string("Argument to 'ord' not a string"); - S.back().isstr=false; + if(S.back().type!=SIT_STR)throw string("Argument to 'ord' not a string"); + S.back().type=SIT_INT; S.back().intval=(int)S.back().strval[0]; - S.back().strval=""; + S.back().strval.clear(); + }}, + {"mkarray",[](vector &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("mkarray",0) + S.emplace_back(Stackitem(vector())); + }}, + //leaves the array on the stack + {"arridx",[](vector &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("arridx",2) + const Stackitem idx=move(S.back()); S.pop_back(); + if(idx.type!=SIT_INT) + throw string("Argument to 'arridx' not a number"); + if(S.back().type!=SIT_ARR) + throw string("Top of stack is not an array in builtin 'arridx'"); + if(idx.intval<0||idx.intval>=S.back().arrval.size()) + throw string("Argument to 'arridx' out of range"); + S.push_back(S.back().arrval[idx.intval]); + }}, + {"rand",[](vector &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("rand",0) + assert(RAND_MAX==0x7fffffff); + S.emplace_back(rand()); + }}, + //`to` exclusive + {"randr",[](vector &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("rand",2) + assert(RAND_MAX==0x7fffffff); + const Stackitem from=move(S.back()); S.pop_back(); + const Stackitem to=move(S.back()); S.pop_back(); + if(from.type!=SIT_INT)throw string("First argument to 'randr' not a number"); + if(to.type!=SIT_INT)throw string("Second argument to 'randr' not a number"); + if(to.intval<=from.intval)throw string("Arguments to 'randr' form an empty range"); + const int rval=rand(); + S.emplace_back(rval%(to.intval-from.intval)+from.intval); }}, {"sleep",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("sleep",1) - if(S.back().isstr)throw string("Argument to 'sleep' not a number"); + if(S.back().type!=SIT_INT)throw string("Argument to 'sleep' not a number"); usleep(S.back().intval); S.pop_back(); }}, @@ -360,8 +433,8 @@ unordered_map&,unordered_map Date: Thu, 10 Sep 2015 15:38:09 +0200 Subject: add mkarrayp --- functions.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'functions.cpp') diff --git a/functions.cpp b/functions.cpp index dacd194..03b8805 100644 --- a/functions.cpp +++ b/functions.cpp @@ -394,6 +394,20 @@ unordered_map&,unordered_map())); }}, + //mkarray prefill + {"mkarrayp",[](vector &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("mkarrayp",1) + const Stackitem nitems=move(S.back()); S.pop_back(); + if(nitems.type!=SIT_INT)throw string("Argument to 'mkarrayp' not a number"); + if(nitems.intval<0)throw string("Negative argument to 'mkarrayp'"); + BUILTIN_GUARD_STACKSIZE("mkarrayp: prefill",nitems.intval); + Stackitem res((vector())); + res.arrval.reserve(nitems.intval); + const int sz=S.size(); + for(int i=sz-nitems.intval;i &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("arridx",2) -- cgit v1.2.3-70-g09d2 From 74151599d4107055c6d8ecef05ad9cbd6b2045bb Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Thu, 10 Sep 2015 15:50:21 +0200 Subject: more array functions --- functions.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'functions.cpp') diff --git a/functions.cpp b/functions.cpp index 03b8805..261405e 100644 --- a/functions.cpp +++ b/functions.cpp @@ -420,6 +420,28 @@ unordered_map&,unordered_map &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("arrlen",1) + if(S.back().type!=SIT_ARR) + throw string("Top of stack is not an array in builtin 'arrlen'"); + S.emplace_back(S.back().arrval.size()); + }}, + //leaves the array on the stack, obviously + {"push",[](vector &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("push",2) + if(S[S.size()-2].type!=SIT_ARR) + throw string("Top of stack is not an array in builtin 'push'"); + S[S.size()-2].arrval.push_back(move(S.back())); + S.pop_back(); + }}, + //leaves the array on the stack, obviously + {"pop",[](vector &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("pop",1) + if(S.back().type!=SIT_ARR) + throw string("Top of stack is not an array in builtin 'pop'"); + S.back().arrval.pop_back(); + }}, {"rand",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("rand",0) assert(RAND_MAX==0x7fffffff); -- cgit v1.2.3-70-g09d2