about summary refs log tree commit diff stats
path: root/baremetal/shell/print.mu
blob: 1b188942db41785088340042b904a464ce72f9a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn print-cell _in: (addr handle cell), out: (addr stream byte), trace: (addr trace) {
  clear-stream out
  var in/eax: (addr handle cell) <- copy _in
  var in-addr/eax: (addr cell) <- lookup *in
  var in-type/ecx: (addr int) <- get in-addr, type
  compare *in-type, 1/number
  {
    break-if-!=
    print-number in-addr, out, trace
    return
  }
  compare *in-type, 2/symbol
  {
    break-if-!=
    print-symbol in-addr, out, trace
    return
  }
}

fn print-symbol _in: (addr cell), out: (addr stream byte), trace: (addr trace) {
  {
    compare trace, 0
    break-if-=
    trace-text trace, "print", "symbol"
  }
  var in/esi: (addr cell) <- copy _in
  var data-ah/eax: (addr handle stream byte) <- get in, text-data
  var _data/eax: (addr stream byte) <- lookup *data-ah
  var data/esi: (addr stream byte) <- copy _data
  rewind-stream data
  {
    var done?/eax: boolean <- stream-empty? data
    compare done?, 0/false
    break-if-!=
    var g/eax: grapheme <- read-grapheme data
    write-grapheme out, g
    loop
  }
}

fn print-number _in: (addr cell), out: (addr stream byte), trace: (addr trace) {
  {
    compare trace, 0
    break-if-=
    trace-text trace, "print", "number"
  }
  var in/esi: (addr cell) <- copy _in
  var val/eax: (addr float) <- get in, number-data
  write-float-decimal-approximate out, *val, 3/precision
}