initial commit
This commit is contained in:
commit
43df8c4b0a
9 changed files with 596 additions and 0 deletions
71
src/ast/mod.rs
Normal file
71
src/ast/mod.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
pub mod expr;
|
||||
pub mod typ;
|
||||
|
||||
pub use crate::ast::expr::{BinaryOperator, Expr};
|
||||
pub use crate::ast::typ::*;
|
||||
|
||||
// XXX: Is this enum actually useful? Is 3:30 AM btw
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Ast {
|
||||
FunctionDefinition(FunctionDefinition),
|
||||
Expr(Expr),
|
||||
Module(Vec<Ast>),
|
||||
Block(Block),
|
||||
Statement(Statement),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct FunctionDefinition {
|
||||
pub name: Identifier,
|
||||
pub parameters: Vec<Parameter>,
|
||||
pub return_type: Option<Type>,
|
||||
pub body: Box<Block>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Block {
|
||||
pub statements: Vec<Statement>,
|
||||
pub value: Option<Expr>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Statement {
|
||||
AssignStatement(Identifier, Expr),
|
||||
ReturnStatement(Option<Expr>),
|
||||
CallStatement(Call),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Call {
|
||||
pub callee: Expr,
|
||||
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(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue