A bit less overhead, by pre-building SV's.

This commit is contained in:
2025-09-16 22:29:43 -04:00
parent 86b5ea6a05
commit 905a01efd3

View File

@ -7,6 +7,7 @@ static_assert( __cplusplus >= 2023'02 );
namespace namespace
{ {
using namespace std::literals::string_literals; using namespace std::literals::string_literals;
using namespace std::literals::string_view_literals;
namespace C namespace C
{ {
@ -79,7 +80,7 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m
if( false ); if( false );
else if( word.empty() ); else if( word.empty() );
else if( word == "}" ) else if( word == "}"sv )
{ {
if( not definition.has_value() ) if( not definition.has_value() )
{ {
@ -96,11 +97,11 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m
if( C::debug ) std::cerr << "Adding word: " << word << " to function definition: " << definition.value() << std::endl; if( C::debug ) std::cerr << "Adding word: " << word << " to function definition: " << definition.value() << std::endl;
words[ definition.value() ].emplace_back( word ); words[ definition.value() ].emplace_back( word );
} }
else if( inConditional and word == "@else" and currentState != Else ) else if( inConditional and word == "@else"sv and currentState != Else )
{ {
currentState= Else; currentState= Else;
} }
else if( inConditional and word == "@endif" ) else if( inConditional and word == "@endif"sv )
{ {
conditionals.pop_back(); conditionals.pop_back();
currentState= resumeConditionals.back(); currentState= resumeConditionals.back();
@ -108,7 +109,7 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m
} }
else if( inConditional and currentState != conditionals.back() ) else if( inConditional and currentState != conditionals.back() )
{ {
if( word == "@if" ) if( word == "@if"sv )
{ {
conditionals.push_back( Skipped ); conditionals.push_back( Skipped );
resumeConditionals.push_back( currentState ); resumeConditionals.push_back( currentState );
@ -116,83 +117,83 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m
} }
else ; // We skip the statement. else ; // We skip the statement.
} }
else if( word == "@else" ) else if( word == "@else"sv )
{ {
throw std::runtime_error{ "Syntax error: unexpected `@else`" }; throw std::runtime_error{ "Syntax error: unexpected `@else`" };
} }
else if( word == "@endif" ) else if( word == "@endif"sv )
{ {
throw std::runtime_error{ "Syntax error: unexpected `@endif`" }; throw std::runtime_error{ "Syntax error: unexpected `@endif`" };
} }
else if( word == "@dup" ) else if( word == "@dup"sv )
{ {
auto val= peek(); auto val= peek();
push( std::move( val ) ); push( std::move( val ) );
} }
else if( word == "@swap" ) else if( word == "@swap"sv )
{ {
auto a= pop(); auto a= pop();
auto b= pop(); auto b= pop();
push( std::move( a ) ); push( std::move( a ) );
push( std::move( b ) ); push( std::move( b ) );
} }
else if( word == "@drop" ) else if( word == "@drop"sv )
{ {
std::ignore= pop(); std::ignore= pop();
} }
else if( word == "@not" ) else if( word == "@not"sv )
{ {
auto val= pop< Integer >(); auto val= pop< Integer >();
push( val == 0 ? 1 : 0 ); push( val == 0 ? 1 : 0 );
} }
else if( word == "@i_inc" ) else if( word == "@i_inc"sv )
{ {
push( pop< Integer >() + 1 ); push( pop< Integer >() + 1 );
} }
else if( word == "@i_dec" ) else if( word == "@i_dec"sv )
{ {
push( pop< Integer >() - 1 ); push( pop< Integer >() - 1 );
} }
else if( word == "@i_add" ) else if( word == "@i_add"sv )
{ {
const auto lhs= pop< Integer >(); const auto lhs= pop< Integer >();
const auto rhs= pop< Integer >(); const auto rhs= pop< Integer >();
push( lhs + rhs ); push( lhs + rhs );
} }
else if( word == "@i_sub" ) else if( word == "@i_sub"sv )
{ {
const auto lhs= pop< Integer >(); const auto lhs= pop< Integer >();
const auto rhs= pop< Integer >(); const auto rhs= pop< Integer >();
push( lhs - rhs ); push( lhs - rhs );
} }
else if( word == "@i_mul" ) else if( word == "@i_mul"sv )
{ {
const auto lhs= pop< Integer >(); const auto lhs= pop< Integer >();
const auto rhs= pop< Integer >(); const auto rhs= pop< Integer >();
push( lhs * rhs ); push( lhs * rhs );
} }
else if( word == "@i_div" ) else if( word == "@i_div"sv )
{ {
const auto lhs= pop< Integer >(); const auto lhs= pop< Integer >();
const auto rhs= pop< Integer >(); const auto rhs= pop< Integer >();
push( lhs / rhs ); push( lhs / rhs );
} }
else if( word == "@print" ) else if( word == "@print"sv )
{ {
output << pop< std::string >() << std::endl; output << pop< std::string >() << std::endl;
} }
else if( word == "@if" ) else if( word == "@if"sv )
{ {
const auto val= pop< Integer >(); const auto val= pop< Integer >();
conditionals.push_back( val ? If : Else ); conditionals.push_back( val ? If : Else );
resumeConditionals.push_back( currentState ); resumeConditionals.push_back( currentState );
currentState= If; currentState= If;
} }
else if( word == "{" ) else if( word == "{"sv )
{ {
if( definition.has_value() ) throw std::runtime_error{ "Nested definitions not supported." }; if( definition.has_value() ) throw std::runtime_error{ "Nested definitions not supported." };
@ -238,7 +239,7 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m
return rv; return rv;
} }
const std::variant< std::string, StackMachine::Integer > & std::variant< std::string, StackMachine::Integer > &
StackMachine::peek() StackMachine::peek()
{ {
if( stack.empty() ) throw std::runtime_error{ "FATAL: No more elements in stack." }; if( stack.empty() ) throw std::runtime_error{ "FATAL: No more elements in stack." };