From 9254635fd7b621abc89966681ba18d677be58822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 10 Oct 2020 22:25:26 +0200 Subject: [PATCH] add examples; do not hard-shutdown when screen is closed - destructors did not run! --- crsn_screen/src/exec.rs | 20 ++++++----- examples/buf_io_frequency_analysis.csn | 50 ++++++++++++++++++++++++++ examples/buf_io_hello.csn | 39 ++++++++++++++++++++ examples/itoa.csn | 24 +++++++++++++ 4 files changed, 124 insertions(+), 9 deletions(-) create mode 100644 examples/buf_io_frequency_analysis.csn create mode 100644 examples/buf_io_hello.csn create mode 100644 examples/itoa.csn diff --git a/crsn_screen/src/exec.rs b/crsn_screen/src/exec.rs index 2c52aec..7985c33 100644 --- a/crsn_screen/src/exec.rs +++ b/crsn_screen/src/exec.rs @@ -97,9 +97,9 @@ impl OpTrait for ScreenOp { let backend: &mut Backend = state.ext_mut(); if force != 0 { - blit(backend) + blit(backend)?; } else { - blit_maybe(backend) + blit_maybe(backend)?; } } @@ -143,7 +143,7 @@ impl OpTrait for ScreenOp { backend.buffer[index as usize] = color as u32; if backend.opts.auto_blit { - blit_maybe(backend); + blit_maybe(backend)?; } } } @@ -274,29 +274,31 @@ fn init(state: &mut RunState, width: Value, height: Value) -> Result<(), Fault> backend.window = Some(window); - blit_maybe(backend); + blit_maybe(backend)?; Ok(()) } -fn blit_maybe(backend: &mut Backend) { +fn blit_maybe(backend: &mut Backend) -> Result<(), Fault> { if backend.last_render.elapsed() >= backend.opts.frame_rate { - blit(backend); + blit(backend) + } else { + Ok(()) } } -fn blit(backend: &mut Backend) { +fn blit(backend: &mut Backend) -> Result<(), Fault> { let w = backend.window.as_mut().unwrap(); if !w.is_open() { - // TODO... - std::process::exit(0); + return Err(Fault::Halt); } w.update_with_buffer(&backend.buffer, backend.width, backend.height) .expect("Update screen"); // TODO fault backend.last_render = Instant::now(); + Ok(()) } fn num2key(num: Value) -> Option { diff --git a/examples/buf_io_frequency_analysis.csn b/examples/buf_io_frequency_analysis.csn new file mode 100644 index 0000000..b9d85c2 --- /dev/null +++ b/examples/buf_io_frequency_analysis.csn @@ -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) + ) +) diff --git a/examples/buf_io_hello.csn b/examples/buf_io_hello.csn new file mode 100644 index 0000000..13bbc19 --- /dev/null +++ b/examples/buf_io_hello.csn @@ -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) + ) +) diff --git a/examples/itoa.csn b/examples/itoa.csn new file mode 100644 index 0000000..b5f63ef --- /dev/null +++ b/examples/itoa.csn @@ -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) + ) +)