don't treat js method as call

pull/5/head
azur 2023-03-07 04:35:16 +07:00
parent 35ffd11321
commit 8b927e4ac7
2 changed files with 9 additions and 31 deletions

View File

@ -14,7 +14,7 @@ pub enum JSExpr {
Op(&'static str, Box<Self>, Option<Box<Self>>),
Call(Box<Self>, Vec<Self>),
Method(Box<Self>, String, Option<Vec<Self>>),
Method(Box<Self>, String),
Lambda {
args: Vec<String>,
body: Vec<Self>,
@ -75,20 +75,7 @@ impl Display for JSExpr {
}
write!(f, ")")
},
JSExpr::Method(c, m, args) => {
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, ")")?;
}
Ok(())
},
JSExpr::Method(c, m) => write!(f, "{}.{}", c, m),
JSExpr::Lambda { args, body } => {
write!(f, "((")?;
for (i, name) in args.iter().enumerate() {

View File

@ -154,24 +154,15 @@ pub fn translate_js_expr(expr: Expr) -> JSExpr {
match *f {
Expr::Sym(ref s) => {
match s.as_str() {
"println" => {
JSExpr::Method(
Box::new(JSExpr::Sym("console".to_string())),
"log".to_string(),
Some(args.into_iter().map(translate_js_expr).collect()),
)
},
"print" => {
JSExpr::Method(
"print" => JSExpr::Call(
Box::new(JSExpr::Method(
Box::new(JSExpr::Sym("process".to_string())),
"stdout".to_string(),
None,
Box::new(JSExpr::Method(
Box::new(JSExpr::Sym("process".to_string())),
"stdout".to_string(),
)),
"write".to_string(),
)),
"write".to_string(),
Some(args.into_iter().map(translate_js_expr).collect()),
)
}
args.into_iter().map(translate_js_expr).collect()),
_ => JSExpr::Call(
Box::new(translate_js_expr(*f)),
args.into_iter().map(translate_js_expr).collect(),