refactor typing and compilation

This commit is contained in:
Romain Paquet 2024-06-25 11:02:05 +02:00
parent e8deab19cc
commit ba838292f6
8 changed files with 175 additions and 131 deletions

View file

@ -1,4 +1,5 @@
use crate::ast::*;
use crate::typing::Type;
#[derive(Debug, PartialEq)]
pub enum Expr {
@ -32,3 +33,39 @@ pub enum Expr {
FloatLiteral(f64),
StringLiteral(String),
}
impl Block {
#[inline]
pub fn ty(&self) -> Type {
// XXX: Cloning may be expensive -> TypeId?
self.typ.clone()
}
}
impl Expr {
pub fn ty(&self) -> Type {
match self {
Expr::BinaryExpression {
lhs: _,
op: _,
rhs: _,
typ,
} => typ.clone(),
Expr::UnaryExpression { op: _, inner } => inner.ty(), // XXX: problems will arise here
Expr::Identifier { name: _, typ } => typ.clone(),
Expr::Call(call) => call.typ.clone(),
Expr::Block(block) => block.typ.clone(),
Expr::IfExpr {
cond: _,
then_body: _,
else_body: _,
typ,
} => typ.clone(),
Expr::UnitLiteral => Type::Unit,
Expr::BooleanLiteral(_) => Type::Bool,
Expr::IntegerLiteral(_) => Type::Int,
Expr::FloatLiteral(_) => Type::Float,
Expr::StringLiteral(_) => Type::Str,
}
}
}