-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from calcit-lang/update-bincode
update bincode usage
- Loading branch information
Showing
16 changed files
with
350 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "calx_vm" | ||
version = "0.1.5" | ||
version = "0.1.6" | ||
authors = ["jiyinyiyong <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT" | ||
|
@@ -9,9 +9,7 @@ homepage = "https://github.com/calcit-lang/calx-vm" | |
documentation = "https://docs.rs/crate/calx_vm/" | ||
repository = "https://github.com/calcit-lang/calx-vm.rs" | ||
readme = "README.md" | ||
exclude = [ | ||
"examples/*", | ||
] | ||
exclude = [] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
fn main () | ||
call fibo | ||
const 34 | ||
echo | ||
|
||
fn fibo (($x i64) -> i64) | ||
block (->) | ||
local.get $x | ||
const 3 | ||
i.lt | ||
br-if 0 | ||
|
||
local.get $x | ||
const -1 | ||
i.add | ||
call fibo | ||
|
||
local.get $x | ||
const -2 | ||
i.add | ||
call fibo | ||
|
||
i.add | ||
|
||
return | ||
|
||
const 1 | ||
return |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//! fibonacci for comparisom | ||
//! still orders in magnitude faster than Calx | ||
fn fibo(n: i64) -> i64 { | ||
if n < 3 { | ||
1 | ||
} else { | ||
fibo(n - 1) + fibo(n - 2) | ||
} | ||
} | ||
|
||
fn main() { | ||
println!("Result {}", fibo(40)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ use calx_vm::{log_calx_value, parse_function, Calx, CalxBinaryProgram, CalxFunc, | |
#[derive(Parser, Debug)] | ||
#[command(name = "Calx VM")] | ||
#[command(author = "Jon Chen <[email protected]>")] | ||
#[command(version = "0.1.4")] | ||
#[command(version = "0.1.6")] | ||
#[command(about = "A toy VM", long_about = None)] | ||
struct Args { | ||
#[arg(short, long, value_name = "SHOW_CODE")] | ||
|
@@ -20,6 +20,8 @@ struct Args { | |
disable_pre: bool, | ||
#[arg(short, long, value_name = "EMIT_BINARY")] | ||
emit_binary: Option<String>, | ||
#[arg(short, long, value_name = "VERBOSE")] | ||
verbose: bool, | ||
#[arg(long, value_name = "EVAL_BINARY")] | ||
eval_binary: bool, | ||
#[arg(value_name = "SOURCE")] | ||
|
@@ -38,7 +40,7 @@ fn main() -> Result<(), String> { | |
let mut fns: Vec<CalxFunc> = vec![]; | ||
|
||
if eval_binary { | ||
let code = fs::read(source).expect("read binar from source file"); | ||
let code = fs::read(source).expect("read binary from source file"); | ||
let program: CalxBinaryProgram = bincode::decode_from_slice(&code, bincode::config::standard()) | ||
.expect("decode functions from binary") | ||
.0; | ||
|
@@ -66,26 +68,15 @@ fn main() -> Result<(), String> { | |
} | ||
|
||
if emit_binary.is_some() { | ||
let mut slice = [0u8; 10000]; | ||
let program = CalxBinaryProgram { | ||
edition: CALX_BINARY_EDITION.to_string(), | ||
fns, | ||
}; | ||
let length = match bincode::encode_into_slice(&program, &mut slice, bincode::config::standard()) { | ||
Ok(l) => { | ||
println!("encoded binary length: {}", l); | ||
l | ||
} | ||
Err(e) => panic!("failed on default length of 10000: {}", e), | ||
}; | ||
let slice = &slice[..length]; | ||
let buf = bincode::encode_to_vec(program, bincode::config::standard()).map_err(|e| e.to_string())?; | ||
let target_file = &emit_binary.unwrap(); | ||
match fs::write(target_file, slice) { | ||
Ok(_) => println!("wrote binary to {}", target_file), | ||
Err(e) => panic!("failed to write binary to {}: {}", target_file, e), | ||
}; | ||
fs::write(target_file, buf).map_err(|e| e.to_string())?; | ||
println!("wrote binary to {}", target_file); | ||
return Ok(()); | ||
// println!("Bytes written: {:?}", slice); | ||
} | ||
|
||
let mut imports: CalxImportsDict = HashMap::new(); | ||
|
@@ -103,7 +94,8 @@ fn main() -> Result<(), String> { | |
|
||
let now = Instant::now(); | ||
if !disable_pre { | ||
vm.preprocess()?; | ||
println!("[calx] start preprocessing"); | ||
vm.preprocess(args.verbose)?; | ||
} else { | ||
println!("Preprocess disabled.") | ||
} | ||
|
@@ -114,11 +106,12 @@ fn main() -> Result<(), String> { | |
} | ||
} | ||
|
||
println!("[calx] start running"); | ||
match vm.run(vec![Calx::I64(1)]) { | ||
Ok(ret) => { | ||
let elapsed = now.elapsed(); | ||
|
||
println!("Took {:.3?}: {:?}", elapsed, ret); | ||
println!("[calx] took {:.3?}: {:?}", elapsed, ret); | ||
Ok(()) | ||
} | ||
Err(e) => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.