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
|
# Incomplete second attempt at parsing headings.
#
# This 'OO' approach seems more scalable. We hoist out all the outer framework
# for deciding when to increment 'col', when to increment 'row' and when to
# start a new page in a whole new part of the screen. Now it gets encapsulated
# into a series of small helpers that can be called from multiple places.
# Objects as coroutines.
#
# In spite of these advances, I need to first wrestle with a parsing issue.
# This text has a heading:
#
# abc *def
# # ghi*
#
# Ugh, so I can't do this translation in a single pass. At the first asterisk
# there's just not enough information to know whether it starts a bold text or
# not.
#
# Then again, maybe I should just keep going and not try to be compatible with
# GitHub-Flavored Markdown. Require that new headings are also new paragraphs.
fn main args: (addr array (addr array byte)) -> exit-status/ebx: int {
var filename/eax: (addr array byte) <- first-arg args
var file/esi: (addr buffered-file) <- load-file filename
enable-screen-grid-mode
enable-keyboard-immediate-mode
var nrows/eax: int <- copy 0
var ncols/ecx: int <- copy 0
nrows, ncols <- screen-size
var screen-position-state-storage: screen-position-state
var screen-position-state: (addr screen-position-state) = address screen-position-state-storage
init-screen-position-state screen-position-state, nrows, ncols
{
render file, screen-position-state
var key/eax: byte <- read-key
compare key, 0x71 # 'q'
loop-if-!=
}
enable-keyboard-type-mode
enable-screen-type-mode
exit-status <- copy 0
}
type screen-position-state {
nrows: int # const
ncols: int # const
toprow: int
botrow: int
leftcol: int
rightcol: int
row: int
col: int
}
fn render in: (addr buffered-file), state: (addr screen-position-state) {
start-drawing state
render-normal in, state
}
fn render-normal in: (addr buffered-file), state: (addr screen-position-state) {
{
# if done-drawing?(state) break
var done?/eax: boolean <- done-drawing? state
compare done?, 0
break-if-!=
#
var c/eax: byte <- read-byte-buffered in
# if (c == EOF) break
compare c, 0xffffffff # EOF marker
break-if-=
# if (c == '*') start-bold, render-until-asterisk(in, state), reset
# else if (c == '_') start-bold, render-until-underscore(in, state), reset
# else if (c == '#') compute-color, start color, render-header-line(in, state), reset
# else add-char(state, c)
}
}
fn render-until-asterisk in: (addr buffered-file), state: (addr screen-position-state) {
{
# if done-drawing?(state) break
var done?/eax: boolean <- done-drawing? state
compare done?, 0
break-if-!=
#
var c/eax: byte <- read-byte-buffered in
# if (c == EOF) break
compare c, 0xffffffff # EOF marker
break-if-=
# if (c == '*') break
# else add-char(state, c)
}
}
fn render-until-underscore in: (addr buffered-file), state: (addr screen-position-state) {
{
# if done-drawing?(state) break
var done?/eax: boolean <- done-drawing? state
compare done?, 0
break-if-!=
#
var c/eax: byte <- read-byte-buffered in
# if (c == EOF) break
compare c, 0xffffffff # EOF marker
break-if-=
# if (c == '_') break
# else add-char(state, c)
}
}
fn render-header-line in: (addr buffered-file), state: (addr screen-position-state) {
{
# if done-drawing?(state) break
var done?/eax: boolean <- done-drawing? state
compare done?, 0
break-if-!=
#
var c/eax: byte <- read-byte-buffered in
# if (c == EOF) break
compare c, 0xffffffff # EOF marker
break-if-=
# if (c == '*') break
# else add-char(state, c)
}
}
fn init-screen-position-state self: (addr screen-position-state), nrows: int, ncols: int {
# hardcoded parameters:
# top-margin
# page-margin
# text-width
var dest/eax: (addr int) <- copy 0
# self->nrows = nrows
# self->ncols = ncols
# self->toprow = top-margin
# self->botrow = nrows
# self->leftcol = page-margin
# self->rightcol = self->leftcol + text-width
# start-drawing(self)
}
fn start-drawing self: (addr screen-position-state) {
# self->row = toprow
# self->col = leftcol
}
fn add-char self: (addr screen-position-state), c: byte {
# print c
# self->col++
# if (self->col > self->rightcol) next-line(self)
}
fn next-line self: (addr screen-position-state) {
# self->row++
# if (self->row > self->botrow) next-page(self)
}
fn next-page self: (addr screen-position-state) {
# self->leftcol = self->rightcol + 5
# self->rightcol = self->leftcol + text-width
}
fn done-drawing? self: (addr screen-position-state) -> result/eax: boolean {
# self->rightcol >= self->ncols
}
fn first-arg args-on-stack: (addr array (addr array byte)) -> out/eax: (addr array byte) {
var args/eax: (addr array (addr array byte)) <- copy args-on-stack
var result/eax: (addr addr array byte) <- index args, 1
out <- copy *result
}
fn load-file filename: (addr array byte) -> out/esi: (addr buffered-file) {
var result: (handle buffered-file)
{
var tmp1/eax: (addr handle buffered-file) <- address result
open filename, 0, tmp1
}
var tmp2/eax: (addr buffered-file) <- lookup result
out <- copy tmp2
}
fn dump in: (addr buffered-file) {
var c/eax: byte <- read-byte-buffered in
compare c, 0xffffffff # EOF marker
break-if-=
print-byte c
loop
}
|