laspa 0.2.0

A simple Lisp-like language built with Rust
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
// #![warn(missing_docs)]

/*!
# läspa
A simple programming language written in Rust. It's a Lisp-like language, but with a more
traditional syntax. Each element of the language is designed to be as easy to implement as
possible (hence the [RPN](https://en.wikipedia.org/wiki/Reverse_Polish_notation)), so that it
can be used as a learning tool for people who want to learn how to write their own programming
language. The language itself is not very useful, but it's a good starting
point for learning how to write a programming language.

## Example
```ignore
fn collatz (n)
    while > n 1
        if == % n 2 0
            := n / n 2
        else
            := n + * 3 n 1
        end
        print n
    end
    return n
end

let x 10;
return collatz (x)
```

## Syntax
The syntax is very simple. Each statement is separated by a newline or a semicolon. Comments are
denoted by `//`. The language is **whitespace sensitive**, but indentation is **not** important. The language
is also case sensitive. The language is also **RPN** (Reverse Polish Notation), so the operator
comes after the operands. For example, `+ 1 2` would equal `3`. 

### Code Blocks and Functions
Every body of code must end (loops, if statements, functions, etc.)
with the keyword `end`. Every function must start with `fn` and end with `end`. The parameters of a function are in the form `(param1 param2 ...)`.

**Warning**: There is little error handling, so if you make a mistake, the program will panic, or the result will be incorrect.

## Usage
The easiest way to use the language is to use the [`Interpreter`] struct. This will interpret the
language and return the result. This is the default compiler for the language. In the future, there
will be more compilers, such as an LLVM compiler or machine code compiler.

## Source Files
Source files are just text files with the `.laspa` extension. You can use any extension you want,
but `.laspa` is the default. You can use the [`Interpreter::from_file`] method to read from a file.

```rust
use laspa::{Interpreter, Compile};

let result = Interpreter::from_source("return + 1 2;");
assert_eq!(result, 3.0);
```
 */

use lazy_static::lazy_static;
use regex::{Regex, Split};
use std::{collections::HashMap, str::SplitWhitespace};

/// The default number type. Every number is a [`f64`] number for simplicity.
#[derive(Debug, PartialEq, Clone)]
pub struct Number(pub f64);

impl Number {
    /// Create a new number from a string. This will return an error if the string is not a valid
    pub fn new(s: &str) -> Result<Self, String> {
        match s.parse::<f64>() {
            Ok(n) => Ok(Self(n)),
            Err(_) => Err(format!("Invalid number: {s}")),
        }
    }
}

/// The default operator type. This is used for arithmetic and comparison operations.
#[derive(Debug, PartialEq, Clone)]
pub enum Op {
    Add,
    Sub,
    Mul,
    Div,
    /// Greater than
    Gt,
    /// Less than
    Lt,
    /// Modulo
    Mod,
    /// Equal to
    Eqt
}

impl Op {
    /// Create a new operator from a string. This will panic if the string is not a valid operator.
    pub fn new(s: &str) -> Self {
        match s {
            "+" => Self::Add,
            "-" => Self::Sub,
            "*" => Self::Mul,
            "/" => Self::Div,
            ">" => Self::Gt,
            "<" => Self::Lt,
            "%" => Self::Mod,
            "==" => Self::Eqt,
            _ => panic!("Invalid operator"),
        }
    }
}

/// The default binary expression type. This is used for arithmetic and comparison operations (e.g. `+ 1 2` would equal `3`).
#[derive(Debug, PartialEq, Clone)]
pub struct BinaryExpr {
    pub op: Op,
    pub lhs: Vec<Node>,
    pub rhs: Vec<Node>,
}

/// The default bind expression type. This is used to bind a value to a variable (e.g. `let x 10` binding the number `10` to `x`).
#[derive(Debug, PartialEq, Clone)]
pub struct BindExpr {
    pub name: String,
    pub value: Vec<Node>,
}

/// The default return expression type. This is used to return a value from a function. If this is not used, the last value in the function will be returned.
#[derive(Debug, PartialEq, Clone)]
pub struct ReturnExpr {
    pub value: Vec<Node>,
}

