Compare commits

...

16 Commits

Author SHA1 Message Date
92bcb6f358 All tests passing when run with --release. Not sure if this is really correct, or if the algorithm is being too wasteful and re-executing versions of functions taht should have been inlined or something (this may esp be true without the parameter/wrap_level stuff, and also I'm a little sus of the env chains - oh hay, it might even just be becasue only legal env chains are allowed which prevents pe-ing) 2023-03-20 01:06:24 -04:00
48de9517c9 An unvaled Pair should have a True override for its IDs 2023-03-19 11:15:22 -04:00
9162b456d2 Oh some were just changes in formatting. First real issue looks like it's VMAP 2023-03-19 02:03:41 -04:00
1df3acccd7 Some tests passing again, namely all of basic and fact 2023-03-19 01:52:18 -04:00
7b334e96df Finally finished sketching out the new version, lets get on the compile errors 2023-03-19 00:59:46 -04:00
04bf1c6249 Website reformat - content still TBD 2023-03-18 14:57:31 -04:00
d4920ec4b6 Notes for hopefully the full simple kraken PE, with initial implementation of new NeededIds (split between head and tails (heads are ids for which progress will be made, tails might not progress but are contained), body_under and if_under). 2023-03-15 01:16:37 -04:00
865fc1b4b6 Update flake, split ast out into ast & pe_ast, and main into main & test 2023-03-14 20:14:17 -04:00
46e6e27f88 pre change 2023-03-14 20:05:39 -04:00
d836a66e66 more numbers of evals/calls 2023-03-01 00:33:43 -05:00
3df2d02286 Merge branch 'master' into partially_broken 2023-02-28 01:15:48 -05:00
af2fb537c7 saving remaining before 2023-02-28 01:14:03 -05:00
4752dde4af Added recursive If tracking, but that still doesn't seem to have solved the problem. Have to dig deeper into the actual failed tests now... 2023-02-24 01:19:55 -05:00
abddf1db06 transition to fake-ids instead of rec-hashes. Still have infinate recursion, need to check on it 2023-02-23 13:14:10 -05:00
6357297cd1 been going down withat's probs the wrong path for solving infinate recursion for bvau, did a lot on suspended IF but that doesn't seem to be the issue... Not sure if this was done in vain 2023-02-23 02:48:40 -05:00
bb6b15c156 some bugfixes, and moving towards a reconstruct-on-error. More bugs remain though, ?uncovered? by this, including another infinate loop. Fixed a tricky one where a function would recursively call it self for real while evaluating itself fake, and create an environment that had a loop indirected through subbing itself in for SuspendedEnv (since the real and the fake had the same ID). 2023-02-23 01:38:46 -05:00
17 changed files with 3811 additions and 1507 deletions

View File

@@ -13,7 +13,7 @@ with partial evaluation during compilation to make it efficient.
koka_bench: Licensed under Apache-2.0, as they are derived from the benchmarks of the Koka project, see the readme and license in koka_bench for more, or https://github.com/koka-lang/koka for the source project. koka_bench: Licensed under Apache-2.0, as they are derived from the benchmarks of the Koka project, see the readme and license in koka_bench for more, or https://github.com/koka-lang/koka for the source project.
Kraken (everything else besides the benchmarks in koka_bench) Licensed under Kraken (everything else besides the benchmarks in koka_bench, and Recursive/CodeJar/Highlight.js in website/) Licensed under
SPDX-License-Identifier: BSD-2-Clause-Patent SPDX-License-Identifier: BSD-2-Clause-Patent
Note: This license is designed to provide: a) a simple permissive license; b) that is compatible with the GNU General Public License (GPL), version 2; and c) which also has an express patent grant included. Note: This license is designed to provide: a) a simple permissive license; b) that is compatible with the GNU General Public License (GPL), version 2; and c) which also has an express patent grant included.

88
basic_dyns Normal file
View File

@@ -0,0 +1,88 @@
RB-TREE
evals all_evals evaled_wrap_1 evaled_wrap_0 compiled_dyn_1 compiled_dyn_0
non-PE10 114_170 5_032_297 1_291_489 398_104 1 0
PE10 0 0 0 0 10 0
nonPE9 97087 4278001 1097965 338383 1 0
PE9 0 0 0 0 9 0
nonPE8 80160 3532131 906548 279379 1 0
PE8 0 0 0 0 8 0
nonPE7 67050 2952848 757932 233513 1 0
PE7 0 0 0 0 7 0
nonPE6 50361 2219792 569723 175593 1 0
PE6 0 0 0 0 6 0
nonPE5 37299 1643049 421745 129938 1 0
PE5 0 0 0 0 5 0
nonPE4 24393 1074730 275873 85000 1 0
PE4 0 0 0 0 4 0
nonPE3 15304 672998 172802 53200 1 0
PE3 0 0 0 0 3 0
nonPE2 6453 284080 72939 22484 1 0
PE2 0 0 0 0 2 0
nonPE1 1385 59899 15413 4750 1 0
PE1 0 0 0 0 1 0
Fib
evals all_evals evaled_wrap_1 evaled_wrap_0 compiled_dyn_1 compiled_dyn_0
non-PE35 29860726 1320920726 337695363 119442881 1 0
non-PE30 2692560 119107888 30450112 10770217 1 0
non-PE25 242808 10740492 2745825 971209 1 0
non-PE20 21914 969010 247731 87633 1 0
non-PE15 1996 87916 22478 7961 1 0
non-PE10 200 8468 2167 777 1 0
PE10 0 0 0 0 0 0
PE15 0 0 0 0 0 0
PE20 0 0 0 0 0 0
PE25 0 0 0 0 0 0
PE30 0 0 0 0 0 0
PE35 0 0 0 0 0 0
cfold
evals all_evals evaled_wrap_1 evaled_wrap_0 compiled_dyn_1 compiled_dyn_0
non-PE5 239660 10897376 2784275 879066 1 0
PE5 0 0 0 0 0 0
deriv
evals all_evals evaled_wrap_1 evaled_wrap_0 compiled_dyn_1 compiled_dyn_0
non-PE2 257693 11708558 2990090 946500 1 0
PE2 0 0 0 0 2 0
nqueens
evals all_evals evaled_wrap_1 evaled_wrap_0 compiled_dyn_1 compiled_dyn_0
non-PE7 271720 13530241 3429161 1108393 1 0
PE7 0 0 0 0 0 0
scheme --script ../../partial_eval.scm rbtree-opt.kp no_partial_eval && mv csc_out.wasm rbtree-opt.wasm && time wasmtime ./rbtree-opt.wasm 10
scheme --script ../../partial_eval.scm rbtree-opt.kp && mv csc_out.wasm rbtree-opt.wasm && time wasmtime ./rbtree-opt.wasm 10

18
flake.lock generated
View File

