Working explicit module system
This commit is contained in:
@@ -3,7 +3,10 @@ use std::{
|
|||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use mist_parser::{ast::TopLevel, error::ParseError};
|
use mist_parser::{
|
||||||
|
ast::{Identifier, TopLevel, Visibility},
|
||||||
|
error::ParseError,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn build(root: &PathBuf) {
|
pub fn build(root: &PathBuf) {
|
||||||
let src_dir = root.join("src");
|
let src_dir = root.join("src");
|
||||||
@@ -112,7 +115,7 @@ pub fn transpile_file(path: &Path, output_path: &Path) -> Result<(), String> {
|
|||||||
let source = fs::read_to_string(path)
|
let source = fs::read_to_string(path)
|
||||||
.map_err(|e| format!("failed to read file {}: {}", path.display(), e))?;
|
.map_err(|e| format!("failed to read file {}: {}", path.display(), e))?;
|
||||||
|
|
||||||
let parser_result = mist_parser::parse(&source, true).map_err(|e| match e {
|
let parser_result = mist_parser::parse(&source).map_err(|e| match e {
|
||||||
ParseError::Ast(e) => {
|
ParseError::Ast(e) => {
|
||||||
// Using components carefully to prevent out-of-bounds or zero layout crashes
|
// Using components carefully to prevent out-of-bounds or zero layout crashes
|
||||||
let start_pos = e.span.start_pos().line_col();
|
let start_pos = e.span.start_pos().line_col();
|
||||||
@@ -146,10 +149,12 @@ pub fn transpile_file(path: &Path, output_path: &Path) -> Result<(), String> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn transpile_text<'a>(source: &'a str) -> Result<String, ParseError<'a, Vec<TopLevel>>> {
|
pub fn transpile_text<'a>(
|
||||||
|
source: &'a str,
|
||||||
|
) -> Result<String, ParseError<'a, (Option<(Visibility, Identifier)>, Vec<TopLevel>)>> {
|
||||||
let mut gc = mist_codegen::RustCodegen::new();
|
let mut gc = mist_codegen::RustCodegen::new();
|
||||||
|
|
||||||
Ok(gc.generate(mist_parser::parse(&source, true)?.1))
|
Ok(gc.generate(mist_parser::parse(&source)?.1))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn should_skip(source: &Path, output: &Path) -> bool {
|
fn should_skip(source: &Path, output: &Path) -> bool {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use std::env;
|
|||||||
|
|
||||||
use mist_parser::error;
|
use mist_parser::error;
|
||||||
use crate::modules;
|
use crate::modules;
|
||||||
|
use mist_codegen::GetRust;
|
||||||
|
|
||||||
pub fn transpile() PathBuf {
|
pub fn transpile() PathBuf {
|
||||||
let start = time::Instant::now();
|
let start = time::Instant::now();
|
||||||
@@ -68,16 +69,16 @@ impl modules::Module {
|
|||||||
|
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
transpile_file(&self.path, &output_file, &output);
|
|
||||||
}
|
|
||||||
|
|
||||||
format!("pub mod {};\n", self.name)
|
format!("pub mod {};\n", self.name)
|
||||||
|
} else {
|
||||||
|
transpile_file(&self.path, output_file, &output)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transpile_file(path *PathBuf, output_file *PathBuf, mod_decl *str) {
|
fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String {
|
||||||
let source = match (fs::read_to_string(path)) {
|
let source = match (fs::read_to_string(&path)) {
|
||||||
Ok[s] => s;
|
Ok[s] => s;
|
||||||
Err[e] => {
|
Err[e] => {
|
||||||
eprintln!("error: failed to read file {}\n {}", path.display(), e);
|
eprintln!("error: failed to read file {}\n {}", path.display(), e);
|
||||||
@@ -88,9 +89,7 @@ fn transpile_file(path *PathBuf, output_file *PathBuf, mod_decl *str) {
|
|||||||
|
|
||||||
let mut gc = mist_codegen::RustCodegen::new();
|
let mut gc = mist_codegen::RustCodegen::new();
|
||||||
|
|
||||||
let is_master = path.file_name().unwrap().to_str().unwrap() == "main.mist";
|
let parsed = match (mist_parser::parse(&source)) {
|
||||||
|
|
||||||
let [[vis, name], ast] = match (mist_parser::parse(&source, is_master)) {
|
|
||||||
Ok[ast] => ast;
|
Ok[ast] => ast;
|
||||||
Err[e] => {
|
Err[e] => {
|
||||||
match (e) {
|
match (e) {
|
||||||
@@ -122,18 +121,49 @@ fn transpile_file(path *PathBuf, output_file *PathBuf, mod_decl *str) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let output = gc.generate(ast);
|
if (let [Some[[vis, name]], ast] = parsed) {
|
||||||
|
let output = gc.generate(ast);
|
||||||
|
|
||||||
let res = fs::write(&output_file, format!("{mod_decl}{output}"));
|
if (let Some[ext] = output_file.extension().map(fn(e) -> e.to_owned())) {
|
||||||
|
let mut new_name = std::ffi::OsString::from(&name.0);
|
||||||
|
|
||||||
if (res.is_err()) {
|
new_name.push(".");
|
||||||
eprintln!(
|
new_name.push(ext);
|
||||||
"error: failed to write output {}\n {}",
|
|
||||||
output_file.display(),
|
|
||||||
res.unwrap_err(),
|
|
||||||
);
|
|
||||||
|
|
||||||
process::exit(1);
|
output_file.set_file_name(new_name);
|
||||||
|
} else {
|
||||||
|
output_file.set_file_name(&name.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
let res = fs::write(&output_file, format!("{mod_decl}{output}"));
|
||||||
|
|
||||||
|
if (res.is_err()) {
|
||||||
|
eprintln!(
|
||||||
|
"error: failed to write output {}\n {}",
|
||||||
|
output_file.display(),
|
||||||
|
res.unwrap_err(),
|
||||||
|
);
|
||||||
|
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
format!("{}mod {};", vis.get_rust(), name.get_rust())
|
||||||
|
} else {
|
||||||
|
let output = gc.generate(parsed.1);
|
||||||
|
|
||||||
|
let res = fs::write(&output_file, format!("{mod_decl}{output}"));
|
||||||
|
|
||||||
|
if (res.is_err()) {
|
||||||
|
eprintln!(
|
||||||
|
"error: failed to write output {}\n {}",
|
||||||
|
output_file.display(),
|
||||||
|
res.unwrap_err(),
|
||||||
|
);
|
||||||
|
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
String::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user