a4-core 0.0.4

A forth-inspired, bytecode-compiled scripting language for Anachro Powerbus
Documentation
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use std::collections::BTreeMap;
use std::sync::Arc;

use crate::{
    ser_de::{SerDict, SerWord},
    std_rt::{
        new_runtime, ser_srw, BuiltinToken, NamedStdRuntimeWord, SerContext, StdFuncSeq,
        StdRuntime, StdRuntimeWord, StdVecStack,
    },
    Error, RuntimeWord, StepResult, VerbSeqInner,
};

pub struct Dict {
    pub bis: BTreeMap<String, BuiltinToken>,
    pub data: BTreeMap<String, StdFuncSeq>,
    pub(crate) shame_idx: usize,
}

impl Dict {
    pub fn new() -> Self {
        Self {
            bis: BTreeMap::new(),
            data: BTreeMap::new(),
            shame_idx: 0,
        }
    }

    pub fn serialize(&self) -> SerDict {
        let mut out: BTreeMap<String, Vec<SerWord>> = BTreeMap::new();
        let mut data_map: Vec<String> = Vec::new();
        let mut ctxt = SerContext::new();

        for (word, val) in self.data.iter() {
            out.insert(word.to_string(), ser_srw(&mut ctxt, &word, val));
        }

        let mut data = Vec::new();
        for word in ctxt.seqs {
            data.push(out.get(&word).unwrap().clone());
            data_map.push(word.clone());
        }

        SerDict {
            data,
            data_map: Some(data_map),
            bis: ctxt.bis,
        }
    }
}

pub struct Context {
    pub rt: StdRuntime,
    pub dict: Dict,
}

impl Context {
    pub fn load_ser_dict(&mut self, data: &SerDict) {
        let data_map = if let Some(dm) = data.data_map.as_ref() {
            dm.clone()
        } else {
            eprintln!("Error: dict has no name map! Refusing to load.");
            return;
        };

        if !data.bis.iter().all(|bi| self.dict.bis.contains_key(bi)) {
            eprintln!("Missing builtins! Refusing to load.");
            return;
        }

        if data_map.len() != data.data.len() {
            eprintln!("Data map size mismatch! Refusing to load.");
            return;
        }

        for (name, word) in data_map.iter().zip(data.data.iter()) {
            let cword = word
                .iter()
                .map(|x| match x {
                    SerWord::LiteralVal(v) => NamedStdRuntimeWord {
                        name: format!("LIT({})", v),
                        word: RuntimeWord::LiteralVal(*v),
                    },
                    SerWord::Verb(i) => {
                        let txt = data.bis.get(*i as usize).unwrap();
                        NamedStdRuntimeWord {
                            name: txt.clone(),
                            word: RuntimeWord::Verb(self.dict.bis.get(txt).unwrap().clone()),
                        }
                    }
                    SerWord::VerbSeq(i) => {
                        let txt = data_map.get(*i as usize).unwrap();
                        NamedStdRuntimeWord {
                            name: txt.clone(),
                            word: RuntimeWord::VerbSeq(VerbSeqInner::from_word(txt.to_string())),
                        }
                    }
                    SerWord::UncondRelativeJump { offset } => NamedStdRuntimeWord {
                        name: format!("UCRJ({})", offset),
                        word: RuntimeWord::UncondRelativeJump { offset: *offset },
                    },
                    SerWord::CondRelativeJump { offset, jump_on } => NamedStdRuntimeWord {
                        name: format!("CRJ({})", offset),
                        word: RuntimeWord::CondRelativeJump {
                            offset: *offset,
                            jump_on: *jump_on,
                        },
                    },
                })
                .collect::<Vec<_>>();

            self.dict.data.insert(
                name.clone(),
                StdFuncSeq {
                    inner: Arc::new(cword),
                },
            );
        }
    }

    fn compile(&mut self, data: &[String]) -> Result<Vec<NamedStdRuntimeWord>, Error> {
        let mut vd_data: VecDeque<String> = data
            .iter()
            .map(String::as_str)
            .map(str::to_lowercase)
            .collect();

        let munched = muncher(&mut vd_data);
        assert!(vd_data.is_empty());

        let conv: Vec<NamedStdRuntimeWord> = munched
            .into_iter()
            .map(|m| m.to_named_rt_words(&mut self.dict))
            .flatten()
            .collect();

        Ok(conv)
    }

