From 1eb3296ab6006b05be58ade1bca9f3e3507d237f Mon Sep 17 00:00:00 2001 From: azur Date: Thu, 2 Mar 2023 12:04:12 +0700 Subject: [PATCH] remove let without in --- a.hlm | 48 ++++++++++------------------------------------- src/read/parse.rs | 11 +---------- src/read/past.rs | 2 +- src/trans/ast.rs | 6 ------ src/trans/js.rs | 6 ------ src/trans/low.rs | 47 ++++++++++++---------------------------------- 6 files changed, 24 insertions(+), 96 deletions(-) diff --git a/a.hlm b/a.hlm index 899c35b..1b601c8 100644 --- a/a.hlm +++ b/a.hlm @@ -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; -} \ No newline at end of file +println((\x: int -> x + 1)(1)); \ No newline at end of file diff --git a/src/read/parse.rs b/src/read/parse.rs index c34a196..bbc35ae 100644 --- a/src/read/parse.rs +++ b/src/read/parse.rs @@ -293,26 +293,17 @@ pub fn expr_parser() -> impl P> { .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"); diff --git a/src/read/past.rs b/src/read/past.rs index 0adc159..6560cf7 100644 --- a/src/read/past.rs +++ b/src/read/past.rs @@ -37,6 +37,6 @@ pub enum PExpr { }, Let { vars: Vec<(String, Type, Spanned)>, - body: Option>>, + body: Box>, }, } \ No newline at end of file diff --git a/src/trans/ast.rs b/src/trans/ast.rs index 98c20a0..c3b4442 100644 --- a/src/trans/ast.rs +++ b/src/trans/ast.rs @@ -34,7 +34,6 @@ pub enum Expr { args: Vec<(String, Type)>, body: Box, }, - Define(Vec<(String, Box)>), } 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) - }) - }, } } } \ No newline at end of file diff --git a/src/trans/js.rs b/src/trans/js.rs index e55e7a6..9329cfe 100644 --- a/src/trans/js.rs +++ b/src/trans/js.rs @@ -19,7 +19,6 @@ pub enum JSExpr { args: Vec<(String, Type)>, body: Box, }, - Let(Vec<(String, Box)>), } 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) - }) - }, } } } \ No newline at end of file diff --git a/src/trans/low.rs b/src/trans/low.rs index 3179fdf..878b85b 100644 --- a/src/trans/low.rs +++ b/src/trans/low.rs @@ -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)> = 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) - } } } \ No newline at end of file