feat(pbs): implement first-class functions and explicit captures
This commit is contained in:
+2
-2
@@ -17,7 +17,7 @@ class PbsBarrelParserTest {
|
||||
mod fn run(state: optional int) -> result<Err> (value: int, meta: float);
|
||||
pub struct Vec2;
|
||||
mod global Palette;
|
||||
pub callback Tick;
|
||||
pub fn tick(dt: int) -> void;
|
||||
""";
|
||||
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
@@ -41,7 +41,7 @@ class PbsBarrelParserTest {
|
||||
|
||||
assertInstanceOf(PbsAst.BarrelStructItem.class, barrel.items().get(2));
|
||||
assertInstanceOf(PbsAst.BarrelGlobalItem.class, barrel.items().get(3));
|
||||
assertInstanceOf(PbsAst.BarrelCallbackItem.class, barrel.items().get(4));
|
||||
assertInstanceOf(PbsAst.BarrelFunctionItem.class, barrel.items().get(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+2
-2
@@ -134,7 +134,7 @@ class PbsExprParserTest {
|
||||
err(Game.Fail);
|
||||
new Vec(v);
|
||||
new Vec.zero(v);
|
||||
bind(ctx, handler);
|
||||
fn [ctx](x: int) -> int { return ctx + x; };
|
||||
return v;
|
||||
}
|
||||
""";
|
||||
@@ -158,7 +158,7 @@ class PbsExprParserTest {
|
||||
assertInstanceOf(PbsAst.ExpressionStatement.class, body.get(4)).expression());
|
||||
assertInstanceOf(PbsAst.NewExpr.class,
|
||||
assertInstanceOf(PbsAst.ExpressionStatement.class, body.get(5)).expression());
|
||||
assertInstanceOf(PbsAst.BindExpr.class,
|
||||
assertInstanceOf(PbsAst.LambdaExpr.class,
|
||||
assertInstanceOf(PbsAst.ExpressionStatement.class, body.get(6)).expression());
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -51,7 +51,7 @@ class PbsParserTest {
|
||||
|
||||
fn first() -> int { return 1; }
|
||||
declare struct Point(x: int, y: int);
|
||||
declare callback Tick(dt: int) -> int;
|
||||
fn tick(dt: int) -> int { return dt; }
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var fileId = new FileId(0);
|
||||
@@ -64,7 +64,7 @@ class PbsParserTest {
|
||||
assertEquals(3, ast.topDecls().size());
|
||||
assertInstanceOf(PbsAst.FunctionDecl.class, ast.topDecls().get(0));
|
||||
assertInstanceOf(PbsAst.StructDecl.class, ast.topDecls().get(1));
|
||||
assertInstanceOf(PbsAst.CallbackDecl.class, ast.topDecls().get(2));
|
||||
assertInstanceOf(PbsAst.FunctionDecl.class, ast.topDecls().get(2));
|
||||
|
||||
final var importDecl = ast.imports().getFirst();
|
||||
assertEquals("core", importDecl.moduleRef().project());
|
||||
@@ -84,7 +84,7 @@ class PbsParserTest {
|
||||
declare service Game { fn tick(x: int) -> int { return x; } }
|
||||
declare error Err { Fail; Crash; }
|
||||
declare enum Mode(Idle = 0, Run = 1);
|
||||
declare callback TickCb(x: int) -> result<Err> int;
|
||||
fn tick(x: int) -> result<Err> int { return ok(x); }
|
||||
declare global STATE: int = 10;
|
||||
declare const LIMIT: int = 10;
|
||||
implements C for S using s { fn run(x: int) -> int { return x; } }
|
||||
@@ -122,7 +122,7 @@ class PbsParserTest {
|
||||
final var enumDecl = assertInstanceOf(PbsAst.EnumDecl.class, ast.topDecls().get(7));
|
||||
assertEquals(2, enumDecl.cases().size());
|
||||
|
||||
final var callbackDecl = assertInstanceOf(PbsAst.CallbackDecl.class, ast.topDecls().get(8));
|
||||
final var callbackDecl = assertInstanceOf(PbsAst.FunctionDecl.class, ast.topDecls().get(8));
|
||||
assertEquals(PbsAst.ReturnKind.RESULT, callbackDecl.returnKind());
|
||||
assertEquals("Err", callbackDecl.resultErrorType().name());
|
||||
|
||||
|
||||
+28
-2
@@ -16,6 +16,32 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
final class PbsEditorialSupportServiceTest {
|
||||
private final PbsEditorialSupportService service = new PbsEditorialSupportService();
|
||||
|
||||
@Test
|
||||
void closuresExposeCapturedObjectsAndOwnParameters() {
|
||||
String source = """
|
||||
declare struct Counter(pub mut value: int);
|
||||
fn main() {
|
||||
let counter = new Counter(40);
|
||||
let f = fn [counter](delta: int) -> int {
|
||||
counter.value += delta;
|
||||
return counter.value;
|
||||
};
|
||||
let answer = f(2);
|
||||
}
|
||||
""";
|
||||
var ast = parseOrdinary(source);
|
||||
int use = source.indexOf("counter.value");
|
||||
assertTrue(service.hover(source,ast,ReadOnlyList.empty(),use).isPresent());
|
||||
assertTrue(service.completion(source,ast,ReadOnlyList.empty(),use+8).stream().anyMatch(c -> c.label().equals("value")));
|
||||
assertFalse(service.definition(source,ast,ReadOnlyList.empty(),source.indexOf("delta;")).isEmpty());
|
||||
var rename = service.rename(source,ast,ReadOnlyList.empty(),use,"state");
|
||||
assertTrue(rename.allowed(),rename.refusal());
|
||||
assertEquals(4,rename.sites().size(),rename.sites().toString());
|
||||
var parameter = service.rename(source,ast,ReadOnlyList.empty(),source.indexOf("delta;"),"amount");
|
||||
assertTrue(parameter.allowed(),parameter.refusal());
|
||||
assertEquals(2,parameter.sites().size(),parameter.sites().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void arrayAndOptionalBindingShareEditorialScope() {
|
||||
String source = """
|
||||
@@ -552,7 +578,7 @@ final class PbsEditorialSupportServiceTest {
|
||||
declare service Physics { fn tick() -> void { return; } }
|
||||
declare error Io { NotFound; Busy; }
|
||||
declare enum Mode(Idle = 0, Run = 1);
|
||||
declare callback OnClick(x: int) -> result<Io> int;
|
||||
fn OnClick(x: int) -> result<Io> int { return ok(x); }
|
||||
declare global STATE: int = 10;
|
||||
declare const LIMIT: int = 10;
|
||||
implements TickLike for Point using s { fn tick(dt: int) -> void { return; } }
|
||||
@@ -613,7 +639,7 @@ final class PbsEditorialSupportServiceTest {
|
||||
assertEquals("0", mode.children().getFirst().detail());
|
||||
|
||||
final var callback = requireNamed(symbols, "OnClick");
|
||||
assertEquals(PbsEditorialSymbolKind.CALLBACK, callback.kind());
|
||||
assertEquals(PbsEditorialSymbolKind.FUNCTION, callback.kind());
|
||||
assertEquals("(x: int) -> result<Io> int", callback.detail());
|
||||
|
||||
assertEquals(PbsEditorialSymbolKind.GLOBAL, requireNamed(symbols, "STATE").kind());
|
||||
|
||||
+2
-2
@@ -199,7 +199,7 @@ class PbsSemanticsDeclarationsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectInvalidCtorServiceAndCallbackShapes() {
|
||||
void shouldRejectInvalidCtorServiceAndFunctionShapes() {
|
||||
final var source = """
|
||||
declare struct S(a: int) {
|
||||
ctor make(x: int) { return; }
|
||||
@@ -211,7 +211,7 @@ class PbsSemanticsDeclarationsTest {
|
||||
fn run(b: int) -> int { return b; }
|
||||
}
|
||||
|
||||
declare callback Tick(a: int, a: int) -> int;
|
||||
fn tick(a: int, a: int) -> int { return a; }
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user