slc/examples/if.slc

63 lines
1.1 KiB
Plaintext
Raw Normal View History

2025-01-27 04:03:44 +01:00
// Implementing if statement if its not a built-in in SLC!
// Not compiler writing in SLC language, but SLCs builtin itself...
::builtin if @cmp @then @else
"" parseblock() // (*)
if@cmp(.)
"" parseblock{}
if@then(_)
// returns 0 if there was no [] block!
// Calling 0 means just calling the
// intstruction pointer ("avoiding call")
"" parseblock[]
if@else(_)
// (*) cmp result at top of stack
gen(!) gen(skipif) if@then
gen(skipif) if@else
;
// Alternatively - by adding "branchcall" to the builtins
::builtin if
"" parseblock()
genpush(_)
"" parseblock{}
genpush(_)
"" parseblock[] // returns 0 if there was no block (its SKIP to 'call' 0)
genpush(_)
gen(branchcall)
;
// Rem.: "branchcall" can be used in "real" code to split which word to call...
: smaller
"smaller"
;
: bigger
"bigger-or-eq"
;
// Gets a b
// returns "smaller" or "bigger-or-eq"
: smaller_or_bigger
<
gen(smaller)
gen(bigger)
branchcall
;
// Better example
: one 1 ;
: zero 0 ;
// Gets a b
// returns 1 if a < b, 0 otherwise
: smaller_or_bigger
<
gen(one)
gen(zero)
branchcall
2025-01-08 01:59:07 +01:00
;