Skip to content

Commit

Permalink
Merge pull request #19 from calcit-lang/debug-fixes
Browse files Browse the repository at this point in the history
fixes related to migrating Respo
  • Loading branch information
soyaine authored Apr 28, 2021
2 parents 60e452f + 0f3d656 commit b01b8ef
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

- run: cargo build --release
- run: node scripts/cp-version.js

- name: Deploy to server
id: deploy
Expand All @@ -54,7 +55,7 @@ jobs:
flags: "-avzr --progress"
options: ""
ssh_options: ""
src: "target/release/calcit_runner"
src: "builds/*"
dest: "rsync-user@calcit-lang.org:/web-assets/repo/calcit-lang/binaries/linux/"

- name: Display status from deploy
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ js-out/
node_modules/

lib/*.js
builds/
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.3.0-a8"
version = "0.3.0-a10"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
4 changes: 4 additions & 0 deletions calcit/snapshots/test-string.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@
assert= "|do 's"
trim $ write-cirru-edn 's

assert= (escape "|\n") "|\"\\n\""
assert= (escape "|\t") "|\"\\t\""
assert= (escape "|a") "|\"a\""

|test-char $ quote
fn ()
log-title "|Test char"
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.3.0-a8",
"version": "0.3.0-a10",
"main": "./lib/calcit.procs.js",
"devDependencies": {
"@types/node": "^14.14.41",
Expand Down
8 changes: 8 additions & 0 deletions scripts/cp-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let fs = require("fs");
let pkg = require("../package.json");

if (!fs.existsSync("builds")) {
fs.mkdirSync("builds/");
}
fs.copyFileSync("target/release/calcit_runner", "builds/calcit_runner");
fs.copyFileSync("builds/calcit_runner", `builds/calcit_runner_${pkg.version}`);
10 changes: 10 additions & 0 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub fn is_proc_name(s: &str) -> bool {
| "quit"
| "get-env"
| "&get-calcit-backend"
| "read-file"
| "write-file"
// logics
| "&="
| "&<"
Expand Down Expand Up @@ -87,6 +89,7 @@ pub fn is_proc_name(s: &str) -> bool {
| "re-find-index"
| "re-find-all"
| "blank?"
| "escape"
// lists
| "[]"
| "'" // used as an alias for `[]`, experimental
Expand Down Expand Up @@ -126,6 +129,8 @@ pub fn is_proc_name(s: &str) -> bool {
// refs
| "deref"
| "reset!"
| "add-watch"
| "remove-watch"
// records
| "new-record"
| "&%{}"
Expand Down Expand Up @@ -162,6 +167,8 @@ pub fn handle_proc(name: &str, args: &CalcitItems) -> Result<Calcit, String> {
"quit" => effects::quit(args),
"get-env" => effects::get_env(args),
"&get-calcit-backend" => effects::call_get_calcit_backend(args),
"read-file" => effects::read_file(args),
"write-file" => effects::write_file(args),
// logics
"&=" => logics::binary_equal(args),
"&<" => logics::binary_less(args),
Expand Down Expand Up @@ -203,6 +210,7 @@ pub fn handle_proc(name: &str, args: &CalcitItems) -> Result<Calcit, String> {
"parse-float" => strings::parse_float(args),
"pr-str" => strings::pr_str(args),
"blank?" => strings::blank_ques(args),
"escape" => strings::escape(args),
// regex
"re-matches" => regexes::re_matches(args),
"re-find" => regexes::re_find(args),
Expand Down Expand Up @@ -247,6 +255,8 @@ pub fn handle_proc(name: &str, args: &CalcitItems) -> Result<Calcit, String> {
// refs
"deref" => refs::deref(args),
"reset!" => refs::reset_bang(args),
"add-watch" => refs::add_watch(args),
"remove-watch" => refs::add_watch(args),
// records
"new-record" => records::new_record(args),
"&%{}" => records::call_record(args),
Expand Down
23 changes: 23 additions & 0 deletions src/builtins/effects.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::fs;
use std::process::exit;
use std::sync::Mutex;
use std::time::Instant;
Expand Down Expand Up @@ -118,3 +119,25 @@ pub fn get_env(xs: &CalcitItems) -> Result<Calcit, String> {
None => Err(String::from("get-env expected an argument, got nothing")),
}
}

pub fn read_file(xs: &CalcitItems) -> Result<Calcit, String> {
match xs.get(0) {
Some(Calcit::Str(s)) => match fs::read_to_string(s) {
Ok(content) => Ok(Calcit::Str(content)),
Err(e) => Err(format!("read-file failed: {}", e)),
},
Some(a) => Err(format!("read-file expected a string, got: {}", a)),
None => Err(String::from("read-file expected a filename, got nothing")),
}
}

pub fn write_file(xs: &CalcitItems) -> Result<Calcit, String> {
match (xs.get(0), xs.get(1)) {
(Some(Calcit::Str(path)), Some(Calcit::Str(content))) => match fs::write(path, content) {
Ok(_) => Ok(Calcit::Nil),
Err(e) => Err(format!("write-file failed, {}", e)),
},
(Some(a), Some(b)) => Err(format!("write-file expected 3 strings, got: {} {}", a, b)),
(a, b) => Err(format!("write-file expected 2 strings, got: {:?} {:?}", a, b)),
}
}
2 changes: 1 addition & 1 deletion src/builtins/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn recur(xs: &CalcitItems) -> Result<Calcit, String> {

pub fn format_to_lisp(xs: &CalcitItems) -> Result<Calcit, String> {
match xs.get(0) {
Some(v) => Ok(Calcit::Str(primes::format_to_lisp(v))),
Some(v) => Ok(Calcit::Str(v.lisp_str())),
None => Err(String::from("format-to-lisp expected 1 argument")),
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/builtins/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,11 @@ pub fn reset_bang(xs: &CalcitItems) -> Result<Calcit, String> {
}
}

// TODO add-watch remove-watch
// TODO
pub fn add_watch(_xs: &CalcitItems) -> Result<Calcit, String> {
Ok(Calcit::Nil)
}
// TODO
pub fn remove_watch(_xs: &CalcitItems) -> Result<Calcit, String> {
Ok(Calcit::Nil)
}
13 changes: 13 additions & 0 deletions src/builtins/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,16 @@ pub fn blank_ques(xs: &CalcitItems) -> Result<Calcit, String> {
None => Err(String::from("blank? expected 1 argument, got nothing")),
}
}

pub fn escape(xs: &CalcitItems) -> Result<Calcit, String> {
match xs.get(0) {
Some(Calcit::Str(s)) => {
let mut chunk = String::from("\"");
chunk.push_str(&s.escape_default().to_string());
chunk.push('"');
Ok(Calcit::Str(chunk))
}
Some(a) => Err(format!("escape expected 1 string, got {}", a)),
None => Err(String::from("escape expected 1 argument, got nothing")),
}
}
5 changes: 5 additions & 0 deletions src/call_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ pub fn show_stack() {
}
}

pub fn clear_stack() {
let stack = &mut CALL_STACK.lock().unwrap();
stack.clear();
}

pub fn display_stack(failure: &str) {
let stack: &Vec<CalcitStack> = &mut CALL_STACK.lock().unwrap();
println!("\ncall stack:");
Expand Down
4 changes: 2 additions & 2 deletions src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@

|&case $ quote
defmacro &case (item default pattern & others)
assert "|expects pattern in a pair"
assert "|`case` expects pattern in a pair"
if (list? pattern) (&= 2 (count pattern)) false
let
x $ first pattern
Expand Down Expand Up @@ -817,7 +817,7 @@
&let nil
echo "|Failed assertion:" (quote ~xs)
raise
~ $ &str-concat (&str-concat message "| ") xs
~ $ &str-concat (&str-concat message "| ") (format-to-lisp xs)

|join-str $ quote
defn join-str (xs0 sep)
Expand Down
4 changes: 2 additions & 2 deletions src/cli_args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::primes;
pub const CALCIT_VERSION: &str = env!("CARGO_PKG_VERSION");

pub fn parse_cli<'a>() -> clap::ArgMatches<'a> {
clap::App::new("Calcit Runner")
.version(primes::CALCI_VERSION)
.version(CALCIT_VERSION)
.author("Jon. <jiyinyiyong@gmail.com>")
.about("Calcit Runner")
.arg(
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/emit_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::path::Path;
use crate::builtins::meta::{js_gensym, reset_js_gensym_index};
use crate::builtins::{is_proc_name, is_syntax_name};
use crate::primes;
use crate::primes::{format_to_lisp, Calcit, CalcitItems, SymbolResolved::*};
use crate::primes::{Calcit, CalcitItems, SymbolResolved::*};
use crate::program;
use crate::util::string::has_ns_part;
use crate::util::string::{matches_js_var, wrap_js_str};
Expand Down Expand Up @@ -308,7 +308,7 @@ fn gen_call_code(
},

"defmacro" => Ok(format!("/* Unexpected macro {} */", xs)),
"quote-replace" | "quasiquote" => Ok(format!("(/* Unexpected quasiquote {} */ null)", format_to_lisp(xs))),
"quote-replace" | "quasiquote" => Ok(format!("(/* Unexpected quasiquote {} */ null)", xs.lisp_str())),

