36 lines
859 B
Rust
36 lines
859 B
Rust
|
use crate::ast_types::ModuleAST;
|
||
|
use super::Transpilable;
|
||
|
|
||
|
impl Transpilable for ModuleAST<'_> {
|
||
|
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);
|
||
|
}
|
||
|
}
|