diff --git a/js4g/StackMachine.cc b/js4g/StackMachine.cc index fca6a0b..f7007a0 100644 --- a/js4g/StackMachine.cc +++ b/js4g/StackMachine.cc @@ -10,7 +10,7 @@ namespace 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 ) { 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(); if( false ); else if( word.empty() ); - else if( word == "@enddef" ) + else if( word == "}" ) { if( not definition.has_value() ) { @@ -85,7 +86,7 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m } else if( inConditional and currentState != conditionals.back() ) { - if( word != "@if" ) + if( word == "@if" ) { conditionals.push_back( Skipped ); resumeConditionals.push_back( currentState ); @@ -169,11 +170,11 @@ namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m resumeConditionals.push_back( currentState ); currentState= If; } - else if( word == "@define" ) + else if( word == "{" ) { 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; } diff --git a/js4g/factorial.js4 b/js4g/factorial.js4 index 8029720..5d29f35 100644 --- a/js4g/factorial.js4 +++ b/js4g/factorial.js4 @@ -1,4 +1,5 @@ -@define factorial +factorial +{ @dup @if @dup @i_dec @@ -7,37 +8,19 @@ @else @i_inc @endif -@enddef +} 6 @factorial @print +5 @factorial +@print -@define fib - @dup - @not - @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 +junk +{ + @print +} -8 @fib @print - 10 @factorial @print diff --git a/js4g/fib.js4 b/js4g/fib.js4 index 8fe16a3..69db2c8 100644 --- a/js4g/fib.js4 +++ b/js4g/fib.js4 @@ -1,4 +1,5 @@ -@define fib +fib +{ @dup @not @if @@ -19,8 +20,28 @@ @i_add @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 + diff --git a/js4g/fib.py b/js4g/fib.py new file mode 100644 index 0000000..8437483 --- /dev/null +++ b/js4g/fib.py @@ -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 ) diff --git a/js4g/funcs.js4 b/js4g/funcs.js4 new file mode 100644 index 0000000..df70d65 --- /dev/null +++ b/js4g/funcs.js4 @@ -0,0 +1,9 @@ +alpha +{ + @print +} + +bravo +{ + @print +}