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

Commit

Permalink
Small improvements (#143)
Browse files Browse the repository at this point in the history
* Add fixme

* update readme

* Improve decl/define handling
  • Loading branch information
gavrilikhin-d authored Apr 28, 2024
1 parent 3879e5a commit 082b395
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
* [x] Check result of the program itself in `test_compiler_result`
* [x] Add `Array` type
* [x] Fix memory leak due to pointers to builtin types
* [x] Destructors for parameters
* [x] No tmps for literals inside of constructors
---
### Current task
* [ ] Sum of series benchmark
---
* [ ] Destructors for parameters
* [ ] No tmps for literals inside of constructors
* [ ] Forbid recursion without `@recursive` annotation
* [ ] Fix recursive trait (AsString with prints)
* [ ] Generate clone for types with clonable members
* [ ] Generate destructors for types with destructible members
* [ ] Add type aliases
Expand Down
1 change: 1 addition & 0 deletions src/semantics/monomorphize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ impl Monomorphize for ParameterOrVariable {
impl Monomorphize for FunctionData {
fn monomorphize(&mut self, context: &mut impl Context) {
if !self.is_generic() {
// FIXME: definition may be overrided
if self.is_from_trait() && !self.is_definition() {
debug!(target: "linking-trait-fn-from", "{self}");

Expand Down
32 changes: 21 additions & 11 deletions src/semantics/to_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ impl ToHIR for ast::Module {

macro_rules! declare {
() => {
|stmt: &S| {
|(i, stmt)| {
let decl: &D = match stmt {
S::Declaration(d) => d,
_ => return,
Expand All @@ -875,7 +875,7 @@ impl ToHIR for ast::Module {
let res = decl.declare(context);
match res {
Ok(decl) => {
decls.insert(stmt.start(), decl);
decls.insert(i, decl);
}
Err(err) => {
errors.push(err);
Expand All @@ -887,13 +887,17 @@ impl ToHIR for ast::Module {

macro_rules! define {
() => {
|stmt: &S| {
|(i, stmt)| {
let decl: &D = match stmt {
S::Declaration(d) => d,
_ => return,
};

let res = decl.define(decls.remove(&decl.start()).unwrap(), context);
if decls.get(&i).is_none() {
return;
}

let res = decl.define(decls.remove(&i).unwrap(), context);
match res {
Ok(mut stmt) => {
stmt.monomorphize(context);
Expand All @@ -908,38 +912,44 @@ impl ToHIR for ast::Module {
// Declare Types & Traits
self.statements
.iter()
.filter(|s| matches!(s, S::Declaration(D::Type(_) | D::Trait(_))))
.enumerate()
.filter(|(_, s)| matches!(s, S::Declaration(D::Type(_) | D::Trait(_))))
.for_each(declare!());

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

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

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

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

// Add rest of statements
self.statements
.iter()
.filter(|s| !matches!(s, S::Use(_) | S::Declaration(_)))
.for_each(|stmt: &S| {
.enumerate()
.filter(|(_, s)| !matches!(s, S::Use(_) | S::Declaration(_)))
.for_each(|(_, stmt)| {
let res = stmt.to_hir(context);
match res {
Ok(mut stmt) => {
Expand Down

0 comments on commit 082b395

Please sign in to comment.