Use ref to a Token instead of a string value

This commit is contained in:
Araozu 2024-03-09 12:21:52 -05:00
parent f97b8e2e07
commit e074e2cd74
6 changed files with 25 additions and 28 deletions

View File

@ -2,11 +2,11 @@ use crate::syntax::ast::FunctionDeclaration;
use super::Transpilable; use super::Transpilable;
impl Transpilable for FunctionDeclaration { impl Transpilable for FunctionDeclaration<'_> {
fn transpile(&self) -> String { fn transpile(&self) -> String {
format!( format!(
"function {}() {{\n{}\n}}", "function {}() {{\n{}\n}}",
self.identifier, self.identifier.value,
self.block.transpile() self.block.transpile()
) )
} }

View File

@ -1,7 +1,7 @@
use super::Transpilable; use super::Transpilable;
use crate::syntax::ast::ModuleAST; 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 /// Transpiles the whole AST into PHP, using this same trait on the
/// nodes and leaves of the AST /// nodes and leaves of the AST
fn transpile(&self) -> String { fn transpile(&self) -> String {

View File

@ -2,7 +2,7 @@ use crate::syntax::ast::TopLevelDeclaration;
use super::Transpilable; use super::Transpilable;
impl Transpilable for TopLevelDeclaration { impl Transpilable for TopLevelDeclaration<'_> {
fn transpile(&self) -> String { fn transpile(&self) -> String {
match self { match self {
TopLevelDeclaration::Binding(binding) => binding.transpile(), TopLevelDeclaration::Binding(binding) => binding.transpile(),

View File

@ -10,7 +10,7 @@ pub trait SemanticCheck {
fn check_semantics(&self, scope: &SymbolTable) -> Result<(), MistiError>; 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 /// Checks that this AST is semantically correct, given a symbol table
fn check_semantics(&self, scope: &SymbolTable) -> Result<(), MistiError> { fn check_semantics(&self, scope: &SymbolTable) -> Result<(), MistiError> {
for declaration in &self.declarations { 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> { fn check_semantics(&self, scope: &SymbolTable) -> Result<(), MistiError> {
match self { match self {
TopLevelDeclaration::Binding(_) => { TopLevelDeclaration::Binding(_) => {
@ -34,15 +34,13 @@ impl SemanticCheck for TopLevelDeclaration {
Err(MistiError::Semantic(error)) Err(MistiError::Semantic(error))
} }
TopLevelDeclaration::FunctionDeclaration(function) => { TopLevelDeclaration::FunctionDeclaration(function) => {
let function_name = function.identifier.as_ref().clone(); let function_name = function.identifier.value.clone();
if scope.test(&function_name) { if scope.test(&function_name) {
let error = SemanticError { let error = SemanticError {
// TODO: Get the position of the function name. For this, these structs error_start: function.identifier.position,
// should store the token instead of just the string error_end: function.identifier.get_end_position(),
error_start: 0, reason: format!("Duplicated function: A function with name {} was already defined", function_name),
error_end: 0,
reason: format!("Function {} already defined", function_name),
}; };
return Err(MistiError::Semantic(error)); return Err(MistiError::Semantic(error));

View File

@ -1,22 +1,24 @@
use crate::lexic::token::Token;
use self::functions::FunctionCall; use self::functions::FunctionCall;
pub mod functions; pub mod functions;
pub mod statement; pub mod statement;
pub mod var_binding; pub mod var_binding;
pub struct ModuleAST { pub struct ModuleAST<'a> {
pub declarations: Vec<TopLevelDeclaration>, pub declarations: Vec<TopLevelDeclaration<'a>>,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum TopLevelDeclaration { pub enum TopLevelDeclaration<'a> {
Binding(var_binding::Binding), Binding(var_binding::Binding),
FunctionDeclaration(FunctionDeclaration), FunctionDeclaration(FunctionDeclaration<'a>),
} }
#[derive(Debug)] #[derive(Debug)]
pub struct FunctionDeclaration { pub struct FunctionDeclaration<'a> {
pub identifier: Box<String>, pub identifier: &'a Token,
pub params_list: Box<ParamsList>, pub params_list: Box<ParamsList>,
pub block: Box<Block>, pub block: Box<Block>,
} }

View File

@ -91,7 +91,7 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<Function
// Construct and return the function declaration // Construct and return the function declaration
ParseResult::Ok( ParseResult::Ok(
FunctionDeclaration { FunctionDeclaration {
identifier: Box::new(identifier.value.clone()), identifier: &identifier,
params_list: Box::new(params_list), params_list: Box::new(params_list),
block: Box::new(block), block: Box::new(block),
}, },
@ -311,10 +311,7 @@ mod tests {
panic!("Expected a function declaration.") panic!("Expected a function declaration.")
}; };
assert_eq!( assert_eq!(function_declaration.identifier.value, String::from("id"));
function_declaration.identifier,
Box::new(String::from("id"))
);
} }
} }
@ -331,7 +328,7 @@ mod whitespace_test {
panic!("Expected a function declaration.") panic!("Expected a function declaration.")
}; };
assert_eq!(declaration.identifier, Box::new(String::from("id"))); assert_eq!(declaration.identifier.value, (String::from("id")));
} }
#[test] #[test]
@ -341,7 +338,7 @@ mod whitespace_test {
panic!("Expected a function declaration.") panic!("Expected a function declaration.")
}; };
assert_eq!(declaration.identifier, Box::new(String::from("id"))); assert_eq!(declaration.identifier.value, (String::from("id")));
} }
#[test] #[test]
@ -351,7 +348,7 @@ mod whitespace_test {
panic!("Expected a function declaration.") panic!("Expected a function declaration.")
}; };
assert_eq!(declaration.identifier, Box::new(String::from("id"))); assert_eq!(declaration.identifier.value, (String::from("id")));
} }
#[test] #[test]
@ -360,7 +357,7 @@ mod whitespace_test {
let ParseResult::Ok(declaration, _) = try_parse(&tokens, 0) else { let ParseResult::Ok(declaration, _) = try_parse(&tokens, 0) else {
panic!("Expected a function declaration.") panic!("Expected a function declaration.")
}; };
assert_eq!(declaration.identifier, Box::new(String::from("id"))); assert_eq!(declaration.identifier.value, (String::from("id")));
} }
#[test] #[test]
@ -369,6 +366,6 @@ mod whitespace_test {
let ParseResult::Ok(declaration, _) = try_parse(&tokens, 0) else { let ParseResult::Ok(declaration, _) = try_parse(&tokens, 0) else {
panic!("Expected a function declaration.") panic!("Expected a function declaration.")
}; };
assert_eq!(declaration.identifier, Box::new(String::from("id"))); assert_eq!(declaration.identifier.value, (String::from("id")));
} }
} }