11import { stringify } from 'yaml' ;
2- import { PdlBlocks , PdlBlock } from './pdl_ast' ;
2+ import { PdlBlocks , PdlBlock , IterationType } from './pdl_ast' ;
33import { match , P } from 'ts-pattern' ;
44import { map_block_children } from './pdl_ast_utils' ;
55
@@ -19,22 +19,55 @@ export function show_output(data: PdlBlocks) {
1919 } )
2020 . with ( { result : P . _ } , data => {
2121 const code = document . createElement ( 'pre' ) ;
22- code . innerHTML = htmlize ( data . result )
22+ code . innerHTML = htmlize ( data . result ) ;
2323 div . appendChild ( code ) ;
2424 } )
2525 . otherwise ( ( ) => {
2626 div . innerHTML = '☐' ;
2727 } ) ;
28- switch_div_on_click ( div , show_blocks , data ) ;
28+ switch_div_on_click ( div , show_program , data ) ;
2929 return div ;
3030}
3131
32- export function show_blocks ( blocks : PdlBlocks ) {
32+ export function show_program ( blocks : PdlBlocks ) {
33+ return show_lastOf ( blocks ) ;
34+ }
35+
36+ export function show_blocks ( iteration_type : IterationType , blocks : PdlBlocks ) {
37+ return match ( iteration_type )
38+ . with ( 'text' , _ => show_text ( blocks ) )
39+ . with ( 'lastOf' , _ => show_lastOf ( blocks ) )
40+ . with ( 'array' , _ => show_array ( blocks ) )
41+ . exhaustive ( ) ;
42+ }
43+
44+ export function show_text ( blocks : PdlBlocks ) {
3345 const doc_fragment = document . createDocumentFragment ( ) ;
3446 match ( blocks )
3547 . with ( P . array ( P . _ ) , data => {
3648 for ( const doc of data ) {
37- const child = show_blocks ( doc ) ;
49+ const child = show_block ( doc ) ;
50+ doc_fragment . appendChild ( child ) ;
51+ }
52+ } )
53+ . otherwise ( block => {
54+ const child = show_block ( block ) ;
55+ doc_fragment . appendChild ( child ) ;
56+ } ) ;
57+ return doc_fragment ;
58+ }
59+
60+ export function show_lastOf ( blocks : PdlBlocks ) {
61+ const doc_fragment = document . createDocumentFragment ( ) ;
62+ match ( blocks )
63+ . with ( P . array ( P . _ ) , data => {
64+ if ( data . length > 0 ) {
65+ for ( const doc of data . slice ( 0 , - 1 ) ) {
66+ const child = show_block ( doc ) ;
67+ child . classList . add ( 'pdl_show_result_false' ) ;
68+ doc_fragment . appendChild ( child ) ;
69+ }
70+ const child = show_block ( data [ data . length - 1 ] ) ;
3871 doc_fragment . appendChild ( child ) ;
3972 }
4073 } )
@@ -45,7 +78,7 @@ export function show_blocks(blocks: PdlBlocks) {
4578 return doc_fragment ;
4679}
4780
48- export function show_array ( array : PdlBlocks [ ] ) {
81+ export function show_array ( array : PdlBlocks ) {
4982 const doc_fragment = document . createDocumentFragment ( ) ;
5083 const open_bracket = document . createElement ( 'pre' ) ;
5184 open_bracket . innerHTML = '[' ;
@@ -54,11 +87,18 @@ export function show_array(array: PdlBlocks[]) {
5487 const close_bracket = document . createElement ( 'pre' ) ;
5588 close_bracket . innerHTML = ']' ;
5689 doc_fragment . appendChild ( open_bracket ) ;
57- for ( const blocks of array ) {
58- const child = show_blocks ( blocks ) ;
59- doc_fragment . appendChild ( child ) ;
60- doc_fragment . appendChild ( comma ) ;
61- }
90+ match ( array )
91+ . with ( P . array ( P . _ ) , data => {
92+ for ( const doc of data ) {
93+ const child = show_lastOf ( doc ) ;
94+ doc_fragment . appendChild ( child ) ;
95+ doc_fragment . appendChild ( comma ) ;
96+ }
97+ } )
98+ . otherwise ( block => {
99+ const child = show_block ( block ) ;
100+ doc_fragment . appendChild ( child ) ;
101+ } ) ;
62102 doc_fragment . appendChild ( close_bracket ) ;
63103 return doc_fragment ;
64104}
@@ -76,7 +116,7 @@ export function show_object(object: {[key: string]: PdlBlocks}) {
76116 const key_column = document . createElement ( 'pre' ) ;
77117 key_column . innerHTML = key + ':' ;
78118 doc_fragment . appendChild ( key_column ) ;
79- const child = show_blocks ( object [ key ] ) ;
119+ const child = show_lastOf ( object [ key ] ) ;
80120 doc_fragment . appendChild ( child ) ;
81121 doc_fragment . appendChild ( comma ) ;
82122 } ) ;
@@ -120,7 +160,7 @@ export function show_block(data: PdlBlock) {
120160 . with ( { kind : 'data' } , data => {
121161 body . classList . add ( 'pdl_data' ) ;
122162 const code = document . createElement ( 'pre' ) ;
123- code . appendChild ( show_result_or_code ( data ) )
163+ code . appendChild ( show_result_or_code ( data ) ) ;
124164 body . appendChild ( code ) ;
125165 } )
126166 . with ( { kind : 'if' } , data => {
@@ -130,9 +170,9 @@ export function show_block(data: PdlBlock) {
130170 } else {
131171 let if_child : DocumentFragment ;
132172 if ( data . if_result ) {
133- if_child = show_blocks ( data ?. then ?? '' ) ;
173+ if_child = show_lastOf ( data ?. then ?? '' ) ;
134174 } else {
135- if_child = show_blocks ( data ?. else ?? '' ) ;
175+ if_child = show_lastOf ( data ?. else ?? '' ) ;
136176 }
137177 body . appendChild ( if_child ) ;
138178 }
@@ -143,56 +183,47 @@ export function show_block(data: PdlBlock) {
143183 body . appendChild ( show_result_or_code ( data ) ) ;
144184 } )
145185 . with ( { kind : 'include' } , data => {
146- // TODO
147186 body . classList . add ( 'pdl_include' ) ;
148- body . appendChild ( show_result_or_code ( data ) ) ;
187+ if ( data . trace ) {
188+ body . appendChild ( show_program ( data . trace ) ) ;
189+ } else {
190+ body . appendChild ( show_result_or_code ( data ) ) ;
191+ }
149192 } )
150- . with ( { kind : 'function' } , data => {
193+ . with ( { kind : 'function' } , _ => {
151194 // TODO
152195 body . classList . add ( 'pdl_function' ) ;
153196 body . classList . add ( 'pdl_show_result_false' ) ;
154- const args = document . createElement ( 'pre' ) ;
155- args . innerHTML = htmlize ( stringify ( { function : data . function } ) ) ;
156- body . appendChild ( args ) ;
157- body . appendChild ( show_blocks ( data . return ) ) ;
197+ body . innerHTML = htmlize ( null ) ;
198+ // const args = document.createElement('pre');
199+ // args.innerHTML = htmlize(stringify({function: data.function}));
200+ // body.appendChild(args);
201+ // body.appendChild(show_blocks(data.return));
158202 } )
159203 . with ( { kind : 'call' } , data => {
160204 body . classList . add ( 'pdl_call' ) ;
161205 if ( data . trace ) {
162- const args = document . createElement ( 'pre' ) ;
163- args . innerHTML = htmlize ( stringify ( { call : data . call , args : data . args } ) ) ;
164- body . appendChild ( args ) ;
165- body . appendChild ( show_blocks ( data . trace ) ) ;
206+ // const args = document.createElement('pre');
207+ // args.innerHTML = htmlize(stringify({call: data.call, args: data.args}));
208+ // body.appendChild(args);
209+ body . appendChild ( show_program ( data . trace ) ) ;
166210 } else {
167211 body . appendChild ( show_result_or_code ( data ) ) ;
168212 }
169213 } )
170214 . with ( { kind : 'text' } , data => {
171215 body . classList . add ( 'pdl_text' ) ;
172- const doc_child = show_blocks ( data . text ) ;
216+ const doc_child = show_text ( data . text ) ;
173217 body . appendChild ( doc_child ) ;
174218 } )
175219 . with ( { kind : 'lastOf' } , data => {
176220 body . classList . add ( 'pdl_lastOf' ) ;
177- const doc_child = show_blocks ( data . lastOf ) ;
221+ const doc_child = show_lastOf ( data . lastOf ) ;
178222 body . appendChild ( doc_child ) ;
179223 } )
180224 . with ( { kind : 'array' } , data => {
181225 body . classList . add ( 'pdl_array' ) ;
182- let doc_child ;
183- if ( data . array instanceof Array ) {
184- doc_child = show_array ( data . array ) ;
185- } else {
186- doc_child = document . createDocumentFragment ( ) ;
187- const open_bracket = document . createElement ( 'pre' ) ;
188- open_bracket . innerHTML = '[' ;
189- const close_bracket = document . createElement ( 'pre' ) ;
190- close_bracket . innerHTML = ']' ;
191- doc_child . appendChild ( open_bracket ) ;
192- const doc_elem = show_blocks ( data . array ) ;
193- doc_child . appendChild ( doc_elem ) ;
194- doc_child . appendChild ( close_bracket ) ;
195- }
226+ const doc_child = show_array ( data . array ) ;
196227 body . appendChild ( doc_child ) ;
197228 } )
198229 . with ( { kind : 'object' } , data => {
@@ -210,7 +241,7 @@ export function show_block(data: PdlBlock) {
210241 const role = document . createElement ( 'pre' ) ;
211242 role . innerHTML = htmlize ( data . role + ': ' ) ;
212243 body . appendChild ( role ) ;
213- const doc_child = show_blocks ( data . content ) ;
244+ const doc_child = show_lastOf ( data . content ) ;
214245 body . appendChild ( doc_child ) ;
215246 } )
216247 . with ( { kind : 'repeat' } , data => {
@@ -250,7 +281,7 @@ export function show_defs(defs: {[k: string]: PdlBlocks}): DocumentFragment {
250281 doc_fragment . appendChild ( div ) ;
251282 div . classList . add ( 'pdl_show_result_false' ) ;
252283 add_def ( div , x ) ;
253- div . appendChild ( show_blocks ( defs [ x ] ) ) ;
284+ div . appendChild ( show_lastOf ( defs [ x ] ) ) ;
254285 }
255286 return doc_fragment ;
256287}
@@ -269,7 +300,7 @@ export function show_loop_trace(trace: PdlBlocks[]): DocumentFragment {
269300 if ( trace . length > 0 ) {
270301 const iteration = document . createElement ( 'div' ) ;
271302 iteration . classList . add ( 'pdl_block' , 'pdl_lastOf' ) ;
272- const child = show_blocks ( trace . slice ( - 1 ) [ 0 ] ) ;
303+ const child = show_blocks ( 'lastOf' , trace . slice ( - 1 ) [ 0 ] ) ; // TODO:
273304 iteration . appendChild ( child ) ;
274305 doc_fragment . appendChild ( iteration ) ;
275306 }
0 commit comments