From 7ecfd5eba9d9f45a2ccd5237bd68d8b24b26978b Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Wed, 16 Sep 2020 23:46:25 -0700 Subject: 6796 Extremely hacky initial stab at a 1-line editor. --- apps/tile/environment.mu | 153 +++++++++++++++++++++++++++++++++++++++++++++++ apps/tile/gap-buffer.mu | 48 ++++++++++----- apps/tile/main.mu | 45 +++----------- apps/tui.mu | 2 +- 4 files changed, 195 insertions(+), 53 deletions(-) create mode 100644 apps/tile/environment.mu (limited to 'apps') diff --git a/apps/tile/environment.mu b/apps/tile/environment.mu new file mode 100644 index 00000000..c14d34af --- /dev/null +++ b/apps/tile/environment.mu @@ -0,0 +1,153 @@ +type environment { + screen: (handle screen) + buf: gap-buffer + cursor-row: int + cursor-col: int +} + +fn initialize-environment _env: (addr environment) { + var env/esi: (addr environment) <- copy _env + var screen-ah/edi: (addr handle screen) <- get env, screen + var _screen/eax: (addr screen) <- lookup *screen-ah + var screen/edi: (addr screen) <- copy _screen + var nrows/eax: int <- copy 0 + var ncols/ecx: int <- copy 0 + nrows, ncols <- screen-size screen + # cursor-col + var midcol/edx: int <- copy ncols + midcol <- shift-right 1 + var start-col/edx: int <- copy midcol + start-col <- add 2 + { + var cursor-col/eax: (addr int) <- get env, cursor-col + copy-to *cursor-col start-col + } + # cursor-row + var midrow/ebx: int <- copy 0 + { + var tmp/eax: int <- try-divide nrows, 3 + midrow <- copy tmp + } + var start-row/ebx: int <- copy midrow + start-row <- subtract 3 + { + var cursor-row/eax: (addr int) <- get env, cursor-row + copy-to *cursor-row start-row + } + # buf + var gap/eax: (addr gap-buffer) <- get env, buf + initialize-gap-buffer gap +} + +fn initialize-environment-with-fake-screen _self: (addr environment), nrows: int, ncols: int { + var self/esi: (addr environment) <- copy _self + var screen-ah/eax: (addr handle screen) <- get self, screen + allocate screen-ah + var screen-addr/eax: (addr screen) <- lookup *screen-ah + initialize-screen screen-addr, nrows, ncols + initialize-environment self +} + +fn render-loop _self: (addr environment) { + var self/esi: (addr environment) <- copy _self + render self + # + $interactive:loop: { + var key/eax: grapheme <- read-key-from-real-keyboard + compare key, 0x71 # 'q' + break-if-= + process self, key + loop + } +} + +fn process _self: (addr environment), key: grapheme { +$process:body: { + var self/esi: (addr environment) <- copy _self + var screen-ah/edi: (addr handle screen) <- get self, screen + var _screen/eax: (addr screen) <- lookup *screen-ah + var screen/edi: (addr screen) <- copy _screen + var buf/ebx: (addr gap-buffer) <- get self, buf + compare key, 0x445b1b # left-arrow + { + break-if-!= + var foo/eax: grapheme <- gap-left buf + compare foo, -1 + { + break-if-= +#? print-string-to-real-screen "XXX\n" + var cursor-col/eax: (addr int) <- get self, cursor-col + decrement *cursor-col +#? print-int32-decimal-to-real-screen *cursor-col +#? print-string-to-real-screen "ZZZ\n" + } + var cursor-row/eax: (addr int) <- get self, cursor-row + var cursor-col/ecx: (addr int) <- get self, cursor-col + move-cursor screen, *cursor-row, *cursor-col + break $process:body + } + compare key, 0x435b1b # right-arrow + { + break-if-!= + var foo/eax: grapheme <- gap-right buf + compare foo, -1 + { + break-if-= + var cursor-col/eax: (addr int) <- get self, cursor-col + increment *cursor-col + } + break $process:body + } + var g/ecx: grapheme <- copy key + var print?/eax: boolean <- real-grapheme? key + { + compare print?, 0 # false + break-if-= + add-grapheme-at-gap buf, g + var cursor-col/eax: (addr int) <- get self, cursor-col + increment *cursor-col + render self + break $process:body + } + # silently ignore other hotkeys +} +} + +fn render _env: (addr environment) { + var env/esi: (addr environment) <- copy _env + var screen-ah/edi: (addr handle screen) <- get env, screen + var _screen/eax: (addr screen) <- lookup *screen-ah + var screen/edi: (addr screen) <- copy _screen + clear-screen screen + var nrows/eax: int <- copy 0 + var ncols/ecx: int <- copy 0 + nrows, ncols <- screen-size screen + var midcol/edx: int <- copy ncols + midcol <- shift-right 1 + draw-vertical-line screen, 1, nrows, midcol + var midrow/ebx: int <- copy 0 + { + var tmp/eax: int <- try-divide nrows, 3 + midrow <- copy tmp + } + var left-col/edx: int <- copy midcol + left-col <- increment + draw-horizontal-line screen, midrow, left-col, ncols +#? # some debug info +#? ncols <- subtract 2 +#? move-cursor screen, 1, ncols + var buf/ecx: (addr gap-buffer) <- get env, buf +#? var foo/eax: int <- gap-buffer-length buf +#? print-int32-decimal screen, foo + # + var start-row/ebx: int <- copy midrow + start-row <- subtract 3 + var start-col/edx: int <- copy left-col + start-col <- increment + move-cursor screen, start-row, start-col + render-gap-buffer screen, buf + # update cursor + var cursor-row/eax: (addr int) <- get env, cursor-row + var cursor-col/ecx: (addr int) <- get env, cursor-col + move-cursor screen, *cursor-row, *cursor-col +} diff --git a/apps/tile/gap-buffer.mu b/apps/tile/gap-buffer.mu index 11cd34b5..9d6b7161 100644 --- a/apps/tile/gap-buffer.mu +++ b/apps/tile/gap-buffer.mu @@ -19,6 +19,17 @@ fn render-gap-buffer screen: (addr screen), _gap: (addr gap-buffer) { render-stack-from-top right, screen } +fn gap-buffer-length _gap: (addr gap-buffer) -> result/eax: int { + var gap/esi: (addr gap-buffer) <- copy _gap + var left/eax: (addr grapheme-stack) <- get gap, left + var tmp/eax: (addr int) <- get left, top + var left-length/ecx: int <- copy *tmp + var right/esi: (addr grapheme-stack) <- get gap, right + tmp <- get right, top + result <- copy *tmp + result <- add left-length +} + # dump stack to screen from bottom to top # don't move the cursor or anything fn render-stack-from-bottom _self: (addr grapheme-stack), screen: (addr screen) { @@ -58,39 +69,48 @@ fn render-stack-from-top _self: (addr grapheme-stack), screen: (addr screen) { } } -fn add-grapheme _self: (addr gap-buffer), g: grapheme { +fn add-grapheme-at-gap _self: (addr gap-buffer), g: grapheme { var self/esi: (addr gap-buffer) <- copy _self var left/eax: (addr grapheme-stack) <- get self, left push-grapheme-stack left, g } -fn cursor-right _self: (addr gap-buffer) { +fn gap-right _self: (addr gap-buffer) -> result/eax: grapheme { +$gap-right:body: { var self/esi: (addr gap-buffer) <- copy _self - var g/ecx: grapheme <- copy 0 + var g/edx: grapheme <- copy 0 { - var right/eax: (addr grapheme-stack) <- get self, right - var tmp/eax: grapheme <- pop-grapheme-stack right - g <- copy tmp + var right/ecx: (addr grapheme-stack) <- get self, right + result <- pop-grapheme-stack right + compare result, -1 + break-if-= $gap-right:body + g <- copy result } { - var left/eax: (addr grapheme-stack) <- get self, left + var left/ecx: (addr grapheme-stack) <- get self, left + # HERE: can't use 'result' here: "unknown variable 'result'" push-grapheme-stack left, g } } +} -fn cursor-left _self: (addr gap-buffer) { +fn gap-left _self: (addr gap-buffer) -> result/eax: grapheme { +$gap-left:body: { var self/esi: (addr gap-buffer) <- copy _self - var g/ecx: grapheme <- copy 0 + var g/edx: grapheme <- copy 0 { - var left/eax: (addr grapheme-stack) <- get self, left - var tmp/eax: grapheme <- pop-grapheme-stack left - g <- copy tmp + var left/ecx: (addr grapheme-stack) <- get self, left + result <- pop-grapheme-stack left + compare result, -1 + break-if-= $gap-left:body + g <- copy result } { - var right/eax: (addr grapheme-stack) <- get self, right + var right/ecx: (addr grapheme-stack) <- get self, right push-grapheme-stack right, g } } +} type grapheme-stack { data: (handle array grapheme) @@ -124,7 +144,7 @@ $pop-grapheme-stack:body: { { compare *top-addr, 0 break-if-> - val <- copy 0 + val <- copy -1 break $pop-grapheme-stack:body } subtract-from *top-addr, 1 diff --git a/apps/tile/main.mu b/apps/tile/main.mu index 7c7065fc..fdbb69ca 100644 --- a/apps/tile/main.mu +++ b/apps/tile/main.mu @@ -15,7 +15,7 @@ fn main args-on-stack: (addr array addr array byte) -> exit-status/ebx: int { break-if-!= var tmp/ecx: (addr addr array byte) <- index args, 1 var tmp2/eax: boolean <- string-equal? *tmp, "test" - compare tmp2, 0 + compare tmp2, 0 # false { break-if-= run-tests @@ -33,46 +33,15 @@ fn main args-on-stack: (addr array addr array byte) -> exit-status/ebx: int { fn interactive args: (addr array addr array byte) -> exit-status/ebx: int { enable-screen-grid-mode enable-keyboard-immediate-mode - var buf-storage: gap-buffer - var buf/esi: (addr gap-buffer) <- address buf-storage - initialize-gap-buffer buf - # - { - render 0, buf - var key/eax: byte <- read-key-from-real-keyboard - compare key, 0x71 # 'q' - break-if-= - var g/ecx: grapheme <- copy key - add-grapheme buf, g - loop - } + var env-storage: environment + var env/esi: (addr environment) <- address env-storage + initialize-environment env + render-loop env enable-keyboard-type-mode enable-screen-type-mode exit-status <- copy 0 } -fn render screen: (addr screen), buf: (addr gap-buffer) { - clear-screen screen - var nrows/eax: int <- copy 0 - var ncols/ecx: int <- copy 0 - nrows, ncols <- screen-size screen - var midcol/edx: int <- copy ncols - midcol <- shift-right 1 - draw-vertical-line screen, 1, nrows, midcol - var midrow/ebx: int <- copy 0 - { - var tmp/eax: int <- try-divide nrows, 3 - midrow <- copy tmp - } - var left-col/edx: int <- copy midcol - left-col <- increment - draw-horizontal-line screen, midrow, left-col, ncols - # initialize cursor - var start-row/ebx: int <- copy midrow - start-row <- subtract 3 - var start-col/edx: int <- copy left-col - start-col <- increment - move-cursor screen, start-row, start-col - # - render-gap-buffer screen, buf +fn real-grapheme? g: grapheme -> result/eax: boolean { + result <- copy 1 # true } diff --git a/apps/tui.mu b/apps/tui.mu index a02d8fb0..be74e1c4 100644 --- a/apps/tui.mu +++ b/apps/tui.mu @@ -23,7 +23,7 @@ fn main -> exit-status/ebx: int { print-string 0, "press a key to see its code: " enable-keyboard-immediate-mode - var x/eax: byte <- read-key-from-real-keyboard + var x/eax: grapheme <- read-key-from-real-keyboard enable-keyboard-type-mode enable-screen-type-mode print-string 0, "You pressed " -- cgit 1.4.1-2-gfad0 80dda7c864de96ba1c854bc2'>a782b70 ^
99c363b ^


e4104a8 ^
546dfcd ^
99c363b ^
a782b70 ^





2349b7d ^

f9d26ee ^



cf66462 ^
f7387f8 ^
257affc ^
2349b7d ^




cce7cb4 ^
2349b7d ^
257affc ^
546dfcd ^
d7fecf7 ^
a782b70 ^
257affc ^
cf66462 ^


2349b7d ^



cf66462 ^
62946ff ^





9e3b602 ^
62946ff ^
626f91c ^

491e360 ^
626f91c ^





491e360 ^
626f91c ^
491e360 ^
62946ff ^


cf66462 ^


62946ff ^



2349b7d ^
5685a17 ^
2349b7d ^


24196d2 ^
a782b70 ^





d7fecf7 ^
99c363b ^




257affc ^
d7fecf7 ^
257affc ^


cf66462 ^

84965d6 ^
0950e39 ^


cf66462 ^


257affc ^

2349b7d ^
b60999c ^
3650b72 ^
b12eba5 ^









b60999c ^
b12eba5 ^
b60999c ^


3650b72 ^
b60999c ^


3650b72 ^
b60999c ^
626f91c ^
9fd6054 ^
334ca89 ^
9fd6054 ^

334ca89 ^
6fcc047 ^
334ca89 ^





9fd6054 ^

d7fecf7 ^

626f91c ^
d7fecf7 ^
626f91c ^
99c363b ^
626f91c ^


d7fecf7 ^
99c363b ^
9fd6054 ^
626f91c ^




491e360 ^
626f91c ^
1b673b5 ^











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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207