"raise" => {
// not core syntax, but treat as macro for better debugging experience
Expand Down Expand Up @@ -546,7 +546,7 @@ fn gen_let_code(
// break unless nested &let is found
loop {
if let_def_body.len() <= 1 {
return Err(format!("Unexpected empty content in let, {:?}", xs));
return Err(format!("unexpected empty content in let, {:?}", xs));
}
let pair = let_def_body[0].clone();
let content = let_def_body.skip(1);
Expand Down
21 changes: 18 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ fn main() -> Result<(), String> {

let mut eval_once = cli_matches.is_present("once");

println!("calcit_runner version: {}", cli_args::CALCIT_VERSION);

// load core libs
let bytes = include_bytes!("./cirru/calcit-core.cirru");
let core_content = String::from_utf8_lossy(bytes).to_string();
Expand Down Expand Up @@ -85,10 +87,22 @@ fn main() -> Result<(), String> {
None => "js-out".to_owned(),
};

if cli_matches.is_present("emit-js") {
run_codegen(&init_fn, &reload_fn, &program_code, &emit_path)?;
let task = if cli_matches.is_present("emit-js") {
run_codegen(&init_fn, &reload_fn, &program_code, &emit_path)
} else {
run_program(&init_fn, &reload_fn, &program_code)
};

if eval_once {
task?;
} else {
run_program(&init_fn, &reload_fn, &program_code)?;
// error are only printed in watch mode
match task {
Ok(_) => {}
Err(e) => {
println!("\nfailed to run, {}", e);
}
}
}

if !eval_once {
Expand All @@ -111,6 +125,7 @@ fn main() -> Result<(), String> {
}
notify::DebouncedEvent::Write(_) => {
println!("\n-------- file change --------\n");
call_stack::clear_stack();

// Steps:
// 1. load changes file, and patch to program_code
Expand Down
6 changes: 4 additions & 2 deletions src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,15 @@ impl PartialEq for Calcit {
pub const CORE_NS: &str = "calcit.core";
pub const GENERATED_NS: &str = "calcit.gen";

pub const CALCI_VERSION: &str = "0.3.0-a8";

impl Calcit {
pub fn turn_string(&self) -> String {
match self {
Calcit::Str(s) => s.clone(),
_ => format!("{}", self),
}
}

pub fn lisp_str(&self) -> String {
format_to_lisp(self)
}
}
8 changes: 4 additions & 4 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn evaluate_expr(
file_ns: &str,
program_code: &program::ProgramCodeData,
) -> Result<Calcit, String> {
// println!("eval code: {}", primes::format_to_lisp(expr));
// println!("eval code: {}", expr.lisp_str());

match expr {
Calcit::Nil => Ok(expr.clone()),
Expand All @@ -33,7 +33,7 @@ pub fn evaluate_expr(
Calcit::List(xs) => match xs.get(0) {
None => Err(format!("cannot evaluate empty expr: {}", expr)),
Some(x) => {
// println!("eval expr: {}", primes::format_to_lisp(expr));
// println!("eval expr: {}", expr.lisp_str());
// println!("eval expr: {}", x);

let mut added_stack = false;
Expand Down Expand Up @@ -64,7 +64,7 @@ pub fn evaluate_expr(

// TODO moving to preprocess
let mut current_values = rest_nodes.clone();
// println!("eval macro: {} {}", x, primes::format_to_lisp(expr));
// println!("eval macro: {} {}", x, expr.lisp_str()));
// println!("macro... {} {}", x, CrListWrap(current_values.clone()));

push_call_stack(file_ns, &name, StackKind::Macro, &Some(expr.clone()), &rest_nodes);
Expand All @@ -79,7 +79,7 @@ pub fn evaluate_expr(
current_values = ys;
}
_ => {
// println!("gen code: {} {}", x, primes::format_to_lisp(&code));
// println!("gen code: {} {}", x, &code.lisp_str()));
break evaluate_expr(&code, scope, file_ns, program_code)?;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/runner/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn process_list_call(
current_values = ys;
}
_ => {
// println!("gen code: {} {}", code, primes::format_to_lisp(&code));
// println!("gen code: {} {}", code, &code.lisp_str());
let (final_code, v) = preprocess_expr(&code, scope_defs, file_ns, program_code)?;
pop_call_stack();
return Ok((final_code, v));
Expand Down

0 comments on commit b01b8ef

Please sign in to comment.