Codegen minimal function declaration
This commit is contained in:
parent
5f25bae3e0
commit
2481b7297c
35
src/codegen/function_declaration.rs
Normal file
35
src/codegen/function_declaration.rs
Normal 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);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -120,9 +120,9 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> Option<SyntaxResult>
|
||||
})
|
||||
};
|
||||
|
||||
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`
|
||||
|
@ -130,7 +130,7 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> Option<SyntaxResult>
|
||||
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
|
||||
|
@ -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<Token>) -> Result<ModuleAST, MistiError
|
||||
|
||||
fn next_construct<'a>(tokens: &'a Vec<Token>, 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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user