hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 3a95dcd8b12844f0720b3a1818cbfd7a1c272db4
parent c56bd3dc2ad04565d118b1c4f5003c90306cab28
Author: Byron Torres <b@torresjrjr.com>
Date:   Tue,  1 Feb 2022 11:55:27 +0000

s/builder/mock

Signed-off-by: Byron Torres <b@torresjrjr.com>

Diffstat:
Mdatetime/README | 8++++----
Mdatetime/arithmetic.ha | 2+-
Mdatetime/datetime.ha | 30+++++++++++++++---------------
Mdatetime/format.ha | 54+++++++++++++++++++++++++++---------------------------
4 files changed, 47 insertions(+), 47 deletions(-)

diff --git a/datetime/README b/datetime/README @@ -6,9 +6,9 @@ the [[chrono::moment]] type, optimized for dealing with the Gregorian chronology. Datetimes are created with [[new]], [[now]], or with one of the various -"from_" functions. Alternatively, use a [[builder]] to construct a -datetime piece-by-piece, by field assignements or by parsing strings -with [[parse]]. +"from_" functions. Alternatively, use a [[mock]] to construct a datetime +piece-by-piece, by field assignements or by parsing strings with +[[parse]]. [[datetime]] instances are designed to be always valid and internally consistent. They should be treated as immutable, and their fields as @@ -19,7 +19,7 @@ pointer, which is used only for internal caching. [[datetime]] fields are accessed, evaluated, and cached via the various "field" functions ([[year]], [[month]], [[day]], etc). Accessing or modifying a [[datetime]]'s fields directly is highly discouraged. See -[[builder]] for "modifiable datetimes". +[[mock]] for "modifiable datetimes". [[datetime]]s may be localized to different [[chrono::timezone]]s via the [[in]] function. The "field" functions will evaluate the correct diff --git a/datetime/arithmetic.ha b/datetime/arithmetic.ha @@ -294,7 +294,7 @@ export fn hop(dt: datetime, pp: period...) datetime = { // days = -4, // 2020-04-09 12:30:45 // }); export fn add(dt: datetime, flag: calculus, pp: period...) datetime = { - // TODO: Use builder to simplify some code. + // TODO: Use [[mock]] to simplify some code. let d_year = year(&dt); let d_month = month(&dt); let d_day = day(&dt); diff --git a/datetime/datetime.ha b/datetime/datetime.ha @@ -102,7 +102,7 @@ export fn new( // TODO: improve variety of errors. // `invaliddatetime = !void` ? -// `invaliddatetime = !datetime::builder` ? +// `invaliddatetime = !datetime::mock` ? ) (datetime | errors::invalid) = { let defaults: [_]int = [ 0, 1, 1, // year month day @@ -195,7 +195,7 @@ export fn from_moment(m: chrono::moment) datetime = { // // TODO: allow the user to specify [[strategy]] for security? export fn from_str(layout: str, s: str) (datetime | insufficient | errors::invalid) = { - const b = newbuilder(); + const b = newmock(); parse(&b, layout, s)?; return build(&b)?; }; @@ -210,28 +210,28 @@ export fn to_moment(dt: datetime) chrono::moment = { }; }; -// A builder has insufficient information and cannot create a valid datetime. +// A [[mock]] has insufficient information and cannot create a valid datetime. export type insufficient = !void; -// Constructs a new datetime. Start with [[newbuilder]]. Collect enough datetime +// Constructs a new datetime. Start with [[newmock]]. Collect enough datetime // information incrementally by direct field assignments or multiple calls to // [[parse]]. Finish with [[build]]. // -// let builder = datetime::newbuilder(); -// datetime::parse(&builder, "Year: %Y", "Year: 2038"); -// datetime::parse(&builder, "Month: %m", "Month: 01"); -// builder.day = 19; -// let dt = datetime::build(&builder, datetime::strategy::YMD); +// let mock = datetime::newmock(); +// datetime::parse(&mock, "Year: %Y", "Year: 2038"); +// datetime::parse(&mock, "Month: %m", "Month: 01"); +// mock.day = 19; +// let dt = datetime::build(&mock, datetime::strategy::YMD); // -export type builder = datetime; +export type mock = datetime; -// Creates a new [[builder]] -export fn newbuilder() builder = init(): builder; +// Creates a new [[mock]] +export fn newmock() mock = init(): mock; -// Returns a datetime from a builder. The provided [[strategy]]s will be tried in +// Returns a datetime from a mock. The provided [[strategy]]s will be tried in // order until a valid datetime is produced, or fail otherwise. The default // strategy is [[strategy::ALL]]. -export fn build(f: *builder, m: strategy...) (datetime | insufficient | errors::invalid) = { +export fn build(f: *mock, m: strategy...) (datetime | insufficient | errors::invalid) = { if (len(m) == 0) { m = [strategy::ALL]; }; @@ -284,7 +284,7 @@ export fn build(f: *builder, m: strategy...) (datetime | insufficient | errors:: return insufficient; }; -// Specifies which [[builder]] fields (and what strategy) to use to calculate the +// Specifies which [[mock]] fields (and what strategy) to use to calculate the // epochal, and thus a valid datetime. export type strategy = enum uint { // year, month, day diff --git a/datetime/format.ha b/datetime/format.ha @@ -55,14 +55,14 @@ def MONTHS_SHORT: [_]str = [ // // See https://en.wikipedia.org/wiki/ISO_8601#Time_intervals -// Parses a datetime string into a [[builder]], using a "layout" format string +// Parses a datetime string into a [[mock]], using a "layout" format string // with a subset of specifiers from POSIX strptime(3). Partial, incremental // parsing is possible. // -// datetime::parse(&b, "%Y-%m-%d", "2038-01-19"); -// datetime::parse(&b, "%H:%M:%S", "03:14:07"); +// datetime::parse(&mok, "%Y-%m-%d", "2038-01-19"); +// datetime::parse(&mok, "%H:%M:%S", "03:14:07"); // -export fn parse(b: *builder, layout: str, s: str) (void | errors::invalid) = { +export fn parse(mok: *mock, layout: str, s: str) (void | errors::invalid) = { const format_iter = strings::iter(layout); const s_iter = strings::iter(s); let escaped = false; @@ -96,29 +96,29 @@ export fn parse(b: *builder, layout: str, s: str) (void | errors::invalid) = { switch (format_r) { // Basic specifiers case 'a' => - b.weekday = get_default_locale_string_index( + mok.weekday = get_default_locale_string_index( &s_iter, WEEKDAYS_SHORT[..])?; case 'A' => - b.weekday = get_default_locale_string_index( + mok.weekday = get_default_locale_string_index( &s_iter, WEEKDAYS[..])?; case 'b' => - b.month = get_default_locale_string_index( + mok.month = get_default_locale_string_index( &s_iter, MONTHS_SHORT[..])?; case 'B' => - b.month = get_default_locale_string_index( + mok.month = get_default_locale_string_index( &s_iter, MONTHS[..])?; case 'd' => let max_n_digits = 2u; - b.day = clamp_int( + mok.day = clamp_int( get_max_n_digits(&s_iter, max_n_digits)?, 1, 31); case 'H' => let max_n_digits = 2u; - b.hour = clamp_int( + mok.hour = clamp_int( get_max_n_digits(&s_iter, max_n_digits)?, 0, 23); case 'I' => let max_n_digits = 2u; const hour = get_max_n_digits(&s_iter, max_n_digits); - b.hour = match (hour) { + mok.hour = match (hour) { case let hour: int => yield if (hour > 12) { yield clamp_int(hour - 12, 1, 12); @@ -129,38 +129,38 @@ export fn parse(b: *builder, layout: str, s: str) (void | errors::invalid) = { return errors::invalid; }; case 'j' => - b.yearday = clamp_int( + mok.yearday = clamp_int( get_max_n_digits(&s_iter, 3)?, 1, 366); case 'm' => - b.month = clamp_int( + mok.month = clamp_int( get_max_n_digits(&s_iter, 2)?, 1, 12); case 'M' => - b.min = clamp_int( + mok.min = clamp_int( get_max_n_digits(&s_iter, 2)?, 0, 59); case 'N' => - b.nsec = clamp_int( + mok.nsec = clamp_int( get_max_n_digits(&s_iter, 3)?, 0, 999); case 'p' => - if (b.hour is void) { + if (mok.hour is void) { // We can't change the hour's am/pm because we // have no hour. return errors::invalid; }; const rest = strings::iter_str(&s_iter); if (strings::hasprefix(rest, "AM")) { - if (b.hour as int > 12) { + if (mok.hour as int > 12) { // 13 AM? return errors::invalid; - } else if (b.hour as int == 12) { - b.hour = 0; + } else if (mok.hour as int == 12) { + mok.hour = 0; }; } else if (strings::hasprefix(rest, "PM")) { - if (b.hour as int > 12) { + if (mok.hour as int > 12) { // 13 PM? return errors::invalid; - } else if (b.hour as int < 12) { - b.hour = - (b.hour as int) + 12; + } else if (mok.hour as int < 12) { + mok.hour = + (mok.hour as int) + 12; }; } else { return errors::invalid; @@ -168,10 +168,10 @@ export fn parse(b: *builder, layout: str, s: str) (void | errors::invalid) = { strings::next(&s_iter); strings::next(&s_iter); case 'S' => - b.sec = clamp_int( + mok.sec = clamp_int( get_max_n_digits(&s_iter, 2)?, 0, 61); case 'u', 'w' => - b.weekday = match (get_max_n_digits(&s_iter, 1)) { + mok.weekday = match (get_max_n_digits(&s_iter, 1)) { case let i: int => yield if (format_r == 'w') { yield if (i == 0) { @@ -186,10 +186,10 @@ export fn parse(b: *builder, layout: str, s: str) (void | errors::invalid) = { return errors::invalid; }; case 'U', 'W' => - b.week = clamp_int( + mok.week = clamp_int( get_max_n_digits(&s_iter, 2)?, 0, 53); case 'Y' => - b.year = get_max_n_digits(&s_iter, 4)?; + mok.year = get_max_n_digits(&s_iter, 4)?; case 'z' => // TODO continue;