-
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 #16 from calcit-lang/reuse-methods
Reuse methods
- Loading branch information
Showing
20 changed files
with
1,516 additions
and
1,031 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.6" | ||
version = "0.2.0-a1" | ||
authors = ["jiyinyiyong <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT" | ||
|
@@ -11,13 +11,17 @@ repository = "https://github.com/calcit-lang/calx-vm.rs" | |
readme = "README.md" | ||
exclude = [] | ||
|
||
[[bin]] | ||
name = "calx" | ||
path = "src/bin/cli.rs" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
cirru_parser = "0.1.25" | ||
cirru_parser = "0.1.26" | ||
regex = "1.10.2" | ||
lazy_static = "1.4.0" | ||
clap = { version = "4.4.13", features = ["derive"] } | ||
clap = { version = "4.4.18", features = ["derive"] } | ||
bincode = "2.0.0-rc.3" | ||
|
||
[profile.release] | ||
|
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,25 @@ | ||
|
||
fn main () | ||
call fibo | ||
const 34 | ||
echo | ||
|
||
fn fibo (($x i64) -> i64) | ||
local.get $x | ||
const 3 | ||
i.lt | ||
if (->) | ||
do | ||
const 1 | ||
return | ||
do | ||
local.get $x | ||
const -1 | ||
i.add | ||
call fibo | ||
local.get $x | ||
const -2 | ||
i.add | ||
call fibo | ||
i.add | ||
return |
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,19 @@ | ||
|
||
fn main () | ||
const 1 | ||
call demo | ||
const 0 | ||
call demo | ||
|
||
fn demo (($a i64) ->) | ||
local.get $a | ||
|
||
if (->) | ||
do | ||
const 11 | ||
echo | ||
do | ||
const 20 | ||
echo | ||
const 3 | ||
echo |
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,112 @@ | ||
mod types; | ||
|
||
use bincode::{Decode, Encode}; | ||
use core::fmt; | ||
use lazy_static::lazy_static; | ||
use regex::Regex; | ||
use std::str::FromStr; | ||
|
||
pub use types::CalxType; | ||
|
||
/// Simplied from Calcit, but trying to be basic and mutable | ||
#[derive(Debug, Clone, PartialEq, PartialOrd, Decode, Encode)] | ||
pub enum Calx { | ||
Nil, | ||
Bool(bool), | ||
I64(i64), | ||
F64(f64), | ||
Str(String), | ||
List(Vec<Calx>), | ||
// to simultate linked structures | ||
// Link(Box<Calx>, Box<Calx>, Box<Calx>), | ||
} | ||
|
||
impl FromStr for Calx { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"nil" => Ok(Calx::Nil), | ||
"true" => Ok(Calx::Bool(true)), | ||
"false" => Ok(Calx::Bool(false)), | ||
"" => Err(String::from("unknown empty string")), | ||
_ => { | ||
let s0 = s.chars().next().unwrap(); | ||
if s0 == '|' || s0 == ':' { | ||
Ok(Calx::Str(s[1..s.len()].to_owned())) | ||
} else if FLOAT_PATTERN.is_match(s) { | ||
match s.parse::<f64>() { | ||
Ok(u) => Ok(Calx::F64(u)), | ||
Err(e) => Err(format!("failed to parse: {}", e)), | ||
} | ||
} else if INT_PATTERN.is_match(s) { | ||
match s.parse::<i64>() { | ||
Ok(u) => Ok(Calx::I64(u)), | ||
Err(e) => Err(format!("failed to parse: {}", e)), | ||
} | ||
} else { | ||
Err(format!("unknown value: {}", s)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Calx { | ||
// for runtime type checking | ||
pub fn typed_as(&self, t: CalxType) -> bool { | ||
match self { | ||
Calx::Nil => t == CalxType::Nil, | ||
Calx::Bool(_) => t == CalxType::Bool, | ||
Calx::I64(_) => t == CalxType::I64, | ||
Calx::F64(_) => t == CalxType::F64, | ||
Calx::Str(_) => t == CalxType::Str, | ||
Calx::List(_) => t == CalxType::List, | ||
// Calx::Link(_, _, _) => t == CalxType::Link, | ||
} | ||
} | ||
|
||
pub fn truthy(&self) -> bool { | ||
match self { | ||
Calx::Nil => false, | ||
Calx::Bool(b) => *b, | ||
Calx::I64(n) => *n != 0, | ||
Calx::F64(n) => *n != 0.0, | ||
Calx::Str(_) => false, | ||
Calx::List(_) => false, | ||
// Calx::Link(_, _, _) => true, | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for Calx { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Calx::Nil => f.write_str("nil"), | ||
Calx::Bool(b) => f.write_str(&b.to_string()), | ||
Calx::I64(n) => f.write_str(&n.to_string()), | ||
Calx::F64(n) => f.write_str(&n.to_string()), | ||
Calx::Str(s) => f.write_str(s), | ||
Calx::List(xs) => { | ||
f.write_str("(")?; | ||
let mut at_head = true; | ||
for x in xs { | ||
if at_head { | ||
at_head = false | ||
} else { | ||
f.write_str(" ")?; | ||
} | ||
x.fmt(f)?; | ||
} | ||
f.write_str(")")?; | ||
Ok(()) | ||
} // Calx::Link(..) => f.write_str("TODO LINK"), | ||
} | ||
} | ||
} | ||
|
||
lazy_static! { | ||
static ref FLOAT_PATTERN: Regex = Regex::new("^-?\\d+\\.(\\d+)?$").unwrap(); | ||
static ref INT_PATTERN: Regex = Regex::new("^-?\\d+$").unwrap(); | ||
static ref USIZE_PATTERN: Regex = Regex::new("^\\d+$").unwrap(); | ||
} |
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,30 @@ | ||
use bincode::{Decode, Encode}; | ||
use std::str::FromStr; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Decode, Encode)] | ||
pub enum CalxType { | ||
Nil, | ||
Bool, | ||
I64, | ||
F64, | ||
Str, | ||
List, | ||
Link, | ||
} | ||
|
||
impl FromStr for CalxType { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"nil" => Ok(CalxType::Nil), | ||
"bool" => Ok(CalxType::Bool), | ||
"i64" => Ok(CalxType::I64), | ||
"f64" => Ok(CalxType::F64), | ||
"str" => Ok(CalxType::Str), | ||
"list" => Ok(CalxType::List), | ||
"link" => Ok(CalxType::Link), | ||
_ => Err(format!("unknown type: {}", s)), | ||
} | ||
} | ||
} |
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,9 +1,10 @@ | ||
mod calx; | ||
mod parser; | ||
mod primes; | ||
mod syntax; | ||
mod util; | ||
mod vm; | ||
|
||
pub use calx::{Calx, CalxType}; | ||
pub use parser::{extract_nested, parse_function}; | ||
pub use primes::{Calx, CalxBinaryProgram, CalxError, CalxFrame, CalxFunc, CALX_BINARY_EDITION}; | ||
pub use util::log_calx_value; | ||
pub use vm::{CalxImportsDict, CalxVM}; | ||
pub use vm::{func::CalxFunc, instr::CALX_INSTR_EDITION, CalxImportsDict, CalxVM}; |
Oops, something went wrong.