/// The default mutate expression type. This is used to mutate a variable (e.g. `:= x 10` setting the value of `x` to `10`).
/// Variables can only be mutable.
#[derive(Debug, PartialEq, Clone)]
pub struct MutateExpr {
    pub name: String,
    pub value: Vec<Node>,
}

/// The default while expression type. This is used to create a while loop (e.g. `while < x 10` will loop while `x` is less than `10`).
#[derive(Debug, PartialEq, Clone)]
pub struct WhileExpr {
    pub condition: Vec<Node>,
    pub body: Vec<Node>,
}

/// The default if expression type. This is used to create an if statement (e.g. `if < x 10` will run the code in the if statement if `x` is less than `10`).
/// The else statement is optional.
#[derive(Debug, PartialEq, Clone)]
pub struct IfExpr {
    pub condition: Vec<Node>,
    pub body: Vec<Node>,
    pub else_body: Vec<Node>,
}

/// The default function expression type. This is used to create a function (e.g. `fn sum (x y);return + x y;end` will create a function called `sum` that takes two arguments, `x` and `y`, and returns the sum of the two).
#[derive(Debug, PartialEq, Clone)]
pub struct FnExpr {
    pub name: String,
    pub args: Vec<Node>,
    pub body: Vec<Node>,
}

/// The default function call expression type. This is used to call a function (e.g. `sum (1 2)` will call the function `sum` with the arguments `1` and `2`).
#[derive(Debug, PartialEq, Clone)]
pub struct FnCallExpr {
    pub name: String,
    pub args: Vec<Node>,
}

/// The default print expression type. This is used to print a value to stdout (e.g. `print 1` will print `1` to stdout).
#[derive(Debug, PartialEq, Clone)]
pub struct PrintStdoutExpr {
    pub value: Vec<Node>,
}

/// The default node type. This is used to represent every element of the language. This is used to create an abstract syntax tree (AST).
#[derive(Debug, PartialEq, Clone)]
pub enum Node {
    Number(Number),
    BinaryExpr(BinaryExpr),
    BindExpr(BindExpr),
    Variable(String),
    ReturnExpr(ReturnExpr),
    MutateExpr(MutateExpr),
    WhileExpr(WhileExpr),
    IfExpr(IfExpr),
    FnExpr(FnExpr),
    FnCallExpr(FnCallExpr),
    PrintStdoutExpr(PrintStdoutExpr),
}

lazy_static! {
    static ref RE: Regex = Regex::new(r"[;\n]").unwrap();
}

/// Lex a string into tokens. This will split the string into tokens, which can then be parsed into an AST.
pub fn lex(s: &str) -> regex::Split<'static, '_> {
    RE.split(s)
}

/// Parse tokens into an AST. This will parse a string of tokens into an AST, which can then be evaluated.
pub fn parse(tokens: &mut Split<'static, '_>, functions: &mut HashMap<String, FnExpr>) -> Vec<Node> {
    let mut nodes = Vec::new();
    while let Some(token) = tokens.next() {
        // println!("token: {}", token);
        if token.trim() == "end" {
            break;
        }

        if let Ok(mut new_nodes) = parse_sentence(&mut token.split_whitespace(), functions) {
            nodes.append(&mut new_nodes);
        }

        if let Some(Node::WhileExpr(e)) = nodes.last_mut() {
            if e.body.is_empty() {
                e.body = parse(tokens, functions);
            }
        }

        if let Some(Node::IfExpr(e)) = nodes.last_mut() {
            if e.body.is_empty() {
                let body = parse(tokens, functions);
                let mut body = body.split(|n| n == &Node::Variable("else".to_string()));
                e.body = body.next().unwrap().to_vec();
                e.else_body = body.next().unwrap_or(&Vec::new()).to_vec();
            }
        }

        if let Some(Node::FnExpr(e)) = nodes.last_mut() {
            if e.body.is_empty() {
                e.body = parse(tokens, functions);
            }
        }
        // println!("nodes: {:?}", nodes)
    }
    nodes
}

