-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
158 lines (145 loc) · 4.06 KB
/
main.rs
File metadata and controls
158 lines (145 loc) · 4.06 KB
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
use crate::array_gc::alloc_array;
use crate::data::Data;
use crate::data::FALSE;
use crate::data::NULL;
use crate::display::format_data;
use crate::instr::Instr;
use crate::instr::LibFunc;
use crate::parser::parse;
use crate::parser_data::DynamicLibFn;
use crate::util::likely;
use crate::util::unlikely;
use inline_colorization::*;
use mimalloc::MiMalloc;
use parser::*;
use std::fs;
use std::time::Instant;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
#[path = "./vm/array_gc.rs"]
mod array_gc;
#[path = "./benchmark.rs"]
mod benchmark;
#[path = "./data.rs"]
mod data;
#[path = "./util/display.rs"]
mod display;
#[path = "./util/errors.rs"]
mod errors;
#[path = "./parser/functions/functions.rs"]
mod functions;
#[path = "./instr.rs"]
mod instr;
#[path = "./parser/functions/method_calls.rs"]
mod method_calls;
#[path = "./parser/parser.rs"]
mod parser;
#[path = "./parser/parser_data.rs"]
mod parser_data;
#[path = "./vm/string_gc.rs"]
mod string_gc;
#[path = "./tests.rs"]
#[cfg(test)]
mod tests;
#[path = "./type_system.rs"]
mod type_system;
#[path = "./util/util.rs"]
mod util;
#[path = "./vm/vm.rs"]
mod vm;
/// Live long and prosper
fn main() {
std::panic::set_hook(Box::new(|info| {
eprintln!("{color_red}SPOCK ERROR{color_reset}\n{info}");
}));
let args: Vec<String> = std::env::args().collect();
if unlikely(args.len() == 1) {
println!(
"{}\n\nSpock is a fast, statically-typed interpreted language that aims to combine Rust-like syntax with Python's ease-of-use.\n\nUsage:\n spock -v\n spock file.spock [--debug] [--bench [--verbose]]",
util::SPOCK_LOGO
);
return;
}
if unlikely(args.iter().any(|x| x == "--version" || x == "-v")) {
if unlikely(args.len() > 2) {
eprintln!(
"{color_red}SPOCK ERROR{color_reset}\nInvalid arguments\nUsage:\n spock -v\n spock file.spock [--debug] [--bench [--verbose]]"
);
return;
}
println!("Spock {}", env!("CARGO_PKG_VERSION"));
return;
}
let debug = args.iter().any(|x| x == "--debug");
if args.iter().any(|x| x == "--bench") {
crate::benchmark::benchmark();
return;
}
#[cfg(debug_assertions)]
let filename = args.get(1).map(String::as_str).unwrap_or("test.spock");
#[cfg(not(debug_assertions))]
let filename = &args.get(1).unwrap_or_else(|| {
println!("{}", util::SPOCK_LOGO);
std::process::exit(0);
});
let contents = fs::read_to_string(filename).unwrap_or_else(|_| {
eprintln!(
"--------------\n{color_red}SPOCK RUNTIME ERROR:{color_reset}\nCannot read {color_bright_red}{style_bold}{filename}{style_reset}{color_reset}\n--------------",
);
std::process::exit(1);
});
if likely(!debug) {
let (
instructions,
mut registers,
mut arrays,
instr_src,
fn_registers,
fn_dyn_libs,
allocated_arg_count,
allocated_call_depth,
sources,
) = parse(&contents, filename, false);
vm::execute(
&instructions,
&mut registers,
&mut arrays,
&instr_src,
&sources,
&fn_registers,
&fn_dyn_libs,
allocated_arg_count,
allocated_call_depth,
);
return;
}
let now = Instant::now();
let (
instructions,
mut registers,
mut arrays,
instr_src,
fn_registers,
fn_dyn_libs,
allocated_arg_count,
allocated_call_depth,
sources,
) = parse(&contents, filename, false);
println!("COMPILATION TIME: {:.2?}", now.elapsed());
let now = Instant::now();
vm::execute(
&instructions,
&mut registers,
&mut arrays,
&instr_src,
&sources,
&fn_registers,
&fn_dyn_libs,
allocated_arg_count,
allocated_call_depth,
);
println!(
"EXECUTION TIME: {:.3}ms",
now.elapsed().as_nanos() / 1000000
);
}