123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- # These are some general functions and tests runnable with minicomum.
- printNum: # prints number as unsigned, doesn't pop
- ?'
- $0
- 0
- ><
- @' # reverse push the chars
- $0
- 10
- %
- "0"
- +
- ><
- 10
- /
- .
- ^
- @' # print
- ->
- .
- ^
- ; # zero
- "0" ->
- .
- .
- printNumS: # prints number as signed, doesn't pop
- $0 -1 2 / >
- ? # negative?
- "-" ->
- -1 >< - 1 +
- .
- printNum
- .
- factR: # computes factorial recursively
- ?'
- $0 -- factR *
- ;
- ^ 1
- .
- .
- factI: # computes factorial iteratively
- $0 --
- @'
- ><
- $1
- *
- ><
- --
- .
- ^
- .
- isPrime: # checks if a number is prime
- $0 1 <=
- ?
- ^ 0 # 0 and 1 are not primes
- ;
- $0 2 /
- @@ # test divisors
- $0 1 <=
- ? # no divisors greater than 1 found?
- ^ ^ 1 !@
- .
- $1 $1 % 0 =
- ? # is divisible?
- ^ ^ 0 !@
- .
- --
- .
- .
- .
- 1 2 3 4 5
- 0 "stack top: " --> $$ printNum ^ 10 ->
- 0 "stack top (should be the same): " --> $$ printNum ^ 10 ->
- 0 "factorial of 6 (recursive and iterative): " -->
- 6 factR printNum ^ " " ->
- 6 factI printNum ^ 10 ->
- 0 10 "primes up to 100: " -->
- 0 # tested number
- @@
- $0 100 = ?
- !@
- .
-
- $0 isPrime ?
- $0 printNum ^ 10 ->
- .
- ++
- .
- 0 "pointer manipulation: " -->
- $$ 3 $+0 $$ $4 -
- 4 ?
- 0 10 "OK" -->
- ;
- 0 10 "ERROR" -->
- .
|