add examples; do not hard-shutdown when screen is closed - destructors did not run!

This commit is contained in:
2020-10-10 22:25:26 +02:00
parent c28fee88fe
commit 9254635fd7
4 changed files with 124 additions and 9 deletions
+50
View File
@@ -0,0 +1,50 @@
(
; Perform frequency analysis of the input stream. Pipe a file to this program.
(def MINCH 33) ; lowest counted char
(def MAXCH 126) ; highest counted char
(sym tab r7)
(mkbf tab 128)
(sym ch r6)
(sym sum r1)
(sym max r2)
(:rch)
(ld ch @cin (eof? (j :rch_end)))
(rcmp ch MINCH MAXCH (ne? (j :rch)))
(bfrd r0 @tab ch)
(inc r0) (inc sum)
(cmp r0 max (>? (ld max r0)))
(bfwr @tab ch r0)
(j :rch)
(:rch_end)
(sym fac r3)
(div fac 100_000 max)
(ld ch MINCH)
(:print)
(bfrd r0 @tab ch)
(cmp r0 max
(eq?
(ld @cout 27) (ld @cout '[') (ld @cout '3') (ld @cout '2') (ld @cout 'm'))
)
(tst r0 (z? (j :print_next)))
(mul r0 fac)
(div r0 1000)
(ld @cout ch)
(ld @cout ' ')
(call bar r0)
(:print_next)
(ld @cout 27) (ld @cout '[') (ld @cout 'm')
(inc ch)
(cmp ch MAXCH (le? (j :print)))
(proc bar/1
(ld r0 arg0)
(:bar)
(ld @cout '#')
(dec r0 (nz? (j :bar)))
(ld @cout '\n')
(ret)
)
)
+39
View File
@@ -0,0 +1,39 @@
(
; This program asks for a name and prints a greeting
(sym name r7)
(mkbf r0 "Your name: ")
(call prompt r0) (del @r0)
(ld name res0)
(ld @cout '\n')
(mkbf r1 "Hello, ")
(call print r1) (del @r1)
(call print name) (del @name)
(ld @cout '!')
(ld @cout '\n')
(halt)
(proc print buf
(bfsz r1 @buf)
(:next)
(cmp r0 r1 (eq? (ret)))
(bfrd @cout @buf r0)
(inc r0)
(j :next)
)
(proc prompt msg
(call print msg)
(sym dest r7)
(mkbf dest)
(:char)
(ld r0 @cin (eof? (ret dest)))
(cmp r0 '\n' (eq? (ret dest)))
(ld @cout r0)
(bfpush @dest r0)
(j :char)
)
)
+24
View File
@@ -0,0 +1,24 @@
(
; This is an example implementation of itoa using a buffer of characters
(ld r0 -123456789)
(mkbf r1)
(call itoa r1 r0)
; print it
(:pn) (bfrpop @cout @r1 (em? (ld @cout '\n') (halt))) (j :pn)
(halt)
(proc itoa buf num
(ld r1 num)
(tst r1 (<0? (mul r1 -1)))
(:next)
(mod r0 r1 10)
(add r0 '0')
(bfrpush @buf r0)
(div r1 10 (z?
(tst num (<0? (bfrpush @buf '-')))
(ret)))
(j :next)
)
)