add generics

This commit is contained in:
paul committed 2026-10-23 02:13:30 -04:00
1 parent 79533e9537
commit 2204d58102
22 files changed
+47358 -37579

No files matched your search

+240 -16
View File
@@ -36,7 +36,7 @@ Point :: &{
y: i32 = 0
}
point := &Point.{ x: 1; y = 2 }
maybe: []{i32?}: [nil, some 1]
maybe: []{i32?}: [|.none, 1->|.some]
---
@@ -73,11 +73,16 @@ maybe: []{i32?}: [nil, some 1]
(primitive_type))))
(collection_literal
(collection_entry
(nil_literal))
(sum_constructor
(identifier)))
(collection_entry
(unary_expression
(pipeline_expression
(signed_numeric_literal
(integer_literal)))))))
(integer_literal))
(direct_pipeline_stage
(pipeline_callable
(sum_constructor
(identifier)))))))))
==================
Control flow and patterns
@@ -302,17 +307,17 @@ unit :: @ () -> i32 { 42 }
Optional and result wrappers
==================
wrap :: @ value: i32 -> i32 ! str { ok value }
wrap :: @ value: i32 -> i32 ! str { value->|.ok }
first :: @ items: []i32 -> i32? {
match items {
[] => nil
[head, ...rest] => some head
[] => |.none
[head, ...rest] => head->|.some
}
}
unwrap :: @ result: i32 ! str -> i32 {
match result {
ok value => value
err _ => 0
|.ok value => value
|.err _ => 0
}
}
@@ -329,8 +334,12 @@ unwrap :: @ result: i32 ! str -> i32 {
(primitive_type)
(primitive_type))
(block
(unary_expression
(identifier)))))
(pipeline_expression
(identifier)
(direct_pipeline_stage
(pipeline_callable
(sum_constructor
(identifier))))))))
(declaration
(identifier)
(function_literal
@@ -346,7 +355,8 @@ unwrap :: @ result: i32 ! str -> i32 {
(match_arm
(pattern
(array_pattern))
(nil_literal))
(sum_constructor
(identifier)))
(match_arm
(pattern
(array_pattern
@@ -355,8 +365,12 @@ unwrap :: @ result: i32 ! str -> i32 {
(identifier)))
(array_pattern_entry
(identifier))))
(unary_expression
(identifier)))))))
(pipeline_expression
(identifier)
(direct_pipeline_stage
(pipeline_callable
(sum_constructor
(identifier))))))))))
(declaration
(identifier)
(function_literal
@@ -371,12 +385,14 @@ unwrap :: @ result: i32 ! str -> i32 {
(identifier)
(match_arm
(pattern
(wrapper_pattern
(sum_pattern
(identifier)
(identifier)))
(identifier))
(match_arm
(pattern
(wrapper_pattern))
(sum_pattern
(identifier)))
(signed_numeric_literal
(integer_literal))))))))
@@ -433,3 +449,211 @@ mixed :: @ (left: i32, right) -> i32 { left + right }
(binary_expression
(identifier)
(identifier))))))
==================
Postfix pipeline stages
==================
mixed :: -3->add_ten + 2 * 3->double - {1 + 1}->double
checked :: not c->is_digit
unwrapped :: input->fallible?->next
---
(source_file
(declaration
(identifier)
(binary_expression
(binary_expression
(unary_expression
(pipeline_expression
(signed_numeric_literal
(integer_literal))
(direct_pipeline_stage
(pipeline_callable
(identifier)))))
(binary_expression
(signed_numeric_literal
(integer_literal))
(pipeline_expression
(signed_numeric_literal
(integer_literal))
(direct_pipeline_stage
(pipeline_callable
(identifier))))))
(pipeline_expression
(block
(binary_expression
(signed_numeric_literal
(integer_literal))
(signed_numeric_literal
(integer_literal))))
(direct_pipeline_stage
(pipeline_callable
(identifier))))))
(declaration
(identifier)
(unary_expression
(pipeline_expression
(identifier)
(direct_pipeline_stage
(pipeline_callable
(identifier))))))
(declaration
(identifier)
(pipeline_expression
(identifier)
(direct_pipeline_stage
(pipeline_callable
(identifier)))
(direct_pipeline_stage
(pipeline_callable
(identifier))))))
==================
Continuations after same-line stages
==================
flat := value->first->second
->third
whole := a + b->f
->g
---
(source_file
(declaration
(identifier)
(pipeline_expression
(identifier)
(direct_pipeline_stage
(pipeline_callable
(identifier)))
(direct_pipeline_stage
(pipeline_callable
(identifier)))
(continuation_pipeline_stage
(pipeline_callable
(identifier)))))
(declaration
(identifier)
(pipeline_expression
(binary_expression
(identifier)
(pipeline_expression
(identifier)
(direct_pipeline_stage
(pipeline_callable
(identifier)))))
(continuation_pipeline_stage
(pipeline_callable
(identifier))))))
==================
Generic binders and constraints
==================
Show :: <>{
to_str: str
...Display
}
pick: <T: Show, (T, T): Ordered> (T, T) -> T: @ (first, second): (T, T) { first }
Card :: &{ label: Output<make_label: Show>: }
---
(source_file
(declaration
(identifier)
(type_definition
(constraint_body
(constraint_entry
(identifier)
(primitive_type))
(constraint_spread
(identifier)))))
(declaration
(identifier)
(binder
(binder_entry
(identifier)
(identifier))
(binder_entry
(tuple_type
(identifier)
(identifier))
(identifier)))
(function_type
(tuple_type
(identifier)
(identifier))
(identifier))
(function_literal
(typed_parameter
(tuple_pattern
(pattern
(identifier))
(pattern
(identifier)))
(tuple_type
(identifier)
(identifier)))
(block
(identifier))))
(declaration
(identifier)
(type_definition
(product_type
(product_field
(identifier)
(output_type
(identifier)
(identifier)
(identifier)))))))
==================
Import filters
==================
use math.{sqrt for f64, pi as p}
use .shapes.area for Circle
pub { area for Circle; sqrt }
---
(source_file
(use_declaration
(module_path
(identifier))
(import_member
(identifier)
(import_filter
(primitive_type)))
(import_member
(identifier)
(identifier)))
(use_declaration
(module_path
(qualified_name
(identifier)
(identifier)))
(import_filter
(identifier)))
(pub_declaration
(pub_entry
(identifier)
(import_filter
(identifier)))
(pub_entry
(identifier))))
==================
Reserved word
:error
==================
context := 1
---
-1
View File
@@ -1 +0,0 @@
value :: @ x: i32 -> i32 ! str { x! }
-1
View File
@@ -1 +0,0 @@
value :: @ x: str -> i32 ! str { !x }
-1
View File
@@ -1 +0,0 @@
ok :: @ value { value }
-1
View File
@@ -1 +0,0 @@
some := 1
-1
View File
@@ -1 +0,0 @@
value: i32?: 1->|.some
-1
View File
@@ -1 +0,0 @@
value :: @ x: i32? { match x { |.some y => y; nil => 0 } }
-1
View File
@@ -1 +0,0 @@
value :: 1 + 2->to_str
+2 -2
View File
@@ -6,8 +6,8 @@ more_codes := [...codes, 'failed' => 500]
empty_numbers: []i32: []
empty_codes: [str]i32: []
optional_numbers: []i32?: nil
optional_elements: []{i32?}: [nil, some 1]
optional_numbers: []i32?: |.none
optional_elements: []{i32?}: [|.none, 1->|.some]
Options :: &{
name: str: 'default'
+2 -2
View File
@@ -11,10 +11,10 @@ choose_block: (bit, i32, i32) -> i32: @ (condition: bit, when_true: i32, when_fa
first_positive: []i32 -> i32?: @ values: []i32 {
for value in values {
if value > 0 {
return some value
return value->|.some
}
}
nil
|.none
}
labelled_search: []i32 -> i32: @ values: []i32 { loop `outer {
+5
View File
@@ -0,0 +1,5 @@
add_ten: i32 -> i32: @ value: i32 { value + 10 }
double: i32 -> i32: @ value: i32 { value * 2 }
# Stages bind tighter than prefix and infix operators: -{3->add_ten} + 2 * {3->double}.
mixed: i32: -3->add_ten + 2 * 3->double - {1 + 1}->double
+2 -2
View File
@@ -15,9 +15,9 @@ commented := 'value' # an inline comment preserves continuation
computed := 1 + 2
->to_str
fallible: i32 -> i32 ! Error: @ value: i32 { ok value }
fallible: i32 -> i32 ! Error: @ value: i32 { value->|.ok }
propagate: i32 -> i32 ! Error: @ value: i32 {
result := {value->fallible}?
ok result
result->|.ok
}
-19
View File
@@ -1,19 +0,0 @@
use math.{pi, sqrt}
use math as m
use random.{Pcg32, pcg32, draw_u32}
use time.{Duration, TimeError, duration_neg}
use time as t
circle: f64: pi
root: f64: 4.0->sqrt
# Host overloads are selected by the operand, so the literal is typed first.
nine: f32: 9.0
qualified_root: f32: nine->m.sqrt
qualified_pi: f32: m.pi
seed: u64: 42
generator: Pcg32: seed->pcg32
draw: u32: generator->draw_u32
negate: Duration => Duration ! TimeError: duration_neg
qualified_negate: t.Duration => t.Duration ! t.TimeError: t.duration_neg
-24
View File
@@ -1,24 +0,0 @@
present: i32?: some 1
absent: i32?: nil
success: i32 ! str: ok 1
failure: i32 ! str: err 'failed'
piped: i32?: some 4->double
grouped: i32?: some {4->double}
double :: @ value { value * 2 }
describe :: @ (label: str, count) -> str { label }
unwrap_or: i32? -> i32: @ value {
match value {
some inner => inner
nil => 0
}
}
handle: i32 ! str -> i32: @ result {
match result {
ok value if value > 0 => value
ok _ => 0
err message => message->length
}
}