    pub fn evaluate(&mut self, data: Vec<String>) -> Result<(), Error> {
        match (data.first(), data.last()) {
            (Some(f), Some(l)) if f == ":" && l == ";" => {
                // Must have ":", "$NAME", "$SOMETHING+", ";"
                assert!(data.len() >= 3);

                let name = data[1].to_lowercase();

                // TODO: Doesn't handle "empty" definitions
                let relevant = &data[2..][..data.len() - 3];

                // let compiled = Arc::new(self.compile(relevant)?);
                let compiled = Arc::new(self.compile(relevant).unwrap());

                self.dict.data.insert(name, StdFuncSeq { inner: compiled });
            }
            _ => {
                // We should interpret this as a line to compile and run
                // (but then discard, because it isn't bound in the dict)
                // let temp_compiled = RuntimeWord::VerbSeq(StdFuncSeq { inner:  });
                if !data.is_empty() {
                    let name = format!("__{}", self.dict.shame_idx);
                    // let comp = self.compile(&data)?;
                    let comp = self.compile(&data).unwrap();
                    self.dict.data.insert(
                        name.clone(),
                        StdFuncSeq {
                            inner: Arc::new(comp),
                        },
                    );
                    self.dict.shame_idx += 1;
                    let temp_compiled = RuntimeWord::VerbSeq(VerbSeqInner::from_word(name));
                    self.push_exec(temp_compiled);
                }
            }
        }

        Ok(())
    }

    pub fn serialize(&self) -> SerDict {
        self.dict.serialize()
    }

    pub fn step(&mut self) -> Result<StepResult<BuiltinToken, String>, Error> {
        self.rt.step()
    }

    pub fn data_stack(&self) -> &StdVecStack<i32> {
        &self.rt.data_stk
    }

    pub fn return_stack(&self) -> &StdVecStack<i32> {
        &self.rt.ret_stk
    }

    pub fn flow_stack(&self) -> &StdVecStack<RuntimeWord<BuiltinToken, String>> {
        &self.rt.flow_stk
    }

