Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for latest Savi syntax. #23

Merged
merged 1 commit into from
Jun 17, 2023
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
36 changes: 18 additions & 18 deletions src/Time.Duration.savi
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

:: Represent a duration of the given number of nanoseconds.
:new nanoseconds(n U64):
if (n < 1000000000) (
if n < 1000000000 (
@total_seconds = 0
@nanosecond = n.u32
|
Expand All @@ -30,7 +30,7 @@

:: Represent a duration of the given number of microseconds.
:new microseconds(n U64)
if (n < 1000000) (
if n < 1000000 (
@total_seconds = 0
@nanosecond = n.u32 * 1000
|
Expand All @@ -40,7 +40,7 @@

:: Represent a duration of the given number of milliseconds.
:new milliseconds(n U64)
if (n < 1000) (
if n < 1000 (
@total_seconds = 0
@nanosecond = n.u32 * 1000000
|
Expand Down Expand Up @@ -182,37 +182,37 @@
printed_anything = False
output << "Time.Duration("

if (weeks > 0) (
if weeks > 0 (
Inspect.into(output, weeks), output << " weeks"
printed_anything = True
)

if (days > 0) (
if printed_anything (output << ", ")
if days > 0 (
if printed_anything output << ", "
Inspect.into(output, days), output << " days"
printed_anything = True
)

if (hours > 0) (
if printed_anything (output << ", ")
if hours > 0 (
if printed_anything output << ", "
Inspect.into(output, hours), output << " hours"
printed_anything = True
)

if (minutes > 0) (
if printed_anything (output << ", ")
if minutes > 0 (
if printed_anything output << ", "
Inspect.into(output, minutes), output << " minutes"
printed_anything = True
)

if (seconds > 0) (
if printed_anything (output << ", ")
if seconds > 0 (
if printed_anything output << ", "
Inspect.into(output, seconds), output << " seconds"
printed_anything = True
)

if (nanoseconds > 0) (
if printed_anything (output << ", ")
if nanoseconds > 0 (
if printed_anything output << ", "
Inspect.into(output, nanoseconds), output << " nanoseconds"
printed_anything = True
)
Expand Down Expand Up @@ -241,7 +241,7 @@
:fun "+"(other Time.Duration'box)
total = @total_seconds + other.total_seconds
nanos = @nanosecond + other.nanosecond
while (nanos >= Time._nanoseconds_per_second) (
while nanos >= Time._nanoseconds_per_second (
nanos -= Time._nanoseconds_per_second
total += 1
)
Expand All @@ -254,7 +254,7 @@
:fun "+!"(other Time.Duration'box)
total = @total_seconds +! other.total_seconds
nanos = @nanosecond + other.nanosecond
while (nanos >= Time._nanoseconds_per_second) (
while nanos >= Time._nanoseconds_per_second (
nanos -= Time._nanoseconds_per_second
total = total +! 1
)
Expand All @@ -267,7 +267,7 @@
:fun "-"(other Time.Duration'box)
total = @total_seconds, other_total = other.total_seconds
nanos = @nanosecond, other_nanos = other.nanosecond
while (nanos < other_nanos) (
while nanos < other_nanos (
nanos += Time._nanoseconds_per_second
total -= 1
)
Expand All @@ -280,7 +280,7 @@
:fun "-!"(other Time.Duration'box)
total = @total_seconds, other_total = other.total_seconds
nanos = @nanosecond, other_nanos = other.nanosecond
while (nanos < other_nanos) (
while nanos < other_nanos (
nanos += Time._nanoseconds_per_second
total = total -! 1
)
Expand Down
28 changes: 14 additions & 14 deletions src/Time.Formatter.savi
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
original_out_size = out.size
is_bad_format = False

while (i < @pattern.size()) (
while i < @pattern.size() (
try (
char U8 = @pattern.byte_at!(i)

ok = True

if (char == '%') (
if char == '%' (
char = @pattern.byte_at!(i + 1)
case (
| char == 'a' | @short_day_name(time, out)
Expand Down Expand Up @@ -328,11 +328,11 @@

:fun hour_12_zero_padded(time Time'box, out String'ref = String.new)
h = (time.hour % 12).u64
@pad2(if (h == 0) (12 | h), '0', out)
@pad2(if h == 0 (12 | h), '0', out)

:fun hour_12_blank_padded(time Time'box, out String'ref = String.new)
h = (time.hour % 12).u64
@pad2(if (h == 0) (12 | h), ' ', out)
@pad2(if h == 0 (12 | h), ' ', out)

:fun minute(time Time'box, out String'ref = String.new)
@pad2(time.minute.u64, '0', out)
Expand Down Expand Up @@ -374,10 +374,10 @@
out

:fun am_pm(time Time'box) String
if (time.hour < 12) ("am" | "pm")
if time.hour < 12 ("am" | "pm")

:fun am_pm_upcase(time Time'box) String
if (time.hour < 12) ("AM" | "PM")
if time.hour < 12 ("AM" | "PM")

:fun day_of_week_monday_1_7(time Time'box, out String'ref = String.new)
Inspect.into(out, time.day_of_week.u64)
Expand Down Expand Up @@ -446,25 +446,25 @@
@get_day_name(time).substring(0, 3)

:fun pad2(value U64, padding U8, out String'ref = String.new)
if (value < 10) out.push_byte(padding)
if value < 10 out.push_byte(padding)
Inspect.into(out, value)
out

:fun pad3(value U64, padding U8, out String'ref = String.new)
if (value < 100) out.push_byte(padding)
if value < 100 out.push_byte(padding)
@pad2(value, padding, out)

:fun pad4(value U64, padding U8, out String'ref = String.new)
if (value < 1000) out.push_byte(padding)
if value < 1000 out.push_byte(padding)
@pad3(value, padding, out)

:fun pad6(value U64, padding U8, out String'ref = String.new)
if (value < 100000) out.push_byte(padding)
if (value < 10000) out.push_byte(padding)
if value < 100000 out.push_byte(padding)
if value < 10000 out.push_byte(padding)
@pad4(value, padding, out)

:fun pad9(value U64, padding U8, out String'ref = String.new)
if (value < 100000000) out.push_byte(padding)
if (value < 10000000) out.push_byte(padding)
if (value < 1000000) out.push_byte(padding)
if value < 100000000 out.push_byte(padding)
if value < 10000000 out.push_byte(padding)
if value < 1000000 out.push_byte(padding)
@pad6(value, padding, out)
6 changes: 3 additions & 3 deletions src/Time.Measure.savi
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
// so we ask it for the timebase and multiple/divide to get nanoseconds.
timebase = Pair(U32, U32).new(0, 0)
timebase_res = _FFI.mach_timebase_info(stack_address_of_variable timebase)
return 0 if (timebase_res != 0)
return 0 if timebase_res != 0
_FFI.mach_absolute_time
* timebase.first.u64
/ timebase.last.u64
Expand Down Expand Up @@ -89,7 +89,7 @@
// so we ask it for the timebase and multiple/divide to get microseconds.
timebase = Pair(U32, U32).new(0, 0)
timebase_res = _FFI.mach_timebase_info(stack_address_of_variable timebase)
return 0 if (timebase_res != 0)
return 0 if timebase_res != 0
_FFI.mach_absolute_time
* timebase.first.u64
/ (timebase.last.u64 * 1000)
Expand Down Expand Up @@ -130,7 +130,7 @@
// so we ask it for the timebase and multiple/divide to get milliseconds.
timebase = Pair(U32, U32).new(0, 0)
timebase_res = _FFI.mach_timebase_info(stack_address_of_variable timebase)
return 0 if (timebase_res != 0)
return 0 if timebase_res != 0
_FFI.mach_absolute_time
* timebase.first.u64
/ (timebase.last.u64 * 1000000)
Expand Down
26 changes: 13 additions & 13 deletions src/Time.savi
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
:: DEPRECATED: Use `Time.Duration.nanoseconds` instead.
:new nanoseconds(n U32):
@total_seconds = 0
while (@_nanoseconds_per_second < n) (
while @_nanoseconds_per_second < n (
n -= @_nanoseconds_per_second
@total_seconds += 1
)
Expand Down Expand Up @@ -197,21 +197,21 @@
total_days -= num400 * @_days_per_400_years

num100 U64 = total_days / @_days_per_100_years // TODO: shouldn't need an explicit U64 here
if (num100 == 4) (num100 = 3) // account for leap years
if num100 == 4 (num100 = 3) // account for leap years
total_days -= num100 * @_days_per_100_years

num4 = total_days / @_days_per_4_years
total_days -= num4 * @_days_per_4_years

numyears U64 = total_days / 365 // TODO: shouldn't need an explicit U64 here
if (numyears == 4) (numyears = 3) // account for leap years
if numyears == 4 (numyears = 3) // account for leap years
total_days -= numyears * 365

year = (num400 * 400 + num100 * 100 + num4 * 4 + numyears + 1).u32

month U8 = 0
days_in_month U64 = 0
while (total_days >= days_in_month) (
while total_days >= days_in_month (
total_days -= days_in_month
month += 1
days_in_month = @_days_in_month(month, year).u64
Expand Down Expand Up @@ -249,12 +249,12 @@
:: by the given year, month, and day in the proleptic Gregorian calendar.
:fun non _absolute_days(year U32, month U8, day U8) U64
// Normalize the month index, treating months beyond 12 as additional years.
while (month > 12) (year += 1, month -= 12)
while month > 12 (year += 1, month -= 12)

// Calculate the number of days since day 1 of the given year.
days_since_jan_1 = day.u64 - 1
scan_month U8 = 1
while (scan_month < month) (
while scan_month < month (
days_since_jan_1 += @_days_in_month(scan_month, year).u64
scan_month += 1
)
Expand All @@ -274,7 +274,7 @@
total_days -= num400 * @_days_per_400_years

num100 U64 = (total_days / @_days_per_100_years)
if (num100 == 4) (// leap
if num100 == 4 (// leap
num100 = 3
)
total_days -= num100 * @_days_per_100_years
Expand All @@ -283,7 +283,7 @@
total_days -= num4 * @_days_per_4_years

numyears = (total_days / 365)
if (numyears == 4) (// leap
if numyears == 4 (// leap
numyears = 3
)
total_days -= numyears * 365
Expand All @@ -297,7 +297,7 @@
try (
while True (
days_in_month = @_days_in_month(month.u8, year.u32).u64
if (total_days < days_in_month) (
if total_days < days_in_month (
error! // TODO use break
)

Expand Down Expand Up @@ -329,7 +329,7 @@
// previous year and the first Monday is actually in week 2.
week_number = ((day_year - day_of_week.u64 + 10) / 7).u64

if (week_number == 0) (
if week_number == 0 (
// Week number 0 means the date belongs to the last week of the previous year.
year -= 1

Expand All @@ -339,18 +339,18 @@
// year and January 1 is on a Saturday.
jan1_day_of_week = Time.DayOfWeek.from_value(((day_of_week.u64 - day_year + 1) % 7).u8)

if ((jan1_day_of_week == Time.DayOfWeek.Friday) || (jan1_day_of_week == Time.DayOfWeek.Saturday && @_is_leap_year(year.u32))) (
if (jan1_day_of_week == Time.DayOfWeek.Friday) || (jan1_day_of_week == Time.DayOfWeek.Saturday && @_is_leap_year(year.u32)) (
week_number = 53
|
week_number = 52
)
|
if (week_number == 53) (
if week_number == 53 (
// Week number 53 is actually week number 1 of the following year, if
// December 31 is on a Monday, Tuesday or Wednesday.
dec31_day_of_week = (day_of_week.u64 + 31 - day) % 7

if (dec31_day_of_week <= Time.DayOfWeek.Wednesday.u64) (
if dec31_day_of_week <= Time.DayOfWeek.Wednesday.u64 (
year += 1
week_number = 1
)
Expand Down
2 changes: 1 addition & 1 deletion src/_FFI.savi
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
:: Wrapper for C functions used to get subsecond values on Windows.
:fun windows_subseconds(factor U64)
freq = @_windows_perf_freq
if (freq <= factor) (
if freq <= factor (
@_windows_perf_counter * (factor / freq)
|
(@_windows_perf_counter * factor) / freq
Expand Down