1 # Functions for reading Antimony code from streams.
4 export _antimony_reader_eof
7 _antimony_reader_eof: word -1
12 import allocate_bytes array blob_builder blob_builder_append_byte \
13 blob_builder_to_blob block dynarray dynarray_add dynarray_to_array \
14 get_namespace make_blob namespace_intern memcpy read_byte_from_stream \
20 let lookahead auto-words 1
21 set @lookahead call read_byte_from_stream stream
22 let expr call read_expr stream lookahead
28 function stream lookahead
30 let bytes auto-bytes capacity
34 set b call read_byte_from_stream stream
42 set @lookahead call read_byte_from_stream stream
43 let heap_bytes call allocate_bytes i
44 call memcpy heap_bytes bytes i
45 return call make_blob heap_bytes i
51 let c call read_byte_from_stream stream
57 function stream lookahead
58 let exprs call dynarray 0 0
60 set @lookahead call read_byte_from_stream stream
62 ifeq @lookahead 125 # }
63 set @lookahead call read_byte_from_stream stream
69 set expr call read_expr stream lookahead
71 call dynarray_add exprs expr
75 let arr call dynarray_to_array exprs
81 function stream lookahead
82 let params call dynarray 0 0
88 set @lookahead call read_byte_from_stream stream
90 ifeq @lookahead 32 # space
91 goto read_expr_read_byte
93 ifeq @lookahead 10 # newline
97 goto read_expr_read_byte
100 ifeq @lookahead 41 # )
103 ifeq @lookahead 125 # }
106 ifeq @lookahead 9 # tab
107 goto read_expr_read_byte
109 set item call read_item stream lookahead
110 ifeq item _antimony_reader_eof
113 call dynarray_add params item
121 return call dynarray_to_array params
126 function stream lookahead
130 return call read_number stream lookahead
134 return call read_number stream lookahead
137 return call read_blob stream lookahead
141 set @lookahead call read_byte_from_stream stream
142 let expr call read_expr stream lookahead
144 set @lookahead call read_byte_from_stream stream
150 return call read_block stream lookahead
153 return _antimony_reader_eof
155 return call read_symbol stream lookahead
160 function stream lookahead
164 set c call read_number_aux stream 0 lookahead
167 set value sub c 48 # 0
168 set value call read_number_aux stream value lookahead
170 set value shl value 2
177 function stream value lookahead
179 read_number_aux_loop:
180 set c call read_byte_from_stream stream
182 goto read_number_aux_done
184 goto read_number_aux_done
186 set value mul value 10
188 set value add value c
189 goto read_number_aux_loop
190 read_number_aux_done:
197 function stream lookahead
198 let builder call blob_builder
200 let namespace @J2anamespaceJ2a
203 goto read_symbol_done
206 goto read_symbol_done
209 goto read_symbol_done
212 goto read_symbol_done
216 let name call blob_builder_to_blob builder
217 let sym call namespace_intern namespace name
218 set namespace call get_namespace sym
219 set builder call blob_builder
220 set c call read_byte_from_stream stream
222 goto read_symbol_loop
225 goto read_symbol_done
227 call blob_builder_append_byte builder c
228 set c call read_byte_from_stream stream
229 goto read_symbol_loop
232 let name call blob_builder_to_blob builder
233 return call namespace_intern namespace name