Implement closures
What a pain. Next steps: More happy path tests, then large values/mutabilty, then redo in bytecode.closure
parent
93266a980e
commit
bc00dbe69e
|
@ -22,3 +22,8 @@ lalrpop = "0.19.6"
|
|||
[dev-dependencies]
|
||||
assert_cmd = "2.0.2"
|
||||
insta = { version = "1.9.0", features = ["glob"] }
|
||||
|
||||
[profile.dev]
|
||||
# https://github.com/rust-lang/rust/issues/92163
|
||||
# https://github.com/rust-lang/rust/issues/92315
|
||||
incremental=false
|
16
src/ast.rs
16
src/ast.rs
|
@ -1,20 +1,24 @@
|
|||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, debug2::Debug, PartialEq)]
|
||||
#[derive(Debug /*/*, debug2::Debug*/*/, PartialEq)]
|
||||
|
||||
crate enum Tree {
|
||||
Leaf(Literal),
|
||||
Define(String, Box<Tree>),
|
||||
If(Box<[Tree; 3]>),
|
||||
Lambda(Lambda),
|
||||
// Its easier to box the lambdas in the parser than the vm, as
|
||||
// here we see all of them exactly once
|
||||
Func(Rc<Func>),
|
||||
Branch(Vec<Tree>),
|
||||
}
|
||||
|
||||
#[derive(Debug, debug2::Debug, PartialEq, Clone)]
|
||||
#[derive(Debug /*/*, debug2::Debug*/*/, PartialEq)]
|
||||
crate struct Func {
|
||||
crate args: Vec<String>,
|
||||
crate body: Vec<Tree>,
|
||||
}
|
||||
|
||||
crate struct Lambda(crate Rc<[String]>, crate Rc<[Tree]>);
|
||||
|
||||
#[derive(Debug, debug2::Debug, PartialEq)]
|
||||
#[derive(Debug /*/*, debug2::Debug*/*/, PartialEq)]
|
||||
|
||||
crate enum Literal {
|
||||
Sym(String),
|
||||
|
|
99
src/eval.rs
99
src/eval.rs
|
@ -1,58 +1,77 @@
|
|||
use std::assert_matches::assert_matches;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::value::Value;
|
||||
use crate::{ast, prims};
|
||||
|
||||
pub(crate) fn eval(t: &ast::Tree, env: &mut Env) -> Result<Value, RTError> {
|
||||
#[derive(/*debug2::Debug,*/ Debug, Clone)]
|
||||
crate struct Lambda {
|
||||
crate func: Rc<ast::Func>,
|
||||
crate captures: Rc<RefCell<Env>>,
|
||||
}
|
||||
|
||||
pub(crate) fn eval(t: &ast::Tree, env: Rc<RefCell<Env>>) -> Result<Value, RTError> {
|
||||
Ok(match t {
|
||||
ast::Tree::Leaf(l) => match l {
|
||||
ast::Literal::Sym(s) => match env.lookup(s) {
|
||||
ast::Literal::Sym(s) => match env.borrow().lookup(s) {
|
||||
Some(v) => v,
|
||||
None => return err(format!("Undefined variable `{}`", s)),
|
||||
},
|
||||
ast::Literal::Num(v) => Value::Num(*v),
|
||||
ast::Literal::Bool(b) => Value::Bool(*b),
|
||||
},
|
||||
ast::Tree::Lambda(l) => Value::Lambda(l.clone()),
|
||||
ast::Tree::Func(l) => Value::Lambda(Lambda {
|
||||
func: Rc::clone(l),
|
||||
captures: Rc::clone(&env),
|
||||
}),
|
||||
ast::Tree::Branch(args) => {
|
||||
let Some(fun) = args.get(0) else { return err("No argument given".to_owned()) };
|
||||
let fun = eval(fun, env)?;
|
||||
let fun = fun.as_func()?;
|
||||
let Some(fun) = args.get(0) else { return err("No func given".to_owned()) };
|
||||
|
||||
let fun = eval(fun, Rc::clone(&env))?.as_func()?;
|
||||
|
||||
let args = args
|
||||
.iter()
|
||||
.skip(1)
|
||||
.map(|a| eval(a, env))
|
||||
.map(|a| eval(a, Rc::clone(&env)))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
// return fun(&args);
|
||||
|
||||
match fun {
|
||||
Callable::Func(f) => f(&args)?,
|
||||
Callable::Lambda(l) => {
|
||||
if l.0.len() == args.len() {
|
||||
let mut env = env.child();
|
||||
for (x, y) in l.0.iter().zip(args) {
|
||||
if l.func.args.len() == args.len() {
|
||||
// let mut env = env.child();
|
||||
let mut env = Env::child(Rc::clone(&l.captures));
|
||||
|
||||
for (x, y) in l.func.args.iter().zip(args) {
|
||||
env.define(x.clone(), y)
|
||||
}
|
||||
let env = Rc::new(RefCell::new(env));
|
||||
|
||||
let [main @ .., tail] = &l.1[..] else { unreachable!("Body has 1+ element by parser") };
|
||||
let [main @ .., tail] = &l.func.body[..] else { unreachable!("Body has 1+ element by parser") };
|
||||
for i in main {
|
||||
eval(i, &mut env)?;
|
||||
eval(i, Rc::clone(&env))?;
|
||||
}
|
||||
|
||||
eval(tail, &mut env)?
|
||||
eval(tail, env)?
|
||||
} else {
|
||||
return err(format!("Need {} args, got {}", l.0.len(), args.len()));
|
||||
return err(format!(
|
||||
"Need {} args, got {}",
|
||||
l.func.args.len(),
|
||||
args.len()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::Tree::Define(name, to) => {
|
||||
let val = eval(to, env)?;
|
||||
env.define(name.to_owned(), val);
|
||||
let val = eval(to, Rc::clone(&env))?;
|
||||
env.borrow_mut().define(name.to_owned(), val);
|
||||
Value::Trap
|
||||
}
|
||||
ast::Tree::If(box [cond, tcase, fcase]) => {
|
||||
let b = eval(cond, env)?.as_bool()?;
|
||||
let b = eval(cond, Rc::clone(&env))?.as_bool()?;
|
||||
let body = if b { tcase } else { fcase };
|
||||
eval(body, env)?
|
||||
}
|
||||
|
@ -66,40 +85,60 @@ pub(crate) fn err<T>(s: String) -> Result<T, RTError> {
|
|||
Err(RTError(s))
|
||||
}
|
||||
|
||||
pub(crate) enum Callable<'a> {
|
||||
Func(prims::Func),
|
||||
Lambda(&'a ast::Lambda),
|
||||
pub(crate) enum Callable {
|
||||
Func(prims::NativeFunc),
|
||||
Lambda(Lambda),
|
||||
}
|
||||
|
||||
pub(crate) struct Env<'a> {
|
||||
pub(crate) vars: HashMap<String, Value>,
|
||||
pub(crate) enclosing: Option<&'a Env<'a>>,
|
||||
#[derive(Clone /*, debug2::Debug*/)]
|
||||
pub(crate) struct Env {
|
||||
vars: HashMap<String, Value>,
|
||||
enclosing: Option<Rc<RefCell<Env>>>,
|
||||
}
|
||||
|
||||
impl<'a> Env<'a> {
|
||||
pub(crate) fn child(&'a self) -> Env<'a> {
|
||||
impl std::fmt::Debug for Env {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
struct Vars<'a>(&'a HashMap<String, Value>);
|
||||
|
||||
impl std::fmt::Debug for Vars<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_set().entries(self.0.keys()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
f.debug_struct("Env")
|
||||
.field("vars", &Vars(&self.vars))
|
||||
.field("enclosing", &self.enclosing)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Env {
|
||||
pub(crate) fn child(this: Rc<RefCell<Self>>) -> Env {
|
||||
Env {
|
||||
vars: HashMap::new(),
|
||||
enclosing: Some(self),
|
||||
enclosing: Some(this),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn lookup(&self, s: &str) -> Option<Value> {
|
||||
if let Some(v) = self.vars.get(s) {
|
||||
Some(v.clone())
|
||||
} else if let Some(parent) = self.enclosing {
|
||||
parent.lookup(s)
|
||||
} else if let Some(parent) = &self.enclosing {
|
||||
parent.borrow().lookup(s)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn define(&mut self, name: String, val: Value) {
|
||||
assert_ne!(val, Value::Trap); // TODO: Better error
|
||||
|
||||
self.vars.insert(name, val);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn default_env() -> Env<'static> {
|
||||
pub(crate) fn default_env() -> Env {
|
||||
let mut vars = HashMap::new();
|
||||
|
||||
for (name, fun) in prims::prims() {
|
||||
|
|
|
@ -17,9 +17,10 @@ Trees = Tree+;
|
|||
pub(crate) Tree: Tree = {
|
||||
"(" <Tree+> ")" => Tree::Branch(<>),
|
||||
"(" "define" <Sym> <BTree> ")" => Tree::Define(<>),
|
||||
"(" "define" "(" <name:Sym> <args:RcSlice<Sym>> ")" <body:RcSlice<Tree>> ")" => Tree::Define(name, Box::new(Tree::Lambda(Lambda(args, body)))),
|
||||
"(" "define" "(" <name:Sym> <args:Sym*> ")" <body:Trees> ")"
|
||||
=> Tree::Define(name, Box::new(Tree::Func(Rc::new(Func{args, body})))),
|
||||
"(" "if" <Tree> <Tree> <Tree> ")" => Tree::If(Box::new([<>])),
|
||||
"(" "lambda (" <RcSlice<Sym>> ")" <RcSlice<Tree>> ")" => Tree::Lambda(Lambda(<>)),
|
||||
"(" "lambda (" <args:Sym*> ")" <body:Trees> ")" => Tree::Func(Rc::new(Func{<>})),
|
||||
Literal => Tree::Leaf(<>),
|
||||
}
|
||||
|
||||
|
|
1351
src/grammar.rs
1351
src/grammar.rs
File diff suppressed because it is too large
Load Diff
19
src/main.rs
19
src/main.rs
|
@ -4,7 +4,9 @@
|
|||
#![feature(crate_visibility_modifier)]
|
||||
#![feature(let_else)]
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::io;
|
||||
use std::rc::Rc;
|
||||
|
||||
use rustyline::validate::{
|
||||
MatchingBracketValidator, ValidationContext, ValidationResult, Validator,
|
||||
|
@ -33,9 +35,9 @@ fn main() {
|
|||
fn run_file(file: &str) -> io::Result<()> {
|
||||
let src = std::fs::read_to_string(file)?;
|
||||
let tree = grammar::FileParser::new().parse(&src).unwrap();
|
||||
let mut env = eval::default_env();
|
||||
let env = Rc::new(RefCell::new(eval::default_env()));
|
||||
for i in tree {
|
||||
eval::eval(&i, &mut env).unwrap();
|
||||
eval::eval(&i, Rc::clone(&env)).unwrap();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -46,14 +48,23 @@ fn repl() {
|
|||
brackets: MatchingBracketValidator::new(),
|
||||
}));
|
||||
|
||||
let mut env = eval::default_env();
|
||||
let env = Rc::new(RefCell::new(eval::default_env()));
|
||||
|
||||
while let Ok(line) = rl.readline("> ") {
|
||||
rl.add_history_entry(&line);
|
||||
|
||||
let tree = grammar::TreeParser::new().parse(&line).unwrap();
|
||||
// dbg!(&tree);
|
||||
println!("< {:?}", eval::eval(&tree, &mut env))
|
||||
match eval::eval(&tree, Rc::clone(&env)) {
|
||||
Ok(v) => {
|
||||
// Cant use == because it will panic. This is the only
|
||||
// valid comparison to trap
|
||||
if !matches!(v, value::Value::Trap) {
|
||||
println!("{}", v)
|
||||
}
|
||||
}
|
||||
Err(e) => println!("! {}", e.0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
38
src/prims.rs
38
src/prims.rs
|
@ -1,9 +1,12 @@
|
|||
use crate::eval::{err, RTError};
|
||||
use crate::value::Value;
|
||||
|
||||
crate type Func = fn(&[Value]) -> Result<Value, RTError>;
|
||||
|
||||
crate fn prims() -> &'static [(&'static str, Func)] {
|
||||
type Result = std::result::Result<Value, RTError>;
|
||||
|
||||
crate type NativeFunc = for<'a> fn(&'a [Value]) -> Result;
|
||||
|
||||
crate fn prims() -> &'static [(&'static str, NativeFunc)] {
|
||||
&[
|
||||
("*", mul),
|
||||
("+", add),
|
||||
|
@ -21,25 +24,27 @@ crate fn prims() -> &'static [(&'static str, Func)] {
|
|||
(">", gt),
|
||||
("<=", le),
|
||||
(">=", ge),
|
||||
|
||||
("_Z_debug", z_debug)
|
||||
]
|
||||
}
|
||||
|
||||
// TODO: DRY +-/*
|
||||
fn add(args: &[Value]) -> Result<Value, RTError> {
|
||||
fn add(args: &[Value]) -> Result {
|
||||
args.iter()
|
||||
.map(Value::as_num)
|
||||
.try_fold(0.0, |a, b| Ok(a + b?))
|
||||
.map(Value::Num)
|
||||
}
|
||||
|
||||
fn mul(args: &[Value]) -> Result<Value, RTError> {
|
||||
fn mul(args: &[Value]) -> Result {
|
||||
args.iter()
|
||||
.map(Value::as_num)
|
||||
.try_fold(1.0, |a, b| Ok(a * b?))
|
||||
.map(Value::Num)
|
||||
}
|
||||
|
||||
fn div(args: &[Value]) -> Result<Value, RTError> {
|
||||
fn div(args: &[Value]) -> Result {
|
||||
let init = args
|
||||
.get(0)
|
||||
.ok_or_else(|| RTError("`div` needs at least one argument".to_owned()))?
|
||||
|
@ -53,7 +58,7 @@ fn div(args: &[Value]) -> Result<Value, RTError> {
|
|||
}))
|
||||
}
|
||||
|
||||
fn sub(args: &[Value]) -> Result<Value, RTError> {
|
||||
fn sub(args: &[Value]) -> Result {
|
||||
let init = args
|
||||
.get(0)
|
||||
.ok_or_else(|| RTError("`sub` needs at least one argument".to_owned()))?
|
||||
|
@ -67,17 +72,17 @@ fn sub(args: &[Value]) -> Result<Value, RTError> {
|
|||
}))
|
||||
}
|
||||
|
||||
fn equals(args: &[Value]) -> Result<Value, RTError> {
|
||||
fn equals(args: &[Value]) -> Result {
|
||||
Ok(Value::Bool(args.array_windows().all(|[l, r]| l == r)))
|
||||
}
|
||||
|
||||
fn display(args: &[Value]) -> Result<Value, RTError> {
|
||||
fn display(args: &[Value]) -> Result {
|
||||
let [arg] = args else {return err("To many args to `display`".to_owned())};
|
||||
print!("{:?}", arg);
|
||||
print!("{}", arg);
|
||||
Ok(Value::Trap)
|
||||
}
|
||||
|
||||
fn newline(args: &[Value]) -> Result<Value, RTError> {
|
||||
fn newline(args: &[Value]) -> Result {
|
||||
if !args.is_empty() {
|
||||
return err("Newline takes no args".to_owned());
|
||||
}
|
||||
|
@ -85,7 +90,7 @@ fn newline(args: &[Value]) -> Result<Value, RTError> {
|
|||
Ok(Value::Trap)
|
||||
}
|
||||
|
||||
fn abs(args: &[Value]) -> Result<Value, RTError> {
|
||||
fn abs(args: &[Value]) -> Result {
|
||||
let [v] = args else { return err("abs takes 1 arg".to_owned()) };
|
||||
let ans = v.as_num()?.abs();
|
||||
Ok(Value::Num(ans))
|
||||
|
@ -95,7 +100,7 @@ crate fn compare_core(
|
|||
args: &[Value],
|
||||
f: fn(f64, f64) -> bool,
|
||||
name: &'static str,
|
||||
) -> Result<Value, RTError> {
|
||||
) -> Result {
|
||||
for [l, r] in args.array_windows() {
|
||||
let (Value::Num(l), Value::Num(r)) = (l,r) else
|
||||
{
|
||||
|
@ -114,11 +119,16 @@ crate fn compare_core(
|
|||
macro_rules! cmps {
|
||||
($(($name:ident $op:tt))*) => {
|
||||
$(
|
||||
fn $name(args: &[Value]) -> Result<Value, RTError> {
|
||||
fn $name(args: &[Value]) -> Result {
|
||||
compare_core(args, |l, r| l $op r, stringify!($op))
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
cmps! { (lt <) (gt >) (le <=) (ge >=) }
|
||||
cmps! { (lt <) (gt >) (le <=) (ge >=) }
|
||||
|
||||
fn z_debug(args: &[Value]) -> Result {
|
||||
eprintln!("{:?}", args);
|
||||
Ok(Value::Trap)
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: run-pass ambig-scope.scm
|
||||
|
||||
---
|
||||
|
||||
42.0
|
||||
1.0
|
||||
2.0
|
||||
3.0
|
||||
2.0
|
||||
1.0
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: run-pass capture.scm
|
||||
|
||||
---
|
||||
#<procedure>
|
||||
2.0
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
source: src/tests.rs
|
||||
assertion_line: 41
|
||||
expression: run-pass curry.scm
|
||||
|
||||
---
|
||||
3.0
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
#lang scheme
|
||||
|
||||
(define (displayln x) (newline) (display x))
|
||||
|
||||
(define (ambig1 x) (lambda (x) x))
|
||||
|
||||
(displayln ((ambig1 13) 42))
|
||||
|
||||
(define (ambig2 a)
|
||||
(displayln a)
|
||||
(define a- a)
|
||||
(lambda (b) (displayln b)
|
||||
(lambda (a)
|
||||
(displayln a)
|
||||
(displayln b)
|
||||
(displayln a-))))
|
||||
|
||||
(((ambig2 1) 2) 3)
|
|
@ -0,0 +1,12 @@
|
|||
#lang scheme
|
||||
|
||||
(define (displayln x) (display x) (newline))
|
||||
|
||||
|
||||
(define (const x) (lambda () x))
|
||||
|
||||
(define two-thunk (const 2))
|
||||
|
||||
(displayln two-thunk)
|
||||
(displayln (two-thunk))
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
#lang scheme
|
||||
|
||||
(define (displayln x) (display x) (newline))
|
||||
|
||||
(define (curry2 f) (lambda (l) (lambda (r) (f l r))))
|
||||
|
||||
(displayln (((curry2 +) 1) 2))
|
41
src/value.rs
41
src/value.rs
|
@ -1,16 +1,32 @@
|
|||
use crate::{ast, eval, prims};
|
||||
use crate::{eval, prims};
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone /*, debug2::Debug*/)]
|
||||
crate enum Value {
|
||||
Num(f64),
|
||||
Func(prims::Func),
|
||||
Lambda(ast::Lambda),
|
||||
// TODO: implement debug2::Debug for `fn` type, and do it right
|
||||
// https://godbolt.org/z/vr9erGeKq
|
||||
Func(prims::NativeFunc),
|
||||
Lambda(eval::Lambda),
|
||||
Bool(bool),
|
||||
/// Result of things that shouldnt have values, like (define x 3)
|
||||
/// TODO: Figure this out
|
||||
Trap,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Value {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Num(arg0) => f.debug_tuple("Num").field(arg0).finish(),
|
||||
// We cant just pass the field because of livetime bs, see https://godbolt.org/z/vr9erGeKq
|
||||
Self::Func(_) => f.debug_struct("Func").finish_non_exhaustive(),
|
||||
Self::Lambda(arg0) => f.debug_tuple("Lambda").field(arg0).finish(),
|
||||
Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(),
|
||||
Self::Trap => write!(f, "Trap"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Value {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
use Value::*;
|
||||
|
@ -18,7 +34,9 @@ impl PartialEq for Value {
|
|||
(Num(l), Num(r)) => l == r,
|
||||
(Func(l), Func(r)) => *l as usize == *r as usize,
|
||||
(Bool(l), Bool(r)) => l == r,
|
||||
(Lambda(l), Lambda(r)) => Rc::ptr_eq(&l.0, &r.0) && Rc::ptr_eq(&l.1, &r.1),
|
||||
(Lambda(l), Lambda(r)) => {
|
||||
Rc::ptr_eq(&l.func, &r.func) && Rc::ptr_eq(&l.captures, &r.captures)
|
||||
}
|
||||
|
||||
(Num(_), _) => false,
|
||||
(Func(_), _) => false,
|
||||
|
@ -30,10 +48,10 @@ impl PartialEq for Value {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Value {
|
||||
impl std::fmt::Display for Value {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Num(n) => n.fmt(f),
|
||||
Self::Num(n) => std::fmt::Debug::fmt(n, f), // TODO: Change to displays
|
||||
Self::Func(_) => f.write_str("#<procedure>"),
|
||||
Self::Lambda(_) => f.write_str("#<procedure>"),
|
||||
Self::Bool(b) => f.write_str(if *b { "#t" } else { "#f" }),
|
||||
|
@ -51,14 +69,11 @@ impl Value {
|
|||
}
|
||||
}
|
||||
|
||||
crate fn as_func(&self) -> Result<eval::Callable, eval::RTError> {
|
||||
crate fn as_func(self) -> Result<eval::Callable, eval::RTError> {
|
||||
match self {
|
||||
Self::Func(f) => Ok(eval::Callable::Func(*f)),
|
||||
Self::Func(f) => Ok(eval::Callable::Func(f)),
|
||||
Self::Lambda(l) => Ok(eval::Callable::Lambda(l)),
|
||||
_ => Err(eval::RTError(format!(
|
||||
"Expected a function, got {:?}",
|
||||
self
|
||||
))),
|
||||
_ => Err(eval::RTError(format!("Expected a function, got {}", self))),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue