ludic/selfhost/emit_time.ludic
Orkuncakilkaya 1a2c6ec2c7
Some checks are pending
docs / build-and-deploy (push) Waiting to run
feat(stdlib): Time/Date/Duration calendar-clock core (issue #9)
Implement the calendar/clock half of #9 as plain-i32 integer epochs — no
new type, no floating point (the issue's "integer epochs to avoid drift") —
so every operation is deterministic and bit-identical on every platform:

  Duration — a span in whole seconds; seconds/minutes/hours/days build one,
             as_seconds/as_minutes/as_hours/as_days read it back. Because a
             duration is just an int, `+` and `>` work with no extra machinery
             (Duration.minutes(5) + Duration.seconds(30), away > Duration.hours(3)).
  Date     — a civil day as days-since-1970 (UTC): new/year/month/day/weekday/
             is_leap/days_in_month/to_epoch/add_days/diff_days.
  DateTime — an instant as seconds-since-1970 (UTC, matching Time.now):
             from/date/add/year/month/day/weekday/hour/minute/second.
  Time.since(past) = now - past, for offline-progress / "time away" checks.

New selfhost/emit_datetime.ludic (is_/emit_ for the three namespaces, wired
into emit_ns_call + the frag list). The two civil<->epoch conversions are
Howard Hinnant's public-domain proleptic-Gregorian algorithms, emitted once
per program as the @fn_days_from_civil / @fn_civil_from_days prelude and gated
by g_uses_datert; days_in_month is next-month-day-0 (no lookup table). Time
gains `since`. Docs (Duration/Date/DateTime sections, 28 method pages +
time-since), inventory, and LSP hover kept in sync; a registered test checks
component math against hand-computed values. Reseeded; C-free fixpoint holds;
all suites green (26 self-host / 45 regression / 29 tools); check.py,
check-impl.py and validate.py OK.

format/parse, a game-controlled simulated clock, and timezones are tracked
follow-ups; v1 is UTC-only and, on the i32 epoch, valid through 2038.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-30 03:37:38 +03:00

33 lines
1.6 KiB
Text

# emit_time.ludic — the Time.* namespace. frame/elapsed/delta are deterministic
# (driven by @L_frame, the per-frame counter the game loop increments); now() is
# the wall clock and is explicitly non-deterministic. The frame clock ticks at a
# fixed 60 per second, so delta is the constant 1/60 s = 1092 in Q16.16.
function is_time_ns(meth: pointer) -> bool {
if (meth == "frame") or (meth == "delta") or (meth == "elapsed") or (meth == "now") { return true }
if (meth == "since") { return true }
return false
}
function emit_time_ns(meth: pointer, e: Node) -> Val {
if (meth == "frame") { # completed frames since start
return val(emit_bind("load i32, ptr @L_frame"), "int")
}
if (meth == "delta") { # seconds per frame, 1/60 (fixed)
return val("1092", "fixed")
}
if (meth == "elapsed") { # seconds since start = frame / 60 (fixed)
let f = emit_bind("load i32, ptr @L_frame")
return val(emit_bind(`mul i32 {f}, 1092`), "fixed")
}
if (meth == "since") { # seconds elapsed from a past instant to now
let past = emit_expr(e.kids[0]) # NON-DETERMINISTIC (reads the wall clock)
let nw = emit_bind("call i64 @time(ptr null)")
let n32 = emit_bind(`trunc i64 {nw} to i32`)
return val(emit_bind(`sub i32 {n32}, {past.code}`), "int")
}
# now: wall-clock seconds since the epoch — NON-DETERMINISTIC, for seeding /
# telemetry only, never the lockstep simulation.
let t = emit_bind("call i64 @time(ptr null)")
return val(emit_bind(`trunc i64 {t} to i32`), "int")
}