after removing each

This commit is contained in:
Mike Nolan 2024-09-01 22:24:01 -05:00
parent 1af3720eb4
commit 1c607b3d7d
1 changed files with 10 additions and 298 deletions

View File

@ -120,24 +120,12 @@ namespace Tesses::WebServer::ScriptEngine
};
typedef std::variant<std::string,ObjectType> IttrType;
class IttrKVP {
public:
IttrType ittr;
int64_t index;
};
typedef std::variant<Undefined,Null,ObjectType,IttrKVP,std::string,int64_t,double,bool,char> ScriptType;
class Ittr : public Object {
public:
IttrType ittr;
int64_t index;
};
typedef std::variant<Undefined,Null,ObjectType,std::string,int64_t,double,bool,char> 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<IttrKVP>(st))
{
auto v = IttrGetValue(st);
return IttrGetKey(st) + ": " + ConvertToString(v);
}
if(std::holds_alternative<Null>(st))
{
@ -477,180 +460,6 @@ namespace Tesses::WebServer::ScriptEngine
}
};
inline ScriptType CreateIttrFor(ScriptType& st)
{
if(std::holds_alternative<std::string>(st))
{
Ittr* ittr =new Ittr();
ittr->index = -1;
ittr->ittr = std::get<std::string>(st);
ObjectType t(ittr);
return t;
}
if(std::holds_alternative<ObjectType>(st))
{
auto ot = std::get<ObjectType>(st).data;
auto ls = dynamic_cast<List*>(ot);
auto dict = dynamic_cast<Dictionary*>(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<ObjectType>(st))
{
auto objIttr = std::get<ObjectType>(st).data;
auto ittr = dynamic_cast<Ittr*>(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<std::string>(ittr->ittr))
{
std::string str = std::get<std::string>(ittr->ittr);
if(ittr->index >= 0 && ittr->index < str.size())
{
return true;
}
}
if(std::holds_alternative<ObjectType>(ittr->ittr))
{
auto ot = std::get<ObjectType>(ittr->ittr).data;
auto ls = dynamic_cast<List*>(ot);
auto dict = dynamic_cast<Dictionary*>(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<ObjectType>(st))
{
auto objIttr = std::get<ObjectType>(st).data;
auto ittr = dynamic_cast<Ittr*>(objIttr);
if(ittr == nullptr) return Null();
if(std::holds_alternative<std::string>(ittr->ittr))
{
std::string str = std::get<std::string>(ittr->ittr);
if(ittr->index >= 0 && ittr->index < str.size())
{
return str[ittr->index];
}
}
if(std::holds_alternative<ObjectType>(ittr->ittr))
{
auto ot = std::get<ObjectType>(ittr->ittr).data;
auto ls = dynamic_cast<List*>(ot);
auto dict = dynamic_cast<Dictionary*>(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<IttrKVP>(st))
{
auto ittr = std::get<IttrKVP>(st);
if(std::holds_alternative<ObjectType>(ittr.ittr))
{
auto ot = std::get<ObjectType>(ittr.ittr).data;
auto dict = dynamic_cast<Dictionary*>(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<IttrKVP>(st))
{
auto ittr = std::get<IttrKVP>(st);
if(std::holds_alternative<ObjectType>(ittr.ittr))
{
auto ot = std::get<ObjectType>(ittr.ittr).data;
auto dict = dynamic_cast<Dictionary*>(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<ScriptType> args);
class RootEnvironment : public Environment
{
@ -1087,19 +896,7 @@ namespace Tesses::WebServer::ScriptEngine
auto obj = std::get<ObjectType>(member).data;
auto ls = dynamic_cast<List*>(obj);
auto dict = dynamic_cast<Dictionary*>(obj);
auto ittr = dynamic_cast<Ittr*>(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<IttrKVP>(instance))
{
if(name == "key")
{
return IttrGetKey(instance);
}
if(name == "value")
{
return IttrGetValue(instance);
}
return Null();
}
if(std::holds_alternative<ObjectType>(instance))
{
auto inst = std::get<ObjectType>(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<IttrKVP>(item))
{
return "ittrkvp";
}
if(std::holds_alternative<bool>(item))
{
return "bool";
@ -3263,8 +3039,7 @@ namespace Tesses::WebServer::ScriptEngine
auto dict = dynamic_cast<Dictionary*>(ot);
auto ifunc = dynamic_cast<InternalFunction*>(ot);
auto efunc = dynamic_cast<ExternalFunction*>(ot);
auto ittr = dynamic_cast<Ittr*>(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<std::string> __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)