chore: create helpers for expression testing in semantic analysis

This commit is contained in:
Fernando Araoz 2024-10-02 16:25:42 -05:00
parent 71095deaa0
commit 4760eea8f4
3 changed files with 22 additions and 7 deletions

View File

@ -350,19 +350,30 @@ impl SemanticCheck for Expression<'_> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{
lexic::token::Token, lexic::{self, get_tokens, token::Token},
semantic::{impls::SemanticCheck, std::populate, symbol_table::SymbolTable}, semantic::{impls::SemanticCheck, std::populate, symbol_table::SymbolTable},
syntax::ast::{ syntax::{
functions::{ArgumentsList, FunctionCall}, ast::{
Expression, functions::{ArgumentsList, FunctionCall},
Expression,
},
parseable::Parseable,
}, },
}; };
fn t(i: &str) -> Vec<Token> {
get_tokens(&i.into()).unwrap()
}
fn exp<'a>(t: &'a Vec<Token>) -> Expression<'a> {
Expression::try_parse(t, 0).unwrap().0
}
#[test] #[test]
fn should_error_on_undefined_symbol() { fn should_error_on_undefined_symbol() {
// source code: `print()` // source code: `print()`
let expr_token = Token::new_identifier("print".into(), 0); let b = t("print()");
let expr_function = Expression::Identifier(&expr_token); let expr_function = exp(&b);
let arguments = ArgumentsList { let arguments = ArgumentsList {
arguments: vec![], arguments: vec![],
paren_open_pos: 5, paren_open_pos: 5,

View File

@ -4,6 +4,10 @@ use super::symbol_table::SymbolTable;
/// Allows this type to have it's semantics checked. /// Allows this type to have it's semantics checked.
pub trait SemanticCheck { pub trait SemanticCheck {
/// Checks the semantics of this AST node and performs typechecking
///
/// Types are provided by the Typed trait, because not every AST node
/// will have a defined type
fn check_semantics(&self, scope: &SymbolTable) -> Result<(), MistiError>; fn check_semantics(&self, scope: &SymbolTable) -> Result<(), MistiError>;
} }

View File

@ -1,7 +1,7 @@
use crate::error_handling::MistiError; use crate::error_handling::MistiError;
mod functions; mod functions;
mod parseable; pub mod parseable;
mod parsers; mod parsers;
mod utils; mod utils;