2023-01-23 14:31:49 +00:00
|
|
|
use crate::ast_types::ModuleAST;
|
|
|
|
use super::Transpilable;
|
|
|
|
|
|
|
|
impl Transpilable for ModuleAST<'_> {
|
2023-02-11 23:13:05 +00:00
|
|
|
/// 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();
|
|
|
|
|
|
|
|
bindings_str.join("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::ast_types::{Expression, ValBinding, Binding};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_ast_should_transpile() {
|
|
|
|
let id = String::from("identifier");
|
|
|
|
let value = String::from("322");
|
|
|
|
let binding = Binding::Val(ValBinding {
|
|
|
|
identifier: &id,
|
|
|
|
expression: Expression::Number(&value),
|
|
|
|
});
|
|
|
|
|
|
|
|
let module = ModuleAST {
|
|
|
|
bindings: vec![binding],
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = module.transpile();
|
|
|
|
|
|
|
|
assert_eq!("const identifier = 322;", result);
|
|
|
|
}
|
|
|
|
}
|