    pub fn with_builtins(bi: &[(&'static str, fn(&mut StdRuntime) -> Result<(), Error>)]) -> Self {
        let mut new = Context {
            rt: new_runtime(),
            dict: Dict::new(),
        };

        for (word, func) in bi {
            new.dict
                .bis
                .insert(word.to_string(), BuiltinToken::new(*func));
        }

        new
    }

    pub fn output(&mut self) -> String {
        self.rt.exchange_output()
    }

    pub fn push_exec(&mut self, word: StdRuntimeWord) {
        self.rt.push_exec(word)
    }
}

// TODO: Expand number parser
// Make this a function to later allow for more custom parsing
// of literals like '0b1111_0000_1111_0000'
//
// See https://github.com/rust-analyzer/rust-analyzer/blob/c96481e25f08d1565cb9b3cac89323216e6f8d7f/crates/syntax/src/ast/token_ext.rs#L616-L662
// for one way of doing this!
fn parse_num(input: &str) -> Option<i32> {
    input.parse::<i32>().ok()
}

/// This struct represents a "chunk" of the AST
#[derive(Debug)]
enum Chunk {
    IfThen {
        if_body: Vec<Chunk>,
    },
    IfElseThen {
        if_body: Vec<Chunk>,
        else_body: Vec<Chunk>,
    },
    DoLoop {
        do_body: Vec<Chunk>,
    },
    Token(String),
    Comment {
        contents: Vec<String>,
    }
}

impl Chunk {
    /// Convert a chunk of AST words into a vec of `NamedStdRuntimeWord`s
    fn to_named_rt_words(self, dict: &mut Dict) -> Vec<NamedStdRuntimeWord> {
        let mut ret = vec![];

        match self {
            Chunk::IfThen { if_body } => {
                // First, convert the body into a sequence
                let mut conv: VecDeque<NamedStdRuntimeWord> = if_body
                    .into_iter()
                    .map(|m| m.to_named_rt_words(dict))
                    .flatten()
                    .collect();

                conv.push_front(NamedStdRuntimeWord {
                    name: "CRJ".into(),
                    word: RuntimeWord::CondRelativeJump {
                        offset: conv.len() as i32,
                        jump_on: false,
                    },
                });

                let conv: Vec<NamedStdRuntimeWord> = conv.into_iter().collect();
                ret.extend(conv);
            }
            Chunk::IfElseThen { if_body, else_body } => {
                let mut if_conv: VecDeque<NamedStdRuntimeWord> = if_body
                    .into_iter()
                    .map(|m| m.to_named_rt_words(dict))
                    .flatten()
                    .collect();

                let else_conv: Vec<NamedStdRuntimeWord> = else_body
                    .into_iter()
                    .map(|m| m.to_named_rt_words(dict))
                    .flatten()
                    .collect();

                if_conv.push_back(NamedStdRuntimeWord {
                    name: "UCRJ".into(),
                    word: RuntimeWord::UncondRelativeJump {
                        offset: else_conv.len() as i32,
                    },
                });

                if_conv.push_front(NamedStdRuntimeWord {
                    name: "CRJ".into(),
                    word: RuntimeWord::CondRelativeJump {
                        offset: if_conv.len() as i32,
                        jump_on: false,
                    },
                });

                let conv: Vec<NamedStdRuntimeWord> =
                    if_conv.into_iter().chain(else_conv.into_iter()).collect();
                ret.extend(conv);
            }
            Chunk::DoLoop { do_body } => {
                // First, convert the body into a sequence
                let mut conv: VecDeque<NamedStdRuntimeWord> = do_body
                    .into_iter()
                    .map(|m| m.to_named_rt_words(dict))
                    .flatten()
                    .collect();

                conv.push_back(NamedStdRuntimeWord {
                    word: RuntimeWord::Verb(BuiltinToken::new(crate::builtins::bi_priv_loop)),
                    name: "PRIV_LOOP".into(),
                });

                let len = conv.len();

                conv.push_front(NamedStdRuntimeWord {
                    word: RuntimeWord::Verb(BuiltinToken::new(crate::builtins::bi_retstk_push)),
                    name: ">r".into(),
                });
                conv.push_front(NamedStdRuntimeWord {
                    word: RuntimeWord::Verb(BuiltinToken::new(crate::builtins::bi_retstk_push)),
                    name: ">r".into(),
                });

                // The Minus One here accounts for the addition of the CRJ. We should not loop back to
                // the double `>r`s, as those only happen once at the top of the loop.
                conv.push_back(NamedStdRuntimeWord {
                    word: RuntimeWord::CondRelativeJump {
                        offset: -1 * len as i32 - 1,
                        jump_on: false,
                    },
                    name: "CRJ".into(),
                });

                let conv: Vec<NamedStdRuntimeWord> = conv.into_iter().collect();
                ret.extend(conv);
            }
            Chunk::Token(tok) => {
                ret.push(if let Some(bi) = dict.bis.get(&tok).cloned() {
                    NamedStdRuntimeWord {
                        name: tok,
                        word: RuntimeWord::Verb(bi.clone()),
                    }
                } else if dict.data.contains_key(&tok) {
                    NamedStdRuntimeWord {
                        word: RuntimeWord::VerbSeq(VerbSeqInner::from_word(tok.clone())),
                        name: tok,
                    }
                } else if let Some(num) = parse_num(&tok) {
                    NamedStdRuntimeWord {
                        word: RuntimeWord::LiteralVal(num),
                        name: format!("LIT({})", num),
                    }
                } else {
                    panic!("{:?}", tok);
                    // return Err(Error::InternalError);
                });
            }
            Chunk::Comment { .. } => {
                // Nothing to do for comments
            }
        }

        ret
    }
}

use std::collections::VecDeque;

fn muncher(data: &mut VecDeque<String>) -> Vec<Chunk> {
    let mut chunks = vec![];
    loop {
        let next = if let Some(t) = data.pop_front() {
            t
        } else {
            break;
        };

        match next.as_str() {
            "do" => {
                chunks.push(munch_do(data));
            }
            "if" => {
                chunks.push(munch_if(data));
            }
            "(" => {
                chunks.push(Chunk::Comment { contents: munch_comment(data) });
            }
            _ => chunks.push(Chunk::Token(next)),
        }
    }

    chunks
}

fn munch_comment(data: &mut VecDeque<String>) -> Vec<String> {
    let mut contents = vec![];
    loop {
        let next = if let Some(t) = data.pop_front() {
            t
        } else {
            break;
        };

        match next.as_str() {
            "(" => {
                contents.extend(munch_comment(data));
            }
            ")" => {
                return contents;
            }
            _ => {
                contents.push(next);
            }
        }
    }

    // We... shouldn't get here. This means we never found our ")" after the "("
    todo!()
}

fn munch_do(data: &mut VecDeque<String>) -> Chunk {
    let mut chunks = vec![];
    loop {
        let next = if let Some(t) = data.pop_front() {
            t
        } else {
            break;
        };

        match next.as_str() {
            "do" => {
                chunks.push(munch_do(data));
            }
            "if" => {
                chunks.push(munch_if(data));
            }
            "loop" => return Chunk::DoLoop { do_body: chunks },
            _ => chunks.push(Chunk::Token(next)),
        }
    }

    // We... shouldn't get here. This means we never found our "loop" after the "do"
    todo!()
}

fn munch_if(data: &mut VecDeque<String>) -> Chunk {
    let mut chunks = vec![];
    loop {
        let next = if let Some(t) = data.pop_front() {
            t
        } else {
            break;
        };

        match next.as_str() {
            "do" => {
                chunks.push(munch_do(data));
            }
            "if" => {
                chunks.push(munch_if(data));
            }
            "then" => return Chunk::IfThen { if_body: chunks },
            "else" => {
                return munch_else(data, chunks);
            }
            _ => chunks.push(Chunk::Token(next)),
        }
    }

    // We... shouldn't get here. This means we never found our "then"/"else" after the "if"
    todo!()
}

fn munch_else(data: &mut VecDeque<String>, if_body: Vec<Chunk>) -> Chunk {
    let mut chunks = vec![];
    loop {
        let next = if let Some(t) = data.pop_front() {
            t
        } else {
            break;
        };

        match next.as_str() {
            "do" => {
                chunks.push(munch_do(data));
            }
            "if" => {
                chunks.push(munch_if(data));
            }
            "then" => {
                return Chunk::IfElseThen {
                    if_body,
                    else_body: chunks,
                }
            }
            _ => chunks.push(Chunk::Token(next)),
        }
    }

    // We... shouldn't get here. This means we never found our "then" after the "else"
    todo!()
}