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

Small improvements #180

Merged
merged 1 commit into from
May 19, 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
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ indexmap = "2.2.6"
gimli = { version = "0.29.0", default-features = false }
insta = "1.38.0"
cmd_lib = "1.9.3"
derive-visitor = "0.3.0"
derive-visitor = { version = "0.3.0", git = "https://github.com/andylokandy/derive-visitor", branch = "fix" }

[build-dependencies]
clap = { version = "4.5.4", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
### Current task
* [ ] Printable trait should take references
---
* [ ] migrate to pass-by-ref (branch `arc`)
* [ ] Prefer candidates with mutable references, when possible
* [ ] Fix problems with to_ir and loading references (especially globals). This causes issues in iterator
* [ ] Benchmark for linear algebra
Expand Down
6 changes: 6 additions & 0 deletions ppl/src/array.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ type Array<T>:
capacity: Integer
data: MemoryAddress

fn<T> default <:Type<Array<T>>> -> Array<T>:
let size = 0
let capacity = 0
let data = default MemoryAddress
return Array<T> { size, capacity, data }

/// Create an empty array
fn<T> <:Type<T>>[] -> Array<T>:
let capacity = 8
Expand Down
19 changes: 19 additions & 0 deletions ppl/src/core.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type None

/// Convert `None` to `String`
fn String from <:None> => "none"

fn default <:Type<None>> => none
//---------------------------------


Expand All @@ -38,6 +40,8 @@ fn String from <:None> => "none"
@builtin
type Bool

fn default <:Type<Bool>> => false

/// Negate boolean value
fn not <x:Bool> -> Bool:
if x:
Expand Down Expand Up @@ -73,6 +77,9 @@ type IntegerImpl
type Integer:
impl: Reference<IntegerImpl>

@builtin
fn default <:Type<Integer>> => 0

@mangle_as("integer_eq_integer")
fn <:Integer> == <:Integer> -> Bool

Expand Down Expand Up @@ -128,6 +135,8 @@ type RationalImpl
type Rational:
impl: Reference<RationalImpl>

fn default <:Type<Rational>> => 0.0

@mangle_as("rational_eq_rational")
fn <:Rational> == <:Rational> -> Bool

Expand Down Expand Up @@ -171,6 +180,8 @@ type StringImpl
type String:
impl: Reference<StringImpl>

fn default <:Type<String>> => ""

/// Concatenate 2 strings
@mangle_as("string_plus_string")
fn <:String> + <:String> -> String
Expand Down Expand Up @@ -241,3 +252,11 @@ trait Clonnable:
fn clone <:&Self> -> Self
//=================================

//=================================
// Default
//=================================
/// Trait for things that have default value
trait Default:
fn default <:Type<Self>> -> Self
//=================================

2 changes: 2 additions & 0 deletions ppl/src/f64.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use core.*
@builtin
type F64

fn default <:Type<F64>> => F64 from 0.0

fn + <x: F64> => x

@mangle_as("minus_f64")
Expand Down
2 changes: 2 additions & 0 deletions ppl/src/i32.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use core.*
@builtin
type I32

fn default <:Type<I32>> => 0 as I32

fn + <x: I32> => x

@mangle_as("minus_i32")
Expand Down
2 changes: 2 additions & 0 deletions ppl/src/memory.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use math.*
type MemoryAddress:
value: Integer

fn default <:Type<MemoryAddress>> => MemoryAddress { value: 0 }

/// Interpret and integer as memory address
fn <value: Integer> as MemoryAddress => MemoryAddress { value }

Expand Down
7 changes: 6 additions & 1 deletion src/hir/declarations/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
sync::{Arc, RwLock},
};

use derive_visitor::DriveMut;
use derive_visitor::{DriveMut, VisitorMut};
use indexmap::IndexMap;

use crate::{
Expand Down Expand Up @@ -99,12 +99,17 @@ pub struct TraitData {
#[drive(skip)]
pub supertraits: Vec<Trait>,
/// Associated functions
#[drive(with = "drive_functions")]
pub functions: IndexMap<String, Function>,
/// Module this trait is located in
#[drive(skip)]
pub module: Module,
}

fn drive_functions<V: VisitorMut>(funcs: &mut IndexMap<String, Function>, visitor: &mut V) {
funcs.values_mut().for_each(|f| f.drive_mut(visitor));
}

impl TraitData {
/// Iterate over all functions
pub fn all_functions(&self) -> impl Iterator<Item = Function> + '_ {
Expand Down
1 change: 0 additions & 1 deletion src/hir/expressions/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct MemberReference {
#[drive(skip)]
pub span: std::ops::Range<usize>,
/// Base expression
#[drive(skip)]
pub base: Box<Expression>,
/// Referenced variable name
#[drive(skip)]
Expand Down
7 changes: 6 additions & 1 deletion src/ir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'llvm> Types<'llvm> {
/// LLVM IR for [`Class`](Type::Class) type
pub fn opaque(&self, name: &str) -> PointerType<'llvm> {
self.get_or_add_opaque_struct(name);
self.llvm.ptr_type(AddressSpace::default())
self.pointer()
}

/// LLVM IR for [`None`](Type::None) type
Expand All @@ -109,6 +109,11 @@ impl<'llvm> Types<'llvm> {

/// LLVM IR for C string type
pub fn c_string(&self) -> PointerType<'llvm> {
self.pointer()
}

/// LLVM IR for pointer type
pub fn pointer(&self) -> PointerType<'llvm> {
self.llvm.ptr_type(AddressSpace::default())
}
}
14 changes: 3 additions & 11 deletions src/runtime/src/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ type F64 = f64;
/// ```
#[no_mangle]
pub extern "C" fn f64_as_string(d: F64) -> String {
let boxed = Box::new(d.to_string());
String {
data: Box::into_raw(boxed),
}
d.to_string().into()
}

/// Negates f64
Expand Down Expand Up @@ -65,9 +62,7 @@ pub extern "C" fn f64_star_f64(x: F64, y: F64) -> F64 {
/// ```
#[no_mangle]
pub extern "C" fn f64_from_rational(r: Rational) -> F64 {
let r = unsafe { r.data.as_ref() }.unwrap();
let res = r.to_f64();
res
r.as_ref().to_f64()
}

/// Create rational from f64
Expand All @@ -80,8 +75,5 @@ pub extern "C" fn f64_from_rational(r: Rational) -> F64 {
/// ```
#[no_mangle]
pub extern "C" fn rational_from_f64(d: F64) -> Rational {
let r = Box::new(rug::Rational::from_f64(d).unwrap());
Rational {
data: Box::into_raw(r),
}
rug::Rational::from_f64(d).unwrap().into()
}
Loading
Loading