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;
impl Transpilable for FunctionDeclaration {
impl Transpilable for FunctionDeclaration<'_> {
fn transpile(&self) -> String {
format!(
"function {}() {{\n{}\n}}",
self.identifier,
self.identifier.value,
self.block.transpile()
)
}

View File

@ -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 {

View File

@ -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(),

View File

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

View File

@ -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<TopLevelDeclaration>,
pub struct ModuleAST<'a> {
pub declarations: Vec<TopLevelDeclaration<'a>>,
}
#[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<String>,
pub struct FunctionDeclaration<'a> {
pub identifier: &'a Token,
pub params_list: Box<ParamsList>,
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
ParseResult::Ok(
FunctionDeclaration {
identifier: Box::new(identifier.value.clone()),
identifier: &identifier,
params_list: Box::new(params_list),
block: Box::new(block),
},
@ -311,10 +311,7 @@ mod tests {
panic!("Expected a function declaration.")
};
assert_eq!(
function_declaration.identifier,
Box::new(String::from("id"))
);
assert_eq!(function_declaration.identifier.value, String::from("id"));
}
}
@ -331,7 +328,7 @@ mod whitespace_test {
panic!("Expected a function declaration.")
};
assert_eq!(declaration.identifier, Box::new(String::from("id")));
assert_eq!(declaration.identifier.value, (String::from("id")));
}
#[test]
@ -341,7 +338,7 @@ mod whitespace_test {
panic!("Expected a function declaration.")
};
assert_eq!(declaration.identifier, Box::new(String::from("id")));
assert_eq!(declaration.identifier.value, (String::from("id")));
}
#[test]
@ -351,7 +348,7 @@ mod whitespace_test {
panic!("Expected a function declaration.")
};
assert_eq!(declaration.identifier, Box::new(String::from("id")));
assert_eq!(declaration.identifier.value, (String::from("id")));
}
#[test]
@ -360,7 +357,7 @@ mod whitespace_test {
let ParseResult::Ok(declaration, _) = try_parse(&tokens, 0) else {
panic!("Expected a function declaration.")
};
assert_eq!(declaration.identifier, Box::new(String::from("id")));
assert_eq!(declaration.identifier.value, (String::from("id")));
}
#[test]
@ -369,6 +366,6 @@ mod whitespace_test {
let ParseResult::Ok(declaration, _) = try_parse(&tokens, 0) else {
panic!("Expected a function declaration.")
};
assert_eq!(declaration.identifier, Box::new(String::from("id")));
assert_eq!(declaration.identifier.value, (String::from("id")));
}
}