#! @file version.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the compiler's version, which a program chooses by in #! `when compiler >= 1.2.0` and reads as `core`'s `compiler()`, and the reading #! of a version as `when` writes one. #! #! A program sees the version of the compiler reading it, not the one it was #! written for, so a library read by an older compiler chooses what that #! compiler understands. That is how `core`, `system`, and `runtime` change #! without breaking the compiler of the release before, which builds the next. module sakura::analyzer; #! A version as `when` compares it: three numbers, compared in order. pub struct version_number : equatable, ordered { pub var major: u32; #! Changes where what a program means changes. pub var minor: u32; #! Changes where the language gains what it lacked. pub var patch: u32; #! Changes where only mistakes are fixed. } #! The compiler's version, the `semv` its blossomfile gives, which changes with #! it. pub const compiler_version: version_number = version_number { 1, 0, 0 }; #! Whether two versions are the same. #! @param this The version. #! @param other The other. #! @returns Whether they are. pub def version_number::equals(&this, other: &version_number): bool { return this.compare(other) == 0; } #! How a version stands to another, part by part. #! @param this The version. #! @param other The other. #! @returns Negative where it comes first, 0 where they are the same, and #! positive where it comes after. pub def version_number::compare(&this, other: &version_number): i64 { return this.major as i64 - other.major as i64 if this.major != other.major; return this.minor as i64 - other.minor as i64 if this.minor != other.minor; return this.patch as i64 - other.patch as i64; } #! The version as it is written, as `1.2.0`. #! @param this The version. #! @returns The text. pub def version_number::text(&this): string { return $"{this.major}.{this.minor}.{this.patch}"; } #! Reads a version as `when` writes one: one to three numbers joined by `.`, as #! `1`, `1.2`, or `1.2.0`, the parts left out counted as 0. #! @param text The text. #! @returns The version, or none where the text is no version. pub def read_version(text: strview): maybe { var parts: u32[3] = [0, 0, 0]; var count: u64 = 0; var digits: u64 = 0; for byte in text { if byte == '.' { return nothing {} if digits == 0 || count == 2; count++; digits = 0; continue; } return nothing {} if !byte.is_digit || parts[count] >= 100000000; parts[count] = parts[count] * 10 + (byte - '0') as u32; digits++; } return nothing {} if digits == 0; return version_number { parts[0], parts[1], parts[2] }; } #! Whether the compiler's version stands to one written as a comparison says, #! as `>=` says of `1.2.0` where the compiler is 1.2.0 or later. #! @param operator The comparison: `==`, `!=`, `<`, `<=`, `>`, or `>=`. #! @param other The version written. #! @returns Whether it does. pub def compiler_compares(operator: tk, other: &version_number): bool { val order := compiler_version.compare(other); return match operator { tk::equals => order == 0, tk::noteq => order != 0, tk::less => order < 0, tk::lesseq => order <= 0, tk::more => order > 0, tk::moreeq => order >= 0, else => false }; }