@@ -36,11 +36,11 @@
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs"
}, },
"locked": { "locked": {
"lastModified": 1676341851, "lastModified": 1678760344,
"narHash": "sha256-T8cmSiriXdpZfqlserNyJ1solTCR0DbD8A75epSDOVY=", "narHash": "sha256-N8u9/O0NWt3PUQc9xmCeod1SFilOFicALjtYtslib2g=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "956ddb5047f98ea08b792b22004b94a9971932c4", "rev": "d907affef544f64bd6886fe6bcc5fa2495a82373",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -67,11 +67,11 @@
}, },
"nixpkgs_stable_new": { "nixpkgs_stable_new": {
"locked": { "locked": {
"lastModified": 1676177817, "lastModified": 1678703398,
"narHash": "sha256-OQnBnuKkpwkfNY31xQyfU5hNpLs1ilWt+hVY6ztEEOM=", "narHash": "sha256-Y1mW3dBsoWLHpYm+UIHb5VZ7rx024NNHaF16oZBx++o=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "1b82144edfcd0c86486d2e07c7298f85510e7fb8", "rev": "67f26c1cfc5d5783628231e776a81c1ade623e0b",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -97,11 +97,11 @@
}, },
"nixpkgs_unstable": { "nixpkgs_unstable": {
"locked": { "locked": {
"lastModified": 1676342081, "lastModified": 1678838343,
"narHash": "sha256-zpHbXgvUYTJ9r1WgKtwhj/dmVthZ/GlW1oBYOdqJ9yg=", "narHash": "sha256-aA48yVAUyppdlVHhMStlWjB8u9uzA5iel3C47xlbkrw=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "4106c7519bff1d14fa5f942da645b3f18d16309e", "rev": "b16f2a75619fe8e6adf4583f5fc6448bc967d482",
"type": "github" "type": "github"
}, },
"original": { "original": {

7
kr/Cargo.lock generated
View File

@@ -11,6 +11,12 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "anyhow"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4"
[[package]] [[package]]
name = "ascii-canvas" name = "ascii-canvas"
version = "3.0.0" version = "3.0.0"
@@ -167,6 +173,7 @@ dependencies = [
name = "kr" name = "kr"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"lalrpop", "lalrpop",
"lalrpop-util", "lalrpop-util",
"once_cell", "once_cell",

View File

@@ -10,6 +10,7 @@ build = "build.rs"
lalrpop-util = {version="0.19.7", features=["lexer"]} lalrpop-util = {version="0.19.7", features=["lexer"]}
regex = "1" regex = "1"
once_cell = "1.17.0" once_cell = "1.17.0"
anyhow = "1"
[build-dependencies] [build-dependencies]
lalrpop = "0.19.7" lalrpop = "0.19.7"

View File

@@ -1,18 +1,18 @@
use std::fmt; use std::fmt;
use std::rc::Rc; use std::rc::Rc;
use std::convert::From; use std::convert::From;
use std::collections::{BTreeSet,BTreeMap,hash_map::DefaultHasher};
use std::result::Result;
use std::iter;
use std::hash::{Hash,Hasher};
// TODO: // TODO:
// -extend vau & env logic and SuspendedPair PE with sequence_params & wrap_level // -extend vau & env logic and SuspendedPair PE with sequence_params & wrap_level
// -add current-hashes to if // -add current-hashes to if
// -expand combiner_Return_ok with (func ...params) | func doesn't take de and func+params are return ok // -expand combiner_Return_ok with (func ...params) | func doesn't take de and func+params are return ok
// -add recursive drop redundent veval // -add recursive drop redundent veval
// -mark rec-hash on DeriComb
// -add compiler // -add compiler
//
// -use current fake comb ids instead of hashes
// -do they have to be added to not-under thing like hashes
// -make cons work only for SuspendedParam/Env
impl From<i32> for Form { fn from(item: i32) -> Self { Form::Int(item) } } impl From<i32> for Form { fn from(item: i32) -> Self { Form::Int(item) } }
impl From<bool> for Form { fn from(item: bool) -> Self { Form::Bool(item) } } impl From<bool> for Form { fn from(item: bool) -> Self { Form::Bool(item) } }
@@ -114,68 +114,6 @@ impl fmt::Display for Form {
} }
} }
} }
impl fmt::Display for MarkedForm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MarkedForm::Nil => write!(f, "nil"),
MarkedForm::Int(i) => write!(f, "{}", i),
MarkedForm::Bool(b) => write!(f, "{}", b),
MarkedForm::Symbol(s) => write!(f, "{}", s),
MarkedForm::Pair(hash, ids, car, cdr) => {
//write!(f, "{:?}#({}", ids, car)?;
write!(f, "({}", car)?;
let mut traverse: Rc<MarkedForm> = Rc::clone(cdr);
loop {
match &*traverse {
MarkedForm::Pair(ref hash, ref ids, ref carp, ref cdrp) => {
write!(f, " {}", carp)?;
traverse = Rc::clone(cdrp);
},
MarkedForm::Nil => {
write!(f, ")")?;
return Ok(());
},
x => {
write!(f, ". {})", x)?;
return Ok(());
},
}
}
},
MarkedForm::SuspendedEnvEval { hash, ids, x, e } => write!(f, "{{veval {} {}}}", x, e),
MarkedForm::SuspendedIf { hash, ids, c, t, e } => write!(f, "{{if {} {} {}}}", c, t, e),
MarkedForm::SuspendedSymbol(name) => write!(f, "{}", name),
MarkedForm::SuspendedEnvLookup { name, id } => write!(f, "{:?}({:?}env)", name, id),
MarkedForm::SuspendedParamLookup { name, id, cdr_num, car } => write!(f, "{:?}({:?}{}{})", name, id, cdr_num, car),
MarkedForm::PrimComb { name, wrap_level, .. } => write!(f, "<{}{}>", name, wrap_level),
//MarkedForm::DeriComb { ids, se, de, id, wrap_level, sequence_params, rest_params, body } => write!(f, "{:?}#[{}/{:?}/{:?}/{}/{:?}/{:?}/{}]", ids, se, de, id, wrap_level, sequence_params, rest_params, body),
MarkedForm::DeriComb { hash, lookup_name, ids, se, de, id, wrap_level, sequence_params, rest_params, body } => write!(f, "{:?}#[{:?}/{:?}/{:?}/{}/{:?}/{:?}/{}]", ids, lookup_name, de, id, wrap_level, sequence_params, rest_params, body),
MarkedForm::SuspendedPair{ hash, ids, attempted, car, cdr } => {
//write!(f, "{:?}{:?}#{{{}", ids, attempted, car)?;
write!(f, "{{{}", car)?;
let mut traverse: Rc<MarkedForm> = Rc::clone(cdr);
loop {
match &*traverse {
MarkedForm::Pair(ref hash, ref ids, ref carp, ref cdrp) => {
write!(f, " {}", carp)?;
traverse = Rc::clone(cdrp);
},
MarkedForm::Nil => {
write!(f, "}}")?;
return Ok(());
},
x => {
write!(f, ". {}}}", x)?;
return Ok(());
},
}
}
},
}
}
}
pub fn eval(e: Rc<Form>, f: Rc<Form>) -> Rc<Form> { pub fn eval(e: Rc<Form>, f: Rc<Form>) -> Rc<Form> {
let mut e = e; let mut e = e;
@@ -220,13 +158,6 @@ pub fn eval(e: Rc<Form>, f: Rc<Form>) -> Rc<Form> {
} }
} }
} }
fn massoc(k: &str, v: Rc<MarkedForm>, l: Rc<MarkedForm>) -> Rc<MarkedForm> {
MarkedForm::new_pair(
MarkedForm::new_pair(
Rc::new(MarkedForm::Symbol(k.to_owned())),
v),
l)
}
fn assoc(k: &str, v: Rc<Form>, l: Rc<Form>) -> Rc<Form> { fn assoc(k: &str, v: Rc<Form>, l: Rc<Form>) -> Rc<Form> {
Rc::new(Form::Pair( Rc::new(Form::Pair(
Rc::new(Form::Pair( Rc::new(Form::Pair(
@@ -244,7 +175,6 @@ fn assoc_vec(kvs: Vec<(&str, Rc<Form>)>) -> Rc<Form> {
pub fn root_env() -> Rc<Form> { pub fn root_env() -> Rc<Form> {
assoc_vec(vec![ assoc_vec(vec![
// TODO: Should be properly tail recursive
("eval", Rc::new(Form::PrimComb("eval".to_owned(), |e, p| { ("eval", Rc::new(Form::PrimComb("eval".to_owned(), |e, p| {
let b = eval(Rc::clone(&e), p.car().unwrap()); let b = eval(Rc::clone(&e), p.car().unwrap());
let e = eval(e, p.cdr().unwrap().car().unwrap()); let e = eval(e, p.cdr().unwrap().car().unwrap());
@@ -406,856 +336,3 @@ pub fn root_env() -> Rc<Form> {
]) ])
} }
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct EnvID(i32);
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct MFHash(u64);
impl MFHash {
pub fn combine(&self, other: &MFHash) -> Self {
let mut h = DefaultHasher::new();
"combine/".hash(&mut h); self.0.hash(&mut h); other.hash(&mut h);
MFHash(h.finish())
}
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum NeededIds {
True(BTreeSet<MFHash>),
None(BTreeSet<MFHash>),
Some(BTreeSet<EnvID>,BTreeSet<MFHash>),
}
impl NeededIds {
fn new_true() -> Self { NeededIds::True( BTreeSet::new()) }
fn new_none() -> Self { NeededIds::None( BTreeSet::new()) }
fn new_single(i: EnvID) -> Self { NeededIds::Some(iter::once(i).collect(), BTreeSet::new()) }
fn needs_nothing(&self) -> bool {
match self {
NeededIds::True(hashes) => false,
NeededIds::None(hashes) => hashes.is_empty(),
NeededIds::Some(set,hashes) => false,
}
}
fn hashes(&self) -> &BTreeSet<MFHash> {
match self {
NeededIds::True(hashes) => hashes,
NeededIds::None(hashes) => hashes,
NeededIds::Some(set,hashes) => hashes,
}
}
fn union(&self, other: &NeededIds) -> Self {
match self {
NeededIds::True(hashes) => NeededIds::True(hashes.union(other.hashes()).cloned().collect()),
NeededIds::None(hashes) => other.union_hashes(hashes),
NeededIds::Some(set, hashes) => match other {
NeededIds::True(ohashes) => NeededIds::True(hashes.union(ohashes).cloned().collect()),
NeededIds::None(ohashes) => NeededIds::Some(set.clone(), hashes.union(ohashes).cloned().collect()),
NeededIds::Some(oset,ohashes) => NeededIds::Some(set.union(oset).cloned().collect(), hashes.union(ohashes).cloned().collect()),
},
}
}
fn union_without(&self, other: &NeededIds, without: EnvID) -> Self {
self.union(other).without(without)
}
fn without(self, without: EnvID) -> Self {
match self {
NeededIds::True(_) => self,
NeededIds::None(_) => self,
NeededIds::Some(set, hashes) => {
let new: BTreeSet<EnvID> = set.into_iter().filter(|x| *x != without).collect();
if new.is_empty() {
NeededIds::None(hashes)
} else {
NeededIds::Some(new, hashes)
}
},
}
}
fn union_hashes(&self, other: &BTreeSet<MFHash>) -> Self {
match self {
NeededIds::True(hashes) => NeededIds::True( other.union(hashes).cloned().collect()),
NeededIds::None(hashes) => NeededIds::None( other.union(hashes).cloned().collect()),
NeededIds::Some(set, hashes) => NeededIds::Some(set.clone(), other.union(hashes).cloned().collect()),
}
}
fn add_hash(&self, h: MFHash) -> Self {
match self {
NeededIds::True(hashes) => NeededIds::True( hashes.iter().cloned().chain(iter::once(h)).collect()),
NeededIds::None(hashes) => NeededIds::None( hashes.iter().cloned().chain(iter::once(h)).collect()),
NeededIds::Some(set, hashes) => NeededIds::Some(set.clone(), hashes.iter().cloned().chain(iter::once(h)).collect()),
}
}
fn add_id(&self, i: EnvID) -> Self {
match self {
NeededIds::True(hashes) => NeededIds::True( hashes.clone()),
NeededIds::None(hashes) => NeededIds::Some(iter::once(i).collect(), hashes.clone()),
NeededIds::Some(set, hashes) => NeededIds::Some(set.iter().cloned().chain(iter::once(i)).collect(), hashes.clone()),
}
}
}
#[derive(Clone)]
pub struct BCtx {
id_counter: i32
}
impl BCtx {
pub fn new_id(mut self) -> (Self, EnvID) {
let new_id = EnvID(self.id_counter);
self.id_counter += 1;
(self, new_id)
}
}
// force is for drop_redundent_eval, which will be integrated now
// memo is only for recording currently executing hashes (calls and if's, current for us)
// only_head is not currently used
//only_head env env_counter memo env_stack force
#[derive(Clone)]
pub struct DCtx {
e : Rc<MarkedForm>,
sus_env_stack: Rc<BTreeMap<EnvID, Rc<MarkedForm>>>,
sus_prm_stack: Rc<BTreeMap<EnvID, Rc<MarkedForm>>>,
real_set: Rc<BTreeSet<EnvID>>,
current: Rc<BTreeSet<MFHash>>,
ident: usize,
}
impl DCtx {
pub fn copy_set_env(&self, e: &Rc<MarkedForm>) -> Self {
DCtx { e: Rc::clone(e), sus_env_stack: Rc::clone(&self.sus_env_stack), sus_prm_stack: Rc::clone(&self.sus_prm_stack), real_set: Rc::clone(&self.real_set), current: Rc::clone(&self.current), ident: self.ident+1 }
}
pub fn copy_push_frame(&self, id: EnvID, se: &Rc<MarkedForm>, de: &Option<String>, e: Option<Rc<MarkedForm>>, rest_params: &Option<String>, prms: Option<Rc<MarkedForm>>, body: &Rc<MarkedForm>) -> Result<Self,MFHash> {
let mut sus_env_stack = Rc::clone(&self.sus_env_stack);
let mut sus_prm_stack = Rc::clone(&self.sus_prm_stack);
let mut real_set = Rc::clone(&self.real_set);
if (e.is_some() || prms.is_some()) {
Rc::make_mut(&mut real_set).insert(id.clone());
} else {
Rc::make_mut(&mut real_set).remove(&id);
}
let inner_env = if let Some(de) = de {
let de_val = if let Some(e) = e {
Rc::make_mut(&mut sus_env_stack).insert(id.clone(), Rc::clone(&e));
e
} else {
Rc::make_mut(&mut sus_env_stack).remove(&id);
Rc::new(MarkedForm::SuspendedEnvLookup { name: Some(de.clone()), id: id.clone() })
};
massoc(de, de_val, Rc::clone(se))
} else { Rc::clone(se) };
// not yet supporting sequence params
let inner_env = if let Some(p) = rest_params {
let p_val = if let Some(prms) = prms {
Rc::make_mut(&mut sus_prm_stack).insert(id.clone(), Rc::clone(&prms));
prms
} else {
Rc::make_mut(&mut sus_prm_stack).remove(&id);
Rc::new(MarkedForm::SuspendedParamLookup { name: Some(p.clone()), id: id.clone(), cdr_num: 0, car: false })
};
massoc(p, p_val, inner_env)
} else { inner_env };
// Push on current frame hash
let new_hash = inner_env.hash().combine(&body.hash());
if self.current.contains(&new_hash) {
println!("Hash Rec Stop!");
Err(new_hash)
} else {
let new_current = Rc::new(self.current.iter().cloned().chain(iter::once(new_hash)).collect());
Ok(DCtx { e: inner_env, sus_env_stack, sus_prm_stack, real_set, current: new_current, ident: self.ident+1 })
}
}
pub fn can_progress(&self, ids: NeededIds) -> bool {
// check if ids is true || ids intersection EnvIDs in our stacks is non empty || ids.hashes - current is non empty
match ids {
NeededIds::True(hashes) => true,
NeededIds::None(hashes) => !self.current.is_superset(&hashes),
NeededIds::Some(ids,hashes) => (!self.real_set.is_disjoint(&ids)) || (!self.current.is_superset(&hashes)),
}
}
}
pub fn new_base_ctxs() -> (BCtx,DCtx) {
let bctx = BCtx { id_counter: 0 };
let (bctx, root_env) = mark(root_env(), bctx);
(bctx, DCtx { e: root_env, sus_env_stack: Rc::new(BTreeMap::new()), sus_prm_stack: Rc::new(BTreeMap::new()), real_set: Rc::new(BTreeSet::new()), current: Rc::new(BTreeSet::new()), ident: 0 } )
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum Attempted {
True(Option<NeededIds>),
False,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum MarkedForm {
Nil,
Int(i32),
Bool(bool),
Symbol(String),
Pair(MFHash, NeededIds, Rc<MarkedForm>,Rc<MarkedForm>),
SuspendedSymbol(String),
SuspendedParamLookup { name: Option<String>, id: EnvID, cdr_num: i32, car: bool },
SuspendedEnvLookup { name: Option<String>, id: EnvID },
// resume hash is folded into ids
SuspendedPair { hash: MFHash, ids: NeededIds, attempted: Attempted, car: Rc<MarkedForm>, cdr: Rc<MarkedForm>},
SuspendedEnvEval { hash: MFHash, ids: NeededIds, x: Rc<MarkedForm>, e: Rc<MarkedForm> },
SuspendedIf { hash: MFHash, ids: NeededIds, c: Rc<MarkedForm>, t: Rc<MarkedForm>, e: Rc<MarkedForm> },
PrimComb { name: String, takes_de: bool, wrap_level: i32, f: fn(BCtx,DCtx,Rc<MarkedForm>) -> Result<(BCtx,Rc<MarkedForm>),String> },
DeriComb { hash: MFHash, lookup_name: Option<String>, ids: NeededIds, se: Rc<MarkedForm>, de: Option<String>, id: EnvID, wrap_level: i32, sequence_params: Vec<String>, rest_params: Option<String>, body: Rc<MarkedForm> },
}
impl MarkedForm {
pub fn new_suspended_env_eval(x: Rc<MarkedForm>, e: Rc<MarkedForm>) -> Rc<MarkedForm> {
let mut h = DefaultHasher::new();
"SuspendedEnvEval(x,e)".hash(&mut h); x.hash().hash(&mut h); e.hash().hash(&mut h);
Rc::new(MarkedForm::SuspendedEnvEval{ hash: MFHash(h.finish()), ids: e.ids(), x, e })
}
pub fn new_suspended_if(c: Rc<MarkedForm>, t: Rc<MarkedForm>, e: Rc<MarkedForm>) -> Rc<MarkedForm> {
let mut h = DefaultHasher::new();
"SuspendedIf(c,t,e)".hash(&mut h); c.hash().hash(&mut h); t.hash().hash(&mut h); e.hash().hash(&mut h);
Rc::new(MarkedForm::SuspendedIf{ hash: MFHash(h.finish()), ids: c.ids().union(&t.ids()).union(&e.ids()), c, t, e })
}
pub fn new_pair(car: Rc<MarkedForm>, cdr: Rc<MarkedForm>) -> Rc<MarkedForm> {
let mut h = DefaultHasher::new();
"Pair(ids,car,cdr)".hash(&mut h); car.hash().hash(&mut h); cdr.hash().hash(&mut h);
Rc::new(MarkedForm::Pair(MFHash(h.finish()), car.ids().union(&cdr.ids()), car, cdr))
}
pub fn new_suspended_pair(attempted: Attempted, car: Rc<MarkedForm>, cdr: Rc<MarkedForm>, rec_hash: Option<MFHash>) -> Rc<MarkedForm> {
let mut h = DefaultHasher::new();
"SuspendedPair".hash(&mut h); attempted.hash(&mut h); car.hash().hash(&mut h); cdr.hash().hash(&mut h);
let ids = car.ids().union(&cdr.ids());
let ids = match ids {
NeededIds::True(_) => ids,
NeededIds::None(hashes) => match &attempted {
Attempted::False => NeededIds::True(hashes),
Attempted::True(Some(oids)) => oids.union_hashes(&hashes),
Attempted::True(None) => NeededIds::None(hashes),
},
NeededIds::Some(_,_) => ids,
};
if let Some(rec_hash) = rec_hash {
ids.add_hash(rec_hash);
}
Rc::new(MarkedForm::SuspendedPair{ hash: MFHash(h.finish()), attempted, ids, car, cdr })
}
pub fn new_deri_comb(se: Rc<MarkedForm>, lookup_name: Option<String>, de: Option<String>, id: EnvID, wrap_level: i32, sequence_params: Vec<String>, rest_params: Option<String>, body: Rc<MarkedForm>) -> Rc<MarkedForm> {
let mut h = DefaultHasher::new();
"DeriComb".hash(&mut h); se.hash().hash(&mut h); de.hash(&mut h); id.hash(&mut h); wrap_level.hash(&mut h);
sequence_params.hash(&mut h); rest_params.hash(&mut h); body.hash().hash(&mut h);
let ids = se.ids().union_without(&body.ids(), id.clone());
Rc::new(MarkedForm::DeriComb{ hash: MFHash(h.finish()), lookup_name, ids, se, de, id, wrap_level, sequence_params, rest_params, body })
}
pub fn tag_name(self: &Rc<MarkedForm>, name: &str) -> Rc<MarkedForm> {
match &**self {
MarkedForm::DeriComb { hash, lookup_name, ids, se, de, id, wrap_level, sequence_params, rest_params, body } =>
MarkedForm::new_deri_comb(Rc::clone(se), Some(name.to_owned()), de.clone(), id.clone(), *wrap_level, sequence_params.clone(), rest_params.clone(), Rc::clone(body)),
_ => Rc::clone(self),
}
}
pub fn hash(&self) -> MFHash {
let mut h = DefaultHasher::new();
{
let state = &mut h;
match self {
MarkedForm::Nil => { "Nil".hash(state); },
MarkedForm::Int(i) => { "Int(i)".hash(state); i.hash(state); },
MarkedForm::Bool(b) => { "Bool(b)".hash(state); b.hash(state); },
MarkedForm::Symbol(s) => { "Symbol(s)".hash(state); s.hash(state); },
MarkedForm::Pair(hash,ids,car,cdr) => { return *hash; },
MarkedForm::SuspendedSymbol(name) => { "SuspendedSymbol(name)".hash(state); name.hash(state); },
MarkedForm::SuspendedParamLookup { name, id, cdr_num, car }=> { "SuspendedParamLookup".hash(state); name.hash(state); id.hash(state); cdr_num.hash(state); car.hash(state); },
MarkedForm::SuspendedEnvEval { hash, .. } => { return *hash; },
MarkedForm::SuspendedIf { hash, .. } => { return *hash; },
MarkedForm::SuspendedEnvLookup { name, id } => { "SuspendedEnvLookup".hash(state); name.hash(state); id.hash(state); },
MarkedForm::SuspendedPair{ hash, .. } => { return *hash; },
MarkedForm::PrimComb { name, wrap_level, .. } => { "PrimComb".hash(state); name.hash(state); wrap_level.hash(state); },
MarkedForm::DeriComb { hash, .. } => { return *hash; },
}
}
MFHash(h.finish())
}
pub fn wrap_level(&self) -> Option<i32> {
match self {
MarkedForm::PrimComb { wrap_level, .. } => Some(*wrap_level),
MarkedForm::DeriComb { wrap_level, .. } => Some(*wrap_level),
_ => None,
}
}
pub fn decrement_wrap_level(&self) -> Option<Rc<Self>> {
match self {
MarkedForm::PrimComb { name, takes_de, wrap_level, f } => Some(Rc::new(MarkedForm::PrimComb { name: name.clone(), takes_de: *takes_de, wrap_level: wrap_level-1, f: *f })),
MarkedForm::DeriComb { hash, lookup_name, ids, se, de, id, wrap_level, sequence_params, rest_params, body } => Some(MarkedForm::new_deri_comb(Rc::clone(se), lookup_name.clone(), de.clone(), id.clone(), wrap_level-1, sequence_params.clone(), rest_params.clone(), Rc::clone(body))),
_ => None,
}
}
pub fn ids(&self) -> NeededIds {
match self {
MarkedForm::Nil => NeededIds::new_none(),
MarkedForm::Int(i) => NeededIds::new_none(),
MarkedForm::Bool(b) => NeededIds::new_none(),
MarkedForm::Symbol(s) => NeededIds::new_none(),
MarkedForm::Pair(hash,ids,car,cdr) => ids.clone(),
MarkedForm::SuspendedSymbol(name) => NeededIds::new_true(),
MarkedForm::SuspendedEnvLookup { id, .. } => NeededIds::new_single(id.clone()),
MarkedForm::SuspendedParamLookup { id, .. } => NeededIds::new_single(id.clone()),
MarkedForm::SuspendedEnvEval { ids, ..} => ids.clone(),
MarkedForm::SuspendedIf { ids, ..} => ids.clone(),
MarkedForm::SuspendedPair{ ids, .. } => ids.clone(),
MarkedForm::PrimComb { .. } => NeededIds::new_none(),
MarkedForm::DeriComb { ids, .. } => ids.clone(),
}
}
// TODO: this might be essentially the same as NeededIds.nothing_needed()
pub fn is_value(&self) -> bool {
match match self {
MarkedForm::Nil => return true,
MarkedForm::Int(i) => return true,
MarkedForm::Bool(b) => return true,
MarkedForm::Symbol(s) => return true,
MarkedForm::SuspendedSymbol(name) => return false,
MarkedForm::SuspendedEnvLookup { id, .. } => return false,
MarkedForm::SuspendedParamLookup { id, .. } => return false,
MarkedForm::SuspendedEnvEval { ids, ..} => return false,
MarkedForm::SuspendedIf { ids, ..} => return false,
MarkedForm::SuspendedPair{ ids, .. } => return false,
MarkedForm::PrimComb { .. } => return true,
MarkedForm::Pair(hash,ids,car,cdr) => ids.clone(),
MarkedForm::DeriComb { ids, .. } => ids.clone(),
} {
NeededIds::True(hashes) => false,
NeededIds::None(hashes) => true,
NeededIds::Some(ids,hashes) => false,
}
}
pub fn unval(self: &Rc<MarkedForm>) -> Result<Rc<MarkedForm>, &'static str> {
match &**self {
MarkedForm::Nil => Ok(Rc::clone(self)),
MarkedForm::Int(i) => Ok(Rc::clone(self)),
MarkedForm::Bool(b) => Ok(Rc::clone(self)),
MarkedForm::Symbol(s) => Ok(Rc::new(MarkedForm::SuspendedSymbol(s.clone()))),
MarkedForm::Pair(hash,ids,car,cdr) => Ok(MarkedForm::new_suspended_pair( Attempted::False, car.unval()?, Rc::clone(cdr), None)),
MarkedForm::SuspendedSymbol(name) => Err("trying to unval a suspended symbol"),
MarkedForm::SuspendedEnvLookup { .. } => Err("trying to unval a suspended env lookup"),
MarkedForm::SuspendedParamLookup { .. } => Err("trying to unval a suspended param lookup"),
MarkedForm::SuspendedEnvEval { .. } => Err("trying to unval a suspended env eval"),
MarkedForm::SuspendedIf { .. } => Err("trying to unval a suspended if"),
MarkedForm::SuspendedPair{ ids, .. } => Err("trying to unval a suspended pair"),
MarkedForm::PrimComb { .. } => Ok(Rc::clone(self)),
MarkedForm::DeriComb { .. } => Ok(Rc::clone(self)),
}
}
pub fn truthy(&self) -> Result<bool,&'static str> {
match self {
MarkedForm::Nil => Ok(false),
MarkedForm::Int(i) => Ok(true),
MarkedForm::Bool(b) => Ok(*b),
MarkedForm::Symbol(s) => Ok(true),
MarkedForm::Pair(hash,ids,car,cdr) => Ok(true),
MarkedForm::SuspendedSymbol(name) => Err("trying to truthy a suspended symbol"),
MarkedForm::SuspendedEnvLookup { .. } => Err("trying to truthy a suspended env lookup"),
MarkedForm::SuspendedParamLookup { .. } => Err("trying to truthy a suspended param lookup"),
MarkedForm::SuspendedEnvEval { .. } => Err("trying to truthy a suspended env eval"),
MarkedForm::SuspendedIf { .. } => Err("trying to truthy a suspended if"),
MarkedForm::SuspendedPair{ ids, .. } => Err("trying to truthy a suspended pair"),
MarkedForm::PrimComb { .. } => Ok(true),
MarkedForm::DeriComb { .. } => Ok(true),
}
}
pub fn sym(&self) -> Result<&str,&'static str> {
match self {
MarkedForm::Symbol(s) => Ok(s),
_ => Err("not a symbol"),
}
}
pub fn int(&self) -> Result<i32,&'static str> {
match self {
MarkedForm::Int(i) => Ok(*i),
_ => Err("not a int"),
}
}
pub fn car(&self) -> Result<Rc<MarkedForm>, &'static str> {
match self {
MarkedForm::Pair(hash,ids,car,cdr) => Ok(Rc::clone(car)),
_ => Err("not a pair"),
}
}
pub fn cdr(&self) -> Result<Rc<MarkedForm>, &'static str> {
match self {
MarkedForm::Pair(hash,ids,car,cdr) => Ok(Rc::clone(cdr)),
_ => Err("not a pair"),
}
}
}
pub fn mark(form: Rc<Form>, bctx: BCtx) -> (BCtx, Rc<MarkedForm>) {
match &*form {
Form::Nil => (bctx, Rc::new(MarkedForm::Nil)),
Form::Int(i) => (bctx, Rc::new(MarkedForm::Int(*i))),
Form::Bool(b) => (bctx, Rc::new(MarkedForm::Bool(*b))),
Form::Symbol(s) => (bctx, Rc::new(MarkedForm::Symbol(s.clone()))),
Form::Pair(car, cdr) => {
let (bctx, car) = mark(Rc::clone(car),bctx);
let (bctx, cdr) = mark(Rc::clone(cdr),bctx);
(bctx, MarkedForm::new_pair(car, cdr))
},
Form::DeriComb { se, de, params, body } => {
panic!();
},
Form::PrimComb(name, _f) => {
(bctx, match &name[..] {
"eval" => Rc::new(MarkedForm::PrimComb { name: "eval".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
Ok((bctx, MarkedForm::new_suspended_env_eval(p.car()?.unval()?, p.cdr()?.car()?)))
}}),
"vau" => Rc::new(MarkedForm::PrimComb { name: "vau".to_owned(), takes_de: true, wrap_level: 0, f: |bctx, dctx, p| {
let de = p.car()?.sym().map(|s| s.to_owned()).ok();
let params = p.cdr()?.car()?.sym()?.to_owned();
let body = p.cdr()?.cdr()?.car()?.unval()?;
let se = Rc::clone(&dctx.e);
let (bctx, id) = bctx.new_id();
let wrap_level = 0;
let sequence_params = vec![];
let rest_params = Some(params);
Ok((bctx, MarkedForm::new_deri_comb( se, None, de, id, wrap_level, sequence_params, rest_params, body )))
}}),
"if" => Rc::new(MarkedForm::PrimComb { name: "if".to_owned(), takes_de: false, wrap_level: 0, f: |bctx, dctx, p| {
Ok((bctx, MarkedForm::new_suspended_if(p.car()?.unval()?, p.cdr()?.car()?.unval()?, p.cdr()?.cdr()?.car()?.unval()?)))
}}),
// TODO: handle these in the context of paritals
"cons" => Rc::new(MarkedForm::PrimComb { name: "cons".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let h = p.car()?;
//println!("Consing with head {}", h);
let t = p.cdr()?.car()?;
//println!("Consing with tail {}", t);
Ok((bctx, MarkedForm::new_pair(h, t)))
}}),
"car" => Rc::new(MarkedForm::PrimComb { name: "car".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
Ok((bctx, p.car()?.car()?))
}}),
"cdr" => Rc::new(MarkedForm::PrimComb { name: "cdr".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
Ok((bctx, p.car()?.cdr()?))
}}),
"quote" => Rc::new(MarkedForm::PrimComb { name: "quote".to_owned(), takes_de: false, wrap_level: 0, f: |bctx, dctx, p| {
Ok((bctx, p.car()?))
}}),
"debug" => Rc::new(MarkedForm::PrimComb { name: "debug".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
// This one is a bit weird - we put the wrap level at 1 so both sides are pe'd,
// and always returns Err so that it's not optimized away
Err("debug can't be partial-evaluated away".to_owned())
}}),
// ditto
"assert" => Rc::new(MarkedForm::PrimComb { name: "assert".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let cond = p.car()?;
if !cond.truthy()? {
println!("Assert failed: {:?}", cond);
}
assert!(cond.truthy()?);
Ok((bctx, p.cdr()?.car()?))
}}),
"=" => Rc::new(MarkedForm::PrimComb { name: "=".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?;
let b = p.cdr()?.car()?;
//println!("DOing (= {} {}) = {}", a, b, a==b);
// TODO: double check that this ignores ids etc. It should, since
// wrap_level=1 should mean that everything's a value
// also, it should just check by hash then?
Ok((bctx, Rc::new(MarkedForm::Bool(a == b))))
}}),
"<" => Rc::new(MarkedForm::PrimComb { name: "<".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?;
let b = p.cdr()?.car()?;
Ok((bctx, Rc::new(MarkedForm::Bool(a.int()? < b.int()?))))
}}),
">" => Rc::new(MarkedForm::PrimComb { name: ">".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?;
let b = p.cdr()?.car()?;
Ok((bctx, Rc::new(MarkedForm::Bool(a.int()? > b.int()?))))
}}),
"<=" => Rc::new(MarkedForm::PrimComb { name: "<=".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?;
let b = p.cdr()?.car()?;
Ok((bctx, Rc::new(MarkedForm::Bool(a.int()? <= b.int()?))))
}}),
">=" => Rc::new(MarkedForm::PrimComb { name: ">=".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?;
let b = p.cdr()?.car()?;
Ok((bctx, Rc::new(MarkedForm::Bool(a.int()? >= b.int()?))))
}}),
"+" => Rc::new(MarkedForm::PrimComb { name: "+".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?.int()?;
let b = p.cdr()?.car()?.int()?;
Ok((bctx, Rc::new(MarkedForm::Int(a + b))))
}}),
"-" => Rc::new(MarkedForm::PrimComb { name: "-".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?.int()?;
let b = p.cdr()?.car()?.int()?;
Ok((bctx, Rc::new(MarkedForm::Int(a - b))))
}}),
"*" => Rc::new(MarkedForm::PrimComb { name: "*".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?.int()?;
let b = p.cdr()?.car()?.int()?;
Ok((bctx, Rc::new(MarkedForm::Int(a * b))))
}}),
"/" => Rc::new(MarkedForm::PrimComb { name: "/".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?.int()?;
let b = p.cdr()?.car()?.int()?;
Ok((bctx, Rc::new(MarkedForm::Int(a / b))))
}}),
"%" => Rc::new(MarkedForm::PrimComb { name: "%".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?.int()?;
let b = p.cdr()?.car()?.int()?;
Ok((bctx, Rc::new(MarkedForm::Int(a % b))))
}}),
"&" => Rc::new(MarkedForm::PrimComb { name: "&".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?.int()?;
let b = p.cdr()?.car()?.int()?;
Ok((bctx, Rc::new(MarkedForm::Int(a & b))))
}}),
"|" => Rc::new(MarkedForm::PrimComb { name: "|".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?.int()?;
let b = p.cdr()?.car()?.int()?;
Ok((bctx, Rc::new(MarkedForm::Int(a | b))))
}}),
"^" => Rc::new(MarkedForm::PrimComb { name: "^".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
let a = p.car()?.int()?;
let b = p.cdr()?.car()?.int()?;
Ok((bctx, Rc::new(MarkedForm::Int(a ^ b))))
}}),
"comb?" => Rc::new(MarkedForm::PrimComb { name: "comb?".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
Ok((bctx, Rc::new(MarkedForm::Bool(match &* p.car()? {
MarkedForm::PrimComb { .. } => true,
MarkedForm::DeriComb { .. } => true,
_ => false,
}))))
}}),
"pair?" => Rc::new(MarkedForm::PrimComb { name: "pair?".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
Ok((bctx, Rc::new(MarkedForm::Bool(match &* p.car()? {
MarkedForm::Pair(_h, _i, _a,_b) => true,
_ => false,
}))))
}}),
"symbol?" => Rc::new(MarkedForm::PrimComb { name: "symbol?".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
Ok((bctx, Rc::new(MarkedForm::Bool(match &* p.car()? {
MarkedForm::Symbol(_) => true,
_ => false,
}))))
}}),
"int?" => Rc::new(MarkedForm::PrimComb { name: "int?".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
Ok((bctx, Rc::new(MarkedForm::Bool(match &* p.car()? {
MarkedForm::Int(_) => true,
_ => false,
}))))
}}),
// maybe bool? but also could be derived. Nil def
"bool?" => Rc::new(MarkedForm::PrimComb { name: "bool?".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
Ok((bctx, Rc::new(MarkedForm::Bool(match &* p.car()? {
MarkedForm::Bool(_) => true,
_ => false,
}))))
}}),
"nil?" => Rc::new(MarkedForm::PrimComb { name: "nil?".to_owned(), takes_de: false, wrap_level: 1, f: |bctx, dctx, p| {
Ok((bctx, Rc::new(MarkedForm::Bool(match &* p.car()? {
MarkedForm::Nil => true,
_ => false,
}))))
}}),
_ => panic!("gah! don't have partial eval version of {}", name),
})
},
}
}
pub fn combiner_return_ok(x: Rc<MarkedForm>, check_id: EnvID) -> bool {
match match &*x {
MarkedForm::Nil => return true,
MarkedForm::Int(_) => return true,
MarkedForm::Bool(_) => return true,
MarkedForm::Symbol(_) => return true,
MarkedForm::Pair(h,ids,_,_) => ids,
MarkedForm::SuspendedSymbol(_) => return false,
MarkedForm::SuspendedParamLookup { id, .. } => return *id != check_id,
MarkedForm::SuspendedEnvLookup { id, .. } => return *id != check_id,
MarkedForm::SuspendedEnvEval { e, .. } => return combiner_return_ok(Rc::clone(e), check_id),
MarkedForm::SuspendedIf { c, t, e, .. } => return combiner_return_ok(Rc::clone(c), check_id.clone()) &&
combiner_return_ok(Rc::clone(t), check_id.clone()) &&
combiner_return_ok(Rc::clone(e), check_id),
MarkedForm::SuspendedPair { .. } => {
// expand with (func ...params) | func doesn't take de and func+params are return ok
return false
},
MarkedForm::PrimComb { .. } => return true,
MarkedForm::DeriComb { ids, .. } => ids,
} {
NeededIds::True(_hashes) => false,
NeededIds::None(_hashes) => true,
NeededIds::Some(ids,_hashes) => !ids.contains(&check_id),
}
//; Handles let 4.3 through macro level leaving it as (<comb wraplevel=1 (y) (+ y x 12)> 13)
//; need handling of symbols (which is illegal for eval but ok for calls) to push it farther
//(combiner_return_ok (rec-lambda combiner_return_ok (func_result env_id)
// (cond ((not (later_head? func_result)) (not (check_for_env_id_in_result env_id func_result)))
// ; special cases now
// ; *(veval body {env}) => (combiner_return_ok {env})
// ; The reason we don't have to check body is that this form is only creatable in ways that body was origionally a value and only need {env}
// ; Either it's created by eval, in which case it's fine, or it's created by something like (eval (array veval x de) de2) and the array has checked it,
// ; or it's created via literal vau invocation, in which case the body is a value.
// ((and (marked_array? func_result)
// (prim_comb? (idx (.marked_array_values func_result) 0))
// (= 'veval (.prim_comb_sym (idx (.marked_array_values func_result) 0)))
// (= 3 (len (.marked_array_values func_result)))
// (combiner_return_ok (idx (.marked_array_values func_result) 2) env_id)) true)
// ; (func ...params) => (and (doesn't take de func) (foldl combiner_return_ok (cons func params)))
// ;
// ((and (marked_array? func_result)
// (not (comb_takes_de? (idx (.marked_array_values func_result) 0) (len (.marked_array_values func_result))))
// (foldl (lambda (a x) (and a (combiner_return_ok x env_id))) true (.marked_array_values func_result))) true)
// ; So that's enough for macro like, but we would like to take it farther
// ; For like (let1 a 12 (wrap (vau (x) (let1 y (+ a 1) (+ y x a)))))
// ; we get to (+ 13 x 12) not being a value, and it reconstructs
// ; (<comb wraplevel=1 (y) (+ y x 12)> 13)
// ; and that's what eval gets, and eval then gives up as well.
// ; That will get caught by the above cases to remain the expansion (<comb wraplevel=1 (y) (+ y x 12)> 13),
// ; but ideally we really want another case to allow (+ 13 x 12) to bubble up
// ; I think it would be covered by the (func ...params) case if a case is added to allow symbols to be bubbled up if their
// ; needed for progress wasn't true or the current environment, BUT this doesn't work for eval, just for functions,
// ; since eval changes the entire env chain (but that goes back to case 1, and might be eliminated at compile if it's an env reachable from the func).
// ;
// ;
// ; Do note a key thing to be avoided is allowing any non-val inside a comb, since that can cause a fake env's ID to
// ; reference the wrong env/comb in the chain.
// ; We do allow calling eval with a fake env, but since it's only callable withbody value and is strict (by calling this)
// ; about it's return conditions, and the env it's called with must be ok in the chain, and eval doesn't introduce a new scope, it works ok.
// ; We do have to be careful about allowing returned later symbols from it though, since it could be an entirely different env chain.
// (true false)
// )
//))
}
pub fn partial_eval(bctx_in: BCtx, dctx_in: DCtx, form: Rc<MarkedForm>) -> Result<(BCtx,Rc<MarkedForm>), String> {
let mut bctx = bctx_in;
let mut dctx = dctx_in;
let mut next_form = Some(form);
let mut force = false;
loop {
let x = next_form.take().unwrap();
//println!("{:ident$}PE: {}", "", x, ident=dctx.ident*4);
if !(force || dctx.can_progress(x.ids())) {
//println!("{:ident$}Shouldn't go!", "", ident=dctx.ident*4);
return Ok((bctx, x));
}
let forced = force;
force = false;
// TODO: check all these error propegating ?'s, maybe they shouldn't propegate but instead
// reform?
match &*x {
MarkedForm::Pair(h,ids,car,cdr) => {
let (bctxp, car) = partial_eval(bctx, dctx.clone(), Rc::clone(car))?;
let (bctxp, cdr) = partial_eval(bctxp, dctx.clone(), Rc::clone(cdr))?;
bctx = bctxp;
next_form = Some(MarkedForm::new_pair(car, cdr));
},
MarkedForm::SuspendedSymbol(name) => {
let mut t = Rc::clone(&dctx.e);
while name != t.car()?.car()?.sym()? {
t = t.cdr()?;
}
next_form = Some(t.car()?.cdr()?.tag_name(name));
},
MarkedForm::SuspendedEnvLookup { name, id } => {
let v = dctx.sus_env_stack.get(id).expect("suspended env lookup can't progress, should be impossible b/c ids");
next_form = Some(if let Some(name) = name { v.tag_name(name) } else { Rc::clone(v) });
},
MarkedForm::SuspendedParamLookup { name, id, cdr_num, car } => {
let v = dctx.sus_prm_stack.get(id).expect("suspended param lookup can't progress, should be impossible b/c ids");
next_form = Some(if let Some(name) = name { v.tag_name(name) } else { Rc::clone(v) });
},
MarkedForm::SuspendedEnvEval { x, e, .. } => {
// this bit is a little tricky - we'd like to tail call, but we can't lose our env
// if it fails.
let (bctxp, e) = partial_eval(bctx, dctx.clone(), Rc::clone(e))?;
let (bctxp, x) = partial_eval(bctxp, dctx.copy_set_env(&e), Rc::clone(x))?;
bctx = bctxp;
if x.is_value() {
next_form = Some(x);
} else {
next_form = Some(MarkedForm::new_suspended_env_eval(x, e));
}
// Note also that we drop redundent vevals at the bottom of the loop tail-call loop
// with force
},
MarkedForm::SuspendedIf { c, t, e, .. } => {
let (bctxp, c) = partial_eval(bctx, dctx.clone(), Rc::clone(c))?;
if let Ok(b) = c.truthy() {
bctx = bctxp;
if b {
next_form = Some(Rc::clone(t));
} else {
next_form = Some(Rc::clone(e));
}
} else {
// TODO: Need to add hash checking to this one
let (bctxp, t) = partial_eval(bctxp, dctx.clone(), Rc::clone(t))?;
let (bctxp, e) = partial_eval(bctxp, dctx.clone(), Rc::clone(e))?;
bctx = bctxp;
next_form = Some(MarkedForm::new_suspended_if(c,t,e));
}
},
MarkedForm::DeriComb { hash, lookup_name, ids, se, de, id, wrap_level, sequence_params, rest_params, body } => {
// TODO: figure out wrap level, sequence params, etc
if forced || !se.ids().needs_nothing() {
let old_se_ids = se.ids();
let se = if !se.ids().needs_nothing() {
// the current env is our new se
Rc::clone(&dctx.e)
} else {
Rc::clone(se)
};
let ident_amount = dctx.ident*4;
if let Ok(inner_dctx) = dctx.copy_push_frame(id.clone(), &se, &de, None, &rest_params, None, body) {
//println!("{:ident$}Doing a body deri for {:?} because ({} || {:?}) which is {}", "", lookup_name, forced, old_se_ids, x, ident=ident_amount);
let (bctxp, body) = partial_eval(bctx, inner_dctx, Rc::clone(&body))?;
//println!("{:ident$}result was {}", "", body, ident=ident_amount);
bctx = bctxp;
next_form = Some(MarkedForm::new_deri_comb(se, lookup_name.clone(), de.clone(), id.clone(), *wrap_level, sequence_params.clone(), rest_params.clone(), body));
} else {
return Ok((bctx, x));
}
} else {
return Ok((bctx, x));
}
// Should this be this, or replacement with e?
//let ident_amount = dctx.ident*4;
//let (bctxp, se) = partial_eval(bctx, dctx.clone(), Rc::clone(&se))?;
//bctx = bctxp;
//if let Ok(inner_dctx) = dctx.copy_push_frame(id.clone(), &se, &de, None, &rest_params, None, body) {
//println!("{:ident$}Doing a body deri for {:?} which is {}", "", lookup_name, x, ident=ident_amount);
//let (bctxp, body) = partial_eval(bctx, inner_dctx, Rc::clone(&body))?;
//bctx = bctxp;
//next_form = Some(MarkedForm::new_deri_comb(se, lookup_name.clone(), de.clone(), id.clone(), *wrap_level, sequence_params.clone(), rest_params.clone(), body));
//} else {
//// Not 100% sure the rec-hash hit is legitamate
//println!("{:ident$}Rec hash stopped deri for {:?} which is {}", "", lookup_name, x, ident=ident_amount);
//return Ok((bctx, x));
//}
},
MarkedForm::SuspendedPair { hash, ids, attempted, car, cdr } => {
let (bctxp, mut car) = partial_eval(bctx, dctx.clone(), Rc::clone(car))?;
let (bctxp, mut cdr) = partial_eval(bctxp, dctx.clone(), Rc::clone(cdr))?;
bctx = bctxp;
let mut new_attempted = attempted.clone();
let mut maybe_rec_hash = None;
let mut return_ok = false;
while let Some(wrap_level) = car.wrap_level() {
if wrap_level > 0 {
fn map_unval_peval(bctx: BCtx, dctx: DCtx, x: Rc<MarkedForm>) -> Result<(BCtx,Rc<MarkedForm>),String> {
match &*x {
MarkedForm::Pair(h, ids, x_car, x_cdr) => {
let (bctx, new_x_car) = partial_eval(bctx, dctx.clone(), x_car.unval()?)?;
let (bctx, new_x_cdr) = map_unval_peval(bctx, dctx.clone(), Rc::clone(x_cdr))?;
return Ok((bctx, MarkedForm::new_pair(new_x_car, new_x_cdr)));
},
MarkedForm::Nil => return Ok((bctx,x)),
_ => return Err("not a list".to_owned()),
}
}
if let Ok((new_bctx, new_cdr)) = map_unval_peval(bctx.clone(), dctx.clone(), Rc::clone(&cdr)) {
car = car.decrement_wrap_level().unwrap();
cdr = new_cdr;
bctx = new_bctx;
} else {
break;
}
} else {
// check to see if can do call
// We might want to enable not pure values for cons/car/cdr?
if !cdr.is_value() {
break;
}
match &*car {
MarkedForm::PrimComb { name, takes_de, wrap_level, f} => {
new_attempted = Attempted::True(if *takes_de { Some(dctx.e.ids()) } else { None });
let ident_amount = dctx.ident*4;
//println!("{:ident$}doing a call eval of {}", "", name, ident=ident_amount);
//println!("{:ident$}parameters {} are a val because {:?}", "", cdr, cdr.ids(), ident=ident_amount);
match f(bctx.clone(), dctx.clone(), Rc::clone(&cdr)) {
Ok((bctxp, r)) => {
bctx = bctxp;
next_form = Some(r);
// TODO: figure out how to drop this to the bottom of the
// loop for redundent veval elim
return_ok = true;
// might be a tail call
force = true;
},
Err(msg) => {
println!("{:ident$}failed {:?}", "", msg, ident=ident_amount);
},
}
}
MarkedForm::DeriComb { hash, lookup_name, ids, se, de, id, wrap_level, sequence_params, rest_params, body } => {
new_attempted = Attempted::True(if de.is_some() { Some(dctx.e.ids()) } else { None });
// not yet supporting sequence params
match dctx.copy_push_frame(id.clone(), &se, &de, Some(Rc::clone(&dctx.e)), &rest_params, Some(Rc::clone(&cdr)), body) {
Ok(inner_dctx) => {
let ident_amount = inner_dctx.ident*4;
//println!("{:ident$}doing a call eval of {} in {}", "", body, inner_dctx.e, ident=inner_dctx.ident*4);
//println!("{:ident$}doing a call eval of {:?}", "", lookup_name, ident=ident_amount);
//println!("{:ident$}with_parameters {}", "", cdr, ident=ident_amount);
//Here is where we could do a tail call instead, but there
//would be no recovery back into the call-form...
match partial_eval(bctx.clone(), inner_dctx, Rc::clone(body)) {
Ok((bctxp, r)) => {
if combiner_return_ok(Rc::clone(&r), id.clone()) {
// here also shouldn't be able to continue, but
// just in case
bctx = bctxp;
next_form = Some(r);
return_ok = true;
} else {
//println!("{:ident$}combiner return not ok {}", "", r, ident=ident_amount);
}
}
Err(msg) => {
println!("{:ident$}failed {:?}", "", msg, ident=ident_amount);
}
}
},
Err(rec_stop_hash) => {
maybe_rec_hash = Some(rec_stop_hash);
},
}
},
_ => {},
}
break;
}
}
if !return_ok {
next_form = Some(MarkedForm::new_suspended_pair( new_attempted, car, cdr, maybe_rec_hash ));
}
},
// Values should never get here b/c ids UNLESS FORCE HAH
_ => return Ok((bctx, x)),
}
// basic Drop redundent veval
// Old one was recursive over parameters to combs, which we might need, since the redundent veval isn't captured by
// ids. TODO!
if let Some(form) = next_form.as_ref() {
if let MarkedForm::SuspendedEnvEval { x, e, .. } = &**form {
if (*e == dctx.e) {
next_form = Some(Rc::clone(x));
force = true;
}
}
}
}
}

