diff --git a/src/codegen/function_declaration.rs b/src/codegen/function_declaration.rs index d0c4359..ca5824a 100644 --- a/src/codegen/function_declaration.rs +++ b/src/codegen/function_declaration.rs @@ -2,11 +2,11 @@ use crate::syntax::ast::FunctionDeclaration; use super::Transpilable; -impl Transpilable for FunctionDeclaration { +impl Transpilable for FunctionDeclaration<'_> { fn transpile(&self) -> String { format!( "function {}() {{\n{}\n}}", - self.identifier, + self.identifier.value, self.block.transpile() ) } diff --git a/src/codegen/module_ast.rs b/src/codegen/module_ast.rs index d4c4067..0c56af6 100644 --- a/src/codegen/module_ast.rs +++ b/src/codegen/module_ast.rs @@ -1,7 +1,7 @@ use super::Transpilable; use crate::syntax::ast::ModuleAST; -impl Transpilable for ModuleAST { +impl Transpilable for ModuleAST<'_> { /// Transpiles the whole AST into PHP, using this same trait on the /// nodes and leaves of the AST fn transpile(&self) -> String { diff --git a/src/codegen/top_level_construct.rs b/src/codegen/top_level_construct.rs index 9c3f412..2e9fc9e 100644 --- a/src/codegen/top_level_construct.rs +++ b/src/codegen/top_level_construct.rs @@ -2,7 +2,7 @@ use crate::syntax::ast::TopLevelDeclaration; use super::Transpilable; -impl Transpilable for TopLevelDeclaration { +impl Transpilable for TopLevelDeclaration<'_> { fn transpile(&self) -> String { match self { TopLevelDeclaration::Binding(binding) => binding.transpile(), diff --git a/src/semantic/impls.rs b/src/semantic/impls.rs index 17735f7..d8600f9 100644 --- a/src/semantic/impls.rs +++ b/src/semantic/impls.rs @@ -10,7 +10,7 @@ pub trait SemanticCheck { fn check_semantics(&self, scope: &SymbolTable) -> Result<(), MistiError>; } -impl SemanticCheck for ModuleAST { +impl SemanticCheck for ModuleAST<'_> { /// Checks that this AST is semantically correct, given a symbol table fn check_semantics(&self, scope: &SymbolTable) -> Result<(), MistiError> { for declaration in &self.declarations { @@ -21,7 +21,7 @@ impl SemanticCheck for ModuleAST { } } -impl SemanticCheck for TopLevelDeclaration { +impl SemanticCheck for TopLevelDeclaration<'_> { fn check_semantics(&self, scope: &SymbolTable) -> Result<(), MistiError> { match self { TopLevelDeclaration::Binding(_) => { @@ -34,15 +34,13 @@ impl SemanticCheck for TopLevelDeclaration { Err(MistiError::Semantic(error)) } TopLevelDeclaration::FunctionDeclaration(function) => { - let function_name = function.identifier.as_ref().clone(); + let function_name = function.identifier.value.clone(); if scope.test(&function_name) { let error = SemanticError { - // TODO: Get the position of the function name. For this, these structs - // should store the token instead of just the string - error_start: 0, - error_end: 0, - reason: format!("Function {} already defined", function_name), + error_start: function.identifier.position, + error_end: function.identifier.get_end_position(), + reason: format!("Duplicated function: A function with name {} was already defined", function_name), }; return Err(MistiError::Semantic(error)); diff --git a/src/syntax/ast/mod.rs b/src/syntax/ast/mod.rs index cbd6da1..5b2cdd8 100644 --- a/src/syntax/ast/mod.rs +++ b/src/syntax/ast/mod.rs @@ -1,22 +1,24 @@ +use crate::lexic::token::Token; + use self::functions::FunctionCall; pub mod functions; pub mod statement; pub mod var_binding; -pub struct ModuleAST { - pub declarations: Vec, +pub struct ModuleAST<'a> { + pub declarations: Vec>, } #[derive(Debug)] -pub enum TopLevelDeclaration { +pub enum TopLevelDeclaration<'a> { Binding(var_binding::Binding), - FunctionDeclaration(FunctionDeclaration), + FunctionDeclaration(FunctionDeclaration<'a>), } #[derive(Debug)] -pub struct FunctionDeclaration { - pub identifier: Box, +pub struct FunctionDeclaration<'a> { + pub identifier: &'a Token, pub params_list: Box, pub block: Box, } diff --git a/src/syntax/functions/function_declaration.rs b/src/syntax/functions/function_declaration.rs index 128167a..5a60481 100644 --- a/src/syntax/functions/function_declaration.rs +++ b/src/syntax/functions/function_declaration.rs @@ -91,7 +91,7 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> ParseResult