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:
2025-09-17 20:59:18 -04:00
parent cc4e57d6b5
commit 4dde1e6f74
2 changed files with 107 additions and 25 deletions

View File

@ -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();
};