Improving compiler
This commit is contained in:
+22
-16
@@ -4,9 +4,8 @@ use serde::Deserialize;
|
|||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Config {
|
struct Config {
|
||||||
entry: String,
|
src: String,
|
||||||
out_dir: String,
|
output: String,
|
||||||
script: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build() {
|
pub fn build() {
|
||||||
@@ -22,10 +21,22 @@ pub fn build() {
|
|||||||
// 2. load config
|
// 2. load config
|
||||||
let config = load_config(&root);
|
let config = load_config(&root);
|
||||||
|
|
||||||
let entry_path = root.join(&config.entry);
|
let src = root.join(&config.src);
|
||||||
let out_dir = root.join(&config.out_dir);
|
|
||||||
|
|
||||||
println!(" → entry: {}", entry_path.display());
|
for entry in fs::read_dir(src).unwrap() {
|
||||||
|
if let Ok(entry) = entry {
|
||||||
|
let entry_path = root.join(&entry.path());
|
||||||
|
let out_dir = root.join(&config.output);
|
||||||
|
let file_name = entry.file_name();
|
||||||
|
let file_name = file_name.to_string_lossy();
|
||||||
|
|
||||||
|
let script = if file_name.ends_with(".ms") {
|
||||||
|
true
|
||||||
|
} else if file_name.ends_with(".mist") {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
// 3. read entry file
|
// 3. read entry file
|
||||||
let source = match fs::read_to_string(&entry_path) {
|
let source = match fs::read_to_string(&entry_path) {
|
||||||
@@ -36,9 +47,7 @@ pub fn build() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
println!(" → parsing...");
|
let parser_result = if script {
|
||||||
|
|
||||||
let parser_result = if config.script {
|
|
||||||
parser::script_parser::parse(&source).map_err(|e| e.to_string())
|
parser::script_parser::parse(&source).map_err(|e| e.to_string())
|
||||||
} else {
|
} else {
|
||||||
parser::parse(&source).map_err(|e| e.to_string())
|
parser::parse(&source).map_err(|e| e.to_string())
|
||||||
@@ -52,31 +61,28 @@ pub fn build() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
println!(" → type checking...");
|
|
||||||
|
|
||||||
semantic::walk_ast(semantic::scope::Scope::from_top(&root, &ast), &mut ast);
|
semantic::walk_ast(semantic::scope::Scope::from_top(&root, &ast), &mut ast);
|
||||||
|
|
||||||
println!(" → generating Go code...");
|
|
||||||
|
|
||||||
let mut gc = codegen::GoCodegen::new();
|
let mut gc = codegen::GoCodegen::new();
|
||||||
let output = gc.generate(&ast);
|
let output = gc.generate(&ast);
|
||||||
|
|
||||||
// 4. ensure build dir
|
|
||||||
if let Err(e) = fs::create_dir_all(&out_dir) {
|
if let Err(e) = fs::create_dir_all(&out_dir) {
|
||||||
eprintln!("error: failed to create build dir\n {}", e);
|
eprintln!("error: failed to create build dir\n {}", e);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let out_file = out_dir.join("main.go");
|
let out_file =
|
||||||
|
out_dir.join(file_name.replace(if script { ".ms" } else { ".mist" }, ".go"));
|
||||||
|
|
||||||
if let Err(e) = fs::write(&out_file, output) {
|
if let Err(e) = fs::write(&out_file, output) {
|
||||||
eprintln!("error: failed to write output\n {}", e);
|
eprintln!("error: failed to write output\n {}", e);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let elapsed = start.elapsed();
|
let elapsed = start.elapsed();
|
||||||
|
|
||||||
println!(" ✓ built {}", out_file.display());
|
|
||||||
println!("build finished in {:.2?}", elapsed);
|
println!("build finished in {:.2?}", elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -1,5 +1,4 @@
|
|||||||
{
|
{
|
||||||
"entry": "src/main.ms",
|
"src": "src",
|
||||||
"out_dir": "build",
|
"output": "build"
|
||||||
"script": false
|
|
||||||
}
|
}
|
||||||
+2
-3
@@ -2,7 +2,6 @@ package main;
|
|||||||
|
|
||||||
import "fmt";
|
import "fmt";
|
||||||
|
|
||||||
void main() {
|
function main() {
|
||||||
let a = 0;
|
|
||||||
let b = a + 10;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user