From 35ffd11321b54a43c94af235c34a4488cac606a5 Mon Sep 17 00:00:00 2001 From: azur Date: Tue, 7 Mar 2023 04:27:24 +0700 Subject: [PATCH] allow js method chaining --- a.hlm | 5 +---- src/asts/js.rs | 18 +++++++++++------- src/trans/low.rs | 13 ++++++++++++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/a.hlm b/a.hlm index 744948e..b1189ce 100644 --- a/a.hlm +++ b/a.hlm @@ -1,4 +1 @@ -let x: num = 1, - y: num = 2, -in - println(if x + y == 3 then 69 else 0); \ No newline at end of file +print("Hello\n"); \ No newline at end of file diff --git a/src/asts/js.rs b/src/asts/js.rs index 1ef385e..276d895 100644 --- a/src/asts/js.rs +++ b/src/asts/js.rs @@ -14,7 +14,7 @@ pub enum JSExpr { Op(&'static str, Box, Option>), Call(Box, Vec), - Method(Box, String, Vec), + Method(Box, String, Option>), Lambda { args: Vec, body: Vec, @@ -76,14 +76,18 @@ impl Display for JSExpr { write!(f, ")") }, JSExpr::Method(c, m, args) => { - write!(f, "{}.{}(", c, m)?; - for (i, arg) in args.iter().enumerate() { - if i > 0 { - write!(f, ", ")?; + write!(f, "{}.{}", c, m)?; + if let Some(args) = args { + write!(f, "(")?; + for (i, arg) in args.iter().enumerate() { + if i > 0 { + write!(f, ", ")?; + } + write!(f, "{}", arg)?; } - write!(f, "{}", arg)?; + write!(f, ")")?; } - write!(f, ")") + Ok(()) }, JSExpr::Lambda { args, body } => { write!(f, "((")?; diff --git a/src/trans/low.rs b/src/trans/low.rs index ca02aab..400ba68 100644 --- a/src/trans/low.rs +++ b/src/trans/low.rs @@ -158,9 +158,20 @@ pub fn translate_js_expr(expr: Expr) -> JSExpr { JSExpr::Method( Box::new(JSExpr::Sym("console".to_string())), "log".to_string(), - args.into_iter().map(translate_js_expr).collect(), + Some(args.into_iter().map(translate_js_expr).collect()), ) }, + "print" => { + JSExpr::Method( + Box::new(JSExpr::Method( + Box::new(JSExpr::Sym("process".to_string())), + "stdout".to_string(), + None, + )), + "write".to_string(), + Some(args.into_iter().map(translate_js_expr).collect()), + ) + } _ => JSExpr::Call( Box::new(translate_js_expr(*f)), args.into_iter().map(translate_js_expr).collect(),