/// Parse a sentence into an AST. This will parse a sentence into an AST, which can then be evaluated.
/// Sentences are separated by newlines or `;` as provided by the regex in the lexer.
fn parse_sentence(tokens: &mut SplitWhitespace, functions: &mut HashMap<String, FnExpr>) -> Result<Vec<Node>, String> {
    let mut nodes = Vec::new();
    match tokens.next() {
        Some(t) => match t {
            "+" | "-" | "*" | "/" | ">" | "<" | "%" | "==" => {
                nodes.push(Node::BinaryExpr(BinaryExpr {
                    op: Op::new(t),
                    lhs: parse_sentence(tokens, functions).unwrap(),
                    rhs: parse_sentence(tokens, functions).unwrap(),
                }));
            }

            "let" => {
                let name = tokens.next().unwrap();
                let value = parse_sentence(tokens, functions).unwrap();
                nodes.push(Node::BindExpr(BindExpr {
                    name: name.to_string(),
                    value,
                }));
            }

            "//" => {
                return Ok(nodes);
            }

            "return" => {
                nodes.push(Node::ReturnExpr(ReturnExpr {
                    value: parse_sentence(tokens, functions).unwrap(),
                }));
            }

            ":=" => {
                let name = tokens.next().unwrap();
                let value = parse_sentence(tokens, functions).unwrap();
                nodes.push(Node::MutateExpr(MutateExpr {
                    name: name.to_string(),
                    value,
                }));
            }

            "while" => {
                let condition = parse_sentence(tokens, functions).unwrap();
                let body = Vec::new();
                nodes.push(Node::WhileExpr(WhileExpr { condition, body }));
            }

            "if" => {
                let condition = parse_sentence(tokens, functions).unwrap();
                let body = Vec::new();
                let else_body = Vec::new();
                nodes.push(Node::IfExpr(IfExpr {
                    condition,
                    body,
                    else_body,
                }));
            }

            "fn" => {
                let name = tokens.next().unwrap();
                let args = parse_args(tokens.collect::<Vec<_>>().join(" "), functions);
                let body = Vec::new();
                let expr = FnExpr {
                    name: name.to_string(),
                    args: args,
                    body,
                };
                functions.insert(name.to_string(), expr.clone());
                nodes.push(Node::FnExpr(expr));
            }

            "print" => {
                nodes.push(Node::PrintStdoutExpr(PrintStdoutExpr {
                    value: parse_sentence(tokens, functions).unwrap(),
                }));
            }

            _ => {
                if let Some(_f) = functions.get(t) {
                    let args = parse_args(tokens.collect::<Vec<_>>().join(" "), functions);
                    nodes.push(Node::FnCallExpr(FnCallExpr {
                        name: t.to_string(),
                        args,
                    }));
                } else {
                    match Number::new(t) {
                        Ok(n) => nodes.push(Node::Number(n)),
                        Err(_) => nodes.push(Node::Variable(t.to_string())),
                    }
                }
            }
        },

        None => return Err("No tokens found".to_string()),
    }

    Ok(nodes)
}

fn parse_args(tokens: String, functions: &mut HashMap<String, FnExpr>) -> Vec<Node> {
    let mut nodes = Vec::new();
    let mut tokens = tokens;
    if !tokens.starts_with("(") && !tokens.ends_with(")") {
        panic!("Invalid function arguments. Must be in the form (arg1 arg2 ...)");
    }

    tokens.remove(0);
    tokens.pop();

    let mut tokens = tokens.split_whitespace();
    while let Some(token) = tokens.next() {
        if let Ok(mut new_nodes) = parse_sentence(&mut token.split_whitespace(), functions) {
            nodes.append(&mut new_nodes);
        }
    }

    nodes
}

