diff --git a/CHANGELOG.md b/CHANGELOG.md index e9c1219..2f1949d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Watch mode - Formatter - Simple language server +- Decide how to handle comments in the syntax (?) ## v0.0.10 diff --git a/src/codegen/block.rs b/src/codegen/block.rs new file mode 100644 index 0000000..bdd3759 --- /dev/null +++ b/src/codegen/block.rs @@ -0,0 +1,14 @@ +use crate::syntax::ast::Block; + +use super::Transpilable; + +impl Transpilable for Block { + fn transpile(&self) -> String { + // TODO: Handle indentation + self.statements + .iter() + .map(|x| x.transpile()) + .collect::>() + .join("\n") + } +} diff --git a/src/codegen/function_declaration.rs b/src/codegen/function_declaration.rs index b3536ac..f02d3b7 100644 --- a/src/codegen/function_declaration.rs +++ b/src/codegen/function_declaration.rs @@ -4,7 +4,11 @@ use super::Transpilable; impl Transpilable for FunctionDeclaration { fn transpile(&self) -> String { - format!("function {}() {{}}", self.identifier) + format!( + "function {}() {{\n{}\n}}", + self.identifier, + self.block.transpile() + ) } } diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index f66c4f0..0f543b7 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1,9 +1,11 @@ use crate::syntax::ast::ModuleAST; mod binding; +mod block; mod expression; mod function_declaration; mod module_ast; +mod statement; mod top_level_construct; /// Trait that the AST and its nodes implement to support transformation to PHP diff --git a/src/codegen/module_ast.rs b/src/codegen/module_ast.rs index b606b30..b9eab6f 100644 --- a/src/codegen/module_ast.rs +++ b/src/codegen/module_ast.rs @@ -11,7 +11,7 @@ impl Transpilable for ModuleAST { .map(|binding| binding.transpile()) .collect(); - bindings_str.join("\n") + format!(" String { + String::from("// TODO (statement)") + } +} diff --git a/src/lexic/mod.rs b/src/lexic/mod.rs index e7ec2a0..9a0639a 100755 --- a/src/lexic/mod.rs +++ b/src/lexic/mod.rs @@ -449,4 +449,12 @@ mod indentation_tests { assert_eq!(TokenType::DEDENT, tokens[8].token_type); assert_eq!(TokenType::EOF, tokens[9].token_type); } + + #[test] + fn should_lex_comments() { + let input = String::from("// ??"); + let tokens = get_tokens(&input).unwrap(); + + assert_eq!(TokenType::Comment, tokens[0].token_type); + } } diff --git a/src/syntax/ast/mod.rs b/src/syntax/ast/mod.rs index fbcf94b..1a309c9 100644 --- a/src/syntax/ast/mod.rs +++ b/src/syntax/ast/mod.rs @@ -18,6 +18,7 @@ pub enum TopLevelDeclaration { pub struct FunctionDeclaration { pub identifier: Box, pub params_list: Box, + pub block: Box, } #[derive(Debug)] diff --git a/src/syntax/functions/function_declaration.rs b/src/syntax/functions/function_declaration.rs index 88c42dd..0d368a9 100644 --- a/src/syntax/functions/function_declaration.rs +++ b/src/syntax/functions/function_declaration.rs @@ -64,7 +64,7 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> ParseResult (block, next_pos), ParseResult::Err(error) => { return ParseResult::Err(error); @@ -91,6 +91,7 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> ParseResult