~starkingdoms/starkingdoms

ref: 426c4c005ea227b6953945c8bacb92575f5e392d starkingdoms/kabel/src/main.rs -rw-r--r-- 1.8 KiB
426c4c00 — ghostly_zsh basic break and continue but it doesn't quite work 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
66
67
68
69
70
71
72
73
74
//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 =
"
for(var i = 0; i < 5; i++) {
    print i;
    continue;
    print i;
}
".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 (ast, 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;
    }
    output += &debug_ast(ast.clone(), 0);
    output += "\n";

    let codegen = run_codegen(program.to_string(), ast);

    let mut vm = codegen.vm;
    output += &debug_bytecode(&vm);
    output += "\n";

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

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