diff --git a/tessesscriptengine.hpp b/tessesscriptengine.hpp index fdcb14f..0ef4ace 100644 --- a/tessesscriptengine.hpp +++ b/tessesscriptengine.hpp @@ -120,24 +120,12 @@ namespace Tesses::WebServer::ScriptEngine }; - typedef std::variant IttrType; - - + - class IttrKVP { - public: - IttrType ittr; - int64_t index; - }; - - typedef std::variant ScriptType; - class Ittr : public Object { - public: - IttrType ittr; - int64_t index; - }; + typedef std::variant ScriptType; + inline bool Equals(ScriptType& left, ScriptType& right) @@ -221,12 +209,7 @@ namespace Tesses::WebServer::ScriptEngine inline std::string ConvertToString(ScriptType& st); inline std::string ConvertToString(ScriptType& st) { - if(std::holds_alternative(st)) - { - auto v = IttrGetValue(st); - - return IttrGetKey(st) + ": " + ConvertToString(v); - } + if(std::holds_alternative(st)) { @@ -477,180 +460,6 @@ namespace Tesses::WebServer::ScriptEngine } }; - inline ScriptType CreateIttrFor(ScriptType& st) - { - if(std::holds_alternative(st)) - { - Ittr* ittr =new Ittr(); - ittr->index = -1; - ittr->ittr = std::get(st); - ObjectType t(ittr); - return t; - } - if(std::holds_alternative(st)) - { - auto ot = std::get(st).data; - auto ls = dynamic_cast(ot); - auto dict = dynamic_cast(ot); - if(ls != nullptr) - { - Ittr* ittr=new Ittr(); - ittr->index = -1; - ittr->ittr = ObjectType(ls); - ObjectType t(ittr); - return t; - } - if(dict != nullptr) - { - Ittr* ittr=new Ittr(); - ittr->index = -1; - ittr->ittr = ObjectType(dict); - ObjectType t(ittr); - return t; - } - } - return Null(); - } - - inline bool IttrMoveNext(ScriptType& st) - { - if(std::holds_alternative(st)) - { - auto objIttr = std::get(st).data; - auto ittr = dynamic_cast(objIttr); - if(ittr == nullptr) return false; - std::cout << "INDEXB: " << ittr->index << std::endl; - ittr->index++; - std::cout << "INDEXA: " << ittr->index << std::endl; - - if(std::holds_alternative(ittr->ittr)) - { - std::string str = std::get(ittr->ittr); - - - if(ittr->index >= 0 && ittr->index < str.size()) - { - return true; - } - } - if(std::holds_alternative(ittr->ittr)) - { - auto ot = std::get(ittr->ittr).data; - auto ls = dynamic_cast(ot); - auto dict = dynamic_cast(ot); - - if(ls != nullptr) - { - - if(ittr->index >= 0 && ittr->index < ls->items.size()) - { - - return true; - } - } - if(dict != nullptr) - { - - if(ittr->index >= 0 && ittr->index < dict->items.size()) - { - - return true; - } - } - } - } - return false; - } - - inline ScriptType IttrCurrent(ScriptType& st) - { - if(std::holds_alternative(st)) - { - auto objIttr = std::get(st).data; - auto ittr = dynamic_cast(objIttr); - if(ittr == nullptr) return Null(); - if(std::holds_alternative(ittr->ittr)) - { - std::string str = std::get(ittr->ittr); - if(ittr->index >= 0 && ittr->index < str.size()) - { - return str[ittr->index]; - } - } - if(std::holds_alternative(ittr->ittr)) - { - auto ot = std::get(ittr->ittr).data; - auto ls = dynamic_cast(ot); - auto dict = dynamic_cast(ot); - - if(ls != nullptr) - { - if(ittr->index >= 0 && ittr->index < ls->items.size()) - { - return ls->items[ittr->index]; - } - } - if(dict != nullptr) - { - if(ittr->index >= 0 && ittr->index < dict->items.size()) - { - IttrKVP kvp; - kvp.index = ittr->index; - kvp.ittr = ittr->ittr; - return kvp; - } - } - } - } - return Null(); - } - inline std::string IttrGetKey(ScriptType& st) - { - if(std::holds_alternative(st)) - { - auto ittr = std::get(st); - - if(std::holds_alternative(ittr.ittr)) - { - auto ot = std::get(ittr.ittr).data; - auto dict = dynamic_cast(ot); - - - if(dict != nullptr) - { - if(ittr.index >= 0 && ittr.index < dict->items.size()) - { - return dict->items[ittr.index].first; - } - } - } - } - return std::string({}); - } - - inline ScriptType IttrGetValue(ScriptType& st) - { - if(std::holds_alternative(st)) - { - auto ittr = std::get(st); - - if(std::holds_alternative(ittr.ittr)) - { - auto ot = std::get(ittr.ittr).data; - auto dict = dynamic_cast(ot); - - - if(dict != nullptr) - { - if(ittr.index >= 0 && ittr.index < dict->items.size()) - { - return dict->items[ittr.index].second; - } - } - } - } - return Null(); - } inline ScriptType TypeOf(Environment* env, std::vector args); class RootEnvironment : public Environment { @@ -1087,19 +896,7 @@ namespace Tesses::WebServer::ScriptEngine auto obj = std::get(member).data; auto ls = dynamic_cast(obj); auto dict = dynamic_cast(obj); - auto ittr = dynamic_cast(obj); - if(ittr != nullptr) - { - if(key == "current") - { - return IttrCurrent(member); - } - if(key == "movenext") - { - return IttrMoveNext(member); - } - - } + if(ls != nullptr) { if(key == "add" && args.size() > 0) @@ -1338,19 +1135,7 @@ namespace Tesses::WebServer::ScriptEngine return (int64_t)str.size(); } } - if(std::holds_alternative(instance)) - { - if(name == "key") - { - return IttrGetKey(instance); - } - - if(name == "value") - { - return IttrGetValue(instance); - } - return Null(); - } + if(std::holds_alternative(instance)) { auto inst = std::get(instance).data; @@ -1803,12 +1588,7 @@ namespace Tesses::WebServer::ScriptEngine } } break; - case Instruction::ITTR: - { - auto ittr=Pop(); - stack.push(CreateIttrFor(ittr)); - } - break; + case Instruction::GETFIELD: { std::string name = PopString(); @@ -3240,10 +3020,6 @@ namespace Tesses::WebServer::ScriptEngine return "double"; } - if(std::holds_alternative(item)) - { - return "ittrkvp"; - } if(std::holds_alternative(item)) { return "bool"; @@ -3263,8 +3039,7 @@ namespace Tesses::WebServer::ScriptEngine auto dict = dynamic_cast(ot); auto ifunc = dynamic_cast(ot); auto efunc = dynamic_cast(ot); - auto ittr = dynamic_cast(ot); - if(ittr != nullptr) return "ittr"; + if(ls != nullptr) return "list"; if(dict != nullptr) return "dictionary"; if(ifunc != nullptr) return "internal_function"; @@ -3537,8 +3312,7 @@ namespace Tesses::WebServer::ScriptEngine std::string _cont = {}; - std::stack __eachs; - + int jmpId = 0; std::string NewJumpId() { @@ -3645,13 +3419,6 @@ namespace Tesses::WebServer::ScriptEngine } if(retNode != nullptr) { - while(!this->__eachs.empty()) - { - auto item = this->__eachs.top(); - GetVariableExpression gve(item); - GenerateFree(&gve,instructions); - this->__eachs.pop(); - } Compile(retNode->retVal,instructions); instructions.push_back(new SimpleInstruction(Instruction::RET)); } @@ -3743,62 +3510,7 @@ namespace Tesses::WebServer::ScriptEngine _cont = oldcont; _brk = oldbrk; } - if(eachLoop != nullptr) - { - - instructions.push_back(new SimpleInstruction(Instruction::SET_LOOP)); - std::string eachIttr = NewJumpId(); - __eachs.push(eachIttr); - instructions.push_back(new StringInstruction(eachIttr)); - Compile(eachLoop->list,instructions); - instructions.push_back(new SimpleInstruction(Instruction::ITTR)); - instructions.push_back(new SimpleInstruction(Instruction::SETVARIABLE)); - instructions.push_back(new SimpleInstruction(Instruction::POP)); - std::string oldcont = _cont; - std::string oldbrk = _brk; - std::string yes = NewJumpId(); - std::string end = NewJumpId(); - std::string begin = NewJumpId(); - - _cont = begin; - _brk = end; - - instructions.push_back(new LabelInstruction(begin)); - GetVariableExpression var(eachIttr); - GetMemberExpression mem(&var,"movenext"); - FunctionCallExpression ce(&mem); - Compile(&ce,instructions); - ce.name = nullptr; - mem.parent=nullptr; - - instructions.push_back(new JumpInstruction(Instruction::JMPC,yes)); - instructions.push_back(new JumpInstruction(Instruction::JMP,end)); - instructions.push_back(new LabelInstruction(yes)); - GetMemberExpression mem2(&var,"current"); - FunctionCallExpression ce2(&mem2); - AssignExpression assignExpr(eachLoop->item,&ce2); - - - Compile(&assignExpr,instructions); - ExpressionPop(&assignExpr,instructions); - - mem2.parent=nullptr; - ce2.name=nullptr; - - assignExpr.left=nullptr; - assignExpr.right = nullptr; - Compile(eachLoop->body,instructions); - ExpressionPop(eachLoop->body,instructions); - instructions.push_back(new JumpInstruction(Instruction::JMP,begin)); - instructions.push_back(new LabelInstruction(end)); - __eachs.pop(); - - GenerateFree(&var,instructions); - instructions.push_back(new SimpleInstruction(Instruction::DESTROY_LOOP)); - - _cont = oldcont; - _brk = oldbrk; - } + if(whileNode != nullptr) { if(whileNode->isDo)