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

floats
Ondřej Hruška 4 years ago
parent c28fee88fe
commit 9254635fd7
Signed by untrusted user: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 20
      crsn_screen/src/exec.rs
  2. 50
      examples/buf_io_frequency_analysis.csn
  3. 39
      examples/buf_io_hello.csn
  4. 24
      examples/itoa.csn

@ -97,9 +97,9 @@ impl OpTrait for ScreenOp {
let backend: &mut Backend = state.ext_mut(); let backend: &mut Backend = state.ext_mut();
if force != 0 { if force != 0 {
blit(backend) blit(backend)?;
} else { } else {
blit_maybe(backend) blit_maybe(backend)?;
} }
} }
@ -143,7 +143,7 @@ impl OpTrait for ScreenOp {
backend.buffer[index as usize] = color as u32; backend.buffer[index as usize] = color as u32;
if backend.opts.auto_blit { 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); backend.window = Some(window);
blit_maybe(backend); blit_maybe(backend)?;
Ok(()) Ok(())
} }
fn blit_maybe(backend: &mut Backend) { fn blit_maybe(backend: &mut Backend) -> Result<(), Fault> {
if backend.last_render.elapsed() >= backend.opts.frame_rate { 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(); let w = backend.window.as_mut().unwrap();
if !w.is_open() { if !w.is_open() {
// TODO... return Err(Fault::Halt);
std::process::exit(0);
} }
w.update_with_buffer(&backend.buffer, backend.width, backend.height) w.update_with_buffer(&backend.buffer, backend.width, backend.height)
.expect("Update screen"); // TODO fault .expect("Update screen"); // TODO fault
backend.last_render = Instant::now(); backend.last_render = Instant::now();
Ok(())
} }
fn num2key(num: Value) -> Option<Key> { fn num2key(num: Value) -> Option<Key> {

@ -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)
)
)

@ -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)
)
)

@ -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)
)
)
Loading…
Cancel
Save