Change if function stack ordering, add some basic math ops to Int, add factorial example

* `if` function stack ordering is changed to use condition, if-false,
  if-true (bottom to top). This is so we can pass a value on top of the
  stack directly to a new `if` in a condition, so we don't have to store
  a new value in a new variable.
* Add __splat__ and __minus__ functions to Int
* Add * and - builtins, which call the __splat__ and __minus__ functions
  of the top stack item, respectively.
* Add factorial example because now factorials are possible with this
  skeleton implementation, albeit a little hacky with the recursion.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-02-11 11:10:14 -08:00
parent a8cf2898e1
commit 1f642b9739
4 changed files with 110 additions and 10 deletions

View File

@@ -1,7 +1,7 @@
[ "fail - 0 is not truthy" println! ] [ "OK" println! ] 0 if!
[ "OK" println! ] [ "fail - 1 is truthy" println! ] 1 if!
[ "OK" println! ] [ "fail - 1.1 is truthy" println! ] 1.1 if!
[ "OK" println! ] [ "fail - 0.1 is truthy" println! ] 0.1 if!
[ "OK" println! ] [ "fail - -0.1 is truthy" println! ] -0.1 if!
[ "fail - -0.0 is not truthy" println! ] [ "OK" println! ] -0.0 if!
[ "fail - 0.0 is not truthy" println! ] [ "OK" println! ] 0.0 if!
0 [ "fail - 0 is not truthy" println! ] [ "OK" println! ] if!
1 [ "OK" println! ] [ "fail - 1 is truthy" println! ] if!
1.1 [ "OK" println! ] [ "fail - 1.1 is truthy" println! ] if!
0.1 [ "OK" println! ] [ "fail - 0.1 is truthy" println! ] if!
-0.1 [ "OK" println! ] [ "fail - -0.1 is truthy" println! ] if!
-0.0 [ "fail - -0.0 is not truthy" println! ] [ "OK" println! ] if!
0.0 [ "fail - 0.0 is not truthy" println! ] [ "OK" println! ] if!

28
examples/factorial.sy Normal file
View File

@@ -0,0 +1,28 @@
[ :x x x ] :dup
0 :factorial
[
dup!
[dup! 1 -! factorial! *!]
[:_ 1]
if!
]
:factorial
[
dup! print!
"! = " print!
factorial!
println!
]
:do-factorial
1 do-factorial!
2 do-factorial!
3 do-factorial!
4 do-factorial!
5 do-factorial!
6 do-factorial!
7 do-factorial!
8 do-factorial!
9 do-factorial!