summary refs log tree commit diff
path: root/input.e
blob: c7093e643a1841fa3eea3123189e6f1058d84413 (plain)
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
~ ~~~~~~~~~~~~~~~~~~~
~ ~~ Input streams ~~
~ ~~~~~~~~~~~~~~~~~~~

~ (pointer to buffer metadata -- pointer to buffer "physical-start" field)
: buffer-physical-start ;
  ~ The physical-start field happens to be the first thing in the metadata, so
  ~ this is an nop, but it still exists as a word because having it reduces
  ~ confusion.
~ (pointer to buffer metadata -- pointer to buffer "physical-length" field)
: buffer-physical-length 8 + ;
~ (pointer to buffer metadata -- pointer to buffer "logical-start" field)
: buffer-logical-start 2 8 * + ;
~ (pointer to buffer metadata -- pointer to buffer "logical-length" field)
: buffer-logical-length 3 8 * + ;
~ (pointer to input buffer metadata -- pointer to input buffer "refill" field)
: input-buffer-refill 4 8 * + ;
~ (pointer to input buffer metadata
~  -- pointer to input buffer "next-source" field)
: input-buffer-next-source 5 8 * + ;


~   Given an initialized buffer (input or otherwise), sets its logical-start
~ and logical-length fields to indicate the buffer is empty. This relies on
~ the buffer having a backing store attached, but does not alter the backing
~ store or its contents.
~
~ (pointer to buffer metadata --)
: clear-buffer
  dup buffer-physical-start @ swap
  ~ (address of backing store, metadata pointer)
  dup 3unroll
  ~ (metadata pointer, address of backing store, metadata pointer)
  buffer-logical-start !
  buffer-logical-length 0 swap ! ;


~   Sets all fields in an input buffer metadata structure to zero,
~ effectively detaching and leaking any backing store that had been attached
~ to it. Suitable for use during initialization.
~
~ (pointer to input buffer metadata --)
: zero-input-buffer-metadata
  dup buffer-physical-start 0 swap !
  dup buffer-physical-length 0 swap !
  dup buffer-logical-start 0 swap !
  dup buffer-logical-length 0 swap !
  dup input-buffer-refill 0 swap !
  ~ Notice the absence of a dup this time.
  input-buffer-next-source 0 swap ! ;


~   Allocates input-buffer metadata, with no backing store attached.
~ Initializes the metadata to all zeroes.
~
~ (-- pointer to input buffer metadata)
: allocate-input-buffer-metadata
  6 8 * allocate
  dup zero-input-buffer-metadata ;


~   Allocates input buffer metadata and a backing store, in one operation.
~ Points the metadata to the backing store.
~
~ (buffer capacity in bytes -- pointer to input buffer metadata)
: allocate-input-buffer
  dup 6 8 * + allocate
  dup zero-input-buffer-metadata
  ~ (capacity in bytes, metadata pointer)
  dup dup 6 8 * +
  ~ (capacity in bytes, metadata pointer, metadata pointer, physical start)
  swap buffer-physical-start !
  ~ (capacity in bytes, metadata pointer)
  dup 3unroll buffer-physical-length !
  ~ (metadata pointer)
  dup clear-buffer ;


~   Sets the backing store of an input buffer to point at a null-teriminated
~ string and read from it.
~
~ (buffer metadata pointer, string pointer --)
: attach-string-to-input-buffer
  swap
  ~ (string pointer, metadata pointer)
  2dup buffer-physical-start !
  ~ (string pointer, metadata pointer)
  2dup buffer-logical-start !
  ~ (string pointer, metadata pointer)
  swap stringlen swap
  ~ (string length, metadata pointer)
  2dup buffer-physical-length !
  ~ (string length, metadata pointer)
  buffer-logical-length ! ;


~   Here we have some imperative code that runs immediately, to initialize
~ some runtime data structures.
~
~   First, we insert a metadata word header to delimit the space. Otherwise
~ "describe" would crash when attempting to describe
~ "attach-string-to-input-buffer".

s" main-input-buffer-metadata" create
s" main-input-buffer-metadata" find 0x01 entry-flags!

~   Having done that, now we do the runtime allocation. Then we also define
~ the variable "main-input-buffer" so we can find it again.
allocate-input-buffer-metadata
s" main-input-buffer" variable

~   We also initialize the metadata here, pointing it to the boot source as
~ its backing store. We could do that later, but it's convenient to do it
~ here.
~
~ TODO attach it to something
~ boot-source attach-string-to-input-buffer



~ TODO
~ consume-from                                          00000010000187c0
~ peek-from                                             0000001000018960
~ key-from                                              0000001000018ab8
~ peek                                                  0000001000018d20
~ consume                                               0000001000018d50
~ key                                                   0000001000018d88