Update
This commit is contained in:
parent
275c83d379
commit
793eff5870
19 changed files with 2117 additions and 1835 deletions
81
comun.md
81
comun.md
|
@ -88,7 +88,86 @@ The following code translates [brainfuck](brainfuck.md) to comun (proving comun
|
|||
.
|
||||
```
|
||||
|
||||
TODO: more, code examples, compare the above with C, ...
|
||||
The following is our standardized **[divisor tree](divisor_tree.md)** program written in comun:
|
||||
|
||||
```
|
||||
# pops a number (< 1000) and prints it
|
||||
numPrint999:
|
||||
$0 99 > ? $0 100 / "0" + -> .
|
||||
$0 9 > ? $0 10 / 10 % "0" + -> .
|
||||
10 % "0" + ->
|
||||
.
|
||||
|
||||
# converts single digit to number (or -1 if invalid)
|
||||
digitToNum:
|
||||
$0
|
||||
|
||||
$0 "0" >= >< "9" <= & ?
|
||||
"0" -
|
||||
;
|
||||
^ -1
|
||||
.
|
||||
.
|
||||
|
||||
# takes x, pops it and recursively prints its divisor tree
|
||||
printDivisorTree:
|
||||
-1 -1 # divisors: a, b
|
||||
|
||||
$2 2 / @@ # for i = x / 2
|
||||
# stack now: x a b i
|
||||
$0 1 <= ?
|
||||
!@
|
||||
.
|
||||
|
||||
$3 $1 % 0 = ? # i divides x?
|
||||
$0 $:3 # a = i
|
||||
$3 $1 / $:2 # b = x / i
|
||||
|
||||
$2 $2 <= ? # a <= b?
|
||||
!@
|
||||
.
|
||||
.
|
||||
|
||||
-- # decrement i
|
||||
.
|
||||
^ # pop i
|
||||
|
||||
"(" ->
|
||||
|
||||
$0 -1 != ?
|
||||
printDivisorTree
|
||||
" " ->
|
||||
>< numPrint999 # print x
|
||||
" " ->
|
||||
printDivisorTree
|
||||
;
|
||||
^ ^ # pop a, b
|
||||
numPrint999
|
||||
.
|
||||
|
||||
")" ->
|
||||
.
|
||||
|
||||
@@ # read numbers from the user
|
||||
0 10 "enter a number:" -->
|
||||
|
||||
0 # x
|
||||
@@ # read x
|
||||
<-
|
||||
$0 10 = ? ^ !@ . # newline?
|
||||
digitToNum
|
||||
$0 -1 = ? !. . # wrong digit? then end
|
||||
>< 10 * +
|
||||
.
|
||||
|
||||
$0 1000 < ? # x < 1000?
|
||||
printDivisorTree
|
||||
10 -> # newline
|
||||
;
|
||||
!@
|
||||
.
|
||||
.
|
||||
```
|
||||
|
||||
## See Also
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue