This uses a tokenizer system for functions to call.
It seems slower, though. I'm preserving it in this branch, for now.
This commit is contained in:
@ -12,6 +12,7 @@ static_assert( __cplusplus >= 2023'02 );
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <list>
|
||||
|
||||
#include <boost/container/flat_map.hpp>
|
||||
@ -50,30 +51,31 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m
|
||||
|
||||
// It's `void *` to break the cycle with itself
|
||||
struct TokenHolder;
|
||||
//using Token= std::variant< std::string, TokenHolder * >;
|
||||
using Token= std::string;
|
||||
using Token= std::variant< std::string, std::shared_ptr< TokenHolder > >;
|
||||
std::vector< std::shared_ptr< TokenHolder > > tokenHolders;
|
||||
|
||||
std::vector< std::string > tokens;
|
||||
std::vector< Token > tokens;
|
||||
|
||||
struct Tokenizer
|
||||
{
|
||||
std::vector< Token >::const_iterator pos;
|
||||
std::vector< Token >::const_iterator end;
|
||||
std::vector< Token >::iterator pos;
|
||||
std::vector< Token >::iterator end;
|
||||
|
||||
explicit
|
||||
Tokenizer( const std::vector< Token > &tokens )
|
||||
Tokenizer( std::vector< Token > &tokens )
|
||||
: pos( tokens.begin() ), end( tokens.end() ) {}
|
||||
|
||||
bool hasNext() const { return pos != end; }
|
||||
|
||||
std::string_view next() { return *pos++; }
|
||||
Token &next() { return *pos++; }
|
||||
};
|
||||
|
||||
std::vector< Tokenizer > tokenStack;
|
||||
Token *current= nullptr;
|
||||
|
||||
|
||||
std::list< std::string > wordNames;
|
||||
std::unordered_map< std::string_view, std::vector< std::string > > words;
|
||||
std::unordered_map< std::string_view, std::vector< Token > > words;
|
||||
|
||||
// Which side of the current conditional to take.
|
||||
enum ConditionalState { If, Else, Skipped };
|
||||
@ -104,20 +106,15 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m
|
||||
void push( const T &t );
|
||||
|
||||
|
||||
std::string_view next();
|
||||
void compile( std::unordered_map< std::string_view, std::vector< Token > >::iterator );
|
||||
const Token &next();
|
||||
|
||||
void recurse( std::string_view func );
|
||||
|
||||
public:
|
||||
StackMachine( std::ostream &output= std::cout );
|
||||
|
||||
void
|
||||
loadProgram( std::vector< std::string > tokens )
|
||||
{
|
||||
this->tokens= std::move( tokens );
|
||||
tokenStack.clear();
|
||||
tokenStack.emplace_back( this->tokens );
|
||||
}
|
||||
void loadProgram( std::vector< std::string > tokens );
|
||||
|
||||
void run();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user