feat(pbs): implement first-class functions and explicit captures
This commit is contained in:
@@ -163,7 +163,6 @@ BarrelItem ::= BarrelFnItem
|
||||
| BarrelServiceItem
|
||||
| BarrelGlobalItem
|
||||
| BarrelConstItem
|
||||
| BarrelCallbackItem
|
||||
BarrelVisibility ::= 'mod' | 'pub'
|
||||
|
||||
BarrelFnItem ::= BarrelVisibility 'fn' Identifier ParamList ReturnAnn? ';'
|
||||
@@ -175,7 +174,6 @@ BarrelEnumItem ::= BarrelVisibility 'enum' Identifier ';'
|
||||
BarrelServiceItem ::= BarrelVisibility 'service' Identifier ';'
|
||||
BarrelGlobalItem ::= BarrelVisibility 'global' Identifier ';'
|
||||
BarrelConstItem ::= BarrelVisibility 'const' Identifier ';'
|
||||
BarrelCallbackItem ::= BarrelVisibility 'callback' Identifier ';'
|
||||
```
|
||||
|
||||
Rules:
|
||||
@@ -191,7 +189,6 @@ Rules:
|
||||
- Multiple `fn` barrel entries with the same identifier are allowed only when signatures differ (overload set).
|
||||
- Barrel labels MAY differ from the implementation labels.
|
||||
- A `fn` barrel entry must resolve to exactly one declaration in module scope.
|
||||
- A `callback` barrel entry must resolve to exactly one top-level callback declaration in module scope.
|
||||
- A `host` barrel entry must resolve to exactly one top-level `declare host` declaration in module scope.
|
||||
- A `struct` barrel entry governs the visibility of the struct declaration and of all methods declared directly in that struct body.
|
||||
- A `host` barrel entry governs the visibility of the host declaration and of all method signatures declared directly in that host body.
|
||||
@@ -210,7 +207,6 @@ pub host Gfx;
|
||||
pub enum GameState;
|
||||
pub service Physics;
|
||||
pub global Palette;
|
||||
pub callback TickCb;
|
||||
```
|
||||
|
||||
## 6. File grammar
|
||||
@@ -272,7 +268,7 @@ Rules:
|
||||
### 6.2 Top-level declarations
|
||||
|
||||
```ebnf
|
||||
TopDecl ::= TypeDecl | CallbackDecl | FunctionDecl | GlobalDecl | ConstDecl | ImplDecl
|
||||
TopDecl ::= TypeDecl | FunctionDecl | GlobalDecl | ConstDecl | ImplDecl
|
||||
```
|
||||
|
||||
Top-level `let` forms and top-level executable statements are forbidden.
|
||||
@@ -366,20 +362,9 @@ Rules:
|
||||
- Contract values are formed from struct values or service singleton values by explicit or contextual conversion, as defined by static semantics.
|
||||
- Structs do not have static methods in v1 core.
|
||||
|
||||
### 7.2 Callbacks
|
||||
### 7.2 First-class function values
|
||||
|
||||
```ebnf
|
||||
CallbackDecl ::= 'declare' 'callback' Identifier ParamList ReturnAnn? ';'
|
||||
```
|
||||
|
||||
Rules:
|
||||
|
||||
- A callback declaration introduces a nominal callback type.
|
||||
- A callback declaration defines exactly one callable signature.
|
||||
- A callback declaration has no body.
|
||||
- A callback declaration is distinct from `contract` and `fn`.
|
||||
- A callback declaration may be exported through `mod.barrel` using `callback`.
|
||||
- Callback values may be formed either by assigning a compatible top-level `fn` or by using `bind(context, fn_name)` under a callback-typed context.
|
||||
Function types use `Fn(T1, T2) -> R`. Lambdas use `fn [captures](name: Type) -> Return { ... }`; `[]` and all signature annotations are mandatory. See [First-class functions and explicit value captures](../../../compiler/pbs/specs/first-class-functions-and-captures.md).
|
||||
|
||||
### 7.3 Services
|
||||
|
||||
@@ -464,7 +449,7 @@ Rules:
|
||||
|
||||
```ebnf
|
||||
TypeRef ::= OptionalType | TypePrimary
|
||||
TypePrimary ::= SelfType | SimpleType | NamedTupleType | GroupType | UnitType
|
||||
TypePrimary ::= FnType | SelfType | SimpleType | NamedTupleType | GroupType | UnitType
|
||||
SelfType ::= 'Self'
|
||||
SimpleType ::= Identifier
|
||||
NamedTupleType ::= '(' NamedTupleField (',' NamedTupleField){0,5} ')'
|
||||
@@ -586,11 +571,13 @@ ArgList ::= Expr (',' Expr)*
|
||||
|
||||
Literal ::= IntLit | FloatLit | StringLit | BoolLit
|
||||
BoolLit ::= 'true' | 'false'
|
||||
PrimaryExpr ::= Literal | Identifier | ThisExpr | NewExpr | BindExpr | SomeExpr | NoneExpr | OkExpr | ErrExpr | UnitExpr | TupleExpr | GroupExpr | Block
|
||||
PrimaryExpr ::= Literal | Identifier | ThisExpr | NewExpr | LambdaExpr | SomeExpr | NoneExpr | OkExpr | ErrExpr | UnitExpr | TupleExpr | GroupExpr | Block
|
||||
ThisExpr ::= 'this'
|
||||
NewExpr ::= 'new' NewTarget '(' ArgList? ')'
|
||||
NewTarget ::= Identifier ('.' Identifier)?
|
||||
BindExpr ::= 'bind' '(' Expr ',' Identifier ')'
|
||||
LambdaExpr ::= 'fn' '[' CaptureNames? ']' ParamList '->' TypeRef Block
|
||||
CaptureNames ::= Identifier (',' Identifier)*
|
||||
FnType ::= 'Fn' '(' (TypeRef (',' TypeRef)*)? ')' '->' TypeRef
|
||||
SomeExpr ::= 'some' '(' Expr ')'
|
||||
NoneExpr ::= 'none'
|
||||
OkExpr ::= 'ok' '(' Expr ')'
|
||||
@@ -635,9 +622,6 @@ Rules:
|
||||
- Tuple expressions may be written with explicit labels (`(a: 1, b: 2)`) or positional sugar (`(1, 2)`).
|
||||
- Positional tuple expression sugar requires an expected named tuple shape supplied by context.
|
||||
- No single-slot tuple literal exists in v1 core syntax.
|
||||
- `bind(context, fn_name)` is the canonical surface for explicit callback context binding.
|
||||
- `bind(context, fn_name)` binds only the first parameter of `fn_name`.
|
||||
- The second operand of `bind` is a function identifier, not an arbitrary expression.
|
||||
- Mixing labeled and unlabeled tuple items in the same `TupleExpr` is a syntax error.
|
||||
- Member selection of a struct, service, host, or contract method is valid only as an immediate call target; bare method extraction such as `let m = s.compute;` is invalid.
|
||||
|
||||
@@ -677,14 +661,12 @@ Diagnostics that require name resolution, overload resolution, or type context a
|
||||
- invalid `ok(...)` construction form,
|
||||
- invalid `err(...)` construction form,
|
||||
- use of `ok(...)` or `err(...)` outside allowed result-flow positions,
|
||||
- invalid `bind(...)` construction form,
|
||||
- invalid `else` extraction form,
|
||||
- invalid `if` expression form,
|
||||
- invalid `switch` expression form,
|
||||
- invalid `for` loop form,
|
||||
- invalid `new` construction form,
|
||||
- invalid enum case path,
|
||||
- invalid callback declaration shape,
|
||||
- invalid struct field access modifier surface,
|
||||
- invalid struct method declaration shape,
|
||||
- invalid service method declaration shape,
|
||||
@@ -923,7 +905,6 @@ This section is the canonical reference example set for PBS v1 syntax.
|
||||
It consolidates the primary surface forms used by tooling and conformance for:
|
||||
|
||||
- `apply` and call sugar,
|
||||
- `bind` and nominal callbacks,
|
||||
- overloads that differ by return shape,
|
||||
- `optional`,
|
||||
- `result<Error>`,
|
||||
@@ -1019,45 +1000,21 @@ declare service Physics {
|
||||
}
|
||||
```
|
||||
|
||||
### 15.6 Nominal callback
|
||||
### 15.6 Function values
|
||||
|
||||
```pbs
|
||||
declare callback TickCb(dt: int) -> void;
|
||||
|
||||
fn on_tick(dt: int) -> void {
|
||||
let _ = dt;
|
||||
}
|
||||
|
||||
fn install(cb: TickCb) -> void {
|
||||
cb apply 16;
|
||||
cb(16);
|
||||
}
|
||||
|
||||
fn demo_callback() -> void {
|
||||
let cb: TickCb = on_tick;
|
||||
install(cb);
|
||||
fn twice(x: int) -> int { return x * 2; }
|
||||
fn demo_function() -> int {
|
||||
let f: Fn(int) -> int = twice;
|
||||
return f(21);
|
||||
}
|
||||
```
|
||||
|
||||
### 15.7 Bound callback
|
||||
### 15.7 Explicit captures
|
||||
|
||||
```pbs
|
||||
declare callback UpdateCb(dt: int) -> void;
|
||||
|
||||
declare struct Enemy(pub hp: int);
|
||||
|
||||
fn enemy_tick(self: Enemy, dt: int) -> void {
|
||||
let _ = self.hp;
|
||||
let _ = dt;
|
||||
}
|
||||
|
||||
fn install_update(cb: UpdateCb) -> void {
|
||||
cb apply 16;
|
||||
}
|
||||
|
||||
fn demo_bind(enemy: Enemy) -> void {
|
||||
let cb: UpdateCb = bind(enemy, enemy_tick);
|
||||
install_update(cb);
|
||||
fn create_adder(n: int) -> Fn(int) -> int {
|
||||
return fn [n](x: int) -> int { return n + x; };
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1240,3 +1197,5 @@ fn test(e: Enum) -> str {
|
||||
## Typed arrays and optional execution extension
|
||||
|
||||
The [typed arrays and optional execution specification](../../../compiler/pbs/specs/typed-arrays-and-optional-execution.md) defines array types/construction/indexing, contextual literals, optional bindings and runtime storage. Its rules extend this specification, including source typing, complete groups, Struct/contract execution and diagnostics.
|
||||
|
||||
Retired `declare callback`, callback exports and `bind(...)` are rejected. They do not provide alternative spellings for Fn or lambdas.
|
||||
|
||||
Reference in New Issue
Block a user