-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring_builder.ex
More file actions
476 lines (378 loc) · 10.9 KB
/
Copy pathstring_builder.ex
File metadata and controls
476 lines (378 loc) · 10.9 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
defmodule SilverOrb.StringBuilder do
@moduledoc """
Build strings with dynamic content.
Here’s an example with components rendering dynamic HTML:
```elixir
defmodule HelloWorldComponent do
use Orb
use SilverOrb.StringBuilder
arena Output, pages: 1 do
use SilverOrb.StringBuilder
# Now we have Output.build!/1 macro
end
defwp daytime?(hour: I32), I32 do
hour >= 6 &&& hour <= 19
end
defw render(hour: I32), Output.MemorySlice do
Output.build! do
"<h1>"
if daytime?(hour) do
"Hello 🌞 sunny world"
else
"Hello 🌛 moonlit world"
end
"</h1>\\n"
end
end
end
defmodule DynamicHTMLPage do
use Orb
use SilverOrb.StringBuilder
Orb.include(HelloWorldComponent)
global do
@hour_of_day 8
end
defw set_hour_of_day(hour: I32) do
@hour_of_day = hour
end
defw text_html(), StringBuilder do
StringBuilder.build! do
\"\"\"
<!doctype html>
<meta charset="utf-8">
\"\"\"
HelloWorldComponent.render(@hour_of_day)
end
end
end
```
"""
use Orb
# use SilverOrb.BumpAllocator
# use SilverOrb.Mem
with @behaviour Orb.CustomType do
@impl Orb.CustomType
def wasm_type(), do: {:i32, :i32}
@impl Orb.CustomType
def type_name(), do: "SilverOrb.StringBuilder"
end
defmodule Format do
@moduledoc false
use Orb.Import, name: :format
defw(f32(value: F32, str_ptr: I32.U8.UnsafePointer), I32)
end
# I32.global(bump_offset: 0, bump_mark: 0, bump_write_level: 0)
defmodule Constants do
def init_offset() do
1 * Memory.page_byte_size()
end
end
Memory.pages(2)
I32.global(
bump_offset: Constants.init_offset(),
bump_mark: 0,
bump_write_level: 0
)
defmacro __using__(_) do
quote do
use SilverOrb.IntFormatter
Orb.include(unquote(__MODULE__))
alias require unquote(__MODULE__)
import unquote(__MODULE__)
# global do
# @bump_write_level 0
# end
Memory.pages(2)
I32.global(
bump_offset: Constants.init_offset(),
bump_mark: 0,
bump_write_level: 0
)
Orb.types([unquote(__MODULE__)])
end
end
defwp bump_write_start() do
if I32.eqz(@bump_write_level) do
# TODO: refactor based on Arena instead?
@bump_offset = Constants.init_offset()
@bump_mark = @bump_offset
end
@bump_write_level = @bump_write_level + 1
end
defwp bump_write_done(), __MODULE__ do
assert!(@bump_write_level > 0)
@bump_write_level = @bump_write_level - 1
{@bump_mark, @bump_offset - @bump_mark}
end
def write_str(str) do
bump_write_str(str)
end
defwp bump_write_str(str: Str),
i: I32,
char: I32 do
return(if: I32.eq(str[:ptr], @bump_mark) ||| I32.eqz(str[:size]))
loop EachChar do
char = Memory.load!(I32.U8, str[:ptr] + i)
Memory.store!(I32.U8, @bump_offset + i, char)
if i < str[:size] do
i = i + 1
EachChar.continue()
end
# EachChar.continue()
# |> if i < len do
# i = i + 1
# end
end
# Memory.store!(I32.U8, @bump_offset + len - 1, ?q)
@bump_offset = @bump_offset + str[:size]
end
defwp bump_written?(), I32 do
@bump_offset > @bump_mark
end
def build_begin!(), do: bump_write_start()
def build_done!(), do: bump_write_done()
def appended?(), do: bump_written?()
defmacro build!(do: block) do
items = __build_block(block)
quote do
Orb.InstructionSequence.new(unquote(__MODULE__), [
build_begin!(),
Orb.InstructionSequence.new(nil, unquote(items)),
build_done!()
])
end
end
def __build_block(block) do
items =
block
|> case do
{:__block__, _, items} -> items
term -> term
end
|> List.wrap()
for item <- items do
quote do
with do
import Orb.IfElse.DSL, only: []
import unquote(__MODULE__).DSL
unquote(__MODULE__).build_item(unquote(item))
end
end
end
end
def build_item("") do
Orb.InstructionSequence.empty()
end
def build_item(string) when is_binary(string) do
append!(string: string)
end
def build_item(%Orb.Str{string: ""}) do
Orb.InstructionSequence.empty()
end
def build_item(%Orb.Str{} = term) do
append!(string: term)
end
def build_item(%{push_type: __MODULE__} = string_builder_call) do
# Orb.Stack.drop(string_builder_call)
%Orb.Stack.Drop{
instruction: string_builder_call,
count: 2
}
end
# TODO: String64
def build_item(%{push_type: Orb.Str} = str_ptr) do
append!(string: str_ptr)
end
def build_item(%struct{push_type: type} = instruction)
when struct in [Orb.Instruction, Orb.VariableReference] and type in [:f32, Orb.F32] do
append!(decimal_f32: instruction)
end
def build_item(term), do: term
# For nested build functions.
# We want inner functions to also return strings for easier debugging of their result, not just append.
def append!(function, a, b, c) when is_atom(function) do
import Orb.DSL
Orb.Instruction.Call.new(I32, [], function, [a, b, c]) |> Orb.Stack.drop()
end
def append!(function, a, b) when is_atom(function) do
import Orb.DSL
Orb.Instruction.Call.new(I32, [], function, [a, b]) |> Orb.Stack.drop()
end
def append!(function, args) when is_atom(function) and is_list(args) do
import Orb.DSL
Orb.Instruction.Call.new(I32, [], function, args) |> Orb.Stack.drop()
end
def append!(function, a) when is_atom(function) do
import Orb.DSL
Orb.Instruction.Call.new(I32, [], function, [a]) |> Orb.Stack.drop()
end
def append!(function) when is_atom(function) do
import Orb.DSL
Orb.Instruction.Call.new(I32, [], function, []) |> Orb.Stack.drop()
end
def append!(constant) when is_binary(constant) do
bump_write_str(constant)
end
def append!(%Orb.Str{string: ""}) do
Orb.InstructionSequence.empty()
end
def append!(%Orb.Str{} = str) do
Orb.InstructionSequence.new(nil, [
str,
Orb.Instruction.Call.new(
nil,
[:i32, :i32],
:bump_write_str,
[]
)
])
# bump_write_str(str)
end
def append!(string: str) do
# write_str(str)
Orb.InstructionSequence.new(nil, [
Orb.Constants.expand_if_needed(str),
Orb.Instruction.Call.new(
nil,
[:i32, :i32],
:bump_write_str,
[Orb.Stack.pop(:i32), Orb.Stack.pop(:i32)]
)
])
# bump_write_str(str)
end
def append!(u8: char) do
Orb.snippet U32 do
Memory.store!(I32.U8, @bump_offset, char)
# {:i32, :store8, @bump_offset, char}
@bump_offset = @bump_offset + 1
end
end
def append!(ascii: char), do: append!(u8: char)
def append!(decimal_u32: int) do
Orb.snippet do
# Orb.Stack.drop(Orb.I32)
# mut!(@bump_offset).write
Orb.InstructionSequence.new(nil, [
SilverOrb.IntFormatter.decimal_u32(int, @bump_offset),
# @bump_offset = @bump_offset + Orb.Stack.pop(Orb.I32)
@bump_offset = Orb.Instruction.i32(:add, @bump_offset),
# FIXME: should be able to use Orb.Stack.pop/1 here
Orb.Stack.drop(%Orb.Nop{push_type: Orb.I32})
])
end
end
# def append!(decimal_f32: f) do
# snippet do
# @bump_offset =
# Format.f32(f, @bump_offset) + @bump_offset
# end
# end
def append!(hex_upper: hex) do
# This might be a bit over the top…
{initial, following} =
case hex do
[_value, {:local_tee, identifier}] ->
{hex, {:local_get, identifier}}
_ ->
{hex, hex}
end
Orb.snippet U32 do
# push(hex)
#
# push(I32.le_u(hex, 9))
#
# :drop
#
# :pop
# memory32_8![@bump_offset] = I32.when?(I32.le_u(hex, 9), do: I32.add(hex, ?0), else: I32.sub(hex, 10) |> I32.add(?A))
# I32.when?(I32.le_u(hex, 9), do: I32.add(hex, ?0), else: I32.sub(hex, 10) |> I32.add(?A))
# push(@bump_offset)
# push(@bump_offset)
# memory32_8![0x0] = hex
# if I32.le_u(memory32_8![0x0].unsigned, 9) do
# memory32_8![@bump_offset] = I32.add(memory32_8![0x0].unsigned, ?0)
# else
# memory32_8![@bump_offset] = I32.sub(memory32_8![0x0].unsigned, 10) |> I32.add(?A)
# end
# I32.when? I32.le_u(:pop, 9) do
# push(hex)
# I32.add(:pop, ?0)
# else
# push(hex)
# I32.sub(:pop, 10) |> I32.add(?A)
# end
# memory32_8![:pop] = :pop
# memory32_8![@bump_offset] =
# initial |> I32.add(I32.when?(I32.le_u(following, 9), do: ?0, else: inline(do: ?A - 10)))
Memory.store!(
I32.U8,
@bump_offset,
I32.add(initial, if(following <= 9, do: i32(?0), else: i32(?A - 10)))
)
# memory32_8![@bump_offset] =
# I32.when?(I32.le_u(initial, 9), do: I32.add(following, ?0), else: I32.sub(following, 10) |> I32.add(?A))
# FIXME: we are evaluating hex multiple times. Do we have to stash it in a variable?
# memory32_8![@bump_offset] =
# I32.when?(I32.le_u(hex, 9), do: I32.add(hex, ?0), else: I32.sub(hex, 10) |> I32.add(?A))
@bump_offset = @bump_offset + 1
end
end
def append!(list) when is_list(list) do
for item <- list do
append!([item])
end
end
# def append!(list) when is_list(list) do
# snippet do
# inline for item <- list do
# # WE NEED TO INCREMENT bump_offset after each round
# case item do
# {:ascii, char} ->
# snippet do
# memory32_8![@bump_offset] = char
# end
#
# {:hex_upper, hex} ->
# snippet do
# memory32_8![@bump_offset] =
# I32.when?(I32.le_u(hex, 9), do: I32.add(hex, ?0), else: I32.sub(hex, 10) |> I32.add(?A))
# end
# end
# end
#
# @bump_offset = I32.add(@bump_offset, length(list))
# end |> dbg()
# end
# defmacro sigil_E({:<<>>, line, pieces}, []) do
# dbg(pieces)
# items = __build_block(pieces)
# quote do
# [
# build_begin!(),
# unquote(items),
# build_done!()
# ]
# end
# end
defmodule DSL do
alias SilverOrb.StringBuilder
defmacro if(condition, do: when_true, else: when_false) do
quote do
Orb.IfElse.new(
unquote(condition),
Orb.InstructionSequence.new(unquote(StringBuilder.__build_block(when_true))),
Orb.InstructionSequence.new(unquote(StringBuilder.__build_block(when_false)))
)
end
end
defmacro if(condition, do: when_true) do
quote do
Orb.IfElse.new(
unquote(condition),
Orb.InstructionSequence.new(unquote(StringBuilder.__build_block(when_true)))
)
end
end
end
end