View File

@@ -4,7 +4,11 @@ lalrpop_mod!(pub grammar);
use std::rc::Rc; use std::rc::Rc;
mod ast; mod ast;
use crate::ast::{mark,partial_eval,new_base_ctxs,eval,root_env,MarkedForm,Form,PossibleTailCall}; use crate::ast::{eval,root_env};
mod pe_ast;
use crate::pe_ast::{mark,partial_eval,new_base_ctxs};
mod test;
fn main() { fn main() {
@@ -15,575 +19,8 @@ fn main() {
let (bctx, marked) = mark(Rc::clone(&parsed_input),bctx); let (bctx, marked) = mark(Rc::clone(&parsed_input),bctx);
let unvaled = marked.unval().unwrap(); let unvaled = marked.unval().unwrap();
println!("Parsed unvaled that is {}", unvaled); println!("Parsed unvaled that is {}", unvaled);
match partial_eval(bctx, dctx, unvaled) { let (bctx, ped) = partial_eval(bctx, dctx, unvaled).unwrap();
Ok((bctx, ped)) => println!("Parsed unvaled pe that is {}", ped),
Err(e) => println!("Partial evaluation error {}", e),
};
let result = eval(root_env(), parsed_input); let result = eval(root_env(), parsed_input);
println!("Result is {} - {:?}", result, result); println!("Result is {} - {:?}", result, result);
} }
#[test]
fn parse_test() {
let g = grammar::TermParser::new();
for test in [
"22", "(22)", "(((22)))",
"(22 )", "()", "( )", "( 44)", "(44 )",
"(22 44 (1) 33 (4 5 (6) 6))", "hello",
"-", "+", "(+ 1 ;hi
3)", "'13", "hello-world", "_",
] {
assert!(g.parse(test).is_ok());
}
assert!(g.parse("((22)").is_err());
}
fn eval_test<T: Into<Form>>(also_pe: bool, gram: &grammar::TermParser, e: &Rc<Form>, code: &str, expected: T) {
println!("Doing test {}", code);
let parsed = Rc::new(gram.parse(code).unwrap());
let basic_result = eval(Rc::clone(e), Rc::clone(&parsed));
assert_eq!(*basic_result, expected.into());
if also_pe {
let (bctx, dctx) = new_base_ctxs();
let (bctx, marked) = mark(parsed,bctx);
let unvaled = marked.unval().unwrap();
let (bctx, ped) = partial_eval(bctx, dctx, unvaled).unwrap();
let (bctx, marked_basic_result) = mark(basic_result,bctx);
println!("Final PE {}", ped);
println!("wanted {}", marked_basic_result);
assert_eq!(*ped, *marked_basic_result);
}
}
#[test]
fn basic_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, "(+ 2 (car (cons 4 '(1 2))))", 6);
eval_test(true, &g, &e, "(= 17 ((vau d p (+ (eval (car p) d) 13)) (+ 1 3)))", true);
eval_test(true, &g, &e, "(if (= 2 2) (+ 1 2) (+ 3 4))", 3);
eval_test(true, &g, &e, "(quote a)", "a");
eval_test(true, &g, &e, "'a", "a");
eval_test(true, &g, &e, "'(1 . a)", (1, "a"));
eval_test(true, &g, &e, "'(1 a)", (1, ("a", Form::Nil)));
eval_test(true, &g, &e, "true", true);
eval_test(true, &g, &e, "false", false);
eval_test(true, &g, &e, "nil", Form::Nil);
eval_test(true, &g, &e, "(+ 1 2)", 3);
eval_test(true, &g, &e, "(- 1 2)", -1);
eval_test(true, &g, &e, "(* 1 2)", 2);
eval_test(true, &g, &e, "(/ 4 2)", 2);
eval_test(true, &g, &e, "(% 3 2)", 1);
eval_test(true, &g, &e, "(& 3 2)", 2);
eval_test(true, &g, &e, "(| 2 1)", 3);
eval_test(true, &g, &e, "(^ 2 1)", 3);
eval_test(true, &g, &e, "(^ 3 1)", 2);
eval_test(true, &g, &e, "(< 3 1)", false);
eval_test(true, &g, &e, "(<= 3 1)", false);
eval_test(true, &g, &e, "(> 3 1)", true);
eval_test(true, &g, &e, "(>= 3 1)", true);
eval_test(true, &g, &e, "(comb? +)", true);
eval_test(true, &g, &e, "(comb? (vau d p 1))", true);
eval_test(true, &g, &e, "(comb? 1)", false);
eval_test(true, &g, &e, "(pair? '(a))", true);
//eval_test(true, &g, &e, "(pair? '())", true);
eval_test(true, &g, &e, "(nil? nil)", true);
eval_test(true, &g, &e, "(nil? 1)", false);
eval_test(true, &g, &e, "(pair? 1)", false);
eval_test(true, &g, &e, "(symbol? 'a)", true);
eval_test(true, &g, &e, "(symbol? 1)", false);
eval_test(true, &g, &e, "(int? 1)", true);
eval_test(true, &g, &e, "(int? true)", false);
eval_test(true, &g, &e, "(bool? true)", true);
eval_test(true, &g, &e, "(bool? 1)", false);
eval_test(true, &g, &e, "!(bool?) 1", false);
eval_test(true, &g, &e, "!(bool?) true", true);
eval_test(true, &g, &e, "((vau root_env _ (eval 'a (cons (cons 'a 2) root_env))))", 2);
eval_test(true, &g, &e, "'name-dash", "name-dash");
}
use once_cell::sync::Lazy;
static LET: Lazy<String> = Lazy::new(|| {
"!((vau root_env p (eval (car p)
(cons (cons 'let1
(vau de p (eval (car (cdr (cdr p))) (cons (cons (car p) (eval (car (cdr p)) de)) de)))
) root_env))))".to_owned()
});
#[test]
fn fib_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (let1 x 10 (+ x 7))", *LET), 17);
let def_fib = "
!(let1 fib (vau de p
!(let1 self (eval (car p) de))
!(let1 n (eval (car (cdr p)) de))
!(if (= 0 n) 0)
!(if (= 1 n) 1)
(+ (self self (- n 1)) (self self (- n 2)))
))";
eval_test(true, &g, &e, &format!("{} {} (fib fib 6)", *LET, def_fib), 8);
}
#[test]
fn fact_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
let def_fact = "
!(let1 fact (vau de p
!(let1 self (eval (car p) de))
!(let1 n (eval (car (cdr p)) de))
!(if (= 0 n) 1)
(* n (self self (- n 1)))
))";
eval_test(true, &g, &e, &format!("{} {} (fact fact 6)", *LET, def_fact), 720);
}
static VAPPLY: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 vapply (vau de p
!(let1 f (eval (car p) de))
!(let1 ip (eval (car (cdr p)) de))
!(let1 nde (eval (car (cdr (cdr p))) de))
(eval (cons f ip) nde)
))", *LET)
});
#[test]
fn vapply_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
// need the vapply to keep env in check because otherwise the env keeps growing
// and the Rc::drop will overflow the stack lol
let def_badid = format!("
{}
!(let1 badid (vau de p
!(let1 inner (vau ide ip
!(let1 self (car ip))
!(let1 n (car (cdr ip)))
!(let1 acc (car (cdr (cdr ip))))
!(if (= 0 n) acc)
(vapply self (cons self (cons (- n 1) (cons (+ acc 1) nil))) de)
))
(vapply inner (cons inner (cons (eval (car p) de) (cons 0 nil))) de)
))", *VAPPLY);
// Won't work unless tail calls work
// so no PE?
eval_test(false, &g, &e, &format!("{} (badid 1000)", def_badid), 1000);
}
static VMAP: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 vmap (vau de p
!(let1 vmap_inner (vau ide ip
!(let1 self (car ip))
!(let1 f (car (cdr ip)))
!(let1 l (car (cdr (cdr ip))))
!(if (= nil l) l)
(cons (vapply f (cons (car l) nil) de) (vapply self (cons self (cons f (cons (cdr l) nil))) de))
))
(vapply vmap_inner (cons vmap_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) nil))) de)
))", *VAPPLY)
});
#[test]
fn vmap_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
// Maybe define in terms of a right fold?
//eval_test(true, &g, &e, &format!("{} (vmap (vau de p (+ 1 (car p))) '(1 2 3))", *VMAP), (2, (3, (4, Form::Nil))));
eval_test(true, &g, &e, &format!("{} (vmap (vau de p (+ 1 (car p))) '(1))", *VMAP), (2, Form::Nil));
}
static WRAP: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 wrap (vau de p
!(let1 f (eval (car p) de))
(vau ide p (vapply f (vmap (vau _ xp (eval (car xp) ide)) p) ide))
))", *VMAP)
});
#[test]
fn wrap_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
// Make sure (wrap (vau ...)) and internal style are optimized the same
eval_test(true, &g, &e, &format!("{} ((wrap (vau _ p (+ (car p) 1))) (+ 1 2))", *WRAP), 4);
}
static UNWRAP: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 unwrap (vau de p
!(let1 f (eval (car p) de))
(vau ide p (vapply f (vmap (vau _ xp (cons quote (cons (car xp) nil))) p) ide))
))", *WRAP)
});
#[test]
fn unwrap_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
// Can't represent prims in tests :( - they do work though, uncommenting and checking the
// failed assert verifies
//eval_test(true, &g, &e, &format!("{} ((unwrap (vau de p (car p))) (+ 1 2))", def_unwrap), ("quote", (("+", (1, (2, Form::Nil))), Form::Nil)));
//eval_test(true, &g, &e, &format!("{} ((unwrap (vau de p (eval (car p) de))) (+ 1 2))", def_unwrap), (("+", (1, (2, Form::Nil))), Form::Nil));
eval_test(true, &g, &e, &format!("{} ((unwrap (vau de p (eval (eval (car p) de) de))) (+ 1 2))", *UNWRAP), 3);
eval_test(true, &g, &e, &format!("{} ((unwrap (vau de p (+ (eval (eval (car p) de) de) 1))) (+ 1 2))", *UNWRAP), 4);
}
static LAPPLY: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 lapply (vau de p
!(let1 f (eval (car p) de))
!(let1 ip (eval (car (cdr p)) de))
!(let1 nde (eval (car (cdr (cdr p))) de))
(eval (cons (unwrap f) ip) nde)
))", *UNWRAP)
});
#[test]
fn lapply_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
// Should this allow envs at all? It technically can, but I feel like it kinda goes against the
// sensible deriviation
let def_lbadid = format!("
{}
!(let1 lbadid (vau de p
!(let1 inner (wrap (vau ide ip
!(let1 self (car ip))
!(let1 n (car (cdr ip)))
!(let1 acc (car (cdr (cdr ip))))
!(if (= 0 n) acc)
(lapply self (cons self (cons (- n 1) (cons (+ acc 1) nil))) de)
)))
(lapply inner (cons inner (cons (eval (car p) de) (cons 0 nil))) de)
))", *LAPPLY);
// Won't work unless tail calls work
// takes a while though
eval_test(false, &g, &e, &format!("{} (lbadid 1000)", def_lbadid), 1000);
}
static VFOLDL: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 vfoldl (vau de p
!(let1 vfoldl_inner (vau ide ip
!(let1 self (car ip))
!(let1 f (car (cdr ip)))
!(let1 a (car (cdr (cdr ip))))
!(let1 l (car (cdr (cdr (cdr ip)))))
!(if (= nil l) a)
(vapply self (cons self (cons f (cons (vapply f (cons a (cons (car l) nil)) de) (cons (cdr l) nil)))) de)
))
(vapply vfoldl_inner (cons vfoldl_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) (cons (eval (car (cdr (cdr p))) de) nil)))) de)
))", *LAPPLY)
});
#[test]
fn vfoldl_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (vfoldl (vau de p (+ (car p) (car (cdr p)))) 0 '(1 2 3))", *VFOLDL), 6);
}
static ZIPD: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 zipd (vau de p
!(let1 zipd_inner (vau ide ip
!(let1 self (car ip))
!(let1 a (car (cdr ip)))
!(let1 b (car (cdr (cdr ip))))
!(if (= nil a) a)
!(if (= nil b) b)
(cons (cons (car a) (car b)) (vapply self (cons self (cons (cdr a) (cons (cdr b) nil))) de))
))
(vapply zipd_inner (cons zipd_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) nil))) de)
))", *VFOLDL)
});
#[test]
fn zipd_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (zipd '(1 2 3) '(4 5 6))", *ZIPD), ((1,4), ((2,5), ((3,6), Form::Nil))));
}
static CONCAT: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 concat (vau de p
!(let1 concat_inner (vau ide ip
!(let1 self (car ip))
!(let1 a (car (cdr ip)))
!(let1 b (car (cdr (cdr ip))))
!(if (= nil a) b)
(cons (car a) (vapply self (cons self (cons (cdr a) (cons b nil))) de))
))
(vapply concat_inner (cons concat_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) nil))) de)
))", *ZIPD)
});
#[test]
fn concat_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (concat '(1 2 3) '(4 5 6))", *CONCAT), (1, (2, (3, (4, (5, (6, Form::Nil)))))));
}
static BVAU: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 match_params (wrap (vau 0 p
!(let1 self (car p))
!(let1 p_ls (car (cdr p)))
!(let1 dp (car (cdr (cdr p))))
!(let1 e (car (cdr (cdr (cdr p)))))
!(if (= nil p_ls) (assert (= nil dp) e))
!(if (symbol? p_ls) (cons (cons p_ls dp) e))
(self self (cdr p_ls) (cdr dp) (self self (car p_ls) (car dp) e))
)))
!(let1 bvau (vau se p
(if (= nil (cdr (cdr p)))
; No de case
!(let1 p_ls (car p))
!(let1 b_v (car (cdr p)))
(vau 0 dp
(eval b_v (match_params match_params p_ls dp se))
)
; de case
!(let1 de_s (car p))
!(let1 p_ls (car (cdr p)))
!(let1 b_v (car (cdr (cdr p))))
(vau dde dp
(eval b_v (match_params match_params p_ls dp (cons (cons de_s dde) se)))
)
)
))", *CONCAT)
});
#[test]
fn bvau_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} ((bvau _ (a b c) (+ a (- b c))) 10 2 3)", *BVAU), 9);
eval_test(true, &g, &e, &format!("{} ((bvau (a b c) (+ a (- b c))) 10 2 3)", *BVAU), 9);
eval_test(true, &g, &e, &format!("{} ((bvau (a b . c) c) 10 2 3)", *BVAU), (3, Form::Nil));
eval_test(true, &g, &e, &format!("{} ((bvau (a b . c) c) 10 2)", *BVAU), Form::Nil);
eval_test(true, &g, &e, &format!("{} ((bvau (a b . c) c) 10 2 3 4 5)", *BVAU), (3, (4, (5, Form::Nil))));
eval_test(true, &g, &e, &format!("{} ((bvau c c) 3 4 5)", *BVAU), (3, (4, (5, Form::Nil))));
eval_test(true, &g, &e, &format!("{} ((bvau c c))", *BVAU), Form::Nil);
eval_test(true, &g, &e, &format!("{} ((bvau ((a b) . c) c) (10 2) 3 4 5)", *BVAU), (3, (4, (5, Form::Nil))));
eval_test(true, &g, &e, &format!("{} ((bvau ((a b) . c) a) (10 2) 3 4 5)", *BVAU), 10);
eval_test(true, &g, &e, &format!("{} ((bvau ((a b) . c) b) (10 2) 3 4 5)", *BVAU), 2);
eval_test(true, &g, &e, &format!("{} ((wrap (bvau _ (a b c) (+ a (- b c)))) (+ 10 1) (+ 2 2) (+ 5 3))", *BVAU), 7);
eval_test(true, &g, &e, &format!("{} ((wrap (bvau (a b c) (+ a (- b c)))) (+ 10 1) (+ 2 2) (+ 5 3))", *BVAU), 7);
}
static LAMBDA: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 lambda (vau de p
(wrap (vapply bvau p de))
))", *BVAU)
});
#[test]
fn lambda_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} ((lambda (a b c) (+ a (- b c))) (+ 10 1) (+ 2 2) (+ 5 3))", *LAMBDA), 7);
eval_test(true, &g, &e, &format!("{} ((lambda (a b . c) c) 10 2 3)", *LAMBDA), (3, Form::Nil));
eval_test(true, &g, &e, &format!("{} ((lambda (a b . c) c) 10 2)", *LAMBDA), Form::Nil);
eval_test(true, &g, &e, &format!("{} ((lambda (a b . c) c) 10 2 3 4 5)", *LAMBDA), (3, (4, (5, Form::Nil))));
eval_test(true, &g, &e, &format!("{} ((lambda c c) 3 4 5)", *LAMBDA), (3, (4, (5, Form::Nil))));
eval_test(true, &g, &e, &format!("{} ((lambda c c))", *LAMBDA), Form::Nil);
eval_test(true, &g, &e, &format!("{} ((lambda ((a b) . c) c) '(10 2) 3 4 5)", *LAMBDA), (3, (4, (5, Form::Nil))));
eval_test(true, &g, &e, &format!("{} ((lambda ((a b) . c) a) '(10 2) 3 4 5)", *LAMBDA), 10);
eval_test(true, &g, &e, &format!("{} ((lambda ((a b) . c) b) '(10 2) 3 4 5)", *LAMBDA), 2);
eval_test(true, &g, &e, &format!("{} ((lambda ((a b . c) d) b) '(10 2 3 4) 3)", *LAMBDA), 2);
eval_test(true, &g, &e, &format!("{} ((lambda ((a b . c) d) c) '(10 2 3 4) 3)", *LAMBDA), (3, (4, Form::Nil)));
// should fail
//eval_test(true, &g, &e, &format!("{} ((lambda (a b c) c) 10 2 3 4)", *LAMBDA), 3);
}
static LET2: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 let1 (bvau dp (s v b)
(eval b (match_params match_params s (eval v dp) dp))
))
", *LAMBDA)
});
#[test]
fn let2_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (let1 x (+ 10 1) (+ x 1))", *LET2), 12);
eval_test(true, &g, &e, &format!("{} (let1 x '(10 1) x)", *LET2), (10, (1, Form::Nil)));
eval_test(true, &g, &e, &format!("{} (let1 (a b) '(10 1) a)", *LET2), 10);
eval_test(true, &g, &e, &format!("{} (let1 (a b) '(10 1) b)", *LET2), 1);
eval_test(true, &g, &e, &format!("{} (let1 (a b . c) '(10 1) c)", *LET2), Form::Nil);
eval_test(true, &g, &e, &format!("{} (let1 (a b . c) '(10 1 2 3) c)", *LET2), (2, (3, Form::Nil)));
eval_test(true, &g, &e, &format!("{} (let1 ((a . b) . c) '((10 1) 2 3) a)", *LET2), 10);
eval_test(true, &g, &e, &format!("{} (let1 ((a . b) . c) '((10 1) 2 3) b)", *LET2), (1, Form::Nil));
// should fail
//eval_test(true, &g, &e, &format!("{} (let1 (a b c) '(10 2 3 4) a)", *LET2), 10);
}
static LIST: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 list (lambda args args))
", *LET2)
});
#[test]
fn list_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (list 1 2 (+ 3 4))", *LIST), (1, (2, (7, Form::Nil))));
}
static Y: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 Y (lambda (f3)
((lambda (x1) (x1 x1))
(lambda (x2) (f3 (wrap (vau app_env y (lapply (x2 x2) y app_env)))))))
)
", *LIST)
});
#[test]
fn y_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} ((Y (lambda (recurse) (lambda (n) (if (= 0 n) 1 (* n (recurse (- n 1))))))) 5)", *Y), 120);
}
static RLAMBDA: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 rlambda (bvau se (n p b)
(eval (list Y (list lambda (list n) (list lambda p b))) se)
))
", *Y)
});
#[test]
fn rlambda_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} ((rlambda recurse (n) (if (= 0 n) 1 (* n (recurse (- n 1))))) 5)", *RLAMBDA), 120);
}
static AND_OR: Lazy<String> = Lazy::new(|| {
// need to extend for varidac
format!("
{}
!(let1 and (bvau se (a b)
!(let1 ae (eval a se))
(if ae (eval b se) ae)
))
!(let1 or (bvau se (a b)
!(let1 ae (eval a se))
(if ae ae (eval b se))
))
", *RLAMBDA)
});
#[test]
fn and_or_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (and true true)", *AND_OR), true);
eval_test(true, &g, &e, &format!("{} (and false true)", *AND_OR), false);
eval_test(true, &g, &e, &format!("{} (and true false)", *AND_OR), false);
eval_test(true, &g, &e, &format!("{} (and false false)", *AND_OR), false);
eval_test(true, &g, &e, &format!("{} (or true true)", *AND_OR), true);
eval_test(true, &g, &e, &format!("{} (or false true)", *AND_OR), true);
eval_test(true, &g, &e, &format!("{} (or true false)", *AND_OR), true);
eval_test(true, &g, &e, &format!("{} (or false false)", *AND_OR), false);
}
static LEN: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 len (lambda (l)
!(let1 len_helper (rlambda len_helper (l a)
(if (pair? l) (len_helper (cdr l) (+ 1 a))
a)
))
(len_helper l 0)
))
", *AND_OR)
});
#[test]
fn len_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (len '())", *LEN), 0);
eval_test(true, &g, &e, &format!("{} (len '(1))", *LEN), 1);
eval_test(true, &g, &e, &format!("{} (len '(1 2))", *LEN), 2);
eval_test(true, &g, &e, &format!("{} (len '(1 2 3))", *LEN), 3);
}
static MATCH: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 match (bvau de (x . cases)
!(let1 evaluate_case (rlambda evaluate_case (access c)
!(if (symbol? c) (list true (lambda (b) (list let1 c access b))))
!(if (and (pair? c) (= 'unquote (car c))) (list (list = access (car (cdr c))) (lambda (b) b)))
!(if (and (pair? c) (= 'quote (car c))) (list (list = access c) (lambda (b) b)))
!(if (pair? c)
!(let1 tests (list and (list pair? access) (list = (len c) (list len access))))
!(let1 (tests body_func) ((rlambda recurse (c tests access body_func) (if (pair? c)
!(let1 (inner_test inner_body_func) (evaluate_case (list car access) (car c)))
(recurse (cdr c)
(list and tests inner_test)
(list cdr access)
(lambda (b) (body_func (inner_body_func b))))
; else
(list tests body_func)
))
c tests access (lambda (b) b)))
(list tests body_func))
(list (list = access c) (lambda (b) b))
))
!(let1 helper (rlambda helper (x_sym cases) (if (= nil cases) (list assert false)
(let1 (test body_func) (evaluate_case x_sym (car cases))
(concat (list if test (body_func (car (cdr cases)))) (list (helper x_sym (cdr (cdr cases)))))))))
(eval (list let1 '___MATCH_SYM x (helper '___MATCH_SYM cases)) de)
;!(let1 expanded (list let1 '___MATCH_SYM x (helper '___MATCH_SYM cases)))
;(debug expanded (eval expanded de))
))
", *LEN)
});
#[test]
fn match_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (match (+ 1 2) 1 2 2 3 3 4 _ 0)", *MATCH), 4);
eval_test(true, &g, &e, &format!("{} (match '(1 2) 1 2 2 3 3 4 _ 0)", *MATCH), 0);
eval_test(true, &g, &e, &format!("{} (match '(1 2) 1 2 2 3 (a b) (+ a (+ 2 b)) _ 0)", *MATCH), 5);
eval_test(true, &g, &e, &format!("{} (match '(1 2) 1 2 2 3 '(1 2) 7 _ 0)", *MATCH), 7);
eval_test(true, &g, &e, &format!("{} (let1 a 70 (match (+ 60 10) (unquote a) 100 2 3 _ 0))", *MATCH), 100);
}
static RBTREE: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 empty (list 'B nil nil nil))
!(let1 E empty)
!(let1 EE (list 'BB nil nil nil))
!(let1 generic-foldl (rlambda generic-foldl (f z t) (match t
(unquote E) z
(c a x b) !(let1 new_left_result (generic-foldl f z a))
!(let1 folded (f new_left_result x))
(generic-foldl f folded b))))
!(let1 blacken (lambda (t) (match t
('R a x b) (list 'B a x b)
t t)))
!(let1 balance (lambda (t) (match t
; figures 1 and 2
('B ('R ('R a x b) y c) z d) (list 'R (list 'B a x b) y (list 'B c z d))
('B ('R a x ('R b y c)) z d) (list 'R (list 'B a x b) y (list 'B c z d))
('B a x ('R ('R b y c) z d)) (list 'R (list 'B a x b) y (list 'B c z d))
('B a x ('R b y ('R c z d))) (list 'R (list 'B a x b) y (list 'B c z d))
; figure 8, double black cases
('BB ('R a x ('R b y c)) z d) (list 'B (list 'B a x b) y (list 'B c z d))
('BB a x ('R ('R b y c) z d)) (list 'B (list 'B a x b) y (list 'B c z d))
; already balenced
t t)))
!(let1 map-insert !(let1 ins (rlambda ins (t k v) (match t
(unquote E) (list 'R t (list k v) t)
(c a x b) !(if (< k (car x)) (balance (list c (ins a k v) x b)))
!(if (= k (car x)) (list c a (list k v) b))
(balance (list c a x (ins b k v))))))
(lambda (t k v) (blacken (ins t k v))))
!(let1 map-empty empty)
!(let1 make-test-tree (rlambda make-test-tree (n t) (if (<= n 0) t
(make-test-tree (- n 1) (map-insert t n (= 0 (% n 10)))))))
!(let1 reduce-test-tree (lambda (tree) (generic-foldl (lambda (a x) (if (car (cdr x)) (+ a 1) a)) 0 tree)))
", *MATCH)
});
#[test]
fn rbtree_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(false, &g, &e, &format!("{} (reduce-test-tree (make-test-tree 10 map-empty))", *RBTREE), 1);
//eval_test(false, &g, &e, &format!("{} (reduce-test-tree (make-test-tree 20 map-empty))", *RBTREE), 2);
}

1042
kr/src/pe_ast.rs Normal file

File diff suppressed because it is too large Load Diff

617
kr/src/test.rs Normal file
View File

@@ -0,0 +1,617 @@
use std::rc::Rc;
use crate::grammar;
use crate::ast::{eval,root_env,Form,PossibleTailCall};
use crate::pe_ast::{mark,partial_eval,new_base_ctxs,MarkedForm};
#[test]
fn parse_test() {
let g = grammar::TermParser::new();
for test in [
"22", "(22)", "(((22)))",
"(22 )", "()", "( )", "( 44)", "(44 )",
"(22 44 (1) 33 (4 5 (6) 6))", "hello",
"-", "+", "(+ 1 ;hi
3)", "'13", "hello-world", "_",
] {
assert!(g.parse(test).is_ok());
}
assert!(g.parse("((22)").is_err());
}
fn eval_test<T: Into<Form>>(also_pe: bool, gram: &grammar::TermParser, e: &Rc<Form>, code: &str, expected: T) {
println!("Doing test {}", code);
let parsed = Rc::new(gram.parse(code).unwrap());
let basic_result = eval(Rc::clone(e), Rc::clone(&parsed));
assert_eq!(*basic_result, expected.into());
if also_pe {
let (bctx, dctx) = new_base_ctxs();
let (bctx, marked) = mark(parsed,bctx);
let unvaled = marked.unval().unwrap();
let (bctx, ped) = partial_eval(bctx, dctx, unvaled).unwrap();
let (bctx, marked_basic_result) = mark(basic_result,bctx);
println!("Final PE {}", ped);
println!("wanted {}", marked_basic_result);
assert_eq!(*ped, *marked_basic_result);
}
}
fn partial_eval_test(gram: &grammar::TermParser, code: &str, expected: &str) {
println!("Doing PE test {}", code);
let parsed = Rc::new(gram.parse(code).unwrap());
let (bctx, dctx) = new_base_ctxs();
let (bctx, marked) = mark(parsed,bctx);
let unvaled = marked.unval().unwrap();
let (bctx, ped) = partial_eval(bctx, dctx, unvaled).unwrap();
println!("Final PE {}", ped);
println!("wanted {}", expected);
assert_eq!(format!("{}", ped), expected);
}
#[test]
fn basic_pe_test() {
let g = grammar::TermParser::new();
partial_eval_test(&g, "(+ 2 (car (cons 4 '(1 2))))", "6");
partial_eval_test(&g, "(vau 0 p (+ 1 2))", "NeededIds { heads: {}, tails: {}, body_stopped: {}, if_stopped: {} }#[None/None/EnvID(1)/0/[]/Some(\"p\")/3]");
}
#[test]
fn basic_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, "(+ 2 (car (cons 4 '(1 2))))", 6);
eval_test(true, &g, &e, "(= 17 ((vau d p (+ (eval (car p) d) 13)) (+ 1 3)))", true);
eval_test(true, &g, &e, "(if (= 2 2) (+ 1 2) (+ 3 4))", 3);
eval_test(true, &g, &e, "(quote a)", "a");
eval_test(true, &g, &e, "'a", "a");
eval_test(true, &g, &e, "'(1 . a)", (1, "a"));
eval_test(true, &g, &e, "'(1 a)", (1, ("a", Form::Nil)));
eval_test(true, &g, &e, "true", true);
eval_test(true, &g, &e, "false", false);
eval_test(true, &g, &e, "nil", Form::Nil);
eval_test(true, &g, &e, "(+ 1 2)", 3);
eval_test(true, &g, &e, "(- 1 2)", -1);
eval_test(true, &g, &e, "(* 1 2)", 2);
eval_test(true, &g, &e, "(/ 4 2)", 2);
eval_test(true, &g, &e, "(% 3 2)", 1);
eval_test(true, &g, &e, "(& 3 2)", 2);
eval_test(true, &g, &e, "(| 2 1)", 3);
eval_test(true, &g, &e, "(^ 2 1)", 3);
eval_test(true, &g, &e, "(^ 3 1)", 2);
eval_test(true, &g, &e, "(< 3 1)", false);
eval_test(true, &g, &e, "(<= 3 1)", false);
eval_test(true, &g, &e, "(> 3 1)", true);
eval_test(true, &g, &e, "(>= 3 1)", true);
eval_test(true, &g, &e, "(comb? +)", true);
eval_test(true, &g, &e, "(comb? (vau d p 1))", true);
eval_test(true, &g, &e, "(comb? 1)", false);
eval_test(true, &g, &e, "(pair? '(a))", true);
//eval_test(true, &g, &e, "(pair? '())", true);
eval_test(true, &g, &e, "(nil? nil)", true);
eval_test(true, &g, &e, "(nil? 1)", false);
eval_test(true, &g, &e, "(pair? 1)", false);
eval_test(true, &g, &e, "(symbol? 'a)", true);
eval_test(true, &g, &e, "(symbol? 1)", false);
eval_test(true, &g, &e, "(int? 1)", true);
eval_test(true, &g, &e, "(int? true)", false);
eval_test(true, &g, &e, "(bool? true)", true);
eval_test(true, &g, &e, "(bool? 1)", false);
eval_test(true, &g, &e, "!(bool?) 1", false);
eval_test(true, &g, &e, "!(bool?) true", true);
eval_test(true, &g, &e, "((vau root_env _ (eval 'a (cons (cons 'a 2) root_env))))", 2);
eval_test(true, &g, &e, "'name-dash", "name-dash");
}
use once_cell::sync::Lazy;
static LET: Lazy<String> = Lazy::new(|| {
"!((vau root_env p (eval (car p)
(cons (cons 'let1
(vau de p (eval (car (cdr (cdr p))) (cons (cons (car p) (eval (car (cdr p)) de)) de)))
) root_env))))".to_owned()
});
#[test]
fn let_pe_test() {
let g = grammar::TermParser::new();
partial_eval_test(&g, &format!("{} (let1 a 2 (+ a (car (cons 4 '(1 2)))))", *LET), "6");
partial_eval_test(&g, &format!("{} (let1 a 2 (vau 0 p (+ 1 a)))", *LET),"NeededIds { heads: {}, tails: {}, body_stopped: {}, if_stopped: {} }#[None/None/EnvID(3)/0/[]/Some(\"p\")/3]");
partial_eval_test(&g, &format!("{}
!(let1 a 2)
(vau 0 p (+ 1 a))
", *LET), "NeededIds { heads: {}, tails: {}, body_stopped: {}, if_stopped: {} }#[None/None/EnvID(3)/0/[]/Some(\"p\")/3]");
partial_eval_test(&g, &format!("{}
!(let1 a 2)
!(let1 b 5)
(vau 0 p (+ b a))
", *LET), "NeededIds { heads: {}, tails: {}, body_stopped: {}, if_stopped: {} }#[None/None/EnvID(3)/0/[]/Some(\"p\")/7]");
/*
partial_eval_test(&g, &format!("{}
(vau 0 p
!(let1 a 2)
!(let1 b 5)
(+ b a)
)
", *LET), "None({})#[None/None/EnvID(0)/0/[]/Some(\"p\")/7]");
partial_eval_test(&g, &format!("{}
(vau d p
!(let1 a 2)
(+ (eval (car p) d) a)
)
", *LET), "None({})#[None/None/EnvID(2)/0/[]/Some(\"p\")/7]");
*/
}
#[test]
fn fib_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (let1 x 10 (+ x 7))", *LET), 17);
let def_fib = "
!(let1 fib (vau de p
!(let1 self (eval (car p) de))
!(let1 n (eval (car (cdr p)) de))
!(if (= 0 n) 0)
!(if (= 1 n) 1)
(+ (self self (- n 1)) (self self (- n 2)))
))";
eval_test(false, &g, &e, &format!("{} {} (fib fib 6)", *LET, def_fib), 8);
}
#[test]
fn fact_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
let def_fact = "
!(let1 fact (vau de p
!(let1 self (eval (car p) de))
!(let1 n (eval (car (cdr p)) de))
!(if (= 0 n) 1)
(* n (self self (- n 1)))
))";
eval_test(true, &g, &e, &format!("{} {} (fact fact 6)", *LET, def_fact), 720);
}
static VAPPLY: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 vapply (vau de p
!(let1 f (eval (car p) de))
!(let1 ip (eval (car (cdr p)) de))
!(let1 nde (eval (car (cdr (cdr p))) de))
(eval (cons f ip) nde)
))", *LET)
});
#[test]
fn vapply_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
// need the vapply to keep env in check because otherwise the env keeps growing
// and the Rc::drop will overflow the stack lol
let def_badid = format!("
{}
!(let1 badid (vau de p
!(let1 inner (vau ide ip
!(let1 self (car ip))
!(let1 n (car (cdr ip)))
!(let1 acc (car (cdr (cdr ip))))
!(if (= 0 n) acc)
(vapply self (cons self (cons (- n 1) (cons (+ acc 1) nil))) de)
))
(vapply inner (cons inner (cons (eval (car p) de) (cons 0 nil))) de)
))", *VAPPLY);
// Won't work unless tail calls work
// so no PE?
eval_test(false, &g, &e, &format!("{} (badid 1000)", def_badid), 1000);
}
static VMAP: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 vmap (vau de p
!(let1 vmap_inner (vau ide ip
!(let1 self (car ip))
!(let1 f (car (cdr ip)))
!(let1 l (car (cdr (cdr ip))))
!(if (= nil l) l)
(cons (vapply f (cons (car l) nil) de) (vapply self (cons self (cons f (cons (cdr l) nil))) de))
))
(vapply vmap_inner (cons vmap_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) nil))) de)
))", *VAPPLY)
});
#[test]
fn vmap_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
// Maybe define in terms of a right fold?
//eval_test(true, &g, &e, &format!("{} (vmap (vau de p (+ 1 (car p))) '(1 2 3))", *VMAP), (2, (3, (4, Form::Nil))));
eval_test(true, &g, &e, &format!("{} (vmap (vau de p (+ 1 (car p))) '(1))", *VMAP), (2, Form::Nil));
}
static WRAP: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 wrap (vau de p
!(let1 f (eval (car p) de))
(vau ide p (vapply f (vmap (vau _ xp (eval (car xp) ide)) p) ide))
))", *VMAP)
});
#[test]
fn wrap_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
// Make sure (wrap (vau ...)) and internal style are optimized the same
eval_test(true, &g, &e, &format!("{} ((wrap (vau _ p (+ (car p) 1))) (+ 1 2))", *WRAP), 4);
}
static UNWRAP: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 unwrap (vau de p
!(let1 f (eval (car p) de))
(vau ide p (vapply f (vmap (vau _ xp (cons quote (cons (car xp) nil))) p) ide))
))", *WRAP)
});
#[test]
fn unwrap_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
// Can't represent prims in tests :( - they do work though, uncommenting and checking the
// failed assert verifies
//eval_test(true, &g, &e, &format!("{} ((unwrap (vau de p (car p))) (+ 1 2))", def_unwrap), ("quote", (("+", (1, (2, Form::Nil))), Form::Nil)));
//eval_test(true, &g, &e, &format!("{} ((unwrap (vau de p (eval (car p) de))) (+ 1 2))", def_unwrap), (("+", (1, (2, Form::Nil))), Form::Nil));
eval_test(true, &g, &e, &format!("{} ((unwrap (vau de p (eval (eval (car p) de) de))) (+ 1 2))", *UNWRAP), 3);
eval_test(true, &g, &e, &format!("{} ((unwrap (vau de p (+ (eval (eval (car p) de) de) 1))) (+ 1 2))", *UNWRAP), 4);
}
static LAPPLY: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 lapply (vau de p
!(let1 f (eval (car p) de))
!(let1 ip (eval (car (cdr p)) de))
!(let1 nde (eval (car (cdr (cdr p))) de))
(eval (cons (unwrap f) ip) nde)
))", *UNWRAP)
});
#[test]
fn lapply_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
// Should this allow envs at all? It technically can, but I feel like it kinda goes against the
// sensible deriviation
let def_lbadid = format!("
{}
!(let1 lbadid (vau de p
!(let1 inner (wrap (vau ide ip
!(let1 self (car ip))
!(let1 n (car (cdr ip)))
!(let1 acc (car (cdr (cdr ip))))
!(if (= 0 n) acc)
(lapply self (cons self (cons (- n 1) (cons (+ acc 1) nil))) de)
)))
(lapply inner (cons inner (cons (eval (car p) de) (cons 0 nil))) de)
))", *LAPPLY);
// Won't work unless tail calls work
// takes a while though
eval_test(false, &g, &e, &format!("{} (lbadid 1000)", def_lbadid), 1000);
}
static VFOLDL: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 vfoldl (vau de p
!(let1 vfoldl_inner (vau ide ip
!(let1 self (car ip))
!(let1 f (car (cdr ip)))
!(let1 a (car (cdr (cdr ip))))
!(let1 l (car (cdr (cdr (cdr ip)))))
!(if (= nil l) a)
(vapply self (cons self (cons f (cons (vapply f (cons a (cons (car l) nil)) de) (cons (cdr l) nil)))) de)
))
(vapply vfoldl_inner (cons vfoldl_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) (cons (eval (car (cdr (cdr p))) de) nil)))) de)
))", *LAPPLY)
});
#[test]
fn vfoldl_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (vfoldl (vau de p (+ (car p) (car (cdr p)))) 0 '(1 2 3))", *VFOLDL), 6);
}
static ZIPD: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 zipd (vau de p
!(let1 zipd_inner (vau ide ip
!(let1 self (car ip))
!(let1 a (car (cdr ip)))
!(let1 b (car (cdr (cdr ip))))
!(if (= nil a) a)
!(if (= nil b) b)
(cons (cons (car a) (car b)) (vapply self (cons self (cons (cdr a) (cons (cdr b) nil))) de))
))
(vapply zipd_inner (cons zipd_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) nil))) de)
))", *VFOLDL)
});
#[test]
fn zipd_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (zipd '(1 2 3) '(4 5 6))", *ZIPD), ((1,4), ((2,5), ((3,6), Form::Nil))));
}
static CONCAT: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 concat (vau de p
!(let1 concat_inner (vau ide ip
!(let1 self (car ip))
!(let1 a (car (cdr ip)))
!(let1 b (car (cdr (cdr ip))))
!(if (= nil a) b)
(cons (car a) (vapply self (cons self (cons (cdr a) (cons b nil))) de))
))
(vapply concat_inner (cons concat_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) nil))) de)
))", *ZIPD)
});
#[test]
fn concat_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (concat '(1 2 3) '(4 5 6))", *CONCAT), (1, (2, (3, (4, (5, (6, Form::Nil)))))));
}
static BVAU: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 match_params (wrap (vau 0 p
!(let1 self (car p))
!(let1 p_ls (car (cdr p)))
!(let1 dp (car (cdr (cdr p))))
!(let1 e (car (cdr (cdr (cdr p)))))
!(if (= nil p_ls) (assert (= nil dp) e))
!(if (symbol? p_ls) (cons (cons p_ls dp) e))
(self self (cdr p_ls) (cdr dp) (self self (car p_ls) (car dp) e))
)))
!(let1 bvau (vau se p
(if (= nil (cdr (cdr p)))
; No de case
!(let1 p_ls (car p))
!(let1 b_v (car (cdr p)))
(vau 0 dp
(eval b_v (match_params match_params p_ls dp se))
)
; de case
!(let1 de_s (car p))
!(let1 p_ls (car (cdr p)))
!(let1 b_v (car (cdr (cdr p))))
(vau dde dp
(eval b_v (match_params match_params p_ls dp (cons (cons de_s dde) se)))
)
)
))", *CONCAT)
});
#[test]
fn bvau_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} ((bvau _ (a b c) (+ a (- b c))) 10 2 3)", *BVAU), 9);
//eval_test(true, &g, &e, &format!("{} ((bvau (a b c) (+ a (- b c))) 10 2 3)", *BVAU), 9);
//eval_test(true, &g, &e, &format!("{} ((bvau (a b . c) c) 10 2 3)", *BVAU), (3, Form::Nil));
//eval_test(true, &g, &e, &format!("{} ((bvau (a b . c) c) 10 2)", *BVAU), Form::Nil);
//eval_test(true, &g, &e, &format!("{} ((bvau (a b . c) c) 10 2 3 4 5)", *BVAU), (3, (4, (5, Form::Nil))));
//eval_test(true, &g, &e, &format!("{} ((bvau c c) 3 4 5)", *BVAU), (3, (4, (5, Form::Nil))));
//eval_test(true, &g, &e, &format!("{} ((bvau c c))", *BVAU), Form::Nil);
//eval_test(true, &g, &e, &format!("{} ((bvau ((a b) . c) c) (10 2) 3 4 5)", *BVAU), (3, (4, (5, Form::Nil))));
//eval_test(true, &g, &e, &format!("{} ((bvau ((a b) . c) a) (10 2) 3 4 5)", *BVAU), 10);
//eval_test(true, &g, &e, &format!("{} ((bvau ((a b) . c) b) (10 2) 3 4 5)", *BVAU), 2);
//eval_test(true, &g, &e, &format!("{} ((wrap (bvau _ (a b c) (+ a (- b c)))) (+ 10 1) (+ 2 2) (+ 5 3))", *BVAU), 7);
//eval_test(true, &g, &e, &format!("{} ((wrap (bvau (a b c) (+ a (- b c)))) (+ 10 1) (+ 2 2) (+ 5 3))", *BVAU), 7);
}
static LAMBDA: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 lambda (vau de p
(wrap (vapply bvau p de))
))", *BVAU)
});
#[test]
fn lambda_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} ((lambda (a b c) (+ a (- b c))) (+ 10 1) (+ 2 2) (+ 5 3))", *LAMBDA), 7);
eval_test(true, &g, &e, &format!("{} ((lambda (a b . c) c) 10 2 3)", *LAMBDA), (3, Form::Nil));
eval_test(true, &g, &e, &format!("{} ((lambda (a b . c) c) 10 2)", *LAMBDA), Form::Nil);
eval_test(true, &g, &e, &format!("{} ((lambda (a b . c) c) 10 2 3 4 5)", *LAMBDA), (3, (4, (5, Form::Nil))));
eval_test(true, &g, &e, &format!("{} ((lambda c c) 3 4 5)", *LAMBDA), (3, (4, (5, Form::Nil))));
eval_test(true, &g, &e, &format!("{} ((lambda c c))", *LAMBDA), Form::Nil);
eval_test(true, &g, &e, &format!("{} ((lambda ((a b) . c) c) '(10 2) 3 4 5)", *LAMBDA), (3, (4, (5, Form::Nil))));
eval_test(true, &g, &e, &format!("{} ((lambda ((a b) . c) a) '(10 2) 3 4 5)", *LAMBDA), 10);
eval_test(true, &g, &e, &format!("{} ((lambda ((a b) . c) b) '(10 2) 3 4 5)", *LAMBDA), 2);
eval_test(true, &g, &e, &format!("{} ((lambda ((a b . c) d) b) '(10 2 3 4) 3)", *LAMBDA), 2);
eval_test(true, &g, &e, &format!("{} ((lambda ((a b . c) d) c) '(10 2 3 4) 3)", *LAMBDA), (3, (4, Form::Nil)));
// should fail
//eval_test(true, &g, &e, &format!("{} ((lambda (a b c) c) 10 2 3 4)", *LAMBDA), 3);
}
static LET2: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 let1 (bvau dp (s v b)
(eval b (match_params match_params s (eval v dp) dp))
))
", *LAMBDA)
});
#[test]
fn let2_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (let1 x (+ 10 1) (+ x 1))", *LET2), 12);
eval_test(true, &g, &e, &format!("{} (let1 x '(10 1) x)", *LET2), (10, (1, Form::Nil)));
eval_test(true, &g, &e, &format!("{} (let1 (a b) '(10 1) a)", *LET2), 10);
eval_test(true, &g, &e, &format!("{} (let1 (a b) '(10 1) b)", *LET2), 1);
eval_test(true, &g, &e, &format!("{} (let1 (a b . c) '(10 1) c)", *LET2), Form::Nil);
eval_test(true, &g, &e, &format!("{} (let1 (a b . c) '(10 1 2 3) c)", *LET2), (2, (3, Form::Nil)));
eval_test(true, &g, &e, &format!("{} (let1 ((a . b) . c) '((10 1) 2 3) a)", *LET2), 10);
eval_test(true, &g, &e, &format!("{} (let1 ((a . b) . c) '((10 1) 2 3) b)", *LET2), (1, Form::Nil));
// should fail
//eval_test(true, &g, &e, &format!("{} (let1 (a b c) '(10 2 3 4) a)", *LET2), 10);
}
static LIST: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 list (lambda args args))
", *LET2)
});
#[test]
fn list_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (list 1 2 (+ 3 4))", *LIST), (1, (2, (7, Form::Nil))));
}
static Y: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 Y (lambda (f3)
((lambda (x1) (x1 x1))
(lambda (x2) (f3 (wrap (vau app_env y (lapply (x2 x2) y app_env)))))))
)
", *LIST)
});
#[test]
fn y_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} ((Y (lambda (recurse) (lambda (n) (if (= 0 n) 1 (* n (recurse (- n 1))))))) 5)", *Y), 120);
}
static RLAMBDA: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 rlambda (bvau se (n p b)
(eval (list Y (list lambda (list n) (list lambda p b))) se)
))
", *Y)
});
#[test]
fn rlambda_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} ((rlambda recurse (n) (if (= 0 n) 1 (* n (recurse (- n 1))))) 5)", *RLAMBDA), 120);
}
static AND_OR: Lazy<String> = Lazy::new(|| {
// need to extend for varidac
format!("
{}
!(let1 and (bvau se (a b)
!(let1 ae (eval a se))
(if ae (eval b se) ae)
))
!(let1 or (bvau se (a b)
!(let1 ae (eval a se))
(if ae ae (eval b se))
))
", *RLAMBDA)
});
#[test]
fn and_or_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (and true true)", *AND_OR), true);
eval_test(true, &g, &e, &format!("{} (and false true)", *AND_OR), false);
eval_test(true, &g, &e, &format!("{} (and true false)", *AND_OR), false);
eval_test(true, &g, &e, &format!("{} (and false false)", *AND_OR), false);
eval_test(true, &g, &e, &format!("{} (or true true)", *AND_OR), true);
eval_test(true, &g, &e, &format!("{} (or false true)", *AND_OR), true);
eval_test(true, &g, &e, &format!("{} (or true false)", *AND_OR), true);
eval_test(true, &g, &e, &format!("{} (or false false)", *AND_OR), false);
}
static LEN: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 len (lambda (l)
!(let1 len_helper (rlambda len_helper (l a)
(if (pair? l) (len_helper (cdr l) (+ 1 a))
a)
))
(len_helper l 0)
))
", *AND_OR)
});
#[test]
fn len_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (len '())", *LEN), 0);
eval_test(true, &g, &e, &format!("{} (len '(1))", *LEN), 1);
eval_test(true, &g, &e, &format!("{} (len '(1 2))", *LEN), 2);
eval_test(true, &g, &e, &format!("{} (len '(1 2 3))", *LEN), 3);
}
static MATCH: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 match (bvau de (x . cases)
!(let1 evaluate_case (rlambda evaluate_case (access c)
!(if (symbol? c) (list true (lambda (b) (list let1 c access b))))
!(if (and (pair? c) (= 'unquote (car c))) (list (list = access (car (cdr c))) (lambda (b) b)))
!(if (and (pair? c) (= 'quote (car c))) (list (list = access c) (lambda (b) b)))
!(if (pair? c)
!(let1 tests (list and (list pair? access) (list = (len c) (list len access))))
!(let1 (tests body_func) ((rlambda recurse (c tests access body_func) (if (pair? c)
!(let1 (inner_test inner_body_func) (evaluate_case (list car access) (car c)))
(recurse (cdr c)
(list and tests inner_test)
(list cdr access)
(lambda (b) (body_func (inner_body_func b))))
; else
(list tests body_func)
))
c tests access (lambda (b) b)))
(list tests body_func))
(list (list = access c) (lambda (b) b))
))
!(let1 helper (rlambda helper (x_sym cases) (if (= nil cases) (list assert false)
(let1 (test body_func) (evaluate_case x_sym (car cases))
(concat (list if test (body_func (car (cdr cases)))) (list (helper x_sym (cdr (cdr cases)))))))))
(eval (list let1 '___MATCH_SYM x (helper '___MATCH_SYM cases)) de)
;!(let1 expanded (list let1 '___MATCH_SYM x (helper '___MATCH_SYM cases)))
;(debug expanded (eval expanded de))
))
", *LEN)
});
#[test]
fn match_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(true, &g, &e, &format!("{} (match (+ 1 2) 1 2 2 3 3 4 _ 0)", *MATCH), 4);
eval_test(true, &g, &e, &format!("{} (match '(1 2) 1 2 2 3 3 4 _ 0)", *MATCH), 0);
eval_test(true, &g, &e, &format!("{} (match '(1 2) 1 2 2 3 (a b) (+ a (+ 2 b)) _ 0)", *MATCH), 5);
eval_test(true, &g, &e, &format!("{} (match '(1 2) 1 2 2 3 '(1 2) 7 _ 0)", *MATCH), 7);
eval_test(true, &g, &e, &format!("{} (let1 a 70 (match (+ 60 10) (unquote a) 100 2 3 _ 0))", *MATCH), 100);
}
static RBTREE: Lazy<String> = Lazy::new(|| {
format!("
{}
!(let1 empty (list 'B nil nil nil))
!(let1 E empty)
!(let1 EE (list 'BB nil nil nil))
!(let1 generic-foldl (rlambda generic-foldl (f z t) (match t
(unquote E) z
(c a x b) !(let1 new_left_result (generic-foldl f z a))
!(let1 folded (f new_left_result x))
(generic-foldl f folded b))))
!(let1 blacken (lambda (t) (match t
('R a x b) (list 'B a x b)
t t)))
!(let1 balance (lambda (t) (match t
; figures 1 and 2
('B ('R ('R a x b) y c) z d) (list 'R (list 'B a x b) y (list 'B c z d))
('B ('R a x ('R b y c)) z d) (list 'R (list 'B a x b) y (list 'B c z d))
('B a x ('R ('R b y c) z d)) (list 'R (list 'B a x b) y (list 'B c z d))
('B a x ('R b y ('R c z d))) (list 'R (list 'B a x b) y (list 'B c z d))
; figure 8, double black cases
('BB ('R a x ('R b y c)) z d) (list 'B (list 'B a x b) y (list 'B c z d))
('BB a x ('R ('R b y c) z d)) (list 'B (list 'B a x b) y (list 'B c z d))
; already balenced
t t)))
!(let1 map-insert !(let1 ins (rlambda ins (t k v) (match t
(unquote E) (list 'R t (list k v) t)
(c a x b) !(if (< k (car x)) (balance (list c (ins a k v) x b)))
!(if (= k (car x)) (list c a (list k v) b))
(balance (list c a x (ins b k v))))))
(lambda (t k v) (blacken (ins t k v))))
!(let1 map-empty empty)
!(let1 make-test-tree (rlambda make-test-tree (n t) (if (<= n 0) t
(make-test-tree (- n 1) (map-insert t n (= 0 (% n 10)))))))
!(let1 reduce-test-tree (lambda (tree) (generic-foldl (lambda (a x) (if (car (cdr x)) (+ a 1) a)) 0 tree)))
", *MATCH)
});
#[test]
fn rbtree_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
eval_test(false, &g, &e, &format!("{} (reduce-test-tree (make-test-tree 10 map-empty))", *RBTREE), 1);
//eval_test(false, &g, &e, &format!("{} (reduce-test-tree (make-test-tree 20 map-empty))", *RBTREE), 2);
}

View File

@@ -1863,6 +1863,9 @@
(global '$num_frees '(mut i32) (i32.const 0)) (global '$num_frees '(mut i32) (i32.const 0))
(global '$num_evals '(mut i32) (i32.const 0)) (global '$num_evals '(mut i32) (i32.const 0))
(global '$num_all_evals '(mut i32) (i32.const 0))
(global '$num_interp_dzero '(mut i32) (i32.const 0))
(global '$num_interp_done '(mut i32) (i32.const 0))
(global '$num_compiled_dzero '(mut i32) (i32.const 0)) (global '$num_compiled_dzero '(mut i32) (i32.const 0))
(global '$num_compiled_done '(mut i32) (i32.const 0)) (global '$num_compiled_done '(mut i32) (i32.const 0))
@@ -4029,6 +4032,7 @@
((k_eval_helper func_idx funcs) (array func_idx (+ 1 func_idx) (concat funcs (func '$eval_helper '(param $it i64) '(param $env i64) '(result i64) '(local $len i32) '(local $ptr i32) '(local $current_env i64) '(local $res i64) '(local $env_ptr i32) '(local $tmp_ptr i32) '(local $i i32) '(local $comb i64) '(local $params i64) '(local $wrap i32) '(local $tmp i64) '(local $rc_bytes i64) '(local $rc_ptr i32) '(local $rc_tmp i32) ((k_eval_helper func_idx funcs) (array func_idx (+ 1 func_idx) (concat funcs (func '$eval_helper '(param $it i64) '(param $env i64) '(result i64) '(local $len i32) '(local $ptr i32) '(local $current_env i64) '(local $res i64) '(local $env_ptr i32) '(local $tmp_ptr i32) '(local $i i32) '(local $comb i64) '(local $params i64) '(local $wrap i32) '(local $tmp i64) '(local $rc_bytes i64) '(local $rc_ptr i32) '(local $rc_tmp i32)
(global.set '$num_all_evals (i32.add (i32.const 1) (global.get '$num_all_evals)))
; The cool thing about Vau calculus / Kernel / Kraken ; The cool thing about Vau calculus / Kernel / Kraken
; is that everything is a value that evaluates to itself except symbols ; is that everything is a value that evaluates to itself except symbols
; and arrays. ; and arrays.
@@ -4112,6 +4116,17 @@
) )
(local.set '$wrap (i32.wrap_i64 (extract_wrap_code (local.get '$comb)))) (local.set '$wrap (i32.wrap_i64 (extract_wrap_code (local.get '$comb))))
(local.set '$params (call '$slice_impl (generate_dup (local.get '$it)) (i32.const 1) (local.get '$len))) (local.set '$params (call '$slice_impl (generate_dup (local.get '$it)) (i32.const 1) (local.get '$len)))
; Pure benchmarking
(_if '$is_wrap_one
(i32.eq (i32.const 1) (local.get '$wrap))
(then (global.set '$num_interp_done (i32.add (i32.const 1) (global.get '$num_interp_done))))
(else
(_if '$is_wrap_zero
(i32.eqz (local.get '$wrap))
(then (global.set '$num_interp_dzero (i32.add (i32.const 1) (global.get '$num_interp_dzero))))
(else (unreachable)))))
; we'll reuse len and ptr now for params ; we'll reuse len and ptr now for params
(local.set '$len (extract_size_code (local.get '$params))) (local.set '$len (extract_size_code (local.get '$params)))
(local.set '$ptr (extract_ptr_code (local.get '$params))) (local.set '$ptr (extract_ptr_code (local.get '$params)))
@@ -6383,12 +6398,21 @@
(mk_int_code_i32s (global.get '$num_compiled_dzero)) (mk_int_code_i32s (global.get '$num_compiled_dzero))
(mk_int_code_i32s (global.get '$num_compiled_done)) (mk_int_code_i32s (global.get '$num_compiled_done))
(mk_int_code_i32s (global.get '$num_interp_dzero))
(mk_int_code_i32s (global.get '$num_interp_done))
(mk_int_code_i32s (global.get '$num_all_evals))
(mk_int_code_i32s (global.get '$num_evals)) (mk_int_code_i32s (global.get '$num_evals))
(call '$print (i64.const newline_msg_val)) (call '$print (i64.const newline_msg_val))
(call '$print (i64.const newline_msg_val)) (call '$print (i64.const newline_msg_val))
(call '$print (i64.const newline_msg_val)) (call '$print (i64.const newline_msg_val))
(call '$print (i64.const newline_msg_val)) (call '$print (i64.const newline_msg_val))
(call '$print )
(call '$print (i64.const newline_msg_val))
(call '$print )
(call '$print (i64.const newline_msg_val))
(call '$print )
(call '$print (i64.const newline_msg_val))
(call '$print ) (call '$print )
(call '$print (i64.const newline_msg_val)) (call '$print (i64.const newline_msg_val))
(call '$print ) (call '$print )

163
website/LICENSE Normal file
View File

@@ -0,0 +1,163 @@
###########################
For Recursive (the font):
###########################
Copyright 2020 The Recursive Project Authors (https://github.com/arrowtype/recursive)
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
###########################
For CodeJar:
###########################
MIT License
Copyright (c) 2020 Anton Medvedev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
###########################
For highlight.js:
###########################
BSD 3-Clause License
Copyright (c) 2006, Ivan Sagalaev.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

BIN
website/Recursive.woff2 Normal file

Binary file not shown.

466
website/codejar.js Normal file
View File

@@ -0,0 +1,466 @@
const globalWindow = window;
export function CodeJar(editor, highlight, opt = {}) {
const options = Object.assign({ tab: '\t', indentOn: /[({\[]$/, moveToNewLine: /^[)}\]]/, spellcheck: false, catchTab: true, preserveIdent: true, addClosing: true, history: true, window: globalWindow }, opt);
const window = options.window;
const document = window.document;
let listeners = [];
let history = [];
let at = -1;
let focus = false;
let callback;
let prev; // code content prior keydown event
editor.setAttribute('contenteditable', 'plaintext-only');
editor.setAttribute('spellcheck', options.spellcheck ? 'true' : 'false');
editor.style.outline = 'none';
editor.style.overflowWrap = 'break-word';
editor.style.overflowY = 'auto';
editor.style.whiteSpace = 'pre-wrap';
let isLegacy = false; // true if plaintext-only is not supported
highlight(editor);
if (editor.contentEditable !== 'plaintext-only')
isLegacy = true;
if (isLegacy)
editor.setAttribute('contenteditable', 'true');
const debounceHighlight = debounce(() => {
const pos = save();
highlight(editor, pos);
restore(pos);
}, 30);
let recording = false;
const shouldRecord = (event) => {
return !isUndo(event) && !isRedo(event)
&& event.key !== 'Meta'
&& event.key !== 'Control'
&& event.key !== 'Alt'
&& !event.key.startsWith('Arrow');
};
const debounceRecordHistory = debounce((event) => {
if (shouldRecord(event)) {
recordHistory();
recording = false;
}
}, 300);
const on = (type, fn) => {
listeners.push([type, fn]);
editor.addEventListener(type, fn);
};
on('keydown', event => {
if (event.defaultPrevented)
return;
prev = toString();
if (options.preserveIdent)
handleNewLine(event);
else
legacyNewLineFix(event);
if (options.catchTab)
handleTabCharacters(event);
if (options.addClosing)
handleSelfClosingCharacters(event);
if (options.history) {
handleUndoRedo(event);
if (shouldRecord(event) && !recording) {
recordHistory();
recording = true;
}
}
if (isLegacy && !isCopy(event))
restore(save());
});
on('keyup', event => {
if (event.defaultPrevented)
return;
if (event.isComposing)
return;
if (prev !== toString())
debounceHighlight();
debounceRecordHistory(event);
if (callback)
callback(toString());
});
on('focus', _event => {
focus = true;
});
on('blur', _event => {
focus = false;
});
on('paste', event => {
recordHistory();
handlePaste(event);
recordHistory();
if (callback)
callback(toString());
});
function save() {
const s = getSelection();
const pos = { start: 0, end: 0, dir: undefined };
let { anchorNode, anchorOffset, focusNode, focusOffset } = s;
if (!anchorNode || !focusNode)
throw 'error1';
// If the anchor and focus are the editor element, return either a full
// highlight or a start/end cursor position depending on the selection
if (anchorNode === editor && focusNode === editor) {
pos.start = (anchorOffset > 0 && editor.textContent) ? editor.textContent.length : 0;
pos.end = (focusOffset > 0 && editor.textContent) ? editor.textContent.length : 0;
pos.dir = (focusOffset >= anchorOffset) ? '->' : '<-';
return pos;
}
// Selection anchor and focus are expected to be text nodes,
// so normalize them.
if (anchorNode.nodeType === Node.ELEMENT_NODE) {
const node = document.createTextNode('');
anchorNode.insertBefore(node, anchorNode.childNodes[anchorOffset]);
anchorNode = node;
anchorOffset = 0;
}
if (focusNode.nodeType === Node.ELEMENT_NODE) {
const node = document.createTextNode('');
focusNode.insertBefore(node, focusNode.childNodes[focusOffset]);
focusNode = node;
focusOffset = 0;
}
visit(editor, el => {
if (el === anchorNode && el === focusNode) {
pos.start += anchorOffset;
pos.end += focusOffset;
pos.dir = anchorOffset <= focusOffset ? '->' : '<-';
return 'stop';
}
if (el === anchorNode) {
pos.start += anchorOffset;
if (!pos.dir) {
pos.dir = '->';
}
else {
return 'stop';
}
}
else if (el === focusNode) {
pos.end += focusOffset;
if (!pos.dir) {
pos.dir = '<-';
}
else {
return 'stop';
}
}
if (el.nodeType === Node.TEXT_NODE) {
if (pos.dir != '->')
pos.start += el.nodeValue.length;
if (pos.dir != '<-')
pos.end += el.nodeValue.length;
}
});
// collapse empty text nodes
editor.normalize();
return pos;
}
function restore(pos) {
const s = getSelection();
let startNode, startOffset = 0;
let endNode, endOffset = 0;
if (!pos.dir)
pos.dir = '->';
if (pos.start < 0)
pos.start = 0;
if (pos.end < 0)
pos.end = 0;
// Flip start and end if the direction reversed
if (pos.dir == '<-') {
const { start, end } = pos;
pos.start = end;
pos.end = start;
}
let current = 0;
visit(editor, el => {
if (el.nodeType !== Node.TEXT_NODE)
return;
const len = (el.nodeValue || '').length;
if (current + len > pos.start) {
if (!startNode) {
startNode = el;
startOffset = pos.start - current;
}
if (current + len > pos.end) {
endNode = el;
endOffset = pos.end - current;
return 'stop';
}
}
current += len;
});
if (!startNode)
startNode = editor, startOffset = editor.childNodes.length;
if (!endNode)
endNode = editor, endOffset = editor.childNodes.length;
// Flip back the selection
if (pos.dir == '<-') {
[startNode, startOffset, endNode, endOffset] = [endNode, endOffset, startNode, startOffset];
}
s.setBaseAndExtent(startNode, startOffset, endNode, endOffset);
}
function beforeCursor() {
const s = getSelection();
const r0 = s.getRangeAt(0);
const r = document.createRange();
r.selectNodeContents(editor);
r.setEnd(r0.startContainer, r0.startOffset);
return r.toString();
}
function afterCursor() {
const s = getSelection();
const r0 = s.getRangeAt(0);
const r = document.createRange();
r.selectNodeContents(editor);
r.setStart(r0.endContainer, r0.endOffset);
return r.toString();
}
function handleNewLine(event) {
if (event.key === 'Enter') {
const before = beforeCursor();
const after = afterCursor();
let [padding] = findPadding(before);
let newLinePadding = padding;
// If last symbol is "{" ident new line
if (options.indentOn.test(before)) {
newLinePadding += options.tab;
}
// Preserve padding
if (newLinePadding.length > 0) {
preventDefault(event);
event.stopPropagation();
insert('\n' + newLinePadding);
}
else {
legacyNewLineFix(event);
}
// Place adjacent "}" on next line
if (newLinePadding !== padding && options.moveToNewLine.test(after)) {
const pos = save();
insert('\n' + padding);
restore(pos);
}
}
}
function legacyNewLineFix(event) {
// Firefox does not support plaintext-only mode
// and puts <div><br></div> on Enter. Let's help.
if (isLegacy && event.key === 'Enter') {
preventDefault(event);
event.stopPropagation();
if (afterCursor() == '') {
insert('\n ');
const pos = save();
pos.start = --pos.end;
restore(pos);
}
else {
insert('\n');
}
}
}
function handleSelfClosingCharacters(event) {
const open = `([{'"`;
const close = `)]}'"`;
const codeAfter = afterCursor();
const codeBefore = beforeCursor();
const escapeCharacter = codeBefore.substr(codeBefore.length - 1) === '\\';
const charAfter = codeAfter.substr(0, 1);
if (close.includes(event.key) && !escapeCharacter && charAfter === event.key) {
// We already have closing char next to cursor.
// Move one char to right.
const pos = save();
preventDefault(event);
pos.start = ++pos.end;
restore(pos);
}
else if (open.includes(event.key)
&& !escapeCharacter
&& (`"'`.includes(event.key) || ['', ' ', '\n'].includes(charAfter))) {
preventDefault(event);
const pos = save();
const wrapText = pos.start == pos.end ? '' : getSelection().toString();
const text = event.key + wrapText + close[open.indexOf(event.key)];
insert(text);
pos.start++;
pos.end++;
restore(pos);
}
}
function handleTabCharacters(event) {
if (event.key === 'Tab') {
preventDefault(event);
if (event.shiftKey) {
const before = beforeCursor();
let [padding, start,] = findPadding(before);
if (padding.length > 0) {
const pos = save();
// Remove full length tab or just remaining padding
const len = Math.min(options.tab.length, padding.length);
restore({ start, end: start + len });
document.execCommand('delete');
pos.start -= len;
pos.end -= len;
restore(pos);
}
}
else {
insert(options.tab);
}
}
}
function handleUndoRedo(event) {
if (isUndo(event)) {
preventDefault(event);
at--;
const record = history[at];
if (record) {
editor.innerHTML = record.html;
restore(record.pos);
}
if (at < 0)
at = 0;
}
if (isRedo(event)) {
preventDefault(event);
at++;
const record = history[at];
if (record) {
editor.innerHTML = record.html;
restore(record.pos);
}
if (at >= history.length)
at--;
}
}
function recordHistory() {
if (!focus)
return;
const html = editor.innerHTML;
const pos = save();
const lastRecord = history[at];
if (lastRecord) {
if (lastRecord.html === html
&& lastRecord.pos.start === pos.start
&& lastRecord.pos.end === pos.end)
return;
}
at++;
history[at] = { html, pos };
history.splice(at + 1);
const maxHistory = 300;
if (at > maxHistory) {
at = maxHistory;
history.splice(0, 1);
}
}
function handlePaste(event) {
preventDefault(event);
const text = (event.originalEvent || event)
.clipboardData
.getData('text/plain')
.replace(/\r/g, '');
const pos = save();
insert(text);
highlight(editor);
restore({
start: Math.min(pos.start, pos.end) + text.length,
end: Math.min(pos.start, pos.end) + text.length,
dir: '<-',
});
}
function visit(editor, visitor) {
const queue = [];
if (editor.firstChild)
queue.push(editor.firstChild);
let el = queue.pop();
while (el) {
if (visitor(el) === 'stop')
break;
if (el.nextSibling)
queue.push(el.nextSibling);
if (el.firstChild)
queue.push(el.firstChild);
el = queue.pop();
}
}
function isCtrl(event) {
return event.metaKey || event.ctrlKey;
}
function isUndo(event) {
return isCtrl(event) && !event.shiftKey && getKeyCode(event) === 'Z';
}
function isRedo(event) {
return isCtrl(event) && event.shiftKey && getKeyCode(event) === 'Z';
}
function isCopy(event) {
return isCtrl(event) && getKeyCode(event) === 'C';
}
function getKeyCode(event) {
let key = event.key || event.keyCode || event.which;
if (!key)
return undefined;
return (typeof key === 'string' ? key : String.fromCharCode(key)).toUpperCase();
}
function insert(text) {
text = text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
document.execCommand('insertHTML', false, text);
}
function debounce(cb, wait) {
let timeout = 0;
return (...args) => {
clearTimeout(timeout);
timeout = window.setTimeout(() => cb(...args), wait);
};
}
function findPadding(text) {
// Find beginning of previous line.
let i = text.length - 1;
while (i >= 0 && text[i] !== '\n')
i--;
i++;
// Find padding of the line.
let j = i;
while (j < text.length && /[ \t]/.test(text[j]))
j++;
return [text.substring(i, j) || '', i, j];
}
function toString() {
return editor.textContent || '';
}
function preventDefault(event) {
event.preventDefault();
}
function getSelection() {
var _a;
if (((_a = editor.parentNode) === null || _a === void 0 ? void 0 : _a.nodeType) == Node.DOCUMENT_FRAGMENT_NODE) {
return editor.parentNode.getSelection();
}
return window.getSelection();
}
return {
updateOptions(newOptions) {
Object.assign(options, newOptions);
},
updateCode(code) {
editor.textContent = code;
highlight(editor);
},
onUpdate(cb) {
callback = cb;
},
toString,
save,
restore,
recordHistory,
destroy() {
for (let [type, fn] of listeners) {
editor.removeEventListener(type, fn);
}
},
};
}

9
website/default.min.css vendored Normal file
View File

@@ -0,0 +1,9 @@
/*!
Theme: Default
Description: Original highlight.js style
Author: (c) Ivan Sagalaev <maniac@softwaremaniacs.org>
Maintainer: @highlightjs/core-team
Website: https://highlightjs.org/
License: see project LICENSE
Touched: 2021
*/pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}.hljs{background:#f3f3f3;color:#444}.hljs-comment{color:#697070}.hljs-punctuation,.hljs-tag{color:#444a}.hljs-tag .hljs-attr,.hljs-tag .hljs-name{color:#444}.hljs-attribute,.hljs-doctag,.hljs-keyword,.hljs-meta .hljs-keyword,.hljs-name,.hljs-selector-tag{font-weight:700}.hljs-deletion,.hljs-number,.hljs-quote,.hljs-selector-class,.hljs-selector-id,.hljs-string,.hljs-template-tag,.hljs-type{color:#800}.hljs-section,.hljs-title{color:#800;font-weight:700}.hljs-link,.hljs-operator,.hljs-regexp,.hljs-selector-attr,.hljs-selector-pseudo,.hljs-symbol,.hljs-template-variable,.hljs-variable{color:#ab5656}.hljs-literal{color:#695}.hljs-addition,.hljs-built_in,.hljs-bullet,.hljs-code{color:#397300}.hljs-meta{color:#1f7199}.hljs-meta .hljs-string{color:#38a}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}

BIN
website/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

1202
website/highlight.min.js vendored Normal file

File diff suppressed because one or more lines are too long

171
website/index.html Normal file
View File

@@ -0,0 +1,171 @@
<!doctype html>
<html lang="en-us">
<meta charset="UTF-8">
<head>
<style>
@font-face {
font-family: 'Recursive';
font-style: oblique 0deg 15deg;
font-weight: 300 1000;
font-display: swap;
src: url(./Recursive.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
:root {
--rec-wght: 400;
--rec-slnt: 0;
--rec-mono: 0;
--rec-casl: 0;
--rec-csrv: 0;
}
* {
font-variation-settings: "wght" var(--rec-wght),
"slnt" var(--rec-slnt),
"MONO" var(--rec-mono),
"CASL" var(--rec-casl),
"CRSV" var(--rec-csrv);
}
body {
max-width: 45em;
margin: 1em auto;
padding: 0 .62em;
font: 1.2em/1.62 'Recursive', sans-serif;
}
h1, h2, h3, h4 {
line-height:1.2;
--rec-wght: 700;
--rec-casl: 1;
--rec-crsv: 1;
}
h1 {
line-height:0.7;
font-size: 4em;
--rec-wght: 900;
--rec-slnt: -15;
text-decoration: underline;
text-decoration-thickness: 0.4rem;
//border-bottom: 0.08em solid;
//border-left: 0.1em solid;
//display: inline-block;
}
h2 { font-size: 3em; }
h3 { font-size: 1.5em; }
h4 { font-size: 1.2em; }
i { --rec-slnt: -14; }
b { --rec-wght: 600; }
.run_container { position: relative; }
.editor {
font-family: 'Recursive', monospace;
font-size: 1rem;
--rec-mono: 1;
border-radius: 6px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2);
height: 7em;
letter-spacing: normal;
tab-size: 4;
}
.output {
margin-block-start: 1rem;
font-family: 'Recursive', monospace;
font-size: 1rem;
--rec-mono: 1;
tab-size: 4;
height: 5em;
width: 100%;
}
.run_button {
font-family: 'Recursive', sans-serif;
font-size: 1em;
--rec-wght: 900;
--rec-slnt: -15;
--rec-casl: 1;
--rec-crsv: 1;
position: absolute;
top: 0;
right: 0;
}
</style>
</head>
<body>
<header><h1>Kraken</h1></header>
<i>FOSS Fexprs: <a title="Kraken on GitHub" href="https://github.com/limvot/kraken">https://github.com/limvot/kraken</a></i>
<br>
<h3>Demo:</h3>
<div class="run_container">
<div class="editor" id="hello_editor">; Of course
(println "Hello World")
; Just print 3
(println "Math works:" (+ 1 2))
</div>
<textarea class="output" id="hello_output">Output will appear here</textarea>
<button class="run_button" onclick="executeKraken(hello_editor_jar.toString(), 'hello_output')"><b>Run</b></button> <br>
</div>
<a name="concept"/>
<h3>Concept:</h3>
<ul>
<li> Minimal, close to the metal Kernel/Scheme (operate on words, bytes, arrays) as AST / core language, with Kernel/Vau calculus inspiration oblivating the need for non-reader macros (<a title="Kernel/Vau calculus thesis" href="https://web.wpi.edu/Pubs/ETD/Available/etd-090110-124904/unrestricted/jshutt.pdf">Kernel/Vau calculus thesis</a>)
<li> Full Context-free (and eventually, context sensitive) reader macros using FUN-GLL (<a title="fun-gll paper" href="https://www.sciencedirect.com/science/article/pii/S2590118420300058">FUN-GLL paper</a>) to extend language's syntax dynamically
<li> Implement Type Systems as Macros (but using Vaus instead of macros) (<a title="type systems as macros paper 1" href="http://www.ccs.neu.edu/home/stchang/pubs/ckg-popl2017.pdf">paper, up to System Fω</a>) (<a title="type systems as macros paper 2" href="https://www.ccs.neu.edu/home/stchang/pubs/cbtb-popl2020.pdf">second paper, up to dependent types</a>)
<li> Use above "type systems as vaus" to create richer language and embed entire other programming languages (syntax, semantics, and type system) for flawless interop/FFI (C, Go, Lua, JS, etc)
<li> File is interpreted, and then if "main" exists it is compiled, spidering backwards to referenced functions and data (Allows interpreted code to do metaprogramming, dependency resolution, generate code, etc, which is then compiled)
<li> Regionalized Value State Dependence Graph as backend-IR, enabling simpler implementations of powerful optimizations (<a title="RSVDG paper" href="https://arxiv.org/pdf/1912.05036.pdf">RSVDG paper</a>) so that embedded languages have good performance when compiled with little code
</ul>
<a name="about"/>
<h3> About:</h3>
<p> Currently, I am bootstrapping this new core Lisp out of my prior compiler for my programming language, Kraken. I have implemented the first version of the FUN-GLL algorithm and have working vaus and context-free reader macros.
<p> The general flow is that the input files will be executed with the core Lisp interpreter, and if there is a "main" symbol defined the compiler emits C code for that function & all other functions & data that it references. In this way the language supports very powerful meta-programming at compile time, including adding syntax to the language, arbitrary computation, and importing other files, and then compiles into a static executable.
<p> Below are a few examples of using the vau / live grammar modification / context-free reader macros to implement basic methods as well as embed the BF language into the core Lisp. The core Lisp implementation has been compiled to WebAssembly and should be able to run in your browser. Feel free to make edits and play around below.
<br>
Note that the current implementation is inefficient, and sometimes has problems running in phone web browsers.
<a name="hello_example"/>
<a name="vau_core"/>
<h4>Vau/Kernel as simple core:</h4>
By constructing our core language on a very simple Vau/Kernel base, we can keep the base truely tiny, and build up normal Lisp functions and programming language features in the language itself. This should help implement other programming languages concisely, and will hopefully make optimization easier and more broadly applicable.
<br>
Below is the current prelude that adds quoting, quasiquoting, syntax for arrays and quoting/quasiquoting, do, if, let, and even lambda itself!
<a name="next_steps"/>
<h3>Next Steps</h3>
<ul>
<li> Implement persistent functional data structures
<ul>
<li> Hash Array-Mapped Trie (HAMT) / Relaxed Radix Balance Tree (RRB-Tree)
<li> Hash Map based on the above
<li> Hash Set based on the above
</ul>
<li> Prototype Type Systems as Macros, may require macro system rewrite/upgrade
<li> Sketch out Kraken language on top of core Lisp, includes basic Hindley-Milner type system implemented with Macros and above data structures
<li> Re-self-host using functional approach in above Kraken language
<li> Use Type System Macros to implement automatic transient creation on HAMT/RBB-Tree as an optimization
<li> Implement RVSDG IR and develop best bang-for-buck optimizations using it
</ul>
<link rel="stylesheet" href="./default.min.css">
<script src="./highlight.min.js"></script>
<script type="module">
import {CodeJar} from './codejar.js'
document.querySelectorAll('.editor').forEach((editor_div) => {
window[editor_div.id + "_jar"] = CodeJar(editor_div, hljs.highlightElement)
});
</script>
<script>
var output_name = ""
var Module = {
noInitialRun: true,
onRuntimeInitialized: () => {
},
print: txt => {
document.getElementById(output_name).value += txt + "\n";
},
printErr: txt => {
document.getElementById(output_name).value += "STDERR:[" + txt + "]\n";
}
};
function executeKraken(code, new_output_name) {
output_name = new_output_name
document.getElementById(new_output_name).value = "running...\n";
Module.callMain(["-C", code]);
}
</script>
<script type="text/javascript" src="k_prime.js"></script>
</body>
</html>