feat(pbs): add array-backed Stack and circular Queue
This commit is contained in:
+21
@@ -124,6 +124,27 @@ class PbsGateUSdkInterfaceConformanceTest {
|
||||
assertTrue(copied.hasErrors());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stackQueueExportsPreserveCapacityRulesAndRejectIndexing() throws IOException {
|
||||
var positive=compileWorkspaceModule(tempDir.resolve("stack-queue-positive"),"""
|
||||
import {stack as Lifo,queue as Fifo} from @sdk:collections;
|
||||
[Frame]fn frame()->void{let n:int=0;let s:Lifo<int>=new Lifo(n);let q:Fifo<int>=new Fifo<>();s.push(20);q.enqueue(22);}
|
||||
""","pub fn frame()->void;",1,null);
|
||||
assertFalse(positive.diagnostics().hasErrors(),positive.diagnostics().toString());
|
||||
for(String type:java.util.List.of("stack","queue")) {
|
||||
for(var entry:java.util.Map.of(
|
||||
"optional","let xs=new "+type+"<optional int>();",
|
||||
"index","let xs=new "+type+"<int>();let invalid=xs[0];",
|
||||
"private","let xs=new "+type+"<int>();let exposed=xs.storage;",
|
||||
"capacity","let xs=new "+type+"<int>(true);",
|
||||
"bounded","let xs=new "+type+"<int>();xs.reserve(4);").entrySet()) {
|
||||
var negative=compileWorkspaceModule(tempDir.resolve(type+"-negative-"+entry.getKey()),
|
||||
"import {"+type+"} from @sdk:collections;[Frame]fn frame()->void{"+entry.getValue()+"}","pub fn frame()->void;",1,null);
|
||||
assertTrue(negative.diagnostics().hasErrors(),type+"/"+entry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapExportsUseOriginCheckedKeyTypingAndPrivateAssociations() throws IOException {
|
||||
var positive=compileWorkspaceModule(tempDir.resolve("map-positive"),"""
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package p.studio.compiler.pbs;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.models.SourceKind;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PbsStackQueueTest {
|
||||
private DiagnosticSink compile(String source) throws Exception {
|
||||
String sdk;
|
||||
try (var stream=getClass().getResourceAsStream("/game/stdlib/1/sdk/collections/main.pbs")) {
|
||||
assertNotNull(stream);sdk=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
|
||||
}
|
||||
var diagnostics=DiagnosticSink.empty();
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0),sdk+"\n"+source,diagnostics,SourceKind.SDK_INTERFACE);
|
||||
return diagnostics;
|
||||
}
|
||||
@Test void capacitiesContextAndOptionalFieldsUseOrdinaryGenericRules() throws Exception {
|
||||
var diagnostics=compile("""
|
||||
declare struct Item(pub value:optional int);
|
||||
fn sample(){
|
||||
let n:int=0;let wide:long=4;
|
||||
let s:stack<int>=new stack(n);let q:queue<int>=new queue<>(wide);
|
||||
let defaultStack:stack<int>=new stack();let defaultQueue:queue<int>=new queue<>();
|
||||
let maybe:optional (stack<Item>)=some(new stack());
|
||||
let nested=new queue<array<int>>();nested.enqueue(new array<int>(0));
|
||||
let items=new stack<Item>();items.push(new Item(none));
|
||||
s.push(42);q.enqueue(42);s.pop();q.dequeue();s.peek();q.peek();s.clear();q.clear();
|
||||
}
|
||||
""");
|
||||
assertFalse(diagnostics.hasErrors(),()->diagnostics.stream().map(d->d.getCode()+":"+d.getMessage()).toList().toString());
|
||||
}
|
||||
@Test void optionalInvariancePrivacyAndUnsupportedSurfacesAreRejected() throws Exception {
|
||||
for (String type:java.util.List.of("stack","queue")) {
|
||||
for (String optional:java.util.List.of("optional int","(optional int)")) {
|
||||
var d=compile("fn f(){let x=new "+type+"<"+optional+">();}");
|
||||
assertTrue(d.stream().anyMatch(i->i.getCode().contains(type.toUpperCase()+"_OPTIONAL_ELEMENT")),d.toString());
|
||||
}
|
||||
for (String statement:java.util.List.of(
|
||||
"let x=new "+type+"();",
|
||||
"let x:"+type+"<long>=new "+type+"<int>();",
|
||||
"let x=new "+type+"<int>(true);",
|
||||
"let x=new "+type+"<int>();let y=x[0];",
|
||||
"let x=new "+type+"<int>();x.reserve(10);",
|
||||
"let x=new "+type+"<int>();x.trim();",
|
||||
"let x=new "+type+"<int>();let y=x.storage;")) {
|
||||
assertTrue(compile("fn f(){"+statement+"}").hasErrors(),statement);
|
||||
}
|
||||
String insert=type.equals("stack")?"push":"enqueue";
|
||||
assertTrue(compile("fn f(){let x=new "+type+"<long>();let n:int=1;x."+insert+"(n);}").hasErrors());
|
||||
}
|
||||
assertTrue(compile("fn f(){let q=new queue<int>();let hidden=q.start;}").hasErrors());
|
||||
}
|
||||
@Test void projectHomonymsDoNotAcquireTrustedSdkBehavior() {
|
||||
for (String type:java.util.List.of("stack","queue")) {
|
||||
var d=DiagnosticSink.empty();
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0),"declare struct "+type+"<T>(pub value:T);fn f(){let x=new "+type+"<optional int>(none);}",d);
|
||||
assertFalse(d.hasErrors(),d.toString());
|
||||
var forged=DiagnosticSink.empty();
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0),"[Sdk"+(type.equals("stack")?"Stack":"Queue")+"]declare struct "+type+"<T>(value:T);",forged);
|
||||
assertTrue(forged.hasErrors());
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package p.studio.compiler.pbs.semantics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.pbs.lexer.PbsLexer;
|
||||
import p.studio.compiler.pbs.parser.PbsParser;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PbsStackQueueEditorialTest {
|
||||
@Test void aliasesShowConcretePublicApiConstructorsAndOriginalSdkDefinitions() throws Exception {
|
||||
var diagnostics=DiagnosticSink.empty();var sdkId=new FileId(1);String sdk;
|
||||
try(var stream=getClass().getResourceAsStream("/game/stdlib/1/sdk/collections/main.pbs")) {
|
||||
assertNotNull(stream);sdk=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
|
||||
}
|
||||
var library=PbsParser.parse(PbsLexer.lex(sdk,sdkId,diagnostics),sdkId,diagnostics,PbsParser.ParseMode.INTERFACE_MODULE);
|
||||
for (String type:java.util.List.of("stack","queue")) {
|
||||
String insert=type.equals("stack")?"push":"enqueue",remove=type.equals("stack")?"pop":"dequeue";
|
||||
String source="import {"+type+" as Values} from @sdk:collections;fn f(values:Values<int>){let x:Values<int>=new Values();values."+insert+"(42);values.storage;}";
|
||||
var id=new FileId(2);var ast=PbsParser.parse(PbsLexer.lex(source,id,diagnostics),id,diagnostics);
|
||||
var declarations=new java.util.ArrayList<PbsAst.TopDecl>(library.topDecls().stream().toList());
|
||||
var origin=library.topDecls().stream().filter(d->d instanceof PbsAst.StructDecl s&&s.name().equals(type)).findFirst().orElseThrow();
|
||||
declarations.add(new PbsAst.TypeBindingDecl("Values",origin,ast.span()));
|
||||
var supplemental=ReadOnlyList.wrap(declarations);var service=new PbsEditorialSupportService();
|
||||
int offset=source.indexOf("values."+insert)+7;
|
||||
assertEquals(Set.of(insert,remove,"peek","size","capacity","isEmpty","clear","cursor"),
|
||||
service.completion(source,ast,supplemental,offset).stream().map(c->c.label()).collect(Collectors.toSet()));
|
||||
assertTrue(service.hover(source,ast,supplemental,offset).orElseThrow().signatures().getFirst().label().contains("int"));
|
||||
assertEquals(sdkId,service.definition(source,ast,supplemental,offset).getFirst().span().getFileId());
|
||||
var constructors=service.signatureHelp(source,ast,supplemental,source.indexOf("new Values(")+11).orElseThrow().signatures();
|
||||
assertEquals(2,constructors.size());
|
||||
assertTrue(constructors.stream().anyMatch(c->c.label().contains("capacity: long")),constructors.toString());
|
||||
assertTrue(constructors.stream().noneMatch(c->c.label().contains("storage")||c.label().contains("$generic$")));
|
||||
int hidden=source.indexOf("values.storage")+7;
|
||||
assertTrue(service.hover(source,ast,supplemental,hidden).isEmpty());
|
||||
assertTrue(service.definition(source,ast,supplemental,hidden).isEmpty());
|
||||
}
|
||||
assertFalse(diagnostics.hasErrors(),diagnostics.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user