Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Simplify order of declarations and definitions #170

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 26 additions & 42 deletions src/semantics/to_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,36 +847,40 @@ impl ToHIR for ast::Module {
/// 1. Use statements
/// 2. Declare Types & Traits
/// 3. Define Types
/// 4. Declare Functions & Global variables
/// 5. Define Traits
/// 6. Define Functions & Global
/// 7. Rest of statements
/// 4. Declare Functions
/// 5. Declare Global variables, Define Traits & Functions & Global & Rest of statements
fn to_hir(&self, context: &mut impl Context) -> Result<Self::HIR, Self::Error> {
use ast::Declaration as D;
use ast::Statement as S;

let mut errors = Vec::new();

macro_rules! to_ir {
() => {
|stmt: &S| {
let res = stmt.to_hir(context);
match res {
Ok(mut stmt) => {
stmt.monomorphize(context);
context.module_mut().statements.push(stmt)
}
Err(err) => errors.push(err),
}
}
};
}

// Import things first
self.statements
.iter()
.filter(|s| matches!(s, ast::Statement::Use(_)))
.for_each(|stmt: &S| {
let res = stmt.to_hir(context);
match res {
Ok(mut stmt) => {
stmt.monomorphize(context);
context.module_mut().statements.push(stmt)
}
Err(err) => errors.push(err),
}
});
.for_each(to_ir!());

let mut decls = HashMap::new();

macro_rules! declare {
() => {
|(i, stmt)| {
|(i, stmt): (usize, &S)| {
let decl: &D = match stmt {
S::Declaration(d) => d,
_ => return,
Expand All @@ -897,7 +901,7 @@ impl ToHIR for ast::Module {

macro_rules! define {
() => {
|(i, stmt)| {
|(i, stmt): (usize, &S)| {
let decl: &D = match stmt {
S::Declaration(d) => d,
_ => return,
Expand Down Expand Up @@ -933,41 +937,21 @@ impl ToHIR for ast::Module {
.filter(|(_, s)| matches!(s, S::Declaration(D::Type(_))))
.for_each(define!());

// Declare Functions & Global variables
// Declare Functions
self.statements
.iter()
.enumerate()
.filter(|(_, s)| matches!(s, S::Declaration(D::Function(_) | D::Variable(_))))
.filter(|(_, s)| matches!(s, S::Declaration(D::Function(_))))
.for_each(declare!());

// Define Traits
self.statements
.iter()
.enumerate()
.filter(|(_, s)| matches!(s, S::Declaration(D::Trait(_))))
.for_each(define!());

// Define Functions & Global variables
self.statements
.iter()
.enumerate()
.filter(|(_, s)| matches!(s, S::Declaration(D::Function(_) | D::Variable(_))))
.for_each(define!());

// Add rest of statements
self.statements
.iter()
.enumerate()
.filter(|(_, s)| !matches!(s, S::Use(_) | S::Declaration(_)))
.for_each(|(_, stmt)| {
let res = stmt.to_hir(context);
match res {
Ok(mut stmt) => {
stmt.monomorphize(context);
context.module_mut().statements.push(stmt)
}
Err(err) => errors.push(err),
}
.filter(|(_, s)| !matches!(s, S::Use(_) | S::Declaration(D::Type(_))))
.for_each(|(i, stmt)| match stmt {
S::Declaration(D::Trait(_) | D::Function(_)) => define!()((i, stmt)),
_ => to_ir!()(stmt),
});

if !errors.is_empty() {
Expand Down
10 changes: 10 additions & 0 deletions src/tests/cant_use_global_before_decl/src/main.ppl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn before decl -> Integer:
return global

let global = 0

fn after decl -> Integer:
return global

before decl
after decl
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ e2es! {
address_of,
array,
candidate_not_viable,
cant_use_global_before_decl,
clone,
common_functions,
constraints,
Expand Down
14 changes: 7 additions & 7 deletions src/tests/snapshots/ppl__tests__address_of.hir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@ source: src/tests/mod.rs
expression: hir
---
let x: Integer = 0
`println <:Integer>`(`clone <:Reference<Integer>>`((x:Integer)))
let address: MemoryAddress = `address of <:Reference<Integer>>`((&x:Reference<Integer>))
let x_mut_ref: ReferenceMut<Integer> = `<:Type<Integer>> at <:Reference<MemoryAddress>>`((Type<Integer>:Type<Integer>), (&address:Reference<MemoryAddress>))
`println <:Integer>`(`clone <:Reference<Integer>>`((x:Integer)))
(x_mut_ref:ReferenceMut<Integer>) = 1
`println <:Integer>`(`clone <:Reference<Integer>>`((x:Integer)))
`destroy <:ReferenceMut<Integer>>`((x:Integer))

==MONOMORPHIZED==


@mangle_as("address_of")
fn<Integer> address of <ref: Reference<Integer>> -> MemoryAddress

@mangle_as("read_memory")
fn<Integer> <ty: Type<Integer>> at <address: Reference<MemoryAddress>> -> ReferenceMut<Integer>

@mangle_as("integer_as_string")
fn String from <$arg0: Integer> -> String

Expand All @@ -28,6 +22,12 @@ fn println <x: Integer> -> None:



@mangle_as("address_of")
fn<Integer> address of <ref: Reference<Integer>> -> MemoryAddress

@mangle_as("read_memory")
fn<Integer> <ty: Type<Integer>> at <address: Reference<MemoryAddress>> -> ReferenceMut<Integer>

@mangle_as("integer_as_string")
fn String from <$arg0: Integer> -> String

Expand Down
70 changes: 35 additions & 35 deletions src/tests/snapshots/ppl__tests__address_of.ir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ define void @main.execute() !dbg !12 {
call void @initialize(), !dbg !13
call void @initialize.1(), !dbg !14
call void @initialize.2(), !dbg !15
call void @initialize.3(), !dbg !16
%1 = call %Integer @clone_integer(ptr @x), !dbg !16
call void @"println <:Integer>"(%Integer %1), !dbg !16
call void @initialize.3(), !dbg !17
call void @initialize.4(), !dbg !14
%1 = call %Integer @clone_integer(ptr @x), !dbg !17
call void @"println <:Integer>"(%Integer %1), !dbg !17
%2 = load ptr, ptr @x_mut_ref, align 8, !dbg !18
%3 = call %Integer @integer_from_i64(i64 1), !dbg !19
store %Integer %3, ptr %2, align 8, !dbg !19
Expand All @@ -84,46 +84,46 @@ return: ; preds = %0
ret void
}

define private void @initialize.3() !dbg !24 {
%1 = call %MemoryAddress @address_of(ptr @x), !dbg !25
store %MemoryAddress %1, ptr @address, align 8, !dbg !25
define private void @"println <:Integer>"(%Integer %0) !dbg !24 {
%x = alloca %Integer, align 8
store %Integer %0, ptr %x, align 8
%2 = load %Integer, ptr %x, align 8, !dbg !25
%3 = call %String @integer_as_string(%Integer %2), !dbg !25
call void @"println <:String>"(%String %3), !dbg !25
br label %return, !dbg !25

return: ; preds = %0
return: ; preds = %1
ret void
}

declare %MemoryAddress @address_of(ptr)
declare void @"println <:String>"(%String)

define private void @initialize.4() !dbg !26 {
%1 = load %"Type<Integer>", ptr @"Type<Integer>", align 8, !dbg !27
%2 = call ptr @read_memory(%"Type<Integer>" %1, ptr @address), !dbg !28
store ptr %2, ptr @x_mut_ref, align 8, !dbg !28
br label %return, !dbg !28
declare %String @integer_as_string(%Integer)

declare %Integer @clone_integer(ptr)

define private void @initialize.3() !dbg !26 {
%1 = call %MemoryAddress @address_of(ptr @x), !dbg !27
store %MemoryAddress %1, ptr @address, align 8, !dbg !27
br label %return, !dbg !27

return: ; preds = %0
ret void
}

declare ptr @read_memory(%"Type<Integer>", ptr)
declare %MemoryAddress @address_of(ptr)

define private void @"println <:Integer>"(%Integer %0) !dbg !29 {
%x = alloca %Integer, align 8
store %Integer %0, ptr %x, align 8
%2 = load %Integer, ptr %x, align 8, !dbg !30
%3 = call %String @integer_as_string(%Integer %2), !dbg !30
call void @"println <:String>"(%String %3), !dbg !30
define private void @initialize.4() !dbg !28 {
%1 = load %"Type<Integer>", ptr @"Type<Integer>", align 8, !dbg !29
%2 = call ptr @read_memory(%"Type<Integer>" %1, ptr @address), !dbg !30
store ptr %2, ptr @x_mut_ref, align 8, !dbg !30
br label %return, !dbg !30

return: ; preds = %1
return: ; preds = %0
ret void
}

declare void @"println <:String>"(%String)

declare %String @integer_as_string(%Integer)

declare %Integer @clone_integer(ptr)
declare ptr @read_memory(%"Type<Integer>", ptr)

declare void @destroy_integer(ptr)

Expand All @@ -146,18 +146,18 @@ declare void @destroy_integer(ptr)
!13 = !DILocation(line: 7, scope: !12)
!14 = !DILocation(line: 4, column: 16, scope: !12)
!15 = !DILocation(line: 0, column: 8, scope: !12)
!16 = !DILocation(line: 3, column: 14, scope: !12)
!17 = !DILocation(line: 1, column: 8, scope: !12)
!16 = !DILocation(line: 1, column: 8, scope: !12)
!17 = !DILocation(line: 3, column: 14, scope: !12)
!18 = !DILocation(line: 5, scope: !12)
!19 = !DILocation(line: 5, column: 12, scope: !12)
!20 = !DILocation(line: 6, column: 8, scope: !12)
!21 = !DILocation(line: 0, scope: !12)
!22 = distinct !DISubprogram(name: "initialize.2", linkageName: "initialize.2", scope: !12, file: !2, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!23 = !DILocation(line: 0, column: 8, scope: !22)
!24 = distinct !DISubprogram(name: "initialize.3", linkageName: "initialize.3", scope: !12, file: !2, line: 3, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!25 = !DILocation(line: 3, column: 25, scope: !24)
!26 = distinct !DISubprogram(name: "initialize.4", linkageName: "initialize.4", scope: !12, file: !2, line: 4, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!27 = !DILocation(line: 4, column: 16, scope: !26)
!28 = !DILocation(line: 4, column: 27, scope: !26)
!29 = distinct !DISubprogram(name: "println <:Integer>", linkageName: "println <:Integer>", scope: !12, file: !2, line: 7, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!30 = !DILocation(line: 7, scope: !29)
!24 = distinct !DISubprogram(name: "println <:Integer>", linkageName: "println <:Integer>", scope: !12, file: !2, line: 7, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!25 = !DILocation(line: 7, scope: !24)
!26 = distinct !DISubprogram(name: "initialize.3", linkageName: "initialize.3", scope: !12, file: !2, line: 3, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!27 = !DILocation(line: 3, column: 25, scope: !26)
!28 = distinct !DISubprogram(name: "initialize.4", linkageName: "initialize.4", scope: !12, file: !2, line: 4, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!29 = !DILocation(line: 4, column: 16, scope: !28)
!30 = !DILocation(line: 4, column: 27, scope: !28)
2 changes: 1 addition & 1 deletion src/tests/snapshots/ppl__tests__array.hir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ source: src/tests/mod.rs
expression: hir
---
let mut arr: Array<Integer> = `<:Type<Integer>> [ ]`((Type<Integer>:Type<Integer>))
let mut i: Integer = 2
`println <:Array<Integer>>`((arr:Array<Integer>))
if `<:Reference<Array<Integer>>> is empty`((&arr:ReferenceMut<Array<Integer>>)):
`println <:String>`("Empty")
Expand All @@ -14,6 +13,7 @@ if `<:Reference<Array<Integer>>> is not empty`((&arr:ReferenceMut<Array<Integer>
`println <:String>`("Not empty")

`println <:Integer>`(`clone <:Reference<Integer>>`((*`<:Reference<Array<Integer>>> [ <:Integer> ]`((&arr:ReferenceMut<Array<Integer>>), 0):Integer)))
let mut i: Integer = 2
while `<:Integer> <= <:Integer>`(`clone <:Reference<Integer>>`((i:Integer)), 10):
`push <:Integer> to <:ReferenceMut<Array<Integer>>>`(`clone <:Reference<Integer>>`((i:Integer)), (&arr:ReferenceMut<Array<Integer>>))
`<:ReferenceMut<Integer>> += <:Integer>`((&i:ReferenceMut<Integer>), 1)
Expand Down
Loading
Loading