Skip to content

Commit dcd9cdc

Browse files
author
realorko
committed
SDK UUID
1 parent f6c49fb commit dcd9cdc

File tree

8 files changed

+1497
-49
lines changed

8 files changed

+1497
-49
lines changed

sdk/math.sn

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,14 @@ native fn fmin(x: double, y: double): double
7171
# Mathematical Constants
7272
# ==============================================================================
7373

74-
fn pi(): double =>
75-
return 3.14159265358979323846
76-
77-
fn e(): double =>
78-
return 2.71828182845904523536
79-
80-
fn tau(): double =>
81-
return 6.28318530717958647692
82-
83-
fn phi(): double =>
84-
return 1.61803398874989484820
85-
86-
fn sqrt2(): double =>
87-
return 1.41421356237309504880
88-
89-
fn sqrt3(): double =>
90-
return 1.73205080756887729352
91-
92-
fn ln2(): double =>
93-
return 0.69314718055994530942
94-
95-
fn ln10(): double =>
96-
return 2.30258509299404568402
74+
fn pi(): double => 3.14159265358979323846
75+
fn e(): double => 2.71828182845904523536
76+
fn tau(): double => 6.28318530717958647692
77+
fn phi(): double => 1.61803398874989484820
78+
fn sqrt2(): double => 1.41421356237309504880
79+
fn sqrt3(): double => 1.73205080756887729352
80+
fn ln2(): double => 0.69314718055994530942
81+
fn ln10(): double => 2.30258509299404568402
9782

9883
# ==============================================================================
9984
# Integer Helper Functions
@@ -411,29 +396,14 @@ native fn fminF(x: float, y: float): float
411396
# Mathematical Constants (float)
412397
# ==============================================================================
413398

414-
fn piF(): float =>
415-
return 3.14159265
416-
417-
fn eF(): float =>
418-
return 2.71828182
419-
420-
fn tauF(): float =>
421-
return 6.28318530
422-
423-
fn phiF(): float =>
424-
return 1.61803398
425-
426-
fn sqrt2F(): float =>
427-
return 1.41421356
428-
429-
fn sqrt3F(): float =>
430-
return 1.73205080
431-
432-
fn ln2F(): float =>
433-
return 0.69314718
434-
435-
fn ln10F(): float =>
436-
return 2.30258509
399+
fn piF(): float => 3.14159265
400+
fn eF(): float => 2.71828182
401+
fn tauF(): float => 6.28318530
402+
fn phiF(): float => 1.61803398
403+
fn sqrt2F(): float => 1.41421356
404+
fn sqrt3F(): float => 1.73205080
405+
fn ln2F(): float => 0.69314718
406+
fn ln10F(): float => 2.30258509
437407

438408
# ==============================================================================
439409
# Float Helper Functions

sdk/uuid.sn

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# ==============================================================================
2+
# sdk/uuid.sn - UUID Type for Sindarin
3+
# ==============================================================================
4+
# Provides a UUID type for generating and manipulating Universally Unique
5+
# Identifiers. Supports v4 (random), v5 (SHA-1 hash), and v7 (time-ordered).
6+
# Drop-in replacement for the builtin UUID type.
7+
#
8+
# Usage:
9+
# import "sdk/uuid"
10+
#
11+
# var id: SnUUID = SnUUID.create()
12+
# print($"UUID: {id.toString()}\n")
13+
# print($"Version: {id.version()}\n")
14+
#
15+
# var v5id: SnUUID = SnUUID.v5(SnUUID.namespaceUrl(), "https://example.com")
16+
#
17+
# ==============================================================================
18+
19+
# Self-contained implementation - compile and link our own C source
20+
#pragma source "uuid.sn.c"
21+
22+
# ==============================================================================
23+
# SnUUID Native Struct Definition
24+
# ==============================================================================
25+
# Uses #pragma alias to map SnUUID to RtUuid in the runtime.
26+
# The 'as ref' modifier means native methods receive self by pointer.
27+
# SnUUID represents an opaque handle type (like built-in UUID).
28+
29+
#pragma alias "RtUuid"
30+
native struct SnUUID as ref =>
31+
#pragma alias "high"
32+
_high: long
33+
34+
#pragma alias "low"
35+
_low: long
36+
37+
# ==========================================================================
38+
# Static Factory Methods - UUID Generation
39+
# ==========================================================================
40+
41+
# Create a UUIDv7 (time-ordered) - recommended default
42+
static fn create(): SnUUID =>
43+
return sn_uuid_create(arena)
44+
45+
# Alias for create() - matches common convention
46+
static fn new(): SnUUID =>
47+
return sn_uuid_create(arena)
48+
49+
# Generate a UUIDv4 (random)
50+
static fn v4(): SnUUID =>
51+
return sn_uuid_v4(arena)
52+
53+
# Generate a UUIDv5 (SHA-1 hash of namespace + name)
54+
static fn v5(namespace: SnUUID, name: str): SnUUID =>
55+
return sn_uuid_v5(arena, namespace, name)
56+
57+
# Generate a UUIDv7 (timestamp + random)
58+
static fn v7(): SnUUID =>
59+
return sn_uuid_v7(arena)
60+
61+
# ==========================================================================
62+
# Static Factory Methods - Parsing
63+
# ==========================================================================
64+
65+
# Parse from standard 36-char format (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
66+
static fn fromString(s: str): SnUUID =>
67+
return sn_uuid_from_string(arena, s)
68+
69+
# Parse from 32-char hex format (no dashes)
70+
static fn fromHex(s: str): SnUUID =>
71+
return sn_uuid_from_hex(arena, s)
72+
73+
# Parse from 22-char URL-safe base64 format
74+
static fn fromBase64(s: str): SnUUID =>
75+
return sn_uuid_from_base64(arena, s)
76+
77+
# ==========================================================================
78+
# Static Factory Methods - Special UUIDs
79+
# ==========================================================================
80+
81+
# Get nil UUID (all zeros: 00000000-0000-0000-0000-000000000000)
82+
static fn zero(): SnUUID =>
83+
return sn_uuid_nil(arena)
84+
85+
# Get max UUID (all ones: ffffffff-ffff-ffff-ffff-ffffffffffff)
86+
static fn max(): SnUUID =>
87+
return sn_uuid_max(arena)
88+
89+
# ==========================================================================
90+
# Static Factory Methods - Namespace Constants
91+
# ==========================================================================
92+
93+
# Get DNS namespace UUID (6ba7b810-9dad-11d1-80b4-00c04fd430c8)
94+
static fn namespaceDns(): SnUUID =>
95+
return sn_uuid_namespace_dns(arena)
96+
97+
# Get URL namespace UUID (6ba7b811-9dad-11d1-80b4-00c04fd430c8)
98+
static fn namespaceUrl(): SnUUID =>
99+
return sn_uuid_namespace_url(arena)
100+
101+
# Get OID namespace UUID (6ba7b812-9dad-11d1-80b4-00c04fd430c8)
102+
static fn namespaceOid(): SnUUID =>
103+
return sn_uuid_namespace_oid(arena)
104+
105+
# Get X.500 namespace UUID (6ba7b814-9dad-11d1-80b4-00c04fd430c8)
106+
static fn namespaceX500(): SnUUID =>
107+
return sn_uuid_namespace_x500(arena)
108+
109+
# ==========================================================================
110+
# Property Getters (native - pass self by pointer via 'as ref')
111+
# ==========================================================================
112+
113+
# Get UUID version (1-8)
114+
#pragma alias "sn_uuid_get_version"
115+
native fn version(): int
116+
117+
# Get UUID variant
118+
#pragma alias "sn_uuid_get_variant"
119+
native fn variant(): int
120+
121+
# Check if UUID is nil (all zeros)
122+
#pragma alias "sn_uuid_is_nil"
123+
native fn isNil(): bool
124+
125+
# ==========================================================================
126+
# Time Extraction (v7 only)
127+
# ==========================================================================
128+
129+
# Get Unix timestamp in milliseconds (v7 only, throws on non-v7)
130+
#pragma alias "sn_uuid_get_timestamp"
131+
native fn timestamp(): long
132+
133+
# ==========================================================================
134+
# Conversion Methods (non-native - need arena for string allocation)
135+
# ==========================================================================
136+
137+
# Format as standard 36-char string (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
138+
fn toString(): str =>
139+
return sn_uuid_to_string(arena, self)
140+
141+
# Format as 32-char hex string (no dashes)
142+
fn toHex(): str =>
143+
return sn_uuid_to_hex(arena, self)
144+
145+
# Format as 22-char URL-safe base64 string
146+
fn toBase64(): str =>
147+
return sn_uuid_to_base64(arena, self)
148+
149+
# ==========================================================================
150+
# Comparison Methods (native - pass self by pointer)
151+
# ==========================================================================
152+
153+
# Check if two UUIDs are equal
154+
#pragma alias "sn_uuid_equals"
155+
native fn equals(other: SnUUID): bool
156+
157+
# Compare two UUIDs (returns -1, 0, or 1)
158+
#pragma alias "sn_uuid_compare"
159+
native fn compare(other: SnUUID): int
160+
161+
# Check if uuid < other
162+
#pragma alias "sn_uuid_is_less_than"
163+
native fn isLessThan(other: SnUUID): bool
164+
165+
# Check if uuid > other
166+
#pragma alias "sn_uuid_is_greater_than"
167+
native fn isGreaterThan(other: SnUUID): bool
168+
169+
# ==============================================================================
170+
# Runtime Function Declarations
171+
# ==============================================================================
172+
# These declare the runtime functions directly.
173+
# SnUUID is an opaque handle type that generates as RtUuid* in C.
174+
175+
# Factory functions - return SnUUID (which is RtUuid*)
176+
native fn sn_uuid_create(a: *void): SnUUID
177+
native fn sn_uuid_v4(a: *void): SnUUID
178+
native fn sn_uuid_v5(a: *void, namespace: SnUUID, name: str): SnUUID
179+
native fn sn_uuid_v7(a: *void): SnUUID
180+
181+
# Parsing functions - return SnUUID
182+
native fn sn_uuid_from_string(a: *void, s: str): SnUUID
183+
native fn sn_uuid_from_hex(a: *void, s: str): SnUUID
184+
native fn sn_uuid_from_base64(a: *void, s: str): SnUUID
185+
186+
# Special UUIDs
187+
native fn sn_uuid_nil(a: *void): SnUUID
188+
native fn sn_uuid_max(a: *void): SnUUID
189+
190+
# Namespace UUIDs
191+
native fn sn_uuid_namespace_dns(a: *void): SnUUID
192+
native fn sn_uuid_namespace_url(a: *void): SnUUID
193+
native fn sn_uuid_namespace_oid(a: *void): SnUUID
194+
native fn sn_uuid_namespace_x500(a: *void): SnUUID
195+
196+
# Conversion functions - take SnUUID (which is RtUuid*)
197+
native fn sn_uuid_to_string(a: *void, u: SnUUID): str
198+
native fn sn_uuid_to_hex(a: *void, u: SnUUID): str
199+
native fn sn_uuid_to_base64(a: *void, u: SnUUID): str

0 commit comments

Comments
 (0)