~starkingdoms/starkingdoms

ref: 0216bf5e52ce141f5a1f5eaaf3e699eb07fed0b1 starkingdoms/kabel/src/main.rs -rw-r--r-- 1.7 KiB
0216bf5e — ghostly_zsh fix issue with not ppopping to clear stack with an expression statement 1 year, 4 months ago
                                                                                
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
//use std::{env, fs};

use std::{env, fs};

use kabel::{debug::{debug_ast, debug_bytecode, debug_stack, debug_token_array}, run_codegen, run_lexer, run_parser, run_semantic_analysis};

fn main() {
    /*let args: Vec<String> = env::args().collect();
    let program =
        fs::read_to_string(args[1].clone()).unwrap();*/

    let program =
"2+2;
".to_string();

    let mut output = "".to_string();

    let lexer = run_lexer(program.to_string());

    for error in lexer.errors.clone() {
        output += &error.to_string();
        output += "\n";
    }
    //output += &format!("{:?}", lexer.output);
    if lexer.errors.len() != 0 || lexer.output.len() == 0 {
        println!("{}", output);
        return;
    }
    //output += &debug_token_array(lexer.output.clone());

    let (ast, parser) = run_parser(program.to_string(), lexer.output);

    for error in parser.errors.clone() {
        output += &error.to_string();
        output += "\n";
    }
    if parser.errors.len() != 0 {
        println!("{}", output);
        return;
    }
    output += &debug_ast(ast.clone(), 0);
    output += "\n\n";
    //output += &format!("{:#?}", ast);
    let analyzer = run_semantic_analysis(program.to_string(), ast.clone());
    for error in analyzer.errors.clone() {
        output += &error.to_string();
        output += "\n";
    }
    if analyzer.errors.len() != 0 {
        println!("{}", output);
        return;
    }

    let codegen = run_codegen(program, ast);

    let mut vm = codegen.vm;
    output += &debug_bytecode(&vm.chunk);
    output += "\n";
    match vm.run(&mut output) {
        Ok(()) => {}
        Err(e) => output += &e.to_string(),
    }

    println!("{}", output);
}