Add files
This commit is contained in:
parent
774f1d65ca
commit
46d9d04c75
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -20,7 +20,7 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thp"
|
name = "thp"
|
||||||
version = "0.0.9"
|
version = "0.0.10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"colored",
|
"colored",
|
||||||
]
|
]
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "thp"
|
name = "thp"
|
||||||
version = "0.0.9"
|
version = "0.0.10"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
|
||||||
|
58
src/semantic/checks/binding.rs
Normal file
58
src/semantic/checks/binding.rs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
use crate::{
|
||||||
|
error_handling::{semantic_error::SemanticError, MistiError},
|
||||||
|
semantic::{impls::SemanticCheck, symbol_table::SymbolEntry},
|
||||||
|
syntax::ast::var_binding::Binding,
|
||||||
|
};
|
||||||
|
|
||||||
|
impl SemanticCheck for Binding<'_> {
|
||||||
|
fn check_semantics(
|
||||||
|
&self,
|
||||||
|
scope: &crate::semantic::symbol_table::SymbolTable,
|
||||||
|
) -> Result<(), crate::error_handling::MistiError> {
|
||||||
|
let binding_name = &self.identifier.value;
|
||||||
|
|
||||||
|
// TODO: Define if variables can be redeclared.
|
||||||
|
// If so, it is irrelevant to check if the variable is already defined
|
||||||
|
if scope.test(binding_name) {
|
||||||
|
let error = SemanticError {
|
||||||
|
error_start: self.identifier.position,
|
||||||
|
error_end: self.identifier.get_end_position(),
|
||||||
|
reason: format!(
|
||||||
|
"Duplicated: A symbol with name {} was already defined",
|
||||||
|
binding_name
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
return Err(MistiError::Semantic(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
todo!("");
|
||||||
|
/*
|
||||||
|
let expression_datatype = self.expression.get_type();
|
||||||
|
|
||||||
|
let datatype = match self.datatype {
|
||||||
|
Some(t) => t.value.clone(),
|
||||||
|
// If the datatype is not defined, we use the expression datatype
|
||||||
|
None => expression_datatype.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Both the declared & actual datatypes must be the same
|
||||||
|
if datatype != expression_datatype {
|
||||||
|
let error = SemanticError {
|
||||||
|
error_start: self.identifier.position,
|
||||||
|
error_end: self.identifier.get_end_position(),
|
||||||
|
reason: format!(
|
||||||
|
"The variable `{}` was declared as `{}` but its expression has type `{}`",
|
||||||
|
binding_name, datatype, expression_datatype
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
return Err(MistiError::Semantic(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
scope.insert(binding_name.clone(), SymbolEntry::new_variable(datatype));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
38
src/semantic/checks/function_declaration.rs
Normal file
38
src/semantic/checks/function_declaration.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
use crate::{
|
||||||
|
error_handling::{semantic_error::SemanticError, MistiError},
|
||||||
|
semantic::{impls::SemanticCheck, symbol_table::SymbolEntry},
|
||||||
|
syntax::ast::FunctionDeclaration,
|
||||||
|
};
|
||||||
|
|
||||||
|
impl SemanticCheck for FunctionDeclaration<'_> {
|
||||||
|
fn check_semantics(
|
||||||
|
&self,
|
||||||
|
scope: &crate::semantic::symbol_table::SymbolTable,
|
||||||
|
) -> Result<(), crate::error_handling::MistiError> {
|
||||||
|
let function_name = self.identifier.value.clone();
|
||||||
|
|
||||||
|
// Check that the function is not already defined
|
||||||
|
if scope.test(&function_name) {
|
||||||
|
let error = SemanticError {
|
||||||
|
error_start: self.identifier.position,
|
||||||
|
error_end: self.identifier.get_end_position(),
|
||||||
|
reason: format!(
|
||||||
|
"Duplicated: A symbol with name {} was already defined",
|
||||||
|
function_name
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
return Err(MistiError::Semantic(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Check the return type of the function
|
||||||
|
// TODO: Check the return type of the function body
|
||||||
|
|
||||||
|
scope.insert(
|
||||||
|
function_name,
|
||||||
|
SymbolEntry::new_function(vec![], "Unit".into()),
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
2
src/semantic/checks/mod.rs
Normal file
2
src/semantic/checks/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod binding;
|
||||||
|
pub mod function_declaration;
|
@ -2,6 +2,7 @@ use crate::{error_handling::MistiError, syntax::ast::ModuleAST};
|
|||||||
|
|
||||||
mod impls;
|
mod impls;
|
||||||
mod symbol_table;
|
mod symbol_table;
|
||||||
|
mod checks;
|
||||||
|
|
||||||
use impls::SemanticCheck;
|
use impls::SemanticCheck;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user