authorgravatar for 11367844+lino-levan@users.noreply.github.comLino Le Van <11367844+lino-levan@users.noreply.github.com> 2024-01-03 18:13:27+01:00
committergravatar for 11367844+lino-levan@users.noreply.github.comLino Le Van <11367844+lino-levan@users.noreply.github.com> 2024-01-03 18:13:27+01:00
logb273a9fc6eaec7ed69a8c10b61d800e2b434f23f
treeadcb2388e49e229932bef172a5f57af1cb279244
parent6c14072860c381a177f886b7c3f00001fafd9abc

chore: make walker return a result


2 files changed, 15 insertions(+), 13 deletions(-)

src/transpile/mod.rs+1-1
......@@ -13,7 +13,7 @@ pub fn transpile(content: String) -> Result<String> {
1313 let tree = ast::parse(tokens)?;
1414 print!("{:?}", tree);
1515 // 3. Transpile the AST into javascript
16 let result = walker::transpile(tree);
16 let result = walker::transpile(tree)?;
1717 // 4. Return the javascript
1818 return Ok(result);
1919}
src/transpile/walker.rs+14-12
......@@ -1,7 +1,9 @@
1use miette::Result;
2
13use super::ast::{Ast, Type};
24
3fn transpile_helper(tree: Ast) -> String {
4 match tree {
5fn transpile_helper(tree: Ast) -> Result<String> {
6 Ok(match tree {
57 Ast::Number(number) => format!("{}", number),
68 Ast::Operation {
79 operator,
......@@ -9,27 +11,27 @@ fn transpile_helper(tree: Ast) -> String {
911 right,
1012 } => format!(
1113 "{} {} {}",
12 transpile_helper(*left),
14 transpile_helper(*left)?,
1315 operator.to_string(),
14 transpile_helper(*right)
16 transpile_helper(*right)?
1517 ),
1618 Ast::VariableDeclaration { name, typ, value } => match typ {
17 Type::Bigint => format!("let {} = BigInt({});\n", name, transpile_helper(*value)),
18 _ => format!("let {} = {};\n", name, transpile_helper(*value)),
19 Type::Bigint => format!("let {} = BigInt({});\n", name, transpile_helper(*value)?),
20 _ => format!("let {} = {};\n", name, transpile_helper(*value)?),
1921 },
2022 Ast::ConstantDeclaration { name, typ, value } => match typ {
21 Type::Bigint => format!("const {} = BigInt({});\n", name, transpile_helper(*value)),
22 _ => format!("const {} = {};\n", name, transpile_helper(*value)),
23 Type::Bigint => format!("const {} = BigInt({});\n", name, transpile_helper(*value)?),
24 _ => format!("const {} = {};\n", name, transpile_helper(*value)?),
2325 },
24 }
26 })
2527}
2628
27pub fn transpile(forest: Vec<Ast>) -> String {
29pub fn transpile(forest: Vec<Ast>) -> Result<String> {
2830 let mut result = String::new();
2931
3032 for tree in forest {
31 result.push_str(&transpile_helper(tree));
33 result.push_str(&transpile_helper(tree)?);
3234 }
3335
34 result
36 Ok(result)
3537}