thp/compiler/src/codegen/module_ast.rs

42 lines
1.0 KiB
Rust
Raw Normal View History

2023-01-23 14:31:49 +00:00
use super::Transpilable;
use crate::ast_types::ModuleAST;
2023-01-23 14:31:49 +00:00
impl Transpilable for ModuleAST<'_> {
/// Transpiles the whole AST into JS, using this same trait on the
/// nodes and leaves of the AST
2023-01-23 14:31:49 +00:00
fn transpile(&self) -> String {
let bindings_str: Vec<String> = self
.bindings
.iter()
.map(|binding| binding.transpile())
.collect();
2023-01-23 14:31:49 +00:00
bindings_str.join("\n")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast_types::{Binding, Expression, ValBinding};
2023-01-23 14:31:49 +00:00
#[test]
fn module_ast_should_transpile() {
let id = String::from("identifier");
let value = String::from("322");
let binding = Binding::Val(ValBinding {
datatype: None,
2023-01-23 14:31:49 +00:00
identifier: &id,
expression: Expression::Number(&value),
});
let module = ModuleAST {
bindings: vec![binding],
};
let result = module.transpile();
assert_eq!("const identifier = 322;", result);
}
2023-01-23 14:31:49 +00:00
}