From 2481b7297c9e0b8edb4d2996e7411ac8e06db529 Mon Sep 17 00:00:00 2001 From: Araozu Date: Fri, 8 Sep 2023 20:28:53 -0500 Subject: [PATCH] Codegen minimal function declaration --- src/codegen/function_declaration.rs | 35 +++++++++++++++++++++++++++++ src/codegen/mod.rs | 1 + src/codegen/top_level_construct.rs | 2 +- src/syntax/binding.rs | 6 ++--- src/syntax/function_declaration.rs | 2 +- src/syntax/mod.rs | 3 ++- 6 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 src/codegen/function_declaration.rs diff --git a/src/codegen/function_declaration.rs b/src/codegen/function_declaration.rs new file mode 100644 index 0000000..01978bf --- /dev/null +++ b/src/codegen/function_declaration.rs @@ -0,0 +1,35 @@ +use crate::syntax::ast::FunctionDeclaration; + +use super::Transpilable; + +impl Transpilable for FunctionDeclaration { + fn transpile(&self) -> String { + format!("function {}() {{}}", self.identifier) + } +} + + +#[cfg(test)] +mod tests { + use super::*; + use crate::{lexic::get_tokens, syntax::{construct_ast, ast::TopLevelConstruct}}; + + + #[test] + fn should_transpile() { + let tokens = get_tokens(&String::from("fun id() {}")).unwrap(); + let result = construct_ast(&tokens).unwrap(); + + let fun_dec = result.bindings.get(0).unwrap(); + + match fun_dec { + TopLevelConstruct::Binding(_) => panic!("Expected function declaration"), + TopLevelConstruct::FunctionDeclaration(fun_decl) => { + let transpiled = fun_decl.transpile(); + + assert_eq!("function id() {}", transpiled); + }, + } + } +} + diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index a7927ae..71fcadf 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -4,6 +4,7 @@ mod binding; mod expression; mod module_ast; mod top_level_construct; +mod function_declaration; /// Trait that the AST and its nodes implement to support transformation to PHP trait Transpilable { diff --git a/src/codegen/top_level_construct.rs b/src/codegen/top_level_construct.rs index caf77fb..487ccba 100644 --- a/src/codegen/top_level_construct.rs +++ b/src/codegen/top_level_construct.rs @@ -6,7 +6,7 @@ impl Transpilable for TopLevelConstruct { fn transpile(&self) -> String { match self { TopLevelConstruct::Binding(binding) => binding.transpile(), - TopLevelConstruct::FunctionDeclaration(_) => todo!(), + TopLevelConstruct::FunctionDeclaration(fun) => fun.transpile(), } } } diff --git a/src/syntax/binding.rs b/src/syntax/binding.rs index aa64e14..20264eb 100644 --- a/src/syntax/binding.rs +++ b/src/syntax/binding.rs @@ -120,9 +120,9 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> Option }) }; - Some(SyntaxResult::Ok(super::ast::TopLevelConstruct::Binding( - binding, - ))) + Some(SyntaxResult::Ok( + super::ast::TopLevelConstruct::Binding(binding) + )) } /// Expects the token at `pos` to be of type `token_type` diff --git a/src/syntax/function_declaration.rs b/src/syntax/function_declaration.rs index cc362a4..122aea6 100644 --- a/src/syntax/function_declaration.rs +++ b/src/syntax/function_declaration.rs @@ -130,7 +130,7 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> Option current_pos += 1; // Parse closing brace - let closing_brace = match try_token_type(tokens, current_pos, TokenType::RightBrace) { + let _closing_brace = match try_token_type(tokens, current_pos, TokenType::RightBrace) { Result3::Ok(t) => t, Result3::Err(t) => { // The parser found a token, but it's not an opening brace diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index cdfa81c..00a4be0 100755 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -8,7 +8,7 @@ mod utils; pub mod ast; use crate::lexic::token::Token; -use ast::{Binding, ModuleAST}; +use ast::ModuleAST; use self::ast::TopLevelConstruct; @@ -46,5 +46,6 @@ pub fn construct_ast<'a>(tokens: &'a Vec) -> Result(tokens: &'a Vec, current_pos: usize) -> SyntaxResult { None.or_else(|| binding::try_parse(tokens, current_pos)) + .or_else(|| function_declaration::try_parse(tokens, current_pos)) .unwrap_or_else(|| SyntaxResult::None) }