#ifndef JSPARSER_H #define JSPARSER_H #include #include #include // for gcc & shared_ptr #include #include // Internal JS types: String, Number, Function, Array, Object // blocks/scopes? class js_internal_storage { public: // toBool // toString // toNumber // toFunction // toArray // toObject // toReference virtual ~js_internal_storage() = default; }; class js_scope { public: js_scope *parent; // what do we need children for? // a callstack only includes it's parents (in JS?) std::vector children; js_scope() { parent = nullptr; } std::map variables; std::map data; // maybe a vector of resolve on execution assignments... // feel like we need an instruction pointer... // esp. for loops // but how we address tokens, by index? }; class js_bool : public js_internal_storage { public: bool value; }; class js_string : public js_internal_storage { public: std::string value; }; class js_number : public js_internal_storage { public: signed long value; }; class js_function : public js_internal_storage { public: std::vector tokens; js_scope scope; }; class js_array : public js_internal_storage { public: std::vector value; }; class js_object : public js_internal_storage { public: // I think 2nd needs to be a pointer... std::map value; }; class js_reference : public js_internal_storage { public: js_internal_storage *ptr; }; bool doAssignment(js_scope &rootScope, std::string token); size_t parseFunctionBody(std::string source, size_t start); size_t getNextExpression(const std::string source, const size_t start); class JavaScript { public: void clear() { tokens.clear(); rootScope.parent = nullptr; rootScope.children.clear(); rootScope.variables.clear(); } // each token is one statement std::vector tokens; // we're just settings the rootScope.variables std::vector definitions; // all var declarations and their expressions std::vector instructions; // then a list of all remaining expressions and function calls js_scope rootScope; }; js_internal_storage *parseExpression(js_scope &rootScope, std::string token); void parseArray(js_scope &rootScope, std::string token); void parseJSON(js_scope &rootScope, std::string token); // this is no members could all be static class JSParser { public: //std::shared_ptr parse(const std::string &javascript) const; void parseTokens(const std::vector &tokens, js_scope *scope) const; std::vector getTokens(const std::string &source) const; std::shared_ptr append(std::shared_ptr &destination, const std::shared_ptr &source) const; }; #endif