The general idea seems to be working.

I also went with "{}" syntax.

Now to see about pre-parsing ints for speed?
This commit is contained in:
2025-09-16 16:21:50 -04:00
parent 245099c07a
commit 02c5daca3c
5 changed files with 63 additions and 34 deletions

View File

@ -10,7 +10,7 @@ namespace
namespace C namespace C
{ {
constexpr bool debug= false or true; constexpr bool debug= false;
} }
} }
@ -52,11 +52,12 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m
StackMachine::runWord( const std::string_view word ) StackMachine::runWord( const std::string_view word )
{ {
if( C::debug ) std::cerr << "Processing token: " << word << std::endl; if( C::debug ) std::cerr << "Processing token: " << word << std::endl;
if( C::debug ) std::cerr << "Conditional depth is: " << conditionals.size() << std::endl;
const bool inConditional= not conditionals.empty(); const bool inConditional= not conditionals.empty();
if( false ); if( false );
else if( word.empty() ); else if( word.empty() );
else if( word == "@enddef" ) else if( word == "}" )
{ {
if( not definition.has_value() ) if( not definition.has_value() )
{ {
@ -85,7 +86,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" )
{ {
conditionals.push_back( Skipped ); conditionals.push_back( Skipped );
resumeConditionals.push_back( currentState ); resumeConditionals.push_back( currentState );
@ -169,11 +170,11 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m
resumeConditionals.push_back( currentState ); resumeConditionals.push_back( currentState );
currentState= If; currentState= If;
} }
else if( word == "@define" ) else if( word == "{" )
{ {
if( definition.has_value() ) throw std::runtime_error{ "Nested definitions not supported." }; if( definition.has_value() ) throw std::runtime_error{ "Nested definitions not supported." };
definition= next(); definition= pop();
if( C::debug ) std::cerr << "Starting function definition for " << definition.value() << std::endl; if( C::debug ) std::cerr << "Starting function definition for " << definition.value() << std::endl;
} }

View File

@ -1,4 +1,5 @@
@define factorial factorial
{
@dup @dup
@if @if
@dup @i_dec @dup @i_dec
@ -7,37 +8,19 @@
@else @else
@i_inc @i_inc
@endif @endif
@enddef }
6 @factorial 6 @factorial
@print @print
5 @factorial
@print
@define fib junk
@dup {
@not @print
@if }
@drop 1
@else
@dup
@i_dec
@not
@if
@drop 1
@else
@i_dec
@dup
@i_dec
@fib
@swap
@fib
@i_add
@endif
@endif
@enddef
8 @fib @print
10 @factorial 10 @factorial
@print @print

View File

@ -1,4 +1,5 @@
@define fib fib
{
@dup @dup
@not @not
@if @if
@ -19,8 +20,28 @@
@i_add @i_add
@endif @endif
@endif @endif
@enddef }
do_fibs
{
@dup
@if
@dup
@i_dec
@do_fibs
@print
@fib
@else
0
@endif
}
junk
{
12 @dup @print @fib @print
}
9 @dup @print @fib @print 37 @do_fibs

15
js4g/fib.py Normal file
View File

@ -0,0 +1,15 @@
def fib( a ):
if a == 0:
return 1
elif a == 1:
return 1
else:
return fib( a - 1 ) + fib( a - 2 )
def do_fibs( cnt ):
if cnt != 0:
do_fibs( cnt - 1 )
print( fib( cnt ) )
do_fibs( 35 )

9
js4g/funcs.js4 Normal file
View File

@ -0,0 +1,9 @@
alpha
{
@print
}
bravo
{
@print
}