Skip to content

Commit

Permalink
rip out interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgrosen committed Jun 7, 2024
1 parent 7bddd12 commit 0867ccb
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 431 deletions.
40 changes: 5 additions & 35 deletions src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,64 @@ use std::collections::HashMap;

use string_interner::DefaultStringInterner;

use crate::expr::{Symbol, Value};
use crate::expr::Symbol;
use crate::ir1::{DebruijnIndex, Op};
use crate::ir2;

type BuiltinFn = for<'a> fn(&[Value<'a>]) -> Result<Value<'a>, &'static str>;

#[derive(Clone, Copy, Debug)]
pub struct Builtin {
pub n_args: usize,
pub body: BuiltinFn,
pub ir2_expr: &'static ir2::Expr<'static>,
}

impl Builtin {
const fn new(n_args: usize, body: BuiltinFn, ir2_expr: &'static ir2::Expr<'static>) -> Builtin {
Builtin { n_args, body, ir2_expr }
const fn new(n_args: usize, ir2_expr: &'static ir2::Expr<'static>) -> Builtin {
Builtin { n_args, ir2_expr }
}
}

macro_rules! builtins {
( $($name:ident [ $nargs:literal ] { $pat:pat => $e:expr } [ $ir2e:expr ] ),* $(,)? ) => {
mod builtins {
use super::*;

$(
pub fn $name<'a>(args: &[Value<'a>]) -> Result<Value<'a>, &'static str> {
match args {
$pat => $e,
_ => Err(concat!("args passed to ", stringify!($name), " do not match the pattern ", stringify!($pat))),
}
}
)*
}
( $($name:ident [ $nargs:literal ] [ $ir2e:expr ] ),* $(,)? ) => {
const BUILTINS: &[(&'static str, Builtin)] = &[
$(
(stringify!($name), Builtin::new($nargs, builtins::$name, $ir2e)),
(stringify!($name), Builtin::new($nargs, $ir2e)),
)*
];
}
}

builtins!(
pi[0]
{ &[] => Ok(Value::Sample(std::f32::consts::PI)) }
[ &ir2::Expr::Op(Op::Pi, &[]) ],
sin[1]
{ &[Value::Sample(s)] => Ok(Value::Sample(s.sin())) }
[ &ir2::Expr::Op(Op::Sin, &[&ir2::Expr::Var(DebruijnIndex(0))]) ],
cos[1]
{ &[Value::Sample(s)] => Ok(Value::Sample(s.cos())) }
[ &ir2::Expr::Op(Op::Cos, &[&ir2::Expr::Var(DebruijnIndex(0))]) ],
add[2]
{ &[Value::Sample(s1), Value::Sample(s2)] => Ok(Value::Sample(s1 + s2)) }
[ &ir2::Expr::Op(Op::FAdd, &[&ir2::Expr::Var(DebruijnIndex(0)), &ir2::Expr::Var(DebruijnIndex(1))]) ],
sub[2]
{ &[Value::Sample(s1), Value::Sample(s2)] => Ok(Value::Sample(s1 - s2)) }
// TODO: make sure this order is correct?
[ &ir2::Expr::Op(Op::FSub, &[&ir2::Expr::Var(DebruijnIndex(0)), &ir2::Expr::Var(DebruijnIndex(1))]) ],
mul[2]
{ &[Value::Sample(s1), Value::Sample(s2)] => Ok(Value::Sample(s1 * s2)) }
[ &ir2::Expr::Op(Op::FMul, &[&ir2::Expr::Var(DebruijnIndex(0)), &ir2::Expr::Var(DebruijnIndex(1))]) ],
div[2]
{ &[Value::Sample(s1), Value::Sample(s2)] => Ok(Value::Sample(s1 / s2)) }
[ &ir2::Expr::Op(Op::FDiv, &[&ir2::Expr::Var(DebruijnIndex(0)), &ir2::Expr::Var(DebruijnIndex(1))]) ],
addone[1]
{ &[Value::Sample(s)] => Ok(Value::Sample(s + 1.0)) }
[ &ir2::Expr::Op(Op::FAdd, &[&ir2::Expr::Var(DebruijnIndex(0)), &ir2::Expr::Op(Op::Const(crate::ir1::Value::Sample(1.0)), &[])]) ],
reinterpi[1]
{ &[Value::Index(i)] => Ok(Value::Sample(f32::from_bits(i as u32))) }
[ &ir2::Expr::Op(Op::ReinterpI2F, &[&ir2::Expr::Var(DebruijnIndex(0))]) ],
reinterpf[1]
{ &[Value::Sample(x)] => Ok(Value::Index(x.to_bits() as usize)) }
[ &ir2::Expr::Op(Op::ReinterpF2I, &[&ir2::Expr::Var(DebruijnIndex(0))]) ],
cast[1]
{ &[Value::Index(i)] => Ok(Value::Sample(i as f32)) }
[ &ir2::Expr::Op(Op::CastI2F, &[&ir2::Expr::Var(DebruijnIndex(0))]) ],
since_tick[1]
{ &[_] => panic!("oops no interpreter") }
[ &ir2::Expr::Op(Op::SinceLastTickStream, &[&ir2::Expr::Var(DebruijnIndex(0))]) ],
wait[1]
{ &[_] => panic!("oops no interpreter") }
[ &ir2::Expr::Op(Op::Wait, &[&ir2::Expr::Var(DebruijnIndex(0))]) ],
sched[3]
{ &[_] => panic!("oops no interpreter") }
[ &ir2::Expr::Op(Op::Schedule, &[&ir2::Expr::Var(DebruijnIndex(2)), &ir2::Expr::Var(DebruijnIndex(1)), &ir2::Expr::Var(DebruijnIndex(0))]) ],
);


pub type BuiltinsMap = HashMap<Symbol, Builtin>;

pub fn make_builtins(interner: &mut DefaultStringInterner) -> BuiltinsMap {
Expand Down
21 changes: 2 additions & 19 deletions src/expr.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
use std::collections::HashMap;
use std::fmt;

use string_interner::{DefaultStringInterner, DefaultSymbol};
use typed_arena::Arena;

use crate::builtin::Builtin;
use crate::typing::{Clock, Type, PrettyClock, PrettyType};

pub type Symbol = DefaultSymbol;

#[derive(Debug, Clone)]
pub enum Value<'a> {
pub enum Value {
Unit,
Sample(f32),
Index(usize),
Pair(Box<Value<'a>>, Box<Value<'a>>),
InL(Box<Value<'a>>),
InR(Box<Value<'a>>),
Gen(Box<Value<'a>>, Box<Value<'a>>),
Closure(Env<'a>, Symbol, &'a Expr<'a, ()>),
Suspend(Env<'a>, &'a Expr<'a, ()>),
BuiltinPartial(Builtin, Box<[Value<'a>]>),
Array(Box<[Value<'a>]>),
Box(Env<'a>, &'a Expr<'a, ()>),
// TODO: this is a bit of a hack
BoxDelay(Env<'a>, &'a Expr<'a, ()>),
}

// TODO: use a better type here... eventually we should resolve symbols and just use de bruijn offsets or similar...
pub type Env<'a> = HashMap<Symbol, Value<'a>>;
// type Env<'a> = imbl::HashMap<Symbol, Value<'a>>;

#[derive(Debug, Clone, Copy)]
pub enum Binop {
FMul,
Expand Down Expand Up @@ -84,7 +67,7 @@ impl Binop {
#[derive(Debug, Clone)]
pub enum Expr<'a, R> {
Var(R, Symbol),
Val(R, Value<'a>),
Val(R, Value),
Annotate(R, &'a Expr<'a, R>, Type),
Lam(R, Symbol, &'a Expr<'a, R>),
App(R, &'a Expr<'a, R>, &'a Expr<'a, R>),
Expand Down
217 changes: 0 additions & 217 deletions src/interp.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/ir1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,6 @@ impl<'a> Translator<'a> {
Expr::Val(Value::Sample(x)),
HExpr::Val(_, HValue::Index(i)) =>
Expr::Val(Value::Index(i)),
HExpr::Val(_, _) =>
panic!("weird value??"),
HExpr::Annotate(_, next, _) =>
self.translate(ctx, next),
HExpr::Lam(_, x, next) => {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod expr;
pub mod builtin;
pub mod parse;
pub mod interp;
pub mod typing;
pub mod ir1;
pub mod ir2;
Expand Down
Loading

0 comments on commit 0867ccb

Please sign in to comment.