~jojo/effem

a72563642bec5fcb38767ed740bb21e8abc02108 — JoJo 1 year, 5 months ago f7568fa
remove ints (for now)

Unnecessary complexity this early on. Just more cases to handle. More
inertia when I want to change stuff. We'll add them back when the
language actually starts getting more feature rich and we need them again.
9 files changed, 12 insertions(+), 20 deletions(-)

M src/abase.rs
M src/ast.rs
M src/base.rs
M src/desugar.rs
M src/eval.rs
M src/fem.rs
M src/main.rs
M src/parse.rs
M src/resolve.rs
M src/abase.rs => src/abase.rs +0 -1
@@ 4,7 4,6 @@ use crate::fem;
pub fn abase(expr: &fem::Expr) -> anyhow::Result<Expr> {
    Ok(match *expr {
        fem::Expr::F64(x) => Expr::F64(x),
        fem::Expr::I64(x) => Expr::I64(x),
        fem::Expr::App(ref f, ref xs) => match **f {
            fem::Expr::Var(ref x) if x.to_string() == "+.builtin." =>
                Expr::Add(Box::new(abase(&xs[0])?), Box::new(abase(&xs[1])?)),

M src/ast.rs => src/ast.rs +0 -1
@@ 17,7 17,6 @@ pub struct Module<N> {
#[derive(Debug, PartialEq)]
pub enum Expr<N> {
    F64(f64),
    I64(i64),
    App(Box<Expr<N>>, Vec<Expr<N>>),
    Var(N),
}

M src/base.rs => src/base.rs +0 -1
@@ 3,7 3,6 @@
#[derive(Clone)]
pub enum Expr {
    F64(f64),
    I64(i64),
    Add(Box<Expr>, Box<Expr>),
    Mul(Box<Expr>, Box<Expr>),
}

M src/desugar.rs => src/desugar.rs +0 -1
@@ 4,7 4,6 @@ use crate::fem::*;
pub fn desugar(expr: &ast::RExpr) -> anyhow::Result<Expr> {
    Ok(match expr {
        ast::Expr::F64(x) => Expr::F64(*x),
        ast::Expr::I64(x) => Expr::I64(*x),
        ast::Expr::Var(x) => Expr::Var(x.clone()),
        ast::Expr::App(f, xs) =>
            Expr::App(Box::new(desugar(f)?), xs.iter().map(|x| desugar(x)).collect::<Result<_, _>>()?),

M src/eval.rs => src/eval.rs +0 -1
@@ 7,7 7,6 @@ pub fn run<'c>(cache: &'c mut Cache, entrypoint: &FullQualName) -> anyhow::Resul
pub fn interpret(cache: &mut Cache, expr: &base::Expr) -> anyhow::Result<Val> {
    Ok(match *expr {
        base::Expr::F64(x) => Val::F64(x),
        base::Expr::I64(x) => Val::F64(x as f64),
        base::Expr::Add(ref x, ref y) => match (interpret(cache, x)?, interpret(cache, y)?) {
            (Val::F64(x), Val::F64(y)) => Val::F64(x + y),
        },

M src/fem.rs => src/fem.rs +0 -1
@@ 11,7 11,6 @@ pub struct Module {
#[derive(Clone, Debug, PartialEq)]
pub enum Expr {
    F64(f64),
    I64(i64),
    Var(FullQualName),
    App(Box<Expr>, Vec<Expr>),
}

M src/main.rs => src/main.rs +2 -2
@@ 55,11 55,11 @@ mod test {

    #[test]
    fn test_main_arithm1() {
        assert_eq!(run_tmp("(def main (+ 1 2))").unwrap(), Val::F64(3.0))
        assert_eq!(run_tmp("(def main (+ 1.0 2.0))").unwrap(), Val::F64(3.0))
    }

    #[test]
    fn test_main_arithm2() {
        assert_eq!(run_tmp("(def main (+ (* 13 100) 37))").unwrap(), Val::F64(1337.0))
        assert_eq!(run_tmp("(def main (+ (* 13.0 100.0) 37.0))").unwrap(), Val::F64(1337.0))
    }
}

M src/parse.rs => src/parse.rs +10 -11
@@ 89,9 89,8 @@ fn def<'s: 't, 't>(inp: Inp<'s, 't>) -> Res<'s, 't, (ItemName, PExpr)> {

fn expr<'s: 't, 't>(inp: Inp<'s, 't>) -> Res<'s, 't, PExpr> {
    // TODO
    alt4(
    alt3(
        map(lit_float, Expr::F64),
        map(lit_int, Expr::I64),
        map(name, Expr::Var),
        map(parens(pair(expr, rest(expr))), |(f, xs)| Expr::App(Box::new(f), xs)),
    )(inp)


@@ 127,12 126,12 @@ fn lit_float<'s: 't, 't>(inp: Inp<'s, 't>) -> Res<'s, 't, f64> {
    })
}

fn lit_int<'s: 't, 't>(inp: Inp<'s, 't>) -> Res<'s, 't, i64> {
    inp.filter_next(|tok| match *tok {
        Tok::Int(x) => Ok(x),
        _ => Err("*integer literal*"),
    })
}
// fn lit_int<'s: 't, 't>(inp: Inp<'s, 't>) -> Res<'s, 't, i64> {
//     inp.filter_next(|tok| match *tok {
//         Tok::Int(x) => Ok(x),
//         _ => Err("*integer literal*"),
//     })
// }

fn parens<'s: 't, 't, A>(mut f: impl Parser<'s, 't, A>) -> impl Parser<'s, 't, A> {
    move |inp| {


@@ 263,12 262,12 @@ mod test {

    #[test]
    fn test_arithm() {
        let m = parse(None, &lex(None, "(def x (+ 1 2))").unwrap()).unwrap();
        let m = parse(None, &lex(None, "(def x (+ 1.0 2.0))").unwrap()).unwrap();
        assert_matches!(
            &m.defs["x"],
            Expr::App(f, xs) if **f == Expr::Var(ParsedName::unqualified("+"))
                             && xs[0] == Expr::I64(1)
                             && xs[1] == Expr::I64(2)
                             && xs[0] == Expr::F64(1.0)
                             && xs[1] == Expr::F64(2.0)
        )
    }
}

M src/resolve.rs => src/resolve.rs +0 -1
@@ 14,7 14,6 @@ pub fn resolve_expr(expr: PExpr) -> anyhow::Result<RExpr> {
    use Expr::*;
    Ok(match expr {
        F64(x) => F64(x),
        I64(x) => I64(x),
        Var(x) => Var(match x {
            // FIXME
            ParsedName { root: None, prefix, name } => FullQualName {