diff --git a/js4g/StackMachine.cc b/js4g/StackMachine.cc index 14ae3c5..e699c72 100644 --- a/js4g/StackMachine.cc +++ b/js4g/StackMachine.cc @@ -277,7 +277,7 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m int main() { - std::deque< std::string > program; + std::vector< std::string > program; while( std::cin ) { diff --git a/js4g/StackMachine.h b/js4g/StackMachine.h index 8036000..b852604 100644 --- a/js4g/StackMachine.h +++ b/js4g/StackMachine.h @@ -12,6 +12,9 @@ static_assert( __cplusplus >= 2023'02 ); #include #include #include +#include + +#include namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m { @@ -36,20 +39,26 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m // It grows on the "back". // A `std::deque` is used instead of `std::vector`, because // it helps cut down on large allocations, by subdividing things. - std::deque< std::variant< std::string, Integer > > stack; + std::vector< std::variant< std::string, Integer > > stack; // TODO: Should these tokens be binary? // Upside: compressed. // Downside: more memory management in the machine end? - std::deque< std::string > tokens; + + // It's `void *` to break the cycle with itself + struct TokenHolder; + //using Token= std::variant< std::string, TokenHolder * >; + using Token= std::string; + + std::vector< std::string > tokens; struct Tokenizer { - std::deque< std::string >::const_iterator pos; - std::deque< std::string >::const_iterator end; + std::vector< Token >::const_iterator pos; + std::vector< Token >::const_iterator end; explicit - Tokenizer( const std::deque< std::string > &tokens ) + Tokenizer( const std::vector< Token > &tokens ) : pos( tokens.begin() ), end( tokens.end() ) {} bool hasNext() const { return pos != end; } @@ -60,8 +69,8 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m std::vector< Tokenizer > tokenStack; - std::deque< std::string > wordNames; - std::unordered_map< std::string_view, std::deque< std::string > > words; + std::list< std::string > wordNames; + std::unordered_map< std::string_view, std::vector< std::string > > words; // Which side of the current conditional to take. enum ConditionalState { If, Else, Skipped }; @@ -78,7 +87,7 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m void runWord( std::string_view word ); std::variant< std::string, Integer > pop(); - const std::variant< std::string, Integer > &peek(); + std::variant< std::string, Integer > &peek(); void push( std::string s ); void push( Integer s ); @@ -99,7 +108,7 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m StackMachine( std::ostream &output= std::cout ); void - loadProgram( std::deque< std::string > tokens ) + loadProgram( std::vector< std::string > tokens ) { this->tokens= std::move( tokens ); tokenStack.clear();