Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support using full namespace #117

Merged
merged 4 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.5.0-a18"
version = "0.5.0-a20"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
32 changes: 16 additions & 16 deletions calcit/snapshots/test.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
|app.main $ {}
:ns $ quote
ns app.main $ :require
[] test-cond.main :as test-cond
[] test-gynienic.main :as test-gynienic
[] test-lens.main :as test-lens
[] test-list.main :as test-list
[] test-macro.main :as test-macro
[] test-map.main :as test-map
[] test-math.main :as test-math
[] test-recursion.main :as test-recursion
[] test-set.main :as test-set
[] test-string.main :as test-string
[] test-js.main :as test-js
[] test-record.main :as test-record
[] test-nil.main :as test-nil
[] test-fn.main :as test-fn
[] test-algebra.main :as test-algebra
test-cond.main :as test-cond
test-gynienic.main :as test-gynienic
test-lens.main :as test-lens
test-list.main :as test-list
test-macro.main :as test-macro
test-map.main :as test-map
test-math.main :as test-math
test-recursion.main :as test-recursion
test-set.main :as test-set
test-string.main :as test-string
test-js.main :as test-js
test-record.main :as test-record
test-nil.main :as test-nil
test-fn.main :as test-fn
test-algebra.main :as test-algebra
util.core :refer $ log-title inside-eval: inside-js:
:defs $ {}
|test-keyword $ quote
Expand Down Expand Up @@ -286,7 +286,7 @@
log-title "|Testing keyword function"
test-keyword

log-title "|Testing detects"
util.core/log-title "|Testing detects"
test-detects

