Skip to content

Commit

Permalink
refactor warnings to block and emit errors file; bump 0.4.9
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Jul 13, 2021
1 parent 19e90f0 commit ce37b50
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_runner"
version = "0.4.8"
version = "0.4.9"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@calcit/procs",
"version": "0.4.8",
"version": "0.4.9",
"main": "./lib/calcit.procs.js",
"devDependencies": {
"@types/node": "^15.12.2",
Expand Down
44 changes: 41 additions & 3 deletions src/bin/cr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::RefCell;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;
Expand All @@ -17,6 +18,8 @@ struct ProgramSettings {
emit_ir: bool,
}

pub const COMPILE_ERRORS_FILE: &str = "calcit.build-errors";

fn main() -> Result<(), String> {
builtins::effects::init_effects_states();
let cli_matches = cli_args::parse_cli();
Expand Down Expand Up @@ -74,6 +77,7 @@ fn main() -> Result<(), String> {
}

let mut program_code = program::extract_program_data(&snapshot)?;
let check_warnings: &RefCell<Vec<String>> = &RefCell::new(vec![]);

// make sure builtin classes are touched
runner::preprocess::preprocess_ns_def(
Expand All @@ -82,6 +86,7 @@ fn main() -> Result<(), String> {
&program_code,
&calcit_runner::primes::BUILTIN_CLASSES_ENTRY,
None,
check_warnings,
)?;

let task = if settings.emit_js {
Expand Down Expand Up @@ -230,8 +235,10 @@ fn run_codegen(
builtins::effects::modify_cli_running_mode(builtins::effects::CliRunningMode::Js)?;
}

let check_warnings: &RefCell<Vec<String>> = &RefCell::new(vec![]);

// preprocess to init
match runner::preprocess::preprocess_ns_def(&init_ns, &init_def, &program_code, &init_def, None) {
match runner::preprocess::preprocess_ns_def(&init_ns, &init_def, &program_code, &init_def, None, check_warnings) {
Ok(_) => (),
Err(failure) => {
println!("\nfailed preprocessing, {}", failure);
Expand All @@ -240,18 +247,49 @@ fn run_codegen(
}
}

let code_emit_path = Path::new(emit_path);
if !code_emit_path.exists() {
let _ = fs::create_dir(code_emit_path);
}

let js_file_path = code_emit_path.join(format!("{}.js", COMPILE_ERRORS_FILE)); // TODO mjs_mode

// preprocess to reload
match runner::preprocess::preprocess_ns_def(&reload_ns, &reload_def, &program_code, &init_def, None) {
match runner::preprocess::preprocess_ns_def(&reload_ns, &reload_def, &program_code, &init_def, None, check_warnings) {
Ok(_) => (),
Err(failure) => {
println!("\nfailed preprocessing, {}", failure);
call_stack::display_stack(&failure)?;
return Err(failure);
}
}
let warnings = check_warnings.to_owned().into_inner();
let mut content: String = String::from("");
if warnings.len() > 0 {
for message in &warnings {
println!("{}", message);
content = format!("{}\n{}", content, message);
}

let _ = fs::write(
&js_file_path,
format!("export default \"{}\";", content.trim().escape_default()),
);
return Err(format!(
"Found {} warnings, codegen blocked. errors in {}.js",
warnings.len(),
COMPILE_ERRORS_FILE,
));
}

// clear if there are no errors
let no_error_code = format!("export default null;");
if !(js_file_path.exists() && fs::read_to_string(js_file_path.to_owned()).unwrap() == no_error_code) {
let _ = fs::write(&js_file_path, no_error_code);
}

if ir_mode {
match codegen::gen_ir::emit_ir(&init_ns, &emit_path, &emit_path) {
match codegen::gen_ir::emit_ir(&init_fn, &reload_fn, &emit_path) {
Ok(_) => (),
Err(failure) => {
println!("\nfailed codegen, {}", failure);
Expand Down
28 changes: 25 additions & 3 deletions src/builtins/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! syntaxes related to data are maintained the corresponding files
//! Rust has limits on Closures, callbacks need to be handled specifically
use std::cell::RefCell;
use std::collections::HashSet;

use crate::builtins;
Expand Down Expand Up @@ -285,6 +286,7 @@ pub fn macroexpand_all(
let mut xs_cloned = xs;
// mutable operation
let mut rest_nodes = xs_cloned.slice(1..);
let check_warnings: &RefCell<Vec<String>> = &RefCell::new(vec![]);
// println!("macro: {:?} ... {:?}", args, rest_nodes);
// keep expanding until return value is not a recur
loop {
Expand All @@ -295,15 +297,35 @@ pub fn macroexpand_all(
rest_nodes = rest_code;
}
_ => {
let (resolved, _v) = runner::preprocess::preprocess_expr(&v, &HashSet::new(), file_ns, program_code)?;
let (resolved, _v) =
runner::preprocess::preprocess_expr(&v, &HashSet::new(), file_ns, program_code, check_warnings)?;
let warnings = check_warnings.to_owned().into_inner();
if warnings.len() > 0 {
for message in &warnings {
println!("{}", message);
}
}

return Ok(resolved);
}
}
}
}
_ => {
let (resolved, _v) =
runner::preprocess::preprocess_expr(&quoted_code, &HashSet::new(), file_ns, program_code)?;
let check_warnings: &RefCell<Vec<String>> = &RefCell::new(vec![]);
let (resolved, _v) = runner::preprocess::preprocess_expr(
&quoted_code,
&HashSet::new(),
file_ns,
program_code,
check_warnings,
)?;
let warnings = check_warnings.to_owned().into_inner();
if warnings.len() > 0 {
for message in &warnings {
println!("{}", message);
}
}
Ok(resolved)
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod snapshot;
pub mod util;

use dirs::home_dir;
use std::cell::RefCell;
use std::fs;
use std::path::Path;

Expand All @@ -36,8 +37,11 @@ pub fn run_program(
program_code: &program::ProgramCodeData,
) -> Result<Calcit, String> {
let (init_ns, init_def) = util::string::extract_ns_def(init_fn)?;

let check_warnings: &RefCell<Vec<String>> = &RefCell::new(vec![]);

// preprocess to init
match runner::preprocess::preprocess_ns_def(&init_ns, &init_def, &program_code, &init_def, None) {
match runner::preprocess::preprocess_ns_def(&init_ns, &init_def, &program_code, &init_def, None, check_warnings) {
Ok(_) => (),
Err(failure) => {
println!("\nfailed preprocessing, {}", failure);
Expand All @@ -46,6 +50,13 @@ pub fn run_program(
}
}

let warnings = check_warnings.to_owned().into_inner();
if warnings.len() > 0 {
for message in &warnings {
println!("{}", message);
}
return Err(format!("Found {} warnings, runner blocked", warnings.len()));
}
match program::lookup_evaled_def(&init_ns, &init_def) {
None => Err(format!("entry not initialized: {}/{}", init_ns, init_def)),
Some(entry) => match entry {
Expand Down
Loading

0 comments on commit ce37b50

Please sign in to comment.