/// Evaluate an AST. This will evaluate an AST and return the result. All variables are in the global scope.
/// This is essentially the interpreter for the language.
pub fn eval(ast: &Vec<Node>, globals: &mut HashMap<String, f64>, functions: &mut HashMap<String, FnExpr>) -> f64 {
    let mut return_val: Option<f64> = None;
    let mut last_val: f64 = 0.0;

    for node in ast {
        last_val = match node {
            Node::Number(n) => n.0,
            Node::BinaryExpr(e) => {
                let lhs = eval(&e.lhs, globals, functions);
                let rhs = eval(&e.rhs, globals, functions);

                match e.op {
                    Op::Add => lhs + rhs,
                    Op::Sub => lhs - rhs,
                    Op::Mul => lhs * rhs,
                    Op::Div => lhs / rhs,
                    Op::Gt => (lhs > rhs) as i32 as f64,
                    Op::Lt => (lhs < rhs) as i32 as f64,
                    Op::Mod => lhs % rhs,
                    Op::Eqt => (lhs == rhs) as i32 as f64,
                }
            }
            Node::BindExpr(e) => {
                let value = eval(&e.value, globals, functions);
                globals.insert(e.name.clone(), value);
                value
            }
            Node::Variable(v) => match globals.get(v) {
                Some(n) => *n,
                None => panic!("Variable not found: {v}"),
            },
            Node::ReturnExpr(e) => {
                return_val = Some(eval(&e.value, globals, functions));
                0.0 // This doesn't matter, because we'll check return_val at the end
            }
            Node::MutateExpr(e) => {
                let value = eval(&e.value, globals, functions);
                if let Some(n) = globals.get_mut(&e.name) {
                    *n = value;
                } else {
                    panic!("Variable not found: {}", e.name);
                }
                value
            }
            Node::WhileExpr(e) => {
                while eval(&e.condition, globals, functions) != 0.0 {
                    eval(&e.body, globals, functions);
                }
                0.0
            }
            Node::IfExpr(e) => {
                if eval(&e.condition, globals, functions) != 0.0 {
                    eval(&e.body, globals, functions)
                } else {
                    eval(&e.else_body, globals, functions)
                }
            }
            Node::FnExpr(e) => {
                functions.insert(e.name.clone(), e.clone());
                0.0
            }
            Node::FnCallExpr(e) => {
                if let Some(f) = functions.get(&e.name).cloned() {
                    let mut local_scope = HashMap::new();
                    for (param, arg) in f.args.iter().zip(&e.args) {
                        let v = eval(&vec![arg.clone()], globals, functions);
                        let k = match param {
                            Node::Variable(v) => v,
                            _ => panic!("Invalid function argument"),
                        };
                        local_scope.insert(k.clone(), v);
                    }
                    eval(&f.body, &mut local_scope, functions)
                } else {
                    panic!("Function not found: {}", e.name);
                }
            }
            Node::PrintStdoutExpr(e) => {
                let value = eval(&e.value, globals, functions);
                println!("{}", value);
                0.0
            }
        };
    }

    return_val.unwrap_or(last_val)
}

/// The default trait for compiling a language. This is used to compile a language from a specific source.
/// This trait can be implemented for any output: llvm, interpreter, etc.
pub trait Compile {
    /// The output type of the compiler. Varies depending on the compiler.
    type Output;

    /// Compile an AST into the output type.
    fn from_ast(nodes: Vec<Node>) -> Self::Output;

    /// Compile a string into the output type.
    fn from_source(source: &str) -> Self::Output {
        let mut tokens = lex(source);
        // println!("tokens: {:?}", lex(source).collect::<Vec<_>>());
        let nodes = parse(&mut tokens, &mut HashMap::new());
        println!("ast: {:?}", nodes);
        Self::from_ast(nodes)
    }

    /// Compile a file into the output type. Supply the crate-relative path to the file.
    fn from_file(path: &str) -> Self::Output {
        let source = std::fs::read_to_string(path).unwrap();
        Self::from_source(&source)
    }
}

/// The default interpreter.
pub struct Interpreter;

impl Compile for Interpreter {
    type Output = f64;