test-if
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.5.0-a18",
"version": "0.5.0-a20",
"main": "./lib/calcit.procs.js",
"devDependencies": {
"@types/node": "^16.10.5",
Expand Down
17 changes: 9 additions & 8 deletions src/bin/cr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod injection;

use notify::{RecommendedWatcher, RecursiveMode, Watcher};

use calcit_runner::{builtins, call_stack, cli_args, codegen, program, runner, snapshot, util};
use calcit_runner::{builtins, call_stack, cli_args, codegen, codegen::emit_js::gen_stack, program, runner, snapshot, util};

struct ProgramSettings {
entry_path: PathBuf,
Expand Down Expand Up @@ -102,6 +102,7 @@ fn main() -> Result<(), String> {
calcit_runner::primes::BUILTIN_CLASSES_ENTRY,
None,
check_warnings,
&im::Vector::new(),
)
.map_err(|e| e.msg)?;

Expand Down Expand Up @@ -190,7 +191,6 @@ fn main() -> Result<(), String> {

fn recall_program(content: &str, init_fn: &str, reload_fn: &str, settings: &ProgramSettings) -> Result<(), String> {
println!("\n-------- file change --------\n");
call_stack::clear_stack();

// Steps:
// 1. load changes file, and patch to program_code
Expand Down Expand Up @@ -257,13 +257,14 @@ fn run_codegen(init_fn: &str, reload_fn: &str, emit_path: &str, ir_mode: bool) -
let js_file_path = code_emit_path.join(format!("{}.js", COMPILE_ERRORS_FILE)); // TODO mjs_mode

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

// preprocess to init
match runner::preprocess::preprocess_ns_def(&init_ns, &init_def, &init_def, None, check_warnings) {
match runner::preprocess::preprocess_ns_def(&init_ns, &init_def, &init_def, None, check_warnings, &im::Vector::new()) {
Ok(_) => (),
Err(failure) => {
println!("\nfailed preprocessing, {}", failure);
call_stack::display_stack(&failure.msg)?;
call_stack::display_stack(&failure.msg, &failure.stack)?;

let _ = fs::write(
&js_file_path,
Expand All @@ -277,11 +278,11 @@ fn run_codegen(init_fn: &str, reload_fn: &str, emit_path: &str, ir_mode: bool) -
}

// preprocess to reload
match runner::preprocess::preprocess_ns_def(&reload_ns, &reload_def, &init_def, None, check_warnings) {
match runner::preprocess::preprocess_ns_def(&reload_ns, &reload_def, &init_def, None, check_warnings, &im::Vector::new()) {
Ok(_) => (),
Err(failure) => {
println!("\nfailed preprocessing, {}", failure);
call_stack::display_stack(&failure.msg)?;
call_stack::display_stack(&failure.msg, &failure.stack)?;
return Err(failure.msg);
}
}
Expand Down Expand Up @@ -312,7 +313,7 @@ fn run_codegen(init_fn: &str, reload_fn: &str, emit_path: &str, ir_mode: bool) -
Ok(_) => (),
Err(failure) => {
println!("\nfailed codegen, {}", failure);
call_stack::display_stack(&failure)?;
call_stack::display_stack(&failure, &gen_stack::get_gen_stack())?;
return Err(failure);
}
}
Expand All @@ -322,7 +323,7 @@ fn run_codegen(init_fn: &str, reload_fn: &str, emit_path: &str, ir_mode: bool) -
Ok(_) => (),
Err(failure) => {
println!("\nfailed codegen, {}", failure);
call_stack::display_stack(&failure)?;
call_stack::display_stack(&failure, &gen_stack::get_gen_stack())?;
return Err(failure);
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/bin/injection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::thread;

use calcit_runner::{
builtins,
call_stack::{display_stack, CallStackVec},
data::edn::{calcit_to_edn, edn_to_calcit},
primes::{Calcit, CalcitErr, CalcitItems, CrListWrap},
runner::track,
Expand All @@ -30,7 +31,7 @@ pub fn inject_platform_apis() {
}

// &call-dylib-edn
pub fn call_dylib_edn(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
pub fn call_dylib_edn(xs: &CalcitItems, _call_stack: &CallStackVec) -> Result<Calcit, CalcitErr> {
if xs.len() < 2 {
return Err(CalcitErr::use_string(format!(
"&call-dylib-edn expected >2 arguments, got {}",
Expand Down Expand Up @@ -76,7 +77,7 @@ pub fn call_dylib_edn(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
}
}

pub fn echo(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
pub fn echo(xs: &CalcitItems, _call_stack: &CallStackVec) -> Result<Calcit, CalcitErr> {
let mut s = String::from("");
for (idx, x) in xs.iter().enumerate() {
if idx > 0 {
Expand All @@ -90,7 +91,7 @@ pub fn echo(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {

/// pass callback function to FFI function, so it can call multiple times
/// currently for HTTP servers
pub fn call_dylib_edn_fn(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
pub fn call_dylib_edn_fn(xs: &CalcitItems, call_stack: &CallStackVec) -> Result<Calcit, CalcitErr> {
if xs.len() < 3 {
return Err(CalcitErr::use_string(format!(
"&call-dylib-edn-fn expected >3 arguments, got {}",
Expand Down Expand Up @@ -146,9 +147,11 @@ pub fn call_dylib_edn_fn(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {

lib_tmp
};
let copied_stack_1 = Arc::new(call_stack.to_owned());

let _handle = thread::spawn(move || {
let func: libloading::Symbol<EdnFfiFn> = unsafe { lib.get(method.as_bytes()).expect("dy function not found") };
let copied_stack = copied_stack_1.clone();
match func(
ys.to_owned(),
Arc::new(move |ps: Vec<Edn>| -> Result<Edn, String> {
Expand All @@ -157,11 +160,11 @@ pub fn call_dylib_edn_fn(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
for p in ps {
real_args.push_back(edn_to_calcit(&p));
}
let r = runner::run_fn(&real_args, def_scope, args, body, def_ns);
let r = runner::run_fn(&real_args, def_scope, args, body, def_ns, &copied_stack);
match r {
Ok(ret) => calcit_to_edn(&ret),
Err(e) => {
println!("[Error] thread callback failed: {}", e);
display_stack(&format!("[Error] thread callback failed: {}", e.msg), &e.stack)?;
Err(format!("Error: {}", e))
}
}
Expand All @@ -175,7 +178,7 @@ pub fn call_dylib_edn_fn(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
Ok(ret) => edn_to_calcit(&ret),
Err(e) => {
track::track_task_release();
println!("failed to call request: {}", e);
let _ = display_stack(&format!("failed to call request: {}", e), &copied_stack_1);
return Err(CalcitErr::use_string(e));
}
};
Expand All @@ -187,12 +190,13 @@ pub fn call_dylib_edn_fn(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {

/// need to put it here since the crate does not compile for dylib
#[no_mangle]
pub fn on_ctrl_c(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
pub fn on_ctrl_c(xs: &CalcitItems, call_stack: &CallStackVec) -> Result<Calcit, CalcitErr> {
if xs.len() == 1 {
let cb = Arc::new(xs[0].to_owned());
let copied_stack = Arc::new(call_stack.to_owned());
ctrlc::set_handler(move || {
if let Calcit::Fn(_name, def_ns, _, def_scope, args, body) = cb.as_ref() {
if let Err(e) = runner::run_fn(&im::vector![], def_scope, args, body, def_ns) {
if let Err(e) = runner::run_fn(&im::vector![], def_scope, args, body, def_ns, &copied_stack) {
println!("error: {}", e);
}
}
Expand Down
60 changes: 40 additions & 20 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ mod syntax;
use std::collections::HashMap;
use std::sync::RwLock;

use crate::call_stack::CallStackVec;
use crate::primes::{Calcit, CalcitErr, CalcitItems, CalcitScope, CalcitSyntax};

pub type FnType = fn(xs: &CalcitItems) -> Result<Calcit, CalcitErr>;
pub type FnType = fn(xs: &CalcitItems, call_stack: &CallStackVec) -> Result<Calcit, CalcitErr>;
pub type SyntaxType = fn(expr: &CalcitItems, scope: &CalcitScope, file_ns: &str) -> Result<Calcit, CalcitErr>;

lazy_static! {
Expand Down Expand Up @@ -191,7 +192,20 @@ pub fn is_proc_name(s: &str) -> bool {
}
}

pub fn handle_proc(name: &str, args: &CalcitItems) -> Result<Calcit, CalcitErr> {
/// make sure that stack information attached in errors from procs
pub fn handle_proc(name: &str, args: &CalcitItems, call_stack: &CallStackVec) -> Result<Calcit, CalcitErr> {
handle_proc_internal(name, args, call_stack).map_err(|e| {
if e.stack.is_empty() {
let mut e2 = e.to_owned();
e2.stack = call_stack.to_owned();
e2
} else {
e
}
})
}

fn handle_proc_internal(name: &str, args: &CalcitItems, call_stack: &CallStackVec) -> Result<Calcit, CalcitErr> {
match name {
// meta
"type-of" => meta::type_of(args),
Expand All @@ -211,7 +225,7 @@ pub fn handle_proc(name: &str, args: &CalcitItems) -> Result<Calcit, CalcitErr>
"&tuple:nth" => meta::tuple_nth(args),
"&tuple:assoc" => meta::assoc(args),
// effects
"&display-stack" => meta::display_stack(args),
"&display-stack" => meta::display_stack(args, call_stack),
"raise" => effects::raise(args),
"quit!" => effects::quit(args),
"get-env" => effects::get_env(args),
Expand Down Expand Up @@ -284,10 +298,10 @@ pub fn handle_proc(name: &str, args: &CalcitItems) -> Result<Calcit, CalcitErr>
"butlast" => lists::butlast(args),
"&list:concat" => lists::concat(args),
"range" => lists::range(args),
"sort" => lists::sort(args),
"foldl" => lists::foldl(args),
"foldl-shortcut" => lists::foldl_shortcut(args),
"foldr-shortcut" => lists::foldr_shortcut(args),
"sort" => lists::sort(args, call_stack),
"foldl" => lists::foldl(args, call_stack),
"foldl-shortcut" => lists::foldl_shortcut(args, call_stack),
"foldr-shortcut" => lists::foldr_shortcut(args, call_stack),
"&list:reverse" => lists::reverse(args),
"&list:slice" => lists::slice(args),
"&list:assoc-before" => lists::assoc_before(args),
Expand Down Expand Up @@ -354,9 +368,9 @@ pub fn handle_proc(name: &str, args: &CalcitItems) -> Result<Calcit, CalcitErr>
let ps = IMPORTED_PROCS.read().unwrap();
if ps.contains_key(name) {
let f = ps[name];
f(args)
f(args, call_stack)
} else {
Err(CalcitErr::use_string(format!("No such proc: {}", a)))
Err(CalcitErr::use_msg_stack(format!("No such proc: {}", a), call_stack))
}
}
}
Expand All @@ -368,22 +382,28 @@ pub fn register_import_proc(name: &str, f: FnType) {
(*ps).insert(name.to_owned(), f);
}

pub fn handle_syntax(name: &CalcitSyntax, nodes: &CalcitItems, scope: &CalcitScope, file_ns: &str) -> Result<Calcit, CalcitErr> {
pub fn handle_syntax(
name: &CalcitSyntax,
nodes: &CalcitItems,
scope: &CalcitScope,
file_ns: &str,
call_stack: &CallStackVec,
) -> Result<Calcit, CalcitErr> {
match name {
CalcitSyntax::Defn => syntax::defn(nodes, scope, file_ns),
CalcitSyntax::Eval => syntax::eval(nodes, scope, file_ns),
CalcitSyntax::Eval => syntax::eval(nodes, scope, file_ns, call_stack),
CalcitSyntax::Defmacro => syntax::defmacro(nodes, scope, file_ns),
CalcitSyntax::Quote => syntax::quote(nodes, scope, file_ns),
CalcitSyntax::Quasiquote => syntax::quasiquote(nodes, scope, file_ns),
CalcitSyntax::If => syntax::syntax_if(nodes, scope, file_ns),
CalcitSyntax::CoreLet => syntax::syntax_let(nodes, scope, file_ns),
CalcitSyntax::Macroexpand => syntax::macroexpand(nodes, scope, file_ns),
CalcitSyntax::Macroexpand1 => syntax::macroexpand_1(nodes, scope, file_ns),
CalcitSyntax::MacroexpandAll => syntax::macroexpand_all(nodes, scope, file_ns),
CalcitSyntax::Try => syntax::call_try(nodes, scope, file_ns),
CalcitSyntax::Quasiquote => syntax::quasiquote(nodes, scope, file_ns, call_stack),
CalcitSyntax::If => syntax::syntax_if(nodes, scope, file_ns, call_stack),
CalcitSyntax::CoreLet => syntax::syntax_let(nodes, scope, file_ns, call_stack),
CalcitSyntax::Macroexpand => syntax::macroexpand(nodes, scope, file_ns, call_stack),
CalcitSyntax::Macroexpand1 => syntax::macroexpand_1(nodes, scope, file_ns, call_stack),
CalcitSyntax::MacroexpandAll => syntax::macroexpand_all(nodes, scope, file_ns, call_stack),
CalcitSyntax::Try => syntax::call_try(nodes, scope, file_ns, call_stack),
// "define reference" although it uses a confusing name "atom"
CalcitSyntax::Defatom => refs::defatom(nodes, scope, file_ns),
CalcitSyntax::Reset => refs::reset_bang(nodes, scope, file_ns),
CalcitSyntax::Defatom => refs::defatom(nodes, scope, file_ns, call_stack),
CalcitSyntax::Reset => refs::reset_bang(nodes, scope, file_ns, call_stack),
// different behavoirs, in Rust interpreter it's nil, in js codegen it's nothing
CalcitSyntax::HintFn => meta::no_op(),
}
Expand Down
Loading