about summary refs log tree commit diff stats
path: root/apps/tile/main.mu
blob: 2defb3649678ea5f4ec973439b1654eb0b082cce (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
fn main args-on-stack: (addr array addr array byte) -> exit-status/ebx: int {
  var args/eax: (addr array addr array byte) <- copy args-on-stack
  var len/ecx: int <- length args
  $main-body: {
    compare len, 2
    {
      break-if-!=
      # if single arg is 'test', run tests
      var tmp/ecx: (addr addr array byte) <- index args, 1
      var tmp2/eax: boolean <- string-equal? *tmp, "test"
      compare tmp2, 0  # false
      {
        break-if-=
        run-tests
        exit-status <- copy 0  # TODO: get at Num-test-failures somehow
        break $main-body
      }
      # if single arg is 'screen', run in full-screen mode
      tmp2 <- string-equal? *tmp, "screen"
      compare tmp2, 0  # false
      {
        break-if-=
        interactive
        exit-status <- copy 0
        break $main-body
      }
      # if single arg is 'type', run in typewriter mode
      tmp2 <- string-equal? *tmp, "type"
      compare tmp2, 0  # false
      {
        break-if-=
        repl
        exit-status <- copy 0
        break $main-body
      }
      # if single arg is 'test' ...
      tmp2 <- string-equal? *tmp, "test2"
      compare tmp2, 0  # false
      {
        break-if-=
        test
        exit-status <- copy 0
        break $main-body
      }
    }
    # otherwise error message
    print-string-to-real-screen "usage:\n"
    print-string-to-real-screen "  to run tests: tile test\n"
    print-string-to-real-screen "  full-screen mode: tile screen\n"
    print-string-to-real-screen "  regular REPL: tile type\n"
    exit-status <- copy 1
  }
}

fn interactive {
  enable-screen-grid-mode
  enable-keyboard-immediate-mode
  var env-storage: environment
  var env/esi: (addr environment) <- address env-storage
  initialize-environment env
  draw-screen env
  {
    var key/eax: grapheme <- read-key-from-real-keyboard
    compare key, 0x11  # 'ctrl-q'
    break-if-=
    process env, key
    render env
    loop
  }
  enable-keyboard-type-mode
  enable-screen-type-mode
}

fn test {
  var env-storage: environment
  var env/esi: (addr environment) <- address env-storage
  initialize-environment-with-fake-screen env, 5, 0xa
  var g/eax: grapheme <- copy 0x31  # '1'
  process env, g
  g <- copy 0x20  # space
  process env, g
  g <- copy 0x32  # '2'
  process env, g
  render env
  g <- copy 4  # 'ctrl-d'
  process env, g
  g <- copy 0x61  # 'a'
  process env, g
  g <- copy 0xa  # newline
  process env, g
  render env
}

fn repl {
  {
    # prompt
    var line-storage: (stream byte 0x100)
    var line/ecx: (addr stream byte) <- address line-storage
    print-string-to-real-screen "> "
    # read
    clear-stream line
    read-line-from-real-keyboard line
    var done?/eax: boolean <- stream-empty? line
    compare done?, 0  # false
    break-if-!=
    # parse
    var env-storage: environment
    var env/esi: (addr environment) <- address env-storage
    initialize-environment env
    {
      var done?/eax: boolean <- stream-empty? line
      compare done?, 0  # false
      break-if-!=
      var g/eax: grapheme <- read-grapheme line
      process env, g
      loop
    }
    # eval
    var stack-storage: value-stack
    var stack/edi: (addr value-stack) <- address stack-storage
    initialize-value-stack stack, 0x10
    evaluate-environment env, stack
    # print
    var empty?/eax: boolean <- value-stack-empty? stack
    {
      compare empty?, 0  # false
      break-if-!=
      var result/eax: int <- pop-int-from-value-stack stack
      print-int32-decimal-to-real-screen result
      print-string-to-real-screen "\n"
    }
    #
    loop
  }
}