Support some, ok and err wrappers and untyped function inputs
Track the LScript changes since the grammar was first written: - some, ok and err are reserved prefix operators and wrapper patterns; the old |.some/|.ok/|.err forms and postfix/prefix ! are rejected - function literal inputs may omit their type annotation, and tuple components may omit theirs; an empty typed tuple pattern is now just the unit pattern - migrate fixtures, corpus, queries, CST contract and Rust tests Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6e4f9cd65a
commit
61ea7166e3
20 files changed
+36187
-35406
No files matched your search
+207
-7
@@ -36,7 +36,7 @@ Point :: &{
|
||||
y: i32 = 0
|
||||
}
|
||||
point := &Point.{ x: 1; y = 2 }
|
||||
maybe: []{i32?}: [nil, 1->|.some]
|
||||
maybe: []{i32?}: [nil, some 1]
|
||||
|
||||
---
|
||||
|
||||
@@ -75,13 +75,9 @@ maybe: []{i32?}: [nil, 1->|.some]
|
||||
(collection_entry
|
||||
(nil_literal))
|
||||
(collection_entry
|
||||
(pipeline_expression
|
||||
(unary_expression
|
||||
(signed_numeric_literal
|
||||
(integer_literal))
|
||||
(direct_pipeline_stage
|
||||
(pipeline_callable
|
||||
(sum_constructor
|
||||
(identifier)))))))))
|
||||
(integer_literal)))))))
|
||||
|
||||
==================
|
||||
Control flow and patterns
|
||||
@@ -157,6 +153,7 @@ ready :: first
|
||||
(binary_expression
|
||||
(identifier)
|
||||
(identifier))))
|
||||
|
||||
==================
|
||||
Compact range after an integer
|
||||
==================
|
||||
@@ -233,3 +230,206 @@ unit :: @ { () }
|
||||
(function_literal
|
||||
(block
|
||||
(unit_literal)))))
|
||||
|
||||
==================
|
||||
Whole-argument function patterns
|
||||
==================
|
||||
|
||||
pair :: @ (left, right): (i32, i32) { left + right }
|
||||
product :: @ {value}: &{value: i32:} { value }
|
||||
callback :: @ operation: {i32 -> i32} { 1->operation }
|
||||
unit :: @ () -> i32 { 42 }
|
||||
|
||||
---
|
||||
|
||||
(source_file
|
||||
(declaration
|
||||
(identifier)
|
||||
(function_literal
|
||||
(typed_parameter
|
||||
(tuple_pattern
|
||||
(pattern
|
||||
(identifier))
|
||||
(pattern
|
||||
(identifier)))
|
||||
(tuple_type
|
||||
(primitive_type)
|
||||
(primitive_type)))
|
||||
(block
|
||||
(binary_expression
|
||||
(identifier)
|
||||
(identifier)))))
|
||||
(declaration
|
||||
(identifier)
|
||||
(function_literal
|
||||
(typed_parameter
|
||||
(product_pattern
|
||||
(product_pattern_field
|
||||
(identifier)))
|
||||
(product_type
|
||||
(product_field
|
||||
(identifier)
|
||||
(primitive_type))))
|
||||
(block
|
||||
(identifier))))
|
||||
(declaration
|
||||
(identifier)
|
||||
(function_literal
|
||||
(typed_parameter
|
||||
(identifier)
|
||||
(grouped_type
|
||||
(function_type
|
||||
(primitive_type)
|
||||
(primitive_type))))
|
||||
(block
|
||||
(pipeline_expression
|
||||
(signed_numeric_literal
|
||||
(integer_literal))
|
||||
(direct_pipeline_stage
|
||||
(pipeline_callable
|
||||
(identifier)))))))
|
||||
(declaration
|
||||
(identifier)
|
||||
(function_literal
|
||||
(typed_parameter
|
||||
(unit_pattern))
|
||||
(primitive_type)
|
||||
(block
|
||||
(signed_numeric_literal
|
||||
(integer_literal))))))
|
||||
|
||||
==================
|
||||
Optional and result wrappers
|
||||
==================
|
||||
|
||||
wrap :: @ value: i32 -> i32 ! str { ok value }
|
||||
first :: @ items: []i32 -> i32? {
|
||||
match items {
|
||||
[] => nil
|
||||
[head, ...rest] => some head
|
||||
}
|
||||
}
|
||||
unwrap :: @ result: i32 ! str -> i32 {
|
||||
match result {
|
||||
ok value => value
|
||||
err _ => 0
|
||||
}
|
||||
}
|
||||
|
||||
---
|
||||
|
||||
(source_file
|
||||
(declaration
|
||||
(identifier)
|
||||
(function_literal
|
||||
(typed_parameter
|
||||
(identifier)
|
||||
(primitive_type))
|
||||
(result_type
|
||||
(primitive_type)
|
||||
(primitive_type))
|
||||
(block
|
||||
(unary_expression
|
||||
(identifier)))))
|
||||
(declaration
|
||||
(identifier)
|
||||
(function_literal
|
||||
(typed_parameter
|
||||
(identifier)
|
||||
(array_type
|
||||
(primitive_type)))
|
||||
(optional_type
|
||||
(primitive_type))
|
||||
(block
|
||||
(match_expression
|
||||
(identifier)
|
||||
(match_arm
|
||||
(pattern
|
||||
(array_pattern))
|
||||
(nil_literal))
|
||||
(match_arm
|
||||
(pattern
|
||||
(array_pattern
|
||||
(array_pattern_entry
|
||||
(pattern
|
||||
(identifier)))
|
||||
(array_pattern_entry
|
||||
(identifier))))
|
||||
(unary_expression
|
||||
(identifier)))))))
|
||||
(declaration
|
||||
(identifier)
|
||||
(function_literal
|
||||
(typed_parameter
|
||||
(identifier)
|
||||
(result_type
|
||||
(primitive_type)
|
||||
(primitive_type)))
|
||||
(primitive_type)
|
||||
(block
|
||||
(match_expression
|
||||
(identifier)
|
||||
(match_arm
|
||||
(pattern
|
||||
(wrapper_pattern
|
||||
(identifier)))
|
||||
(identifier))
|
||||
(match_arm
|
||||
(pattern
|
||||
(wrapper_pattern))
|
||||
(signed_numeric_literal
|
||||
(integer_literal))))))))
|
||||
|
||||
==================
|
||||
Untyped function inputs
|
||||
==================
|
||||
|
||||
double :: @ value { value * 2 }
|
||||
sum: (i32, i32) -> i32: @ (left, right) { left + right }
|
||||
mixed :: @ (left: i32, right) -> i32 { left + right }
|
||||
|
||||
---
|
||||
|
||||
(source_file
|
||||
(declaration
|
||||
(identifier)
|
||||
(function_literal
|
||||
(typed_parameter
|
||||
(identifier))
|
||||
(block
|
||||
(binary_expression
|
||||
(identifier)
|
||||
(signed_numeric_literal
|
||||
(integer_literal))))))
|
||||
(declaration
|
||||
(identifier)
|
||||
(function_type
|
||||
(tuple_type
|
||||
(primitive_type)
|
||||
(primitive_type))
|
||||
(primitive_type))
|
||||
(function_literal
|
||||
(typed_parameter
|
||||
(tuple_pattern
|
||||
(pattern
|
||||
(identifier))
|
||||
(pattern
|
||||
(identifier))))
|
||||
(block
|
||||
(binary_expression
|
||||
(identifier)
|
||||
(identifier)))))
|
||||
(declaration
|
||||
(identifier)
|
||||
(function_literal
|
||||
(typed_tuple_pattern
|
||||
(typed_tuple_component
|
||||
(identifier)
|
||||
(primitive_type))
|
||||
(typed_tuple_component
|
||||
(identifier)))
|
||||
(primitive_type)
|
||||
(block
|
||||
(binary_expression
|
||||
(identifier)
|
||||
(identifier))))))
|
||||
@@ -1 +0,0 @@
|
||||
identity :: @ value { value }
|
||||
+1
@@ -0,0 +1 @@
|
||||
value :: @ x: i32 -> i32 ! str { x! }
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
value :: @ x: str -> i32 ! str { !x }
|
||||
@@ -0,0 +1 @@
|
||||
ok :: @ value { value }
|
||||
@@ -0,0 +1 @@
|
||||
some := 1
|
||||
@@ -0,0 +1 @@
|
||||
value: i32?: 1->|.some
|
||||
@@ -0,0 +1 @@
|
||||
value :: @ x: i32? { match x { |.some y => y; nil => 0 } }
|
||||
Vendored
+1
-1
@@ -7,7 +7,7 @@ more_codes := [...codes, 'failed' => 500]
|
||||
empty_numbers: []i32: []
|
||||
empty_codes: [str]i32: []
|
||||
optional_numbers: []i32?: nil
|
||||
optional_elements: []{i32?}: [nil, 1->|.some]
|
||||
optional_elements: []{i32?}: [nil, some 1]
|
||||
|
||||
Options :: &{
|
||||
name: str: 'default'
|
||||
|
||||
Vendored
+1
-1
@@ -11,7 +11,7 @@ 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 value->|.some
|
||||
return some value
|
||||
}
|
||||
}
|
||||
nil
|
||||
|
||||
Vendored
+2
-2
@@ -15,9 +15,9 @@ commented := 'value' # an inline comment preserves continuation
|
||||
computed := 1 + 2
|
||||
->to_str
|
||||
|
||||
fallible: i32 -> i32 ! Error: @ value: i32 { value! }
|
||||
fallible: i32 -> i32 ! Error: @ value: i32 { ok value }
|
||||
|
||||
propagate: i32 -> i32 ! Error: @ value: i32 {
|
||||
result := {value->fallible}?
|
||||
result!
|
||||
ok result
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
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
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user