Magari puoi prendere uno spunto da Norbert. http://members.aon.at/nkehrer/a800_c64_hp_emu.html
class FunctionEntity : public ScriptEntity{public: FunctionEntity(); virtual ~FunctionEntity(); virtual bool canCastTo(ScriptEntity * other) const; virtual std::string getTypeName() const; virtual std::string toString() const; virtual void setArgs(std::vector<ScriptEntity *> args); // The return value is inside the special scratch register defined in env virtual Result* call(ExecutionEnvironment * env, std::vector<ScriptEntity *> args) = 0;protected: // This is the ordered set of input arguments the function expects. // Type checking is done at runtime std::vector<ScriptEntity *> args;};
typedef Result * (*BuiltinFunction)(ExecutionEnvironment *, std::vector<ScriptEntity *>);class BuiltinFunctionEntity : public FunctionEntity{public: BuiltinFunctionEntity(BuiltinFunction hook); virtual ~BuiltinFunctionEntity(); virtual Result* call(ExecutionEnvironment * env, std::vector<ScriptEntity *> args);private: BuiltinFunction hook;};
void BuiltinFunctions::registerFunctions(MathEngine *engine){ ExecutionEnvironment * env = engine->getEnvironment(); env->getGlobalEnvironment()->defineEntity("print", new BuiltinFunctionEntity(&print)); env->getGlobalEnvironment()->defineEntity("concat", new BuiltinFunctionEntity(&concat));}// Prints the given arguments as stringsResult * BuiltinFunctions::print(ExecutionEnvironment *env, std::vector<ScriptEntity *> args){ std::string result = ""; for (const auto& arg : args) { result += arg->toString(); } return new StringResult(result);}Result * BuiltinFunctions::concat(ExecutionEnvironment * env, std::vector<ScriptEntity*> args){ std::string result = ""; for (const auto& arg : args) { result += arg->toString(); } env->setReturnValue(new StringEntity(result)); return new OkResult();}
class FunctionCallStatement : public Statement{public: FunctionCallStatement(FunctionEntity * function, std::vector<ScriptEntity *> args); virtual ~FunctionCallStatement(); virtual Result* execute(ExecutionEnvironment *env);private: FunctionEntity * function; std::vector<ScriptEntity *> args;};
Come vi sembra l'approccio?
Il progetto è ancora molto fluido, per cui non ho fissato nulla sulla pietra.Devo studiarmi meglio std::function, che per me è roba totalmente nuova
Dato che è un progetto personale e non hai vincoli decisionali da parte di terzi, hai pensato ad un linguaggio migliore, e con complexity ridotto, del C++?