Skip to content

Commit

Permalink
refactor(ast): store spans for expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkeeyCat committed Jan 11, 2025
1 parent b6d384b commit 718d544
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 248 deletions.
8 changes: 7 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ pub enum Stmt {
}

#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
pub struct Expr {
pub kind: ExprKind,
pub span: Span,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ExprKind {
Binary {
op: BinOp,
left: Box<Expr>,
Expand Down
5 changes: 5 additions & 0 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ pub mod span {
}

impl Span {
pub const DUMMY: Self = Self {
start: usize::MAX,
end: usize::MAX,
};

pub fn to(self, end: Span) -> Span {
Span {
start: std::cmp::min(self.start, end.start),
Expand Down
24 changes: 12 additions & 12 deletions src/lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ impl<'a, 'ir> Lowering<'a, 'ir> {
}

fn lower_expr(&mut self, expr: ast::Expr) -> ir::Expr<'ir> {
match expr {
ast::Expr::Binary {
match expr.kind {
ast::ExprKind::Binary {
op,
ref left,
ref right,
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<'a, 'ir> Lowering<'a, 'ir> {
),
}
}
ast::Expr::Ident(ref ident) => {
ast::ExprKind::Ident(ref ident) => {
let id = self.scopes.get_symbol(ident).unwrap();
let ty = self.expr_ty(&expr);

Expand All @@ -257,7 +257,7 @@ impl<'a, 'ir> Lowering<'a, 'ir> {
kind: ir::ExprKind::Ident(id),
}
}
ast::Expr::Lit(ref lit) => {
ast::ExprKind::Lit(ref lit) => {
let ty = self.expr_ty(&expr);
let kind = match lit {
ast::ExprLit::Int(lit) => ir::ExprKind::Lit(ir::ExprLit::Int(*lit)),
Expand All @@ -271,7 +271,7 @@ impl<'a, 'ir> Lowering<'a, 'ir> {

ir::Expr { ty, kind }
}
ast::Expr::Unary { op, expr } => {
ast::ExprKind::Unary { op, expr } => {
let ir_expr = self.lower_expr(*expr);
let ty = match op {
UnOp::Address => self.ctx.allocator.alloc(ir::Ty::Ptr(ir_expr.ty)),
Expand All @@ -298,7 +298,7 @@ impl<'a, 'ir> Lowering<'a, 'ir> {
kind: ir::ExprKind::Unary(op, self.ctx.allocator.alloc(ir_expr)),
}
}
ast::Expr::Struct { name, fields } => {
ast::ExprKind::Struct { name, fields } => {
let ty = self.lower_ty(ast::Ty::Ident(name));
let ir::Ty::Struct(id) = ty else {
unreachable!();
Expand Down Expand Up @@ -340,7 +340,7 @@ impl<'a, 'ir> Lowering<'a, 'ir> {
kind: ir::ExprKind::Struct(fields),
}
}
ast::Expr::Field { expr, field } => {
ast::ExprKind::Field { expr, field } => {
let expr = self.lower_expr(*expr);
let ty = self.lower_ty(ast::Ty::Infer);
let field = self.ctx.allocator.alloc_str(field.as_str());
Expand All @@ -354,7 +354,7 @@ impl<'a, 'ir> Lowering<'a, 'ir> {
kind: ir::ExprKind::Field(self.ctx.allocator.alloc(expr), field),
}
}
ast::Expr::Cast { expr, ty } => {
ast::ExprKind::Cast { expr, ty } => {
let expr = self.lower_expr(*expr);
let ty = self.lower_ty(ty);

Expand Down Expand Up @@ -431,20 +431,20 @@ impl<'a, 'ir> Lowering<'a, 'ir> {
}

fn expr_ty(&mut self, expr: &ast::Expr) -> &'ir ir::Ty<'ir> {
match expr {
ast::Expr::Binary { .. } => self
match &expr.kind {
ast::ExprKind::Binary { .. } => self
.ctx
.allocator
.alloc(ir::Ty::Infer(self.ctx.ty_problem.new_infer_ty_var())),
ast::Expr::Lit(lit) => match lit {
ast::ExprKind::Lit(lit) => match lit {
ast::ExprLit::Bool(_) => &ir::Ty::Bool,
ast::ExprLit::String(_) => &ir::Ty::Ptr(&ir::Ty::UInt(UintTy::U8)),
_ => self
.ctx
.allocator
.alloc(ir::Ty::Infer(self.ctx.ty_problem.new_infer_ty_var())),
},
ast::Expr::Ident(ident) => {
ast::ExprKind::Ident(ident) => {
let id = self.scopes.get_symbol(ident).unwrap();

match self.nodes_map.get(&id).unwrap() {
Expand Down
Loading

0 comments on commit 718d544

Please sign in to comment.