Skip to content

Commit

Permalink
clean up ty check stage
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Sep 9, 2024
1 parent 2118e01 commit eb4aef9
Show file tree
Hide file tree
Showing 40 changed files with 154 additions and 191 deletions.
4 changes: 2 additions & 2 deletions src/codegen/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use inkwell::{
builder::Builder,
context::Context,
module::Module as LlvmModule,
types::{BasicType, BasicTypeEnum},
types::{BasicType as _, BasicTypeEnum},
values::{BasicValue, FunctionValue, IntValue, PointerValue},
IntPredicate,
};
Expand All @@ -19,8 +19,8 @@ use crate::{
BasicBlockIdx, BinaryOp, ConstantValue, Function, Terminator, Triple, TripleRef,
UnaryOp, Value,
},
ty::Ty,
},
ty::Ty,
};

/// A single LLVM module.
Expand Down
8 changes: 3 additions & 5 deletions src/compiler/function_manager.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::ty::Ty;
use std::collections::HashMap;

use index_vec::IndexVec;

use crate::{
repr::{
identifier::{FunctionIdx, ScopedBinding},
ty::Ty,
},
stage::type_check::FunctionSignature,
repr::identifier::{FunctionIdx, ScopedBinding},
ty::FunctionSignature,
};

use super::Symbol;
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use self::function_manager::*;
use crate::{
hir::SolveType,
repr::ir,
stage::{self, parse::ParseError, type_check::TyError},
stage::{self, parse::ParseError},
ty::TyError,
};

/// A symbol represents an interned string.
Expand Down
4 changes: 1 addition & 3 deletions src/hir/expression/array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::{ParseError, Precedence};

use super::*;

