Codegen minimal function declaration

master
Araozu 2023-09-08 20:28:53 -05:00
parent 5f25bae3e0
commit 2481b7297c
6 changed files with 43 additions and 6 deletions

View File

@ -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);
},
}
}
}

View File

@ -4,6 +4,7 @@ mod binding;
mod expression; mod expression;
mod module_ast; mod module_ast;
mod top_level_construct; mod top_level_construct;
mod function_declaration;
/// Trait that the AST and its nodes implement to support transformation to PHP /// Trait that the AST and its nodes implement to support transformation to PHP
trait Transpilable { trait Transpilable {

View File

@ -6,7 +6,7 @@ impl Transpilable for TopLevelConstruct {
fn transpile(&self) -> String { fn transpile(&self) -> String {
match self { match self {
TopLevelConstruct::Binding(binding) => binding.transpile(), TopLevelConstruct::Binding(binding) => binding.transpile(),
TopLevelConstruct::FunctionDeclaration(_) => todo!(), TopLevelConstruct::FunctionDeclaration(fun) => fun.transpile(),
} }
} }
} }

View File

@ -120,9 +120,9 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> Option<SyntaxResult>
}) })
}; };
Some(SyntaxResult::Ok(super::ast::TopLevelConstruct::Binding( Some(SyntaxResult::Ok(
binding, super::ast::TopLevelConstruct::Binding(binding)
))) ))
} }
/// Expects the token at `pos` to be of type `token_type` /// Expects the token at `pos` to be of type `token_type`

View File

@ -130,7 +130,7 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> Option<SyntaxResult>
current_pos += 1; current_pos += 1;
// Parse closing brace // 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::Ok(t) => t,
Result3::Err(t) => { Result3::Err(t) => {
// The parser found a token, but it's not an opening brace // The parser found a token, but it's not an opening brace

View File

@ -8,7 +8,7 @@ mod utils;
pub mod ast; pub mod ast;
use crate::lexic::token::Token; use crate::lexic::token::Token;
use ast::{Binding, ModuleAST}; use ast::ModuleAST;
use self::ast::TopLevelConstruct; use self::ast::TopLevelConstruct;
@ -46,5 +46,6 @@ pub fn construct_ast<'a>(tokens: &'a Vec<Token>) -> Result<ModuleAST, MistiError
fn next_construct<'a>(tokens: &'a Vec<Token>, current_pos: usize) -> SyntaxResult { fn next_construct<'a>(tokens: &'a Vec<Token>, current_pos: usize) -> SyntaxResult {
None.or_else(|| binding::try_parse(tokens, current_pos)) None.or_else(|| binding::try_parse(tokens, current_pos))
.or_else(|| function_declaration::try_parse(tokens, current_pos))
.unwrap_or_else(|| SyntaxResult::None) .unwrap_or_else(|| SyntaxResult::None)
} }