| ... | @@ -1,7 +1,9 @@ | ... | @@ -1,7 +1,9 @@ |
| | 1 | use miette::Result; |
| | 2 | |
| 1 | use super::ast::{Ast, Type}; | 3 | use super::ast::{Ast, Type}; |
| 2 | | 4 | |
| 3 | fn transpile_helper(tree: Ast) -> String { | 5 | fn transpile_helper(tree: Ast) -> Result<String> { |
| 4 | match tree { | 6 | Ok(match tree { |
| 5 | Ast::Number(number) => format!("{}", number), | 7 | Ast::Number(number) => format!("{}", number), |
| 6 | Ast::Operation { | 8 | Ast::Operation { |
| 7 | operator, | 9 | operator, |
| ... | @@ -9,27 +11,27 @@ fn transpile_helper(tree: Ast) -> String { | ... | @@ -9,27 +11,27 @@ fn transpile_helper(tree: Ast) -> String { |
| 9 | right, | 11 | right, |
| 10 | } => format!( | 12 | } => format!( |
| 11 | "{} {} {}", | 13 | "{} {} {}", |
| 12 | transpile_helper(*left), | 14 | transpile_helper(*left)?, |
| 13 | operator.to_string(), | 15 | operator.to_string(), |
| 14 | transpile_helper(*right) | 16 | transpile_helper(*right)? |
| 15 | ), | 17 | ), |
| 16 | Ast::VariableDeclaration { name, typ, value } => match typ { | 18 | Ast::VariableDeclaration { name, typ, value } => match typ { |
| 17 | Type::Bigint => format!("let {} = BigInt({});\n", name, transpile_helper(*value)), | 19 | Type::Bigint => format!("let {} = BigInt({});\n", name, transpile_helper(*value)?), |
| 18 | _ => format!("let {} = {};\n", name, transpile_helper(*value)), | 20 | _ => format!("let {} = {};\n", name, transpile_helper(*value)?), |
| 19 | }, | 21 | }, |
| 20 | Ast::ConstantDeclaration { name, typ, value } => match typ { | 22 | Ast::ConstantDeclaration { name, typ, value } => match typ { |
| 21 | Type::Bigint => format!("const {} = BigInt({});\n", name, transpile_helper(*value)), | 23 | Type::Bigint => format!("const {} = BigInt({});\n", name, transpile_helper(*value)?), |
| 22 | _ => format!("const {} = {};\n", name, transpile_helper(*value)), | 24 | _ => format!("const {} = {};\n", name, transpile_helper(*value)?), |
| 23 | }, | 25 | }, |
| 24 | } | 26 | }) |
| 25 | } | 27 | } |
| 26 | | 28 | |
| 27 | pub fn transpile(forest: Vec<Ast>) -> String { | 29 | pub fn transpile(forest: Vec<Ast>) -> Result<String> { |
| 28 | let mut result = String::new(); | 30 | let mut result = String::new(); |
| 29 | | 31 | |
| 30 | for tree in forest { | 32 | for tree in forest { |
| 31 | result.push_str(&transpile_helper(tree)); | 33 | result.push_str(&transpile_helper(tree)?); |
| 32 | } | 34 | } |
| 33 | | 35 | |
| 34 | result | 36 | Ok(result) |
| 35 | } | 37 | } |