ast_node! {
Expand Down Expand Up @@ -70,7 +68,7 @@ impl SolveType for Array<UntypedAstMetadata> {
self,
compiler: &mut crate::compiler::Compiler,
state: &mut Self::State,
) -> Result<Self::Typed, crate::stage::type_check::TyError> {
) -> Result<Self::Typed, crate::ty::TyError> {
// Type check each of the init items
let init = self
.init
Expand Down
4 changes: 1 addition & 3 deletions src/hir/expression/assign.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::{ParseError, Precedence};

use super::*;

ast_node! {
Expand Down Expand Up @@ -58,7 +56,7 @@ impl SolveType for Assign<UntypedAstMetadata> {
self,
compiler: &mut crate::compiler::Compiler,
state: &mut Self::State,
) -> Result<Self::Typed, crate::stage::type_check::TyError> {
) -> Result<Self::Typed, crate::ty::TyError> {
// Work out what type the variable has to be
let (binding, ty) = state
.resolve(self.binding)
Expand Down
4 changes: 1 addition & 3 deletions src/hir/expression/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::{ParseError, Precedence};

use super::*;

ast_node! {
Expand Down Expand Up @@ -66,7 +64,7 @@ impl SolveType for Block<UntypedAstMetadata> {
self,
compiler: &mut crate::compiler::Compiler,
state: &mut Self::State,
) -> Result<Self::Typed, crate::stage::type_check::TyError> {
) -> Result<Self::Typed, crate::ty::TyError> {
// Enter a new scope
let block_scope = state.enter();

Expand Down
4 changes: 1 addition & 3 deletions src/hir/expression/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::{Lexer, ParseError};

use super::*;

ast_node! {
Expand Down Expand Up @@ -52,7 +50,7 @@ impl SolveType for Boolean<UntypedAstMetadata> {
self,
_compiler: &mut crate::compiler::Compiler,
_state: &mut Self::State,
) -> Result<Self::Typed, crate::stage::type_check::TyError> {
) -> Result<Self::Typed, crate::ty::TyError> {
Ok(Boolean {
value: self.value,
span: self.span,
Expand Down
6 changes: 1 addition & 5 deletions src/hir/expression/call.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use std::iter;

use crate::stage::parse::{ParseError, Precedence};

use super::*;

ast_node! {
Expand Down Expand Up @@ -29,7 +25,7 @@ impl<M: AstMetadata> Parsable for Call<M> {
};

// Consume the args
let args = iter::from_fn(|| {
let args = std::iter::from_fn(|| {
match lexer.peek_token()? {
Token::RightParen => None,
Token::LeftParen | Token::Comma => {
Expand Down
13 changes: 7 additions & 6 deletions src/hir/expression/cast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stage::parse::{parse_ty, ParseError};
use crate::ty::TySpanned;

use super::*;

Expand All @@ -15,7 +15,7 @@ impl<M: AstMetadata> Parsable for Cast<M> {
fn register(parser: &mut Parser) {
assert!(parser.register_infix::<Expression<UntypedAstMetadata>>(
Token::As,
|_, _, lexer, left| {
|parser, compiler, lexer, left| {
let as_span = match lexer.next_spanned().unwrap() {
(Token::As, span) => span,
(token, _) => {
Expand All @@ -28,12 +28,12 @@ impl<M: AstMetadata> Parsable for Cast<M> {
};

// Parse out type from right hand side
let (target_ty, target_ty_span) = parse_ty(lexer)?;
let ty: TySpanned = parser.parse(compiler, lexer, Precedence::Lowest)?;

Ok(Expression::Cast(Cast {
value: Box::new(left),
target_ty,
span: as_span.start..target_ty_span.end,
target_ty: ty.ty,
span: as_span.start..ty.span.end,
ty_info: None,
}))
}
Expand All @@ -48,7 +48,7 @@ impl SolveType for Cast<UntypedAstMetadata> {
self,
compiler: &mut crate::compiler::Compiler,
state: &mut Self::State,
) -> Result<Self::Typed, crate::stage::type_check::TyError> {
) -> Result<Self::Typed, crate::ty::TyError> {
let value = self.value.solve(compiler, state)?;

// Make sure that the value can be cast to the desired type
Expand Down Expand Up @@ -87,6 +87,7 @@ mod test {

// Helper parsers
Integer::<UntypedAstMetadata>::register(&mut parser);
TySpanned::register(&mut parser);

parser
}
Expand Down
4 changes: 1 addition & 3 deletions src/hir/expression/ident.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::ParseError;

use super::*;

use std::hash::Hash;
Expand Down Expand Up @@ -47,7 +45,7 @@ impl SolveType for Ident<UntypedAstMetadata> {
self,
_compiler: &mut crate::compiler::Compiler,
state: &mut Self::State,
) -> Result<Self::Typed, crate::stage::type_check::TyError> {
) -> Result<Self::Typed, crate::ty::TyError> {
let (binding, ty) = state
.resolve(self.binding)
.ok_or(TyError::SymbolNotFound(self.binding))?;
Expand Down
4 changes: 1 addition & 3 deletions src/hir/expression/if_else.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::{ParseError, Precedence};

use super::*;

ast_node! {
Expand Down Expand Up @@ -75,7 +73,7 @@ impl SolveType for If<UntypedAstMetadata> {
self,
compiler: &mut crate::compiler::Compiler,
state: &mut Self::State,
) -> Result<Self::Typed, crate::stage::type_check::TyError> {
) -> Result<Self::Typed, crate::ty::TyError> {
// Make sure the condition is correctly typed
let condition = self.condition.solve(compiler, state)?;
let condition_ty = condition.get_ty_info();
Expand Down
4 changes: 1 addition & 3 deletions src/hir/expression/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::{ParseError, Precedence};

use super::*;

ast_node! {
Expand Down Expand Up @@ -71,7 +69,7 @@ impl SolveType for Index<UntypedAstMetadata> {
self,
compiler: &mut crate::compiler::Compiler,
state: &mut Self::State,
) -> Result<Self::Typed, crate::stage::type_check::TyError> {
) -> Result<Self::Typed, crate::ty::TyError> {
// Ensure the inner parts are correct
let index = self.index.solve(compiler, state)?;

Expand Down
2 changes: 0 additions & 2 deletions src/hir/expression/infix.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::{Lexer, ParseError, Precedence};

use super::*;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand Down
2 changes: 0 additions & 2 deletions src/hir/expression/integer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::ParseError;

use super::*;

ast_node! {
Expand Down
4 changes: 1 addition & 3 deletions src/hir/expression/loop_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::{ParseError, Precedence};

use super::*;

ast_node! {
Expand Down Expand Up @@ -53,7 +51,7 @@ impl SolveType for Loop<UntypedAstMetadata> {
self,
compiler: &mut crate::compiler::Compiler,
state: &mut Self::State,
) -> Result<Self::Typed, crate::stage::type_check::TyError> {
) -> Result<Self::Typed, TyError> {
// Type check the body
let body = self.body.solve(compiler, state)?;

Expand Down
2 changes: 1 addition & 1 deletion src/hir/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl SolveType for Expression<UntypedAstMetadata> {
self,
compiler: &mut crate::compiler::Compiler,
state: &mut Self::State,
) -> Result<Self::Typed, crate::stage::type_check::TyError> {
) -> Result<Self::Typed, crate::ty::TyError> {
Ok(match self {
Expression::Infix(e) => Expression::Infix(e.solve(compiler, state)?),
Expression::Integer(e) => Expression::Integer(e.solve(compiler, state)?),
Expand Down
9 changes: 1 addition & 8 deletions src/hir/function.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
use crate::{
ast_node,
repr::{ast::untyped::UntypedAstMetadata, ty::Ty},
stage::type_check::TyError,
util::scope::Scope,
};

use super::{expression::Block, SolveType};
use super::*;

ast_node! {
Function<M> {
Expand Down
6 changes: 3 additions & 3 deletions src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use crate::{
ast_node,
compiler::Compiler,
repr::{
ast::{typed::TyInfo, untyped::UntypedAstMetadata, AstMetadata},
ast::{untyped::UntypedAstMetadata, AstMetadata},
token::Token,
ty::Ty,
},
stage::{parse::parser::Parser, type_check::TyError},
stage::parse::{parser::Parser, Lexer, ParseError, Precedence},
ty::{Ty, TyError, TyInfo},
util::{scope::Scope, span::Span},
};

Expand Down
8 changes: 2 additions & 6 deletions src/hir/program.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use crate::{
ast_node,
repr::{ast::untyped::UntypedAstMetadata, ty::Ty},
stage::type_check::{FunctionSignature, TyError},
};
use crate::ty::FunctionSignature;

use super::{function::Function, SolveType};
use super::*;

ast_node! {
Program<M> {
Expand Down
2 changes: 0 additions & 2 deletions src/hir/statement/s_break.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::ParseError;

use super::*;

ast_node! {
Expand Down
2 changes: 0 additions & 2 deletions src/hir/statement/s_continue.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::ParseError;

use super::*;

ast_node! {
Expand Down
2 changes: 0 additions & 2 deletions src/hir/statement/s_expression.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::Precedence;

use super::*;

ast_node! {
Expand Down
2 changes: 0 additions & 2 deletions src/hir/statement/s_let.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::{ParseError, Precedence};

use super::*;

ast_node! {
Expand Down
2 changes: 0 additions & 2 deletions src/hir/statement/s_return.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::stage::parse::{ParseError, Precedence};

use super::*;

ast_node! {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod compiler;
mod hir;
pub mod repr;
pub mod stage;
mod ty;
pub mod util;

pub fn compile_and_run(source: &'static str, debug: bool) -> i64 {
Expand Down
12 changes: 2 additions & 10 deletions src/repr/ast/typed.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
use crate::{
generate_ast,
repr::{
identifier::{FunctionIdx, ScopedBinding},
ty::Ty,
},
repr::identifier::{FunctionIdx, ScopedBinding},
ty::TyInfo,
util::span::Span,
};

use super::AstMetadata;

#[derive(Clone, Debug)]
pub struct TyInfo {
pub ty: Ty,
pub return_ty: Option<Ty>,
}

pub struct TypedAstMetadata;
impl AstMetadata for TypedAstMetadata {
type FnIdentifier = FunctionIdx;
Expand Down
2 changes: 1 addition & 1 deletion src/repr/ast/untyped.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{compiler::Symbol, generate_ast, repr::ty::Ty, util::span::Span};
use crate::{compiler::Symbol, generate_ast, ty::Ty, util::span::Span};

use super::AstMetadata;

Expand Down
2 changes: 1 addition & 1 deletion src/repr/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use index_vec::IndexVec;

use crate::{
repr::identifier::{FunctionIdx, ScopedBinding},
stage::type_check::FunctionSignature,
ty::FunctionSignature,
};

use super::BasicBlock;
Expand Down
1 change: 0 additions & 1 deletion src/repr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ pub mod ast;
pub mod identifier;
pub mod ir;
pub mod token;
pub mod ty;
19 changes: 0 additions & 19 deletions src/repr/ty.rs

This file was deleted.

Loading

0 comments on commit eb4aef9

Please sign in to comment.