added first steps on pbs pipeline
This commit is contained in:
+49
@@ -0,0 +1,49 @@
|
||||
package p.studio.compiler.pbs;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.messages.BuildingIssueSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PbsFrontendCompilerTest {
|
||||
|
||||
@Test
|
||||
void shouldLowerFunctionsToIr() {
|
||||
final var source = """
|
||||
fn a(): int {
|
||||
return 1;
|
||||
}
|
||||
|
||||
fn b(x: int): int {
|
||||
return x;
|
||||
}
|
||||
""";
|
||||
|
||||
final var issues = BuildingIssueSink.empty();
|
||||
final var compiler = new PbsFrontendCompiler();
|
||||
final var functions = compiler.compileFile(new FileId(0), source, "compile-test.pbs", issues);
|
||||
|
||||
assertTrue(issues.isEmpty(), "Valid program should not report issues");
|
||||
assertEquals(2, functions.size());
|
||||
assertEquals("a", functions.get(0).name());
|
||||
assertEquals(0, functions.get(0).parameterCount());
|
||||
assertEquals("b", functions.get(1).name());
|
||||
assertEquals(1, functions.get(1).parameterCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReportDuplicateFunctionNames() {
|
||||
final var source = """
|
||||
fn a(): int { return 1; }
|
||||
fn a(): int { return 2; }
|
||||
""";
|
||||
|
||||
final var issues = BuildingIssueSink.empty();
|
||||
final var compiler = new PbsFrontendCompiler();
|
||||
compiler.compileFile(new FileId(0), source, "compile-test.pbs", issues);
|
||||
|
||||
assertTrue(issues.hasErrors());
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package p.studio.compiler.pbs.lexer;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.messages.BuildingIssueSink;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PbsLexerTest {
|
||||
|
||||
@Test
|
||||
void shouldLexFunctionTokens() {
|
||||
final var source = "fn sum(a: int, b: int): int { return a + b; }";
|
||||
final var issues = BuildingIssueSink.empty();
|
||||
|
||||
final var tokens = PbsLexer.lex(source, "test.pbs", issues);
|
||||
|
||||
assertEquals(PbsTokenKind.FN, tokens.get(0).kind());
|
||||
assertEquals(PbsTokenKind.IDENTIFIER, tokens.get(1).kind());
|
||||
assertEquals(PbsTokenKind.LEFT_PAREN, tokens.get(2).kind());
|
||||
assertEquals(PbsTokenKind.RETURN, tokens.stream()
|
||||
.filter(t -> t.kind() == PbsTokenKind.RETURN)
|
||||
.findFirst()
|
||||
.orElseThrow()
|
||||
.kind());
|
||||
assertEquals(PbsTokenKind.EOF, tokens.getLast().kind());
|
||||
assertTrue(issues.isEmpty(), "Lexer should not report issues for valid input");
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package p.studio.compiler.pbs.parser;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.messages.BuildingIssueSink;
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.pbs.lexer.PbsLexer;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PbsParserTest {
|
||||
|
||||
@Test
|
||||
void shouldParseSingleFunction() {
|
||||
final var source = """
|
||||
fn sum(a: int, b: int): int {
|
||||
return a + b;
|
||||
}
|
||||
""";
|
||||
final var issues = BuildingIssueSink.empty();
|
||||
final var tokens = PbsLexer.lex(source, "parser-test.pbs", issues);
|
||||
|
||||
final PbsAst.File ast = PbsParser.parse(tokens, new FileId(0), "parser-test.pbs", issues);
|
||||
|
||||
assertTrue(issues.isEmpty(), "Parser should not report issues for valid function");
|
||||
assertEquals(1, ast.functions().size());
|
||||
|
||||
final var fn = ast.functions().getFirst();
|
||||
assertEquals("sum", fn.name());
|
||||
assertEquals(2, fn.parameters().size());
|
||||
assertEquals("int", fn.returnType().name());
|
||||
assertEquals(1, fn.body().statements().size());
|
||||
assertInstanceOf(PbsAst.ReturnStatement.class, fn.body().statements().getFirst());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user