    fn from_ast(nodes: Vec<Node>) -> Self::Output {
        eval(&nodes, &mut HashMap::new(), &mut HashMap::new())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_number() {
        assert_eq!(Number::new("1.0").unwrap(), Number(1.0));
        assert_eq!(Number::new("4").unwrap(), Number(4.0));
    }

    #[test]
    fn parse_add() {
        assert_eq!(Op::new("+"), Op::Add);
    }

    #[test]
    fn parse_sub() {
        assert_eq!(Op::new("-"), Op::Sub);
    }

    #[test]
    fn parse_mul() {
        assert_eq!(Op::new("*"), Op::Mul);
    }

    #[test]
    fn parse_div() {
        assert_eq!(Op::new("/"), Op::Div);
    }

    #[test]
    fn parse_gt() {
        assert_eq!(Op::new(">"), Op::Gt);
    }

    #[test]
    fn parse_lt() {
        assert_eq!(Op::new("<"), Op::Lt);
    }

    #[test]
    fn parse_expr() {
        let mut tokens = lex("+ * -2 3 - 2 3.5");
        let nodes = parse(&mut tokens, &mut HashMap::new());
        assert_eq!(
            nodes,
            vec![Node::BinaryExpr(BinaryExpr {
                op: Op::Add,
                lhs: vec![Node::BinaryExpr(BinaryExpr {
                    op: Op::Mul,
                    lhs: vec![Node::Number(Number(-2.0))],
                    rhs: vec![Node::Number(Number(3.0))],
                })],
                rhs: vec![Node::BinaryExpr(BinaryExpr {
                    op: Op::Sub,
                    lhs: vec![Node::Number(Number(2.0))],
                    rhs: vec![Node::Number(Number(3.5))],
                })],
            }),]
        )
    }

    #[test]
    fn eval_expr() {
        let mut tokens = lex("return + * -2 3 - 2 3.5");
        let nodes = parse(&mut tokens, &mut HashMap::new());
        assert_eq!(eval(&nodes, &mut HashMap::new(), &mut HashMap::new()), -7.5);
    }

    #[test]
    fn interpret() {
        assert_eq!(Interpreter::from_source("+ * -2 3 - 2 3.5"), -7.5);
    }

    #[test]
    fn define_variable() {
        assert_eq!(
            Interpreter::from_source(
                r#"
            let x 1
        "#
            ),
            1.0
        );
    }

    #[test]
    fn variable_arithmetic() {
        assert_eq!(
            Interpreter::from_source(
                "let x 2;
        let y 1;
        + x y;"
            ),
            3.0
        );
    }

    #[test]
    fn variable_arithmetic_complex() {
        assert_eq!(
            Interpreter::from_source(
                "let x 2;
        let y 1;
        let z + x * y 2;
        z;"
            ),
            4.0
        );
    }

    #[test]
    fn return_only() {
        assert_eq!(Interpreter::from_source("+ 2 3;return 1;"), 1.0);
    }

    #[test]
    fn while_loop() {
        assert_eq!(
            Interpreter::from_source(
                r#"
        let x 0;
        // let y 0;
        
        while < x 1000
            let i 0;
            while < i 100
                := x + x 1;
                := i + i 1;
            end
        end
        
        return + x i;
        "#
            ),
            1100.0
        );
    }

    #[test]
    fn if_else() {
        assert_eq!(
            Interpreter::from_source(
                r#"
        let x 0;
        if < x 1
            return 1;
        else
            return 2;
        end
        "#
            ),
            1.0
        );
    }

    #[test]
    fn only_if() {
        assert_eq!(
            Interpreter::from_source(
                r#"
                let x 10;
                let y 2
                
                if < x y
                    return y
                end
                
                return x
        "#
            ),
            10.0
        );
    }

    #[test]
    fn function_call() {
        assert_eq!(
            Interpreter::from_source(
                r#"
                fn sum (x y)
                    return + x y;
                end

                let i 10;
                let d 2;

                let z sum (i d);

                return z
        "#
        ),
            12.0
        );
    }

    #[test]
    fn collatz_conjecture() {
        assert_eq!(
            Interpreter::from_source(
                r#"
                fn collatz (n)
                    while > n 1
                        if == % n 2 0
                            := n / n 2
                        else
                            := n + * 3 n 1
                        end
                        print n
                    end
                    return n
                end

                return collatz (123)
        "#
        ),
            1.0
        );
    }

    #[test]
    fn read_from_file() {
        assert_eq!(Interpreter::from_file("examples/test.laspa"), 1.0);
    }
}