Skip to content

Commit

Permalink
Std: use new import system
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Nov 21, 2024
1 parent 99e6808 commit 7fae681
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 57 deletions.
42 changes: 22 additions & 20 deletions src/std/float.no
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
pub type Float

pub let neg = fn(float): Float {
negFloat(float)
}
impl Num for Float {
neg = fn(self): Self {
negFloat(self)
}

pub let abs = fn(float): Float {
absFloat(float)
}
abs = fn(self): Self {
absFloat(self)
}

pub let add = fn(float, other: Float): Float {
addFloat(float, other)
}
add = fn(self, other: Self): Self {
addFloat(self, other)
}

pub let sub = fn(float, other: Float): Float {
subFloat(float, other)
}
sub = fn(self, other: Self): Self {
subFloat(self, other)
}

pub let mult = fn(float, other: Float): Float {
multFloat(float, other)
}
mult = fn(self, other: Self): Self {
multFloat(self, other)
}

pub let div = fn(float, other: Float): Float {
divFloat(float, other)
}
div = fn(self, other: Self): Self {
divFloat(self, other)
}

pub let exp = fn(float, other: Float): Float {
expFloat(float, other)
exp = fn(self, other: Self): Self {
expFloat(self, other)
}
}

impl Eq for Float {
Expand Down
12 changes: 7 additions & 5 deletions src/std/future/mod.no
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::ref::eqRef
use std::list::{ add }
use std::future::runtime::{ Runtime, runtime }

pub type Future<T>(
f: fn(resolve: fn(result: T): Unit): Unit,
Expand All @@ -16,8 +18,8 @@ pub type FutureState<T> {

impl <T> Trace for FutureState<T> {}

pub let new = fn<T>(f: fn(resolve: fn(result: T): Unit): Unit): Self {
Future(f, [], FutureState::Created())
pub let new = fn<T>(f: fn(resolve: fn(result: T): Unit): Unit): Future<T> {
Future(f, [], Created())
}

pub let onResolve = fn<T>(future: Future<T>, f: fn(result: T): Unit): Unit {
Expand All @@ -37,18 +39,18 @@ pub let onResolve = fn<T>(future: Future<T>, f: fn(result: T): Unit): Unit {
// TODO: belongs to std::control::Functor
pub let map = fn<T, U>(future: Future<T>, f: fn(value: T): U): Future<U> {
// TODO: use .await
Future::new(fn(resolve) { future.onResolve(fn(res) { resolve(f(res)) }) })
new(fn(resolve) { future.onResolve(fn(res) { resolve(f(res)) }) })
}

// TODO: belongs to std::control::Monad
pub let flatMap = fn<T, U>(future: Future<T>, f: fn(value: T): Future<U>): Future<U> {
// TODO: use .await
Future::new(fn(resolve) {
new(fn(resolve) {
future.onResolve(fn (res) { f(res).onResolve(fn(inner) { resolve(inner) }) })
})
}

pub let spawn = fn<T>(future: Future<T>, runtime: Runtime): Self {
pub let spawn = fn<T>(future: Future<T>, runtime: Runtime): Future<T> {
runtime.spawn(future)
}

21 changes: 13 additions & 8 deletions src/std/future/runtime.no
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
use std::future::{ self, FutureState, state, subscribers }
use std::list::{ add, count, popAt, clear, popFront }
use std::iter::{ Iter::position }
use std::ref::eqRef

pub type Runtime(
// TODO: use std::time::Duration
pollingRate: Int,
queue: List<Future<_>>,
pending: List<Future<_>>,
)

pub let runtime: Runtime = Runtime::new()
pub let runtime: Runtime = new()

pub let new = fn(): Runtime {
Runtime::withPollingRate(10)
withPollingRate(10)
}

pub let withPollingRate = fn(pollingRate: Int): Runtime {
Runtime::Runtime(pollingRate, [], [])
Runtime(pollingRate, [], [])
}

pub let loop = fn(runtime): Unit {
while runtime.queue.count() > 0 {
let next = runtime.queue.popFront()!
next.state = FutureState::Pending()
next.state = future::Pending()
runtime.pending.add(next)
let _ = {next.f}(fn(res) {
next.state = FutureState::Resolved(res)
let _ = {next.future::f}(fn(res) {
next.state = future::Resolved(res)
for s in next.subscribers {
s(res)
}
Expand All @@ -39,13 +44,13 @@ pub let loop = fn(runtime): Unit {

pub let spawn = fn<T>(runtime, future: Future<T>): Future<T> {
runtime.queue.add(future)
future.state = FutureState::Queued()
future.state = future::Queued()
future
}

// TODO: use std::time::Duration
pub let delay = fn(delay: Int): Future<Unit> {
Future::new(fn(resolve) { deferFor(fn() { resolve(unit) }, delay) })
future::new(fn(resolve) { deferFor(fn() { resolve(unit) }, delay) })
}

pub let defer = fn(f: fn(): Unit): Unit {
Expand Down
42 changes: 22 additions & 20 deletions src/std/int.no
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
pub type Int

pub let neg = fn(int): Int {
negInt(int)
}
impl Num for Int {
neg = fn(self): Self {
negInt(self)
}

pub let abs = fn(int): Int {
absInt(int)
}
abs = fn(self): Self {
absInt(self)
}

pub let add = fn(int, other: Int): Int {
addInt(int, other)
}
add = fn(self, other: Self): Self {
addInt(self, other)
}

pub let sub = fn(int, other: Int): Int {
subInt(int, other)
}
sub = fn(self, other: Self): Self {
subInt(self, other)
}

pub let mult = fn(int, other: Int): Int {
multInt(int, other)
}
mult = fn(self, other: Self): Self {
multInt(self, other)
}

pub let div = fn(int, other: Int): Int {
divInt(int, other)
}
div = fn(self, other: Self): Self {
divInt(self, other)
}

pub let exp = fn(int, other: Int): Int {
expInt(int, other)
exp = fn(self, other: Self): Self {
expInt(self, other)
}
}

pub let mod = fn(int, divisor: Int): Int {
Expand Down
2 changes: 2 additions & 0 deletions src/std/iter/peekable.no
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::iter::Iter::next

pub type PeekableIter<T>(
iter: Iter<T>,
peeked: Option<Option<T>>
Expand Down
2 changes: 1 addition & 1 deletion src/std/iter/repeat.no
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ impl <T> Iter<T> for RepeatIter<T> {
}

pub let repeat = fn<T>(item: T, count: Int): RepeatIter<T> {
RepeatIter(range: RangeIter::RangeIter(0, count), item)
RepeatIter(range: RangeIter(0, count), item)
}

15 changes: 15 additions & 0 deletions src/std/num.no
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pub trait Num {
neg = fn(self): Self

abs = fn(self): Self

add = fn(self, other: Self): Self

sub = fn(self, other: Self): Self

mult = fn(self, other: Self): Self

div = fn(self, other: Self): Self

exp = fn(self, other: Self): Self
}
5 changes: 3 additions & 2 deletions src/std/prelude.no
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ pub use std::{
int::{ self, Int },
string::{ self, String },
char::{ self, Char },
bool::{ self, Bool },
bool::{ self, Bool, not },
num::Num,
eq::Eq,
copy::{ Copy, copy },
ord::{ Ordering, Ord },
iter::{ Iterable, Iter, Collector },
iter::{ Iterable::{ self, iter }, Iter, Collector },
iter::range::range,
iter::repeat::repeat,
list::{ self, List },
Expand Down
2 changes: 1 addition & 1 deletion src/std/string.no
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::iter::MapAdapter
pub type String

// TODO: belongs to the Monoid trait
pub concat = fn(string: String, other: Self): String {
pub let concat = fn(string: String, other: String): String {
concatString(string, other)
}

Expand Down

0 comments on commit 7fae681

Please sign in to comment.