remove let without in

pull/5/head
azur 2023-03-02 12:04:12 +07:00
parent 2afbb05bb6
commit f877bcf20b
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 + "!");
let a = 17, b = 35 in
let c = a * 2 in
let a: num = 17, b: num = 35 in
let c: num = a * 2 in
println(b + c);
func foo (a: int, b: int) {
let c = a * 2;
// func foo (a: int, b: int) {
// let c = a * 2;
//
// let res = b + c in
// return res + a;
// }
let res = b + c in
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;
}
println((\x: int -> x + 1)(1));

View File

@ -293,26 +293,17 @@ pub fn expr_parser() -> impl P<Spanned<PExpr>> {
.then(expr.clone())
.map(|(vars, body)| PExpr::Let {
vars,
body: Some(Box::new(body)),
body: Box::new(body),
})
.boxed()
.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
.or(sym)
.or(vec)
.or(paren_expr)
.or(lam)
.or(let_in)
.or(let_def)
.map_with_span(|e, s| (e, s))
.boxed()
.labelled("atom");

View File

@ -37,6 +37,6 @@ pub enum PExpr {
},
Let {
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)>,
body: Box<Self>,
},
Define(Vec<(String, Box<Self>)>),
}
impl Display for Expr {
@ -72,11 +71,6 @@ impl Display for Expr {
}
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)>,
body: Box<Self>,
},
Let(Vec<(String, Box<Self>)>),
}
impl Display for JSExpr {
@ -77,11 +76,6 @@ impl Display for JSExpr {
}
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)),
},
PExpr::Let { vars, body } => {
if let Some(body) = body {
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
// e.g.: let x : t = e1 in e2; => (lambda (x : t) = e2)(e1)
// Build up the lambda
expr = Expr::Lambda {
args: vec![(name, ty)],
body: Box::new(expr),
};
// Call the lambda with the value
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)
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
// e.g.: let x : t = e1 in e2; => (lambda (x : t) = e2)(e1)
// Build up the lambda
expr = Expr::Lambda {
args: vec![(name, ty)],
body: Box::new(expr),
};
// Call the lambda with the value
let val = translate_expr(val.0);
expr = Expr::Call(Box::new(expr), vec![val]);
}
expr
}
}
}
@ -143,13 +128,5 @@ pub fn translate_js(expr: Expr) -> JSExpr {
args,
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)
}
}
}