restructure parsing and typing modules

* parsing backend submodules
* move typing to its own module
This commit is contained in:
Romain Paquet 2023-07-05 16:14:30 +02:00
parent 43df8c4b0a
commit 99434748fa
16 changed files with 1315 additions and 316 deletions

View file

@ -1,17 +1,29 @@
pub mod expr;
pub mod typ;
pub mod module;
use std::path::Path;
pub use crate::ast::expr::{BinaryOperator, Expr};
pub use crate::ast::typ::*;
use crate::ast::module::*;
use crate::typing::Type;
pub type Identifier = String;
// XXX: Is this enum actually useful? Is 3:30 AM btw
#[derive(Debug, PartialEq)]
pub enum Ast {
Module(Module),
}
#[derive(Debug, PartialEq)]
pub enum Definition {
FunctionDefinition(FunctionDefinition),
Expr(Expr),
Module(Vec<Ast>),
Block(Block),
Statement(Statement),
//StructDefinition(StructDefinition),
}
#[derive(Debug, PartialEq)]
pub struct Location {
pub file: Box<Path>,
}
#[derive(Debug, PartialEq)]
@ -20,6 +32,7 @@ pub struct FunctionDefinition {
pub parameters: Vec<Parameter>,
pub return_type: Option<Type>,
pub body: Box<Block>,
pub line_col: (usize, usize),
}
#[derive(Debug, PartialEq)]
@ -30,9 +43,13 @@ pub struct Block {
#[derive(Debug, PartialEq)]
pub enum Statement {
DeclareStatement(Identifier, Expr),
AssignStatement(Identifier, Expr),
ReturnStatement(Option<Expr>),
CallStatement(Call),
UseStatement(ModulePath),
IfStatement(Expr, Block),
WhileStatement(Box<Expr>, Box<Block>),
}
#[derive(Debug, PartialEq)]
@ -41,31 +58,9 @@ pub struct Call {
pub args: Vec<Expr>,
}
pub type Identifier = String;
#[derive(Debug, PartialEq)]
pub struct Parameter {
pub name: Identifier,
pub typ: Type,
}
impl Ast {
/// Type checks the AST and add missing return types.
pub fn check_return_types(&mut self) -> Result<(), TypeError> {
match self {
Ast::Module(defs) => {
for def in defs {
if let Ast::FunctionDefinition { .. } = def {
def.check_return_types()?;
}
}
}
Ast::FunctionDefinition(func) => {
let typ = func.typ(&mut TypeContext::default())?;
func.return_type = Some(typ.clone());
}
_ => unreachable!(),
}
Ok(())
}
}