1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

remove let without in

This commit is contained in:
azur 2023-03-02 12:04:12 +07:00
parent f6a5f904c0
commit 1eb3296ab6
6 changed files with 24 additions and 96 deletions

48
a.hlm
View file

@ -1,43 +1,15 @@
let name: str = "john";
println("Hello, " + name + "!"); println("Hello, " + name + "!");
let a = 17, b = 35 in let a: num = 17, b: num = 35 in
let c = a * 2 in let c: num = a * 2 in
println(b + c); println(b + c);
func foo (a: int, b: int) { // func foo (a: int, b: int) {
let c = a * 2; // let c = a * 2;
//
// let res = b + c in
// return res + a;
// }
let res = b + c in println((\x: int -> x + 1)(1));
return res + a;
}
println((\x: int -> x + 1)(1));
──────────────────────────────────────────────────
(println (+ "Hello, " name "!"))
(let [a 17] [b 35]
(let [c (* a 2)]
(println (+ b c))))
(func foo [a int b int] (block
(let [c (* a 2)])
(let [res (+ b c)]
(return (+ res a)))
))
──────────────────────────────────────────────────
console.log("Hello, " + name + "!");
let a = 17;
let b = 35;
let c = a * 2;
console.log(b + c);
const foo = (a, b) => {
let c = a * 2;
let res = b + c;
return res + a;
}

View file

@ -293,26 +293,17 @@ pub fn expr_parser() -> impl P<Spanned<PExpr>> {
.then(expr.clone()) .then(expr.clone())
.map(|(vars, body)| PExpr::Let { .map(|(vars, body)| PExpr::Let {
vars, vars,
body: Some(Box::new(body)), body: Box::new(body),
}) })
.boxed() .boxed()
.labelled("let..in"); .labelled("let..in");
let let_def = just(Token::Let)
.ignore_then(let_binds)
.map(|vars| PExpr::Let {
vars,
body: None,
})
.labelled("let");
let atom = lit let atom = lit
.or(sym) .or(sym)
.or(vec) .or(vec)
.or(paren_expr) .or(paren_expr)
.or(lam) .or(lam)
.or(let_in) .or(let_in)
.or(let_def)
.map_with_span(|e, s| (e, s)) .map_with_span(|e, s| (e, s))
.boxed() .boxed()
.labelled("atom"); .labelled("atom");

View file

@ -37,6 +37,6 @@ pub enum PExpr {
}, },
Let { Let {
vars: Vec<(String, Type, Spanned<Self>)>, vars: Vec<(String, Type, Spanned<Self>)>,
body: Option<Box<Spanned<Self>>>, body: Box<Spanned<Self>>,
}, },
} }

View file

@ -34,7 +34,6 @@ pub enum Expr {
args: Vec<(String, Type)>, args: Vec<(String, Type)>,
body: Box<Self>, body: Box<Self>,
}, },
Define(Vec<(String, Box<Self>)>),
} }
impl Display for Expr { impl Display for Expr {
@ -72,11 +71,6 @@ impl Display for Expr {
} }
write!(f, " {})", body) write!(f, " {})", body)
}, },
Expr::Define(vars) => {
vars.iter().try_for_each(|(name, val)| {
write!(f, "(define {} {})", name, val)
})
},
} }
} }
} }

View file

@ -19,7 +19,6 @@ pub enum JSExpr {
args: Vec<(String, Type)>, args: Vec<(String, Type)>,
body: Box<Self>, body: Box<Self>,
}, },
Let(Vec<(String, Box<Self>)>),
} }
impl Display for JSExpr { impl Display for JSExpr {
@ -77,11 +76,6 @@ impl Display for JSExpr {
} }
write!(f, ") => {})", body) write!(f, ") => {})", body)
}, },
JSExpr::Let(vars) => {
vars.iter().try_for_each(|(name, expr)| {
write!(f, "let {} = {};", name, expr)
})
},
} }
} }
} }

View file

@ -52,34 +52,19 @@ pub fn translate_expr(expr: PExpr) -> Expr {
body: Box::new(translate_expr((*body).0)), body: Box::new(translate_expr((*body).0)),
}, },
PExpr::Let { vars, body } => { PExpr::Let { vars, body } => {
if let Some(body) = body { let mut expr: Expr = translate_expr(body.0); // The expression we're building up
let mut expr: Expr = translate_expr(body.0); // The expression we're building up for (name, ty, val) in vars.into_iter().rev() { // Reverse so we can build up the lambda
for (name, ty, val) in vars.into_iter().rev() { // Reverse so we can build up the lambda // e.g.: let x : t = e1 in e2; => (lambda (x : t) = e2)(e1)
// e.g.: let x : t = e1 in e2; => (lambda (x : t) = e2)(e1) // Build up the lambda
expr = Expr::Lambda {
// Build up the lambda args: vec![(name, ty)],
expr = Expr::Lambda { body: Box::new(expr),
args: vec![(name, ty)], };
body: Box::new(expr), // Call the lambda with the value
}; let val = translate_expr(val.0);
// Call the lambda with the value expr = Expr::Call(Box::new(expr), vec![val]);
let val = translate_expr(val.0);
expr = Expr::Call(Box::new(expr), vec![val]);
}
expr
} else {
// e.g. let a : t = 1; => (define a 1)
// let a : t = 2, b : t = 3; => (define a 2) (define b 3)
let mut xs: Vec<(String, Box<Expr>)> = vec![];
for (name, _, val) in vars {
let val = translate_expr(val.0);
xs.push((name, Box::new(val)));
}
Expr::Define(xs)
} }
expr
} }
} }
} }
@ -143,13 +128,5 @@ pub fn translate_js(expr: Expr) -> JSExpr {
args, args,
body: Box::new(translate_js(*body)), body: Box::new(translate_js(*body)),
}, },
Expr::Define(xs) => {
let ns = xs
.into_iter()
.map(|(name, val)| (name, Box::new(translate_js(*val))))
.collect();
JSExpr::Let(ns)
}
} }
} }