Skip to content

Commit

Permalink
Merge pull request #772 from candy-lang/chrono-package
Browse files Browse the repository at this point in the history
Chrono package
  • Loading branch information
jwbot authored Dec 25, 2023
2 parents 7aaaee1 + b8c127d commit d36e515
Show file tree
Hide file tree
Showing 12 changed files with 426 additions and 9 deletions.
21 changes: 15 additions & 6 deletions Cargo.lock

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

16 changes: 16 additions & 0 deletions compiler/vm/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::{
io::{self, BufRead},
net::SocketAddr,
str::FromStr,
time::SystemTime,
};
use tiny_http::{Request, Response, Server};
use tracing::info;
Expand Down Expand Up @@ -61,6 +62,8 @@ pub struct DefaultEnvironment {
stdin_handle: Handle,
stdout_handle: Handle,

system_clock_handle: Handle,

dynamic_handles: FxHashMap<Handle, DynamicHandle>,
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down Expand Up @@ -89,6 +92,7 @@ impl DefaultEnvironment {
let http_server_handle = Handle::new(heap, 1);
let stdin_handle = Handle::new(heap, 0);
let stdout_handle = Handle::new(heap, 1);
let system_clock_handle = Handle::new(heap, 0);
let environment_object = Struct::create_with_symbol_keys(
heap,
true,
Expand All @@ -101,6 +105,7 @@ impl DefaultEnvironment {
(heap.default_symbols().http_server, **http_server_handle),
(heap.default_symbols().stdin, **stdin_handle),
(heap.default_symbols().stdout, **stdout_handle),
(heap.default_symbols().system_clock, **system_clock_handle),
],
);
let environment = Self {
Expand All @@ -109,6 +114,7 @@ impl DefaultEnvironment {
http_server_states: vec![],
stdin_handle,
stdout_handle,
system_clock_handle,
dynamic_handles: FxHashMap::default(),
};
(environment_object, environment)
Expand All @@ -128,6 +134,8 @@ impl Environment for DefaultEnvironment {
Self::stdin(heap, &call.arguments)
} else if call.handle == self.stdout_handle {
Self::stdout(heap, &call.arguments)
} else if call.handle == self.system_clock_handle {
Self::system_clock(heap, &call.arguments)
} else {
let dynamic_handle = self.dynamic_handles.get(&call.handle).unwrap_or_else(|| {
panic!(
Expand Down Expand Up @@ -402,6 +410,14 @@ impl DefaultEnvironment {
Tag::create_nothing(heap).into()
}

fn system_clock(heap: &mut Heap, arguments: &[InlineObject]) -> InlineObject {
let [] = arguments else { unreachable!() };

let now = SystemTime::now();
let since_unix_epoch = now.duration_since(SystemTime::UNIX_EPOCH).unwrap();
Int::create(heap, true, since_unix_epoch.as_nanos()).into()
}

fn create_dynamic_handle(
&mut self,
heap: &mut Heap,
Expand Down
6 changes: 5 additions & 1 deletion compiler/vm/src/heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ pub struct DefaultSymbols {
pub stdin: Text,
pub stdout: Text,
pub struct_: Text,
pub system_clock: Text,
pub tag: Text,
pub text: Text,
pub true_: Text,
Expand Down Expand Up @@ -280,6 +281,7 @@ impl DefaultSymbols {
stdin: Text::create(heap, false, "Stdin"),
stdout: Text::create(heap, false, "Stdout"),
struct_: Text::create(heap, false, "Struct"),
system_clock: Text::create(heap, false, "SystemClock"),
tag: Text::create(heap, false, "Tag"),
text: Text::create(heap, false, "Text"),
true_: Text::create(heap, false, "True"),
Expand Down Expand Up @@ -323,6 +325,7 @@ impl DefaultSymbols {
stdin: clone_to_heap(heap, address_map, self.stdin),
stdout: clone_to_heap(heap, address_map, self.stdout),
struct_: clone_to_heap(heap, address_map, self.struct_),
system_clock: clone_to_heap(heap, address_map, self.system_clock),
tag: clone_to_heap(heap, address_map, self.tag),
text: clone_to_heap(heap, address_map, self.text),
true_: clone_to_heap(heap, address_map, self.true_),
Expand All @@ -338,7 +341,7 @@ impl DefaultSymbols {
.map(|it| symbols[it])
}
#[must_use]
pub const fn all_symbols(&self) -> [Text; 26] {
pub const fn all_symbols(&self) -> [Text; 27] {
[
self.arguments,
self.builtin,
Expand All @@ -363,6 +366,7 @@ impl DefaultSymbols {
self.stdin,
self.stdout,
self.struct_,
self.system_clock,
self.tag,
self.text,
self.true_,
Expand Down
4 changes: 4 additions & 0 deletions packages/Chrono/_.candy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
clock := use ".clock"
constants := use ".constants"
duration := use ".duration"
instant := use ".instant"
Empty file added packages/Chrono/_package.candy
Empty file.
21 changes: 21 additions & 0 deletions packages/Chrono/clock.candy
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[tag, function] = use "Core"
instant = use "..instant"
duration = use "..duration"

is c := c %
Clock f -> function.is0 f
_ -> False

fromSystemClock systemClock :=
needs (function.is0 systemClock)
Clock {
systemClock | function.run | duration.fromNanoseconds | instant.fromUnixDuration
}

mockFromInstant inst :=
needs (instant.is inst)
Clock { instant }

now clock :=
needs (is clock)
clock | tag.getValue | function.run
36 changes: 36 additions & 0 deletions packages/Chrono/constants.candy
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[int] = use "Core"

daysPerNormalWeek := 7

hoursPerNormalDay := 24
hoursPerNormalWeek := hoursPerNormalDay | int.multiply daysPerNormalWeek

minutesPerHour := 60
minutesPerNormalDay := minutesPerHour | int.multiply hoursPerNormalDay
minutesPerNormalWeek := minutesPerNormalDay | int.multiply daysPerNormalWeek

secondsPerMinute := 60
secondsPerHour := secondsPerMinute | int.multiply minutesPerHour
secondsPerNormalDay := secondsPerHour | int.multiply hoursPerNormalDay
secondsPerNormalWeek := secondsPerNormalDay | int.multiply daysPerNormalWeek

millisecondsPerSecond := 1000
millisecondsPerMinute := millisecondsPerSecond | int.multiply secondsPerMinute
millisecondsPerHour := millisecondsPerMinute | int.multiply minutesPerHour
millisecondsPerNormalDay := millisecondsPerHour | int.multiply hoursPerNormalDay
millisecondsPerNormalWeek := millisecondsPerNormalDay | int.multiply daysPerNormalWeek

microsecondsPerMillisecond := 1000
microsecondsPerSecond := microsecondsPerMillisecond | int.multiply millisecondsPerSecond
microsecondsPerMinute := microsecondsPerSecond | int.multiply secondsPerMinute
microsecondsPerHour := microsecondsPerMinute | int.multiply minutesPerHour
microsecondsPerNormalDay := microsecondsPerHour | int.multiply hoursPerNormalDay
microsecondsPerNormalWeek := microsecondsPerNormalDay | int.multiply daysPerNormalWeek

nanosecondsPerMicrosecond := 1000
nanosecondsPerMillisecond := nanosecondsPerMicrosecond | int.multiply microsecondsPerMillisecond
nanosecondsPerSecond := nanosecondsPerMillisecond | int.multiply millisecondsPerSecond
nanosecondsPerMinute := nanosecondsPerSecond | int.multiply secondsPerMinute
nanosecondsPerHour := nanosecondsPerMinute | int.multiply minutesPerHour
nanosecondsPerNormalDay := nanosecondsPerHour | int.multiply hoursPerNormalDay
nanosecondsPerNormalWeek := nanosecondsPerNormalDay | int.multiply daysPerNormalWeek
Loading

1 comment on commit d36e515

@jwbot
Copy link
Collaborator Author

@jwbot jwbot commented on d36e515 Dec 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiler

Benchmark suite Current: d36e515 Previous: 7aaaee1 Ratio
Time: Compiler/hello_world 17369168 ns/iter (± 355109) 16618340 ns/iter (± 475512) 1.05
Time: Compiler/fibonacci 137159030 ns/iter (± 1001993) 132556819 ns/iter (± 837747) 1.03
Time: VM Runtime/hello_world 32486 ns/iter (± 49450) 32580 ns/iter (± 2017) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.