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> {...@@ -13,7 +13,7 @@ pub fn transpile(content: String) -> Result<String> {
13 let tree = ast::parse(tokens)?;13 let tree = ast::parse(tokens)?;
14 print!("{:?}", tree);14 print!("{:?}", tree);
15 // 3. Transpile the AST into javascript15 // 3. Transpile the AST into javascript
16 let result = walker::transpile(tree);16 let result = walker::transpile(tree)?;
17 // 4. Return the javascript17 // 4. Return the javascript
18 return Ok(result);18 return Ok(result);
19}19}
src/transpile/walker.rs+14-12
...@@ -1,7 +1,9 @@...@@ -1,7 +1,9 @@
1use miette::Result;
2
1use super::ast::{Ast, Type};3use super::ast::{Ast, Type};
24
3fn transpile_helper(tree: Ast) -> String {5fn 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}
2628
27pub fn transpile(forest: Vec<Ast>) -> String {29pub fn transpile(forest: Vec<Ast>) -> Result<String> {
28 let mut result = String::new();30 let mut result = String::new();
2931
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 }
3335
34 result36 Ok(result)
35}37}