Skip to content

Commit 91529eb

Browse files
author
Eric Hartford
committed
progress
1 parent e4df9c8 commit 91529eb

47 files changed

Lines changed: 1514 additions & 2248 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/sema_codegen_ownership.md

Lines changed: 101 additions & 93 deletions
Large diffs are not rendered by default.

lib/std/async.w

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// once up front into an internal Vec for stable ordering and cancellation
66
// bookkeeping.
77

8+
use std.collections
9+
use std.result
10+
811
/// Await all tasks. Returns Vec[T] in input order.
912
/// Fails fast on first Err.
1013
pub async fn await_all[T, E](tasks: impl IntoIter[Task[Result[T, E]]]) -> Result[Vec[T], E]:

lib/std/builtins.w

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// std.builtins — ambient user-facing names provided by the prelude.
2+
//
3+
// These are ordinary std declarations. The compiler may still lower some of
4+
// them with intrinsic semantics later, but name resolution should happen
5+
// through imports, not through hardcoded undefined-name allowlists.
6+
7+
extern fn print(s: str) -> void
8+
extern fn with_println_str(s: str) -> void
9+
extern fn with_println_i32(n: i32) -> void
10+
extern fn with_println_i64(n: i64) -> void
11+
extern fn with_println_bool(v: bool) -> void
12+
extern fn with_panic(msg: str, file: str, line: i32) -> void
13+
14+
pub fn println(s: str) -> void:
15+
with_println_str(s)
16+
17+
pub fn println_i32(n: i32) -> void:
18+
with_println_i32(n)
19+
20+
pub fn println_i64(n: i64) -> void:
21+
with_println_i64(n)
22+
23+
pub fn println_bool(v: bool) -> void:
24+
with_println_bool(v)
25+
26+
pub fn assert(cond: bool) -> void:
27+
if not cond:
28+
with_panic("assertion failed", "", 0)

lib/std/collections.w

Lines changed: 15 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,21 @@
1-
// std.collections — collection conveniences in pure With.
1+
// std.collections — collection type surface imported by the prelude.
22
//
3-
// Core collection types (Vec, HashMap, HashSet) are language built-ins.
4-
// This module adds generic helper functions over those built-ins.
5-
6-
pub fn update(map: HashMap[str, i32], key: str, default_value: i32, f: fn(i32) -> i32) -> void:
7-
map.update(key, default_value, f)
8-
9-
pub fn increment(map: HashMap[str, i32], key: str) -> void:
10-
map.increment(key)
11-
12-
pub fn decrement(map: HashMap[str, i32], key: str) -> void:
13-
map.decrement(key)
14-
15-
pub fn append(map: HashMap[str, Vec[i32]], key: str, value: i32) -> void:
16-
map.append(key, value)
17-
18-
pub fn sequence_option(xs: Vec[?i32]) -> ?Vec[i32]:
19-
xs.sequence()
20-
21-
pub fn sequence_result(xs: Vec[Result[i32, i32]]) -> Result[Vec[i32], i32]:
22-
xs.sequence()
23-
24-
// --- Additional collections (concrete scaffolding) ---
25-
26-
extern fn with_i32_to_str(n: i32) -> str
27-
28-
type Handle = {
29-
key: str,
30-
generation: i32,
3+
// Keep this module intentionally minimal for selfhost compatibility.
4+
// The compiler still owns the lowering/runtime behavior for these
5+
// collection types; this module provides the user-facing names so they
6+
// resolve through normal imports instead of hardcoded sema allowlists.
7+
8+
type Vec[T] = {
9+
ptr: *const T,
10+
len: i64,
11+
cap: i64,
12+
elem_size: i64,
3113
}
3214

33-
type SlotMap = {
34-
generations: HashMap[str, i32],
35-
values: HashMap[str, i32],
15+
type HashMap[K, V] = {
16+
ptr: *const i8,
3617
}
3718

38-
pub fn slotmap_new -> SlotMap:
39-
SlotMap {
40-
generations: HashMap.new(),
41-
values: HashMap.new(),
42-
}
43-
44-
pub fn slotmap_insert(map: SlotMap, value: i32) -> (SlotMap, Handle):
45-
let key = map.generations.len() as i32 |> with_i32_to_str
46-
let generation_value = map.generations.get(key).unwrap_or(0) + 1
47-
map.generations.insert(key, generation_value)
48-
map.values.insert(key, value)
49-
(map, Handle { key: key, generation: generation_value })
50-
51-
pub fn slotmap_get(map: SlotMap, h: Handle) -> ?i32:
52-
let g = map.generations.get(h.key)
53-
if g.is_none() then map.values.get("__with_slotmap_absent__")
54-
else if g.unwrap() != h.generation then map.values.get("__with_slotmap_absent__")
55-
else map.values.get(h.key)
56-
57-
pub fn slotmap_contains(map: SlotMap, h: Handle) -> bool:
58-
let g = map.generations.get(h.key)
59-
if g.is_none() then false
60-
else if g.unwrap() != h.generation then false
61-
else map.values.get(h.key).is_some()
62-
63-
pub fn slotmap_remove(map: SlotMap, h: Handle) -> (SlotMap, bool):
64-
let g = map.generations.get(h.key)
65-
if g.is_none() then (map, false)
66-
else if g.unwrap() != h.generation then (map, false)
67-
else
68-
let removed = map.values.remove(h.key)
69-
if removed:
70-
map.generations.insert(h.key, h.generation + 1)
71-
(map, removed)
72-
73-
pub fn slotmap_len(map: SlotMap) -> i32:
74-
map.values.len() as i32
75-
76-
type BTreeMap = {
77-
inner: HashMap[str, i32],
19+
type HashSet[T] = {
20+
ptr: *const i8,
7821
}
79-
80-
pub fn btree_new -> BTreeMap:
81-
BTreeMap { inner: HashMap.new() }
82-
83-
pub fn btree_insert(map: BTreeMap, key: str, value: i32) -> BTreeMap:
84-
map.inner.insert(key, value)
85-
map
86-
87-
pub fn btree_get(map: BTreeMap, key: str) -> ?i32:
88-
map.inner.get(key)
89-
90-
pub fn btree_contains(map: BTreeMap, key: str) -> bool:
91-
map.inner.contains(key)
92-
93-
pub fn btree_remove(map: BTreeMap, key: str) -> (BTreeMap, bool):
94-
let removed = map.inner.remove(key)
95-
(map, removed)
96-
97-
pub fn btree_len(map: BTreeMap) -> i64:
98-
map.inner.len()

lib/std/iter.w

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// Helper functions for common iterator patterns.
44

5+
use std.collections
6+
57
extern fn with_vec_len(v: &Vec[i32]) -> i64
68
extern fn with_vec_get_i32(v: &Vec[i32], index: i64) -> i32
79
extern fn with_vec_get_str(v: &Vec[str], index: i64) -> str

lib/std/option.w

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,7 @@
1-
// std.option — Option wrappers over built-in combinators.
1+
// std.option — Option type surface imported by the prelude.
22
//
3-
// Note: wrappers are currently concrete for i32 payloads due generic
4-
// signature limitations in the current Sema path.
3+
// Keep this module minimal for selfhost compatibility. The compiler owns
4+
// the lowering/runtime behavior for option methods and constructors; this
5+
// module provides the user-facing type name so it resolves by import.
56

6-
pub fn is_some(opt: ?i32) -> bool:
7-
opt.is_some()
8-
9-
pub fn is_none(opt: ?i32) -> bool:
10-
opt.is_none()
11-
12-
pub fn unwrap_or(opt: ?i32, fallback: i32) -> i32:
13-
opt.unwrap_or(fallback)
14-
15-
pub fn map(opt: ?i32, f: fn(i32) -> i32) -> ?i32:
16-
opt.map(f)
17-
18-
pub fn and_then(opt: ?i32, f: fn(i32) -> ?i32) -> ?i32:
19-
opt.and_then(f)
20-
21-
pub fn or_else(opt: ?i32, f: fn() -> ?i32) -> ?i32:
22-
opt.or_else(f)
23-
24-
pub fn filter(opt: ?i32, pred: fn(i32) -> bool) -> ?i32:
25-
opt.filter(pred)
26-
27-
pub fn zip(a: ?i32, b: ?i32) -> ?(i32, i32):
28-
a.zip(b)
29-
30-
pub fn cloned(opt: ?i32) -> ?i32:
31-
opt.cloned()
32-
33-
pub fn transpose(opt: ?Result[i32, str]) -> Result[?i32, str]:
34-
opt.transpose()
7+
type Option[T] = | Some(T) | None

lib/std/prelude.w

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
use std.math
77
use std.string
8+
use std.builtins
9+
use std.collections
10+
use std.option
11+
use std.result
812
use std.iter
913
use std.fs
1014
use std.process

lib/std/prelude_core.w

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22

33
use std.string
44
use std.math
5+
use std.builtins
6+
use std.collections
7+
use std.option
8+
use std.result

lib/std/process.w

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// Provides process-level operations wrapping C stdlib.
44

5+
use std.collections
6+
57
extern fn exit(code: i32) -> void
68
extern fn getpid() -> i32
79
extern fn system(cmd: *const i8) -> i32

lib/std/result.w

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,12 @@
1-
// std.result — Result wrappers over built-in combinators.
1+
// std.result — Result type surface imported by the prelude.
22
//
3-
// Note: wrappers are currently concrete due generic signature limitations
4-
// in the current Sema path.
3+
// Keep this module minimal for selfhost compatibility. The compiler owns
4+
// the lowering/runtime behavior for result methods and constructors; this
5+
// module provides the user-facing type names so they resolve by import.
56

6-
pub fn is_ok(r: Result[i32, i32]) -> bool:
7-
r.is_ok()
7+
type Result[T, E] = | Ok(T) | Err(E)
88

9-
pub fn is_err(r: Result[i32, i32]) -> bool:
10-
r.is_err()
11-
12-
pub fn unwrap_or(r: Result[i32, i32], fallback: i32) -> i32:
13-
r.unwrap_or(fallback)
14-
15-
pub fn map(r: Result[i32, i32], f: fn(i32) -> i32) -> Result[i32, i32]:
16-
r.map(f)
17-
18-
pub fn map_err(r: Result[i32, i32], f: fn(i32) -> i32) -> Result[i32, i32]:
19-
r.map_err(f)
20-
21-
pub fn and_then(r: Result[i32, i32], f: fn(i32) -> Result[i32, i32]) -> Result[i32, i32]:
22-
r.and_then(f)
23-
24-
pub fn or_else(r: Result[i32, i32], f: fn(i32) -> Result[i32, i64]) -> Result[i32, i64]:
25-
r.or_else(f)
26-
27-
pub fn ok(r: Result[i32, i32]) -> ?i32:
28-
r.ok()
29-
30-
pub fn err(r: Result[i32, i32]) -> ?i32:
31-
r.err()
32-
33-
pub fn transpose(r: Result[?i32, str]) -> ?Result[i32, str]:
34-
r.transpose()
35-
36-
pub fn context(r: Result[i32, i32], msg: str) -> Result[i32, ContextError[i32]]:
37-
r.context(msg)
38-
39-
pub fn with_context(r: Result[i32, i32], f: fn() -> str) -> Result[i32, ContextError[i32]]:
40-
r.with_context(f)
9+
type ContextError[E] = {
10+
msg: str
11+
source: E
12+
}

0 commit comments

Comments
 (0)