added first steps on pbs pipeline
This commit is contained in:
+55
@@ -0,0 +1,55 @@
|
||||
package p.studio.compiler.pbs;
|
||||
|
||||
import p.studio.compiler.messages.BuildingIssueSink;
|
||||
import p.studio.compiler.models.IRFunction;
|
||||
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.identifiers.FileId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public final class PbsFrontendCompiler {
|
||||
public ReadOnlyList<IRFunction> compileFile(
|
||||
final FileId fileId,
|
||||
final String source,
|
||||
final String sourceLabel,
|
||||
final BuildingIssueSink issues) {
|
||||
final var tokens = PbsLexer.lex(source, sourceLabel, issues);
|
||||
final var ast = PbsParser.parse(tokens, fileId, sourceLabel, issues);
|
||||
validateFunctionNames(ast, sourceLabel, issues);
|
||||
return lowerFunctions(fileId, ast);
|
||||
}
|
||||
|
||||
private void validateFunctionNames(
|
||||
final PbsAst.File ast,
|
||||
final String sourceLabel,
|
||||
final BuildingIssueSink issues) {
|
||||
final Set<String> names = new HashSet<>();
|
||||
for (final var fn : ast.functions()) {
|
||||
if (names.add(fn.name())) {
|
||||
continue;
|
||||
}
|
||||
issues.report(builder -> builder
|
||||
.error(true)
|
||||
.message("[PBS:E_RESOLVE_DUPLICATE_SYMBOL] Duplicate function '%s' at %s:[%d,%d)"
|
||||
.formatted(fn.name(), sourceLabel, fn.span().getStart(), fn.span().getEnd())));
|
||||
}
|
||||
}
|
||||
|
||||
private ReadOnlyList<IRFunction> lowerFunctions(final FileId fileId, final PbsAst.File ast) {
|
||||
final var functions = new ArrayList<IRFunction>(ast.functions().size());
|
||||
for (final var fn : ast.functions()) {
|
||||
functions.add(new IRFunction(
|
||||
fileId,
|
||||
fn.name(),
|
||||
fn.parameters().size(),
|
||||
fn.returnType() != null,
|
||||
fn.span()));
|
||||
}
|
||||
return ReadOnlyList.wrap(functions);
|
||||
}
|
||||
}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
package p.studio.compiler.pbs.ast;
|
||||
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
public final class PbsAst {
|
||||
private PbsAst() {
|
||||
}
|
||||
|
||||
public record File(
|
||||
ReadOnlyList<FunctionDecl> functions,
|
||||
Span span) {
|
||||
}
|
||||
|
||||
public record FunctionDecl(
|
||||
String name,
|
||||
ReadOnlyList<Parameter> parameters,
|
||||
TypeRef returnType,
|
||||
Expression elseFallback,
|
||||
Block body,
|
||||
Span span) {
|
||||
}
|
||||
|
||||
public record Parameter(
|
||||
String name,
|
||||
boolean mutable,
|
||||
TypeRef typeRef,
|
||||
Span span) {
|
||||
}
|
||||
|
||||
public record TypeRef(
|
||||
String name,
|
||||
Span span) {
|
||||
}
|
||||
|
||||
public record Block(
|
||||
ReadOnlyList<Statement> statements,
|
||||
Span span) {
|
||||
}
|
||||
|
||||
public sealed interface Statement permits LetStatement, ReturnStatement, ExpressionStatement {
|
||||
Span span();
|
||||
}
|
||||
|
||||
public record LetStatement(
|
||||
String name,
|
||||
boolean mutable,
|
||||
TypeRef explicitType,
|
||||
Expression initializer,
|
||||
Span span) implements Statement {
|
||||
}
|
||||
|
||||
public record ReturnStatement(
|
||||
Expression value,
|
||||
Span span) implements Statement {
|
||||
}
|
||||
|
||||
public record ExpressionStatement(
|
||||
Expression expression,
|
||||
Span span) implements Statement {
|
||||
}
|
||||
|
||||
public sealed interface Expression permits IdentifierExpr,
|
||||
IntLiteralExpr,
|
||||
FloatLiteralExpr,
|
||||
BoundedLiteralExpr,
|
||||
StringLiteralExpr,
|
||||
BoolLiteralExpr,
|
||||
UnaryExpr,
|
||||
BinaryExpr,
|
||||
CallExpr,
|
||||
GroupExpr {
|
||||
Span span();
|
||||
}
|
||||
|
||||
public record IdentifierExpr(
|
||||
String name,
|
||||
Span span) implements Expression {
|
||||
}
|
||||
|
||||
public record IntLiteralExpr(
|
||||
long value,
|
||||
Span span) implements Expression {
|
||||
}
|
||||
|
||||
public record FloatLiteralExpr(
|
||||
double value,
|
||||
Span span) implements Expression {
|
||||
}
|
||||
|
||||
public record BoundedLiteralExpr(
|
||||
int value,
|
||||
Span span) implements Expression {
|
||||
}
|
||||
|
||||
public record StringLiteralExpr(
|
||||
String value,
|
||||
Span span) implements Expression {
|
||||
}
|
||||
|
||||
public record BoolLiteralExpr(
|
||||
boolean value,
|
||||
Span span) implements Expression {
|
||||
}
|
||||
|
||||
public record UnaryExpr(
|
||||
String operator,
|
||||
Expression expression,
|
||||
Span span) implements Expression {
|
||||
}
|
||||
|
||||
public record BinaryExpr(
|
||||
String operator,
|
||||
Expression left,
|
||||
Expression right,
|
||||
Span span) implements Expression {
|
||||
}
|
||||
|
||||
public record CallExpr(
|
||||
Expression callee,
|
||||
ReadOnlyList<Expression> arguments,
|
||||
Span span) implements Expression {
|
||||
}
|
||||
|
||||
public record GroupExpr(
|
||||
Expression expression,
|
||||
Span span) implements Expression {
|
||||
}
|
||||
}
|
||||
+242
@@ -0,0 +1,242 @@
|
||||
package p.studio.compiler.pbs.lexer;
|
||||
|
||||
import p.studio.compiler.messages.BuildingIssueSink;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class PbsLexer {
|
||||
private static final Map<String, PbsTokenKind> KEYWORDS = buildKeywords();
|
||||
|
||||
private final String source;
|
||||
private final String sourceLabel;
|
||||
private final BuildingIssueSink issues;
|
||||
private final ArrayList<PbsToken> tokens = new ArrayList<>();
|
||||
|
||||
private int start;
|
||||
private int current;
|
||||
|
||||
private PbsLexer(final String source, final String sourceLabel, final BuildingIssueSink issues) {
|
||||
this.source = source == null ? "" : source;
|
||||
this.sourceLabel = sourceLabel == null ? "<unknown>" : sourceLabel;
|
||||
this.issues = issues;
|
||||
}
|
||||
|
||||
public static ReadOnlyList<PbsToken> lex(
|
||||
final String source,
|
||||
final String sourceLabel,
|
||||
final BuildingIssueSink issues) {
|
||||
final var lexer = new PbsLexer(source, sourceLabel, issues);
|
||||
return lexer.lexInternal();
|
||||
}
|
||||
|
||||
private ReadOnlyList<PbsToken> lexInternal() {
|
||||
while (!isAtEnd()) {
|
||||
start = current;
|
||||
scanToken();
|
||||
}
|
||||
tokens.add(new PbsToken(PbsTokenKind.EOF, "", current, current));
|
||||
return ReadOnlyList.wrap(tokens);
|
||||
}
|
||||
|
||||
private void scanToken() {
|
||||
final char c = advance();
|
||||
switch (c) {
|
||||
case ' ', '\r', '\t', '\n' -> {
|
||||
// Deliberately ignored.
|
||||
}
|
||||
case '(' -> addToken(PbsTokenKind.LEFT_PAREN);
|
||||
case ')' -> addToken(PbsTokenKind.RIGHT_PAREN);
|
||||
case '{' -> addToken(PbsTokenKind.LEFT_BRACE);
|
||||
case '}' -> addToken(PbsTokenKind.RIGHT_BRACE);
|
||||
case '[' -> addToken(PbsTokenKind.LEFT_BRACKET);
|
||||
case ']' -> addToken(PbsTokenKind.RIGHT_BRACKET);
|
||||
case ',' -> addToken(PbsTokenKind.COMMA);
|
||||
case ':' -> addToken(PbsTokenKind.COLON);
|
||||
case ';' -> addToken(PbsTokenKind.SEMICOLON);
|
||||
case '@' -> addToken(PbsTokenKind.AT);
|
||||
case '+' -> addToken(PbsTokenKind.PLUS);
|
||||
case '-' -> addToken(PbsTokenKind.MINUS);
|
||||
case '*' -> addToken(PbsTokenKind.STAR);
|
||||
case '%' -> addToken(PbsTokenKind.PERCENT);
|
||||
case '.' -> {
|
||||
if (match('.')) {
|
||||
addToken(PbsTokenKind.DOT_DOT);
|
||||
return;
|
||||
}
|
||||
report("E_LEX_INVALID_CHAR", "Unexpected '.'");
|
||||
}
|
||||
case '!' -> addToken(match('=') ? PbsTokenKind.BANG_EQUAL : PbsTokenKind.BANG);
|
||||
case '=' -> addToken(match('=') ? PbsTokenKind.EQUAL_EQUAL : PbsTokenKind.EQUAL);
|
||||
case '<' -> addToken(match('=') ? PbsTokenKind.LESS_EQUAL : PbsTokenKind.LESS);
|
||||
case '>' -> addToken(match('=') ? PbsTokenKind.GREATER_EQUAL : PbsTokenKind.GREATER);
|
||||
case '&' -> {
|
||||
if (match('&')) {
|
||||
addToken(PbsTokenKind.AND_AND);
|
||||
return;
|
||||
}
|
||||
report("E_LEX_INVALID_CHAR", "Unexpected '&'");
|
||||
}
|
||||
case '|' -> {
|
||||
if (match('|')) {
|
||||
addToken(PbsTokenKind.OR_OR);
|
||||
return;
|
||||
}
|
||||
report("E_LEX_INVALID_CHAR", "Unexpected '|'");
|
||||
}
|
||||
case '/' -> {
|
||||
if (match('/')) {
|
||||
// Line comment
|
||||
while (!isAtEnd() && peek() != '\n') {
|
||||
advance();
|
||||
}
|
||||
return;
|
||||
}
|
||||
addToken(PbsTokenKind.SLASH);
|
||||
}
|
||||
case '"' -> string();
|
||||
default -> {
|
||||
if (isDigit(c)) {
|
||||
number();
|
||||
return;
|
||||
}
|
||||
if (isIdentifierStart(c)) {
|
||||
identifier();
|
||||
return;
|
||||
}
|
||||
report("E_LEX_INVALID_CHAR", "Invalid character: '%s'".formatted(c));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void identifier() {
|
||||
while (!isAtEnd() && isIdentifierPart(peek())) {
|
||||
advance();
|
||||
}
|
||||
|
||||
final String text = source.substring(start, current);
|
||||
final PbsTokenKind kind = KEYWORDS.getOrDefault(text, PbsTokenKind.IDENTIFIER);
|
||||
addToken(kind);
|
||||
}
|
||||
|
||||
private void number() {
|
||||
while (!isAtEnd() && isDigit(peek())) {
|
||||
advance();
|
||||
}
|
||||
|
||||
var isFloat = false;
|
||||
if (!isAtEnd() && peek() == '.' && peekNext() != '.') {
|
||||
isFloat = true;
|
||||
advance();
|
||||
while (!isAtEnd() && isDigit(peek())) {
|
||||
advance();
|
||||
}
|
||||
}
|
||||
|
||||
if (!isFloat && !isAtEnd() && peek() == 'b') {
|
||||
advance();
|
||||
addToken(PbsTokenKind.BOUNDED_LITERAL);
|
||||
return;
|
||||
}
|
||||
|
||||
addToken(isFloat ? PbsTokenKind.FLOAT_LITERAL : PbsTokenKind.INT_LITERAL);
|
||||
}
|
||||
|
||||
private void string() {
|
||||
while (!isAtEnd() && peek() != '"') {
|
||||
if (peek() == '\\' && !isAtEnd()) {
|
||||
advance();
|
||||
if (!isAtEnd()) {
|
||||
advance();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
advance();
|
||||
}
|
||||
|
||||
if (isAtEnd()) {
|
||||
report("E_LEX_UNTERMINATED_STRING", "Unterminated string literal");
|
||||
return;
|
||||
}
|
||||
|
||||
// Closing quote.
|
||||
advance();
|
||||
addToken(PbsTokenKind.STRING_LITERAL);
|
||||
}
|
||||
|
||||
private void addToken(final PbsTokenKind kind) {
|
||||
final String lexeme = source.substring(start, current);
|
||||
tokens.add(new PbsToken(kind, lexeme, start, current));
|
||||
}
|
||||
|
||||
private boolean match(final char expected) {
|
||||
if (isAtEnd()) return false;
|
||||
if (source.charAt(current) != expected) return false;
|
||||
current++;
|
||||
return true;
|
||||
}
|
||||
|
||||
private char advance() {
|
||||
return source.charAt(current++);
|
||||
}
|
||||
|
||||
private char peek() {
|
||||
if (isAtEnd()) return '\0';
|
||||
return source.charAt(current);
|
||||
}
|
||||
|
||||
private char peekNext() {
|
||||
if (current + 1 >= source.length()) return '\0';
|
||||
return source.charAt(current + 1);
|
||||
}
|
||||
|
||||
private boolean isAtEnd() {
|
||||
return current >= source.length();
|
||||
}
|
||||
|
||||
private boolean isDigit(final char c) {
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
||||
private boolean isIdentifierStart(final char c) {
|
||||
return c == '_' || Character.isAlphabetic(c);
|
||||
}
|
||||
|
||||
private boolean isIdentifierPart(final char c) {
|
||||
return c == '_' || Character.isAlphabetic(c) || Character.isDigit(c);
|
||||
}
|
||||
|
||||
private void report(final String code, final String message) {
|
||||
issues.report(builder -> builder
|
||||
.error(true)
|
||||
.message("[PBS:%s] %s at %s:[%d,%d)".formatted(code, message, sourceLabel, start, current)));
|
||||
}
|
||||
|
||||
private static Map<String, PbsTokenKind> buildKeywords() {
|
||||
final var map = new HashMap<String, PbsTokenKind>();
|
||||
map.put("import", PbsTokenKind.IMPORT);
|
||||
map.put("from", PbsTokenKind.FROM);
|
||||
map.put("as", PbsTokenKind.AS);
|
||||
map.put("pub", PbsTokenKind.PUB);
|
||||
map.put("mod", PbsTokenKind.MOD);
|
||||
map.put("service", PbsTokenKind.SERVICE);
|
||||
map.put("fn", PbsTokenKind.FN);
|
||||
map.put("let", PbsTokenKind.LET);
|
||||
map.put("mut", PbsTokenKind.MUT);
|
||||
map.put("declare", PbsTokenKind.DECLARE);
|
||||
map.put("struct", PbsTokenKind.STRUCT);
|
||||
map.put("contract", PbsTokenKind.CONTRACT);
|
||||
map.put("error", PbsTokenKind.ERROR);
|
||||
map.put("if", PbsTokenKind.IF);
|
||||
map.put("else", PbsTokenKind.ELSE);
|
||||
map.put("when", PbsTokenKind.WHEN);
|
||||
map.put("for", PbsTokenKind.FOR);
|
||||
map.put("in", PbsTokenKind.IN);
|
||||
map.put("return", PbsTokenKind.RETURN);
|
||||
map.put("true", PbsTokenKind.TRUE);
|
||||
map.put("false", PbsTokenKind.FALSE);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package p.studio.compiler.pbs.lexer;
|
||||
|
||||
public record PbsToken(
|
||||
PbsTokenKind kind,
|
||||
String lexeme,
|
||||
int start,
|
||||
int end) {
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package p.studio.compiler.pbs.lexer;
|
||||
|
||||
public enum PbsTokenKind {
|
||||
EOF,
|
||||
IDENTIFIER,
|
||||
INT_LITERAL,
|
||||
FLOAT_LITERAL,
|
||||
BOUNDED_LITERAL,
|
||||
STRING_LITERAL,
|
||||
|
||||
// Keywords
|
||||
IMPORT,
|
||||
FROM,
|
||||
AS,
|
||||
PUB,
|
||||
MOD,
|
||||
SERVICE,
|
||||
FN,
|
||||
LET,
|
||||
MUT,
|
||||
DECLARE,
|
||||
STRUCT,
|
||||
CONTRACT,
|
||||
ERROR,
|
||||
IF,
|
||||
ELSE,
|
||||
WHEN,
|
||||
FOR,
|
||||
IN,
|
||||
RETURN,
|
||||
TRUE,
|
||||
FALSE,
|
||||
|
||||
// Punctuation / operators
|
||||
LEFT_PAREN,
|
||||
RIGHT_PAREN,
|
||||
LEFT_BRACE,
|
||||
RIGHT_BRACE,
|
||||
LEFT_BRACKET,
|
||||
RIGHT_BRACKET,
|
||||
COMMA,
|
||||
COLON,
|
||||
SEMICOLON,
|
||||
AT,
|
||||
DOT_DOT,
|
||||
|
||||
PLUS,
|
||||
MINUS,
|
||||
STAR,
|
||||
SLASH,
|
||||
PERCENT,
|
||||
BANG,
|
||||
BANG_EQUAL,
|
||||
EQUAL,
|
||||
EQUAL_EQUAL,
|
||||
LESS,
|
||||
LESS_EQUAL,
|
||||
GREATER,
|
||||
GREATER_EQUAL,
|
||||
AND_AND,
|
||||
OR_OR
|
||||
}
|
||||
+490
@@ -0,0 +1,490 @@
|
||||
package p.studio.compiler.pbs.parser;
|
||||
|
||||
import p.studio.compiler.messages.BuildingIssueSink;
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.pbs.lexer.PbsToken;
|
||||
import p.studio.compiler.pbs.lexer.PbsTokenKind;
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public final class PbsParser {
|
||||
private final ArrayList<PbsToken> tokens;
|
||||
private final FileId fileId;
|
||||
private final String sourceLabel;
|
||||
private final BuildingIssueSink issues;
|
||||
private int current;
|
||||
|
||||
private PbsParser(
|
||||
final ReadOnlyList<PbsToken> tokens,
|
||||
final FileId fileId,
|
||||
final String sourceLabel,
|
||||
final BuildingIssueSink issues) {
|
||||
this.tokens = new ArrayList<>();
|
||||
for (final var token : tokens) {
|
||||
this.tokens.add(token);
|
||||
}
|
||||
this.fileId = fileId;
|
||||
this.sourceLabel = sourceLabel == null ? "<unknown>" : sourceLabel;
|
||||
this.issues = issues;
|
||||
}
|
||||
|
||||
public static PbsAst.File parse(
|
||||
final ReadOnlyList<PbsToken> tokens,
|
||||
final FileId fileId,
|
||||
final String sourceLabel,
|
||||
final BuildingIssueSink issues) {
|
||||
return new PbsParser(tokens, fileId, sourceLabel, issues).parseFile();
|
||||
}
|
||||
|
||||
private PbsAst.File parseFile() {
|
||||
final var functions = new ArrayList<PbsAst.FunctionDecl>();
|
||||
|
||||
while (!isAtEnd()) {
|
||||
if (match(PbsTokenKind.IMPORT)) {
|
||||
parseAndDiscardImport();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (match(PbsTokenKind.FN)) {
|
||||
functions.add(parseFunction(previous()));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (match(PbsTokenKind.MOD, PbsTokenKind.PUB)) {
|
||||
report(previous(), "E_PARSE_VISIBILITY_IN_SOURCE",
|
||||
"Visibility modifiers are barrel-only and cannot appear in .pbs declarations");
|
||||
synchronizeTopLevel();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (check(PbsTokenKind.EOF)) {
|
||||
break;
|
||||
}
|
||||
|
||||
report(peek(), "E_PARSE_UNEXPECTED_TOKEN", "Expected top-level declaration ('fn') or import");
|
||||
synchronizeTopLevel();
|
||||
}
|
||||
|
||||
final var eof = peek();
|
||||
return new PbsAst.File(ReadOnlyList.wrap(functions), span(0, eof.end()));
|
||||
}
|
||||
|
||||
private void parseAndDiscardImport() {
|
||||
// Supports both forms:
|
||||
// import @core:math;
|
||||
// import { A, B as C } from @core:math;
|
||||
if (match(PbsTokenKind.LEFT_BRACE)) {
|
||||
while (!check(PbsTokenKind.RIGHT_BRACE) && !isAtEnd()) {
|
||||
if (match(PbsTokenKind.IDENTIFIER)) {
|
||||
if (match(PbsTokenKind.AS)) {
|
||||
consume(PbsTokenKind.IDENTIFIER, "E_PARSE_EXPECTED_TOKEN",
|
||||
"Expected alias identifier after 'as'");
|
||||
}
|
||||
match(PbsTokenKind.COMMA);
|
||||
continue;
|
||||
}
|
||||
report(peek(), "E_PARSE_UNEXPECTED_TOKEN", "Invalid import item");
|
||||
advance();
|
||||
}
|
||||
consume(PbsTokenKind.RIGHT_BRACE, "E_PARSE_EXPECTED_TOKEN", "Expected '}' in import list");
|
||||
consume(PbsTokenKind.FROM, "E_PARSE_EXPECTED_TOKEN", "Expected 'from' in named import");
|
||||
}
|
||||
|
||||
parseModuleRef();
|
||||
consume(PbsTokenKind.SEMICOLON, "E_PARSE_EXPECTED_TOKEN", "Expected ';' after import");
|
||||
}
|
||||
|
||||
private void parseModuleRef() {
|
||||
consume(PbsTokenKind.AT, "E_PARSE_EXPECTED_TOKEN", "Expected '@' in module reference");
|
||||
consume(PbsTokenKind.IDENTIFIER, "E_PARSE_EXPECTED_TOKEN", "Expected project identifier in module reference");
|
||||
consume(PbsTokenKind.COLON, "E_PARSE_EXPECTED_TOKEN", "Expected ':' in module reference");
|
||||
consume(PbsTokenKind.IDENTIFIER, "E_PARSE_EXPECTED_TOKEN", "Expected module identifier");
|
||||
while (match(PbsTokenKind.SLASH)) {
|
||||
consume(PbsTokenKind.IDENTIFIER, "E_PARSE_EXPECTED_TOKEN", "Expected module path segment after '/'");
|
||||
}
|
||||
}
|
||||
|
||||
private PbsAst.FunctionDecl parseFunction(final PbsToken fnToken) {
|
||||
final var name = consume(PbsTokenKind.IDENTIFIER, "E_PARSE_EXPECTED_TOKEN", "Expected function name");
|
||||
consume(PbsTokenKind.LEFT_PAREN, "E_PARSE_EXPECTED_TOKEN", "Expected '(' after function name");
|
||||
|
||||
final var parameters = new ArrayList<PbsAst.Parameter>();
|
||||
if (!check(PbsTokenKind.RIGHT_PAREN)) {
|
||||
do {
|
||||
final var pStart = peek();
|
||||
final boolean mutable = match(PbsTokenKind.MUT);
|
||||
final var pName = consume(PbsTokenKind.IDENTIFIER, "E_PARSE_EXPECTED_TOKEN", "Expected parameter name");
|
||||
consume(PbsTokenKind.COLON, "E_PARSE_EXPECTED_TOKEN", "Expected ':' after parameter name");
|
||||
final var typeRef = parseTypeRef();
|
||||
parameters.add(new PbsAst.Parameter(
|
||||
pName.lexeme(),
|
||||
mutable,
|
||||
typeRef,
|
||||
span(pStart.start(), typeRef.span().getEnd())));
|
||||
} while (match(PbsTokenKind.COMMA));
|
||||
}
|
||||
consume(PbsTokenKind.RIGHT_PAREN, "E_PARSE_EXPECTED_TOKEN", "Expected ')' after parameter list");
|
||||
|
||||
PbsAst.TypeRef returnType = null;
|
||||
if (match(PbsTokenKind.COLON)) {
|
||||
returnType = parseTypeRef();
|
||||
}
|
||||
|
||||
PbsAst.Expression elseFallback = null;
|
||||
if (match(PbsTokenKind.ELSE)) {
|
||||
elseFallback = parseExpression();
|
||||
}
|
||||
|
||||
final var body = parseBlock();
|
||||
return new PbsAst.FunctionDecl(
|
||||
name.lexeme(),
|
||||
ReadOnlyList.wrap(parameters),
|
||||
returnType,
|
||||
elseFallback,
|
||||
body,
|
||||
span(fnToken.start(), body.span().getEnd()));
|
||||
}
|
||||
|
||||
private PbsAst.TypeRef parseTypeRef() {
|
||||
final var identifier = consume(PbsTokenKind.IDENTIFIER, "E_PARSE_EXPECTED_TOKEN", "Expected type name");
|
||||
return new PbsAst.TypeRef(identifier.lexeme(), span(identifier.start(), identifier.end()));
|
||||
}
|
||||
|
||||
private PbsAst.Block parseBlock() {
|
||||
final var leftBrace = consume(PbsTokenKind.LEFT_BRACE, "E_PARSE_EXPECTED_TOKEN", "Expected '{' to start block");
|
||||
final var statements = new ArrayList<PbsAst.Statement>();
|
||||
while (!check(PbsTokenKind.RIGHT_BRACE) && !isAtEnd()) {
|
||||
statements.add(parseStatement());
|
||||
}
|
||||
final var rightBrace = consume(PbsTokenKind.RIGHT_BRACE, "E_PARSE_EXPECTED_TOKEN", "Expected '}' to end block");
|
||||
return new PbsAst.Block(ReadOnlyList.wrap(statements), span(leftBrace.start(), rightBrace.end()));
|
||||
}
|
||||
|
||||
private PbsAst.Statement parseStatement() {
|
||||
if (match(PbsTokenKind.LET)) {
|
||||
return parseLetStatement(previous());
|
||||
}
|
||||
if (match(PbsTokenKind.RETURN)) {
|
||||
return parseReturnStatement(previous());
|
||||
}
|
||||
return parseExpressionStatement();
|
||||
}
|
||||
|
||||
private PbsAst.Statement parseLetStatement(final PbsToken letToken) {
|
||||
final boolean mutable = match(PbsTokenKind.MUT);
|
||||
final var name = consume(PbsTokenKind.IDENTIFIER, "E_PARSE_EXPECTED_TOKEN", "Expected variable name");
|
||||
|
||||
PbsAst.TypeRef explicitType = null;
|
||||
if (match(PbsTokenKind.COLON)) {
|
||||
explicitType = parseTypeRef();
|
||||
}
|
||||
|
||||
consume(PbsTokenKind.EQUAL, "E_PARSE_EXPECTED_TOKEN", "Expected '=' in let statement");
|
||||
final var initializer = parseExpression();
|
||||
final var semicolon = consume(PbsTokenKind.SEMICOLON, "E_PARSE_EXPECTED_TOKEN", "Expected ';' after let statement");
|
||||
|
||||
return new PbsAst.LetStatement(
|
||||
name.lexeme(),
|
||||
mutable,
|
||||
explicitType,
|
||||
initializer,
|
||||
span(letToken.start(), semicolon.end()));
|
||||
}
|
||||
|
||||
private PbsAst.Statement parseReturnStatement(final PbsToken returnToken) {
|
||||
PbsAst.Expression value = null;
|
||||
if (!check(PbsTokenKind.SEMICOLON)) {
|
||||
value = parseExpression();
|
||||
}
|
||||
final var semicolon = consume(PbsTokenKind.SEMICOLON, "E_PARSE_EXPECTED_TOKEN", "Expected ';' after return");
|
||||
return new PbsAst.ReturnStatement(value, span(returnToken.start(), semicolon.end()));
|
||||
}
|
||||
|
||||
private PbsAst.Statement parseExpressionStatement() {
|
||||
final var expression = parseExpression();
|
||||
final var semicolon = consume(PbsTokenKind.SEMICOLON, "E_PARSE_EXPECTED_TOKEN", "Expected ';' after expression");
|
||||
return new PbsAst.ExpressionStatement(expression, span(expression.span().getStart(), semicolon.end()));
|
||||
}
|
||||
|
||||
private PbsAst.Expression parseExpression() {
|
||||
return parseOr();
|
||||
}
|
||||
|
||||
private PbsAst.Expression parseOr() {
|
||||
var expression = parseAnd();
|
||||
while (match(PbsTokenKind.OR_OR)) {
|
||||
final var operator = previous();
|
||||
final var right = parseAnd();
|
||||
expression = new PbsAst.BinaryExpr(operator.lexeme(), expression, right,
|
||||
span(expression.span().getStart(), right.span().getEnd()));
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
private PbsAst.Expression parseAnd() {
|
||||
var expression = parseEquality();
|
||||
while (match(PbsTokenKind.AND_AND)) {
|
||||
final var operator = previous();
|
||||
final var right = parseEquality();
|
||||
expression = new PbsAst.BinaryExpr(operator.lexeme(), expression, right,
|
||||
span(expression.span().getStart(), right.span().getEnd()));
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
private PbsAst.Expression parseEquality() {
|
||||
var expression = parseComparison();
|
||||
if (match(PbsTokenKind.EQUAL_EQUAL, PbsTokenKind.BANG_EQUAL)) {
|
||||
final var operator = previous();
|
||||
final var right = parseComparison();
|
||||
expression = new PbsAst.BinaryExpr(operator.lexeme(), expression, right,
|
||||
span(expression.span().getStart(), right.span().getEnd()));
|
||||
if (check(PbsTokenKind.EQUAL_EQUAL) || check(PbsTokenKind.BANG_EQUAL)) {
|
||||
report(peek(), "E_PARSE_NON_ASSOC", "Chained equality is not allowed");
|
||||
while (match(PbsTokenKind.EQUAL_EQUAL, PbsTokenKind.BANG_EQUAL)) {
|
||||
parseComparison();
|
||||
}
|
||||
}
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
private PbsAst.Expression parseComparison() {
|
||||
var expression = parseTerm();
|
||||
if (match(PbsTokenKind.LESS, PbsTokenKind.LESS_EQUAL, PbsTokenKind.GREATER, PbsTokenKind.GREATER_EQUAL)) {
|
||||
final var operator = previous();
|
||||
final var right = parseTerm();
|
||||
expression = new PbsAst.BinaryExpr(operator.lexeme(), expression, right,
|
||||
span(expression.span().getStart(), right.span().getEnd()));
|
||||
if (check(PbsTokenKind.LESS) || check(PbsTokenKind.LESS_EQUAL)
|
||||
|| check(PbsTokenKind.GREATER) || check(PbsTokenKind.GREATER_EQUAL)) {
|
||||
report(peek(), "E_PARSE_NON_ASSOC", "Chained comparison is not allowed");
|
||||
while (match(PbsTokenKind.LESS, PbsTokenKind.LESS_EQUAL, PbsTokenKind.GREATER, PbsTokenKind.GREATER_EQUAL)) {
|
||||
parseTerm();
|
||||
}
|
||||
}
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
private PbsAst.Expression parseTerm() {
|
||||
var expression = parseFactor();
|
||||
while (match(PbsTokenKind.PLUS, PbsTokenKind.MINUS)) {
|
||||
final var operator = previous();
|
||||
final var right = parseFactor();
|
||||
expression = new PbsAst.BinaryExpr(operator.lexeme(), expression, right,
|
||||
span(expression.span().getStart(), right.span().getEnd()));
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
private PbsAst.Expression parseFactor() {
|
||||
var expression = parseUnary();
|
||||
while (match(PbsTokenKind.STAR, PbsTokenKind.SLASH, PbsTokenKind.PERCENT)) {
|
||||
final var operator = previous();
|
||||
final var right = parseUnary();
|
||||
expression = new PbsAst.BinaryExpr(operator.lexeme(), expression, right,
|
||||
span(expression.span().getStart(), right.span().getEnd()));
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
private PbsAst.Expression parseUnary() {
|
||||
if (match(PbsTokenKind.BANG, PbsTokenKind.MINUS)) {
|
||||
final var operator = previous();
|
||||
final var right = parseUnary();
|
||||
return new PbsAst.UnaryExpr(
|
||||
operator.lexeme(),
|
||||
right,
|
||||
span(operator.start(), right.span().getEnd()));
|
||||
}
|
||||
return parseCall();
|
||||
}
|
||||
|
||||
private PbsAst.Expression parseCall() {
|
||||
var expression = parsePrimary();
|
||||
|
||||
while (match(PbsTokenKind.LEFT_PAREN)) {
|
||||
final var open = previous();
|
||||
final var arguments = new ArrayList<PbsAst.Expression>();
|
||||
if (!check(PbsTokenKind.RIGHT_PAREN)) {
|
||||
do {
|
||||
arguments.add(parseExpression());
|
||||
} while (match(PbsTokenKind.COMMA));
|
||||
}
|
||||
final var close = consume(PbsTokenKind.RIGHT_PAREN, "E_PARSE_EXPECTED_TOKEN", "Expected ')' after arguments");
|
||||
expression = new PbsAst.CallExpr(
|
||||
expression,
|
||||
ReadOnlyList.wrap(arguments),
|
||||
span(expression.span().getStart(), close.end()));
|
||||
|
||||
// Avoid endless loops on malformed "f((" forms.
|
||||
if (open.start() == close.start()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return expression;
|
||||
}
|
||||
|
||||
private PbsAst.Expression parsePrimary() {
|
||||
if (match(PbsTokenKind.TRUE)) {
|
||||
final var token = previous();
|
||||
return new PbsAst.BoolLiteralExpr(true, span(token.start(), token.end()));
|
||||
}
|
||||
if (match(PbsTokenKind.FALSE)) {
|
||||
final var token = previous();
|
||||
return new PbsAst.BoolLiteralExpr(false, span(token.start(), token.end()));
|
||||
}
|
||||
if (match(PbsTokenKind.INT_LITERAL)) {
|
||||
final var token = previous();
|
||||
return new PbsAst.IntLiteralExpr(parseLongOrDefault(token.lexeme(), 0L), span(token.start(), token.end()));
|
||||
}
|
||||
if (match(PbsTokenKind.FLOAT_LITERAL)) {
|
||||
final var token = previous();
|
||||
return new PbsAst.FloatLiteralExpr(parseDoubleOrDefault(token.lexeme(), 0.0d), span(token.start(), token.end()));
|
||||
}
|
||||
if (match(PbsTokenKind.BOUNDED_LITERAL)) {
|
||||
final var token = previous();
|
||||
final var raw = token.lexeme().substring(0, Math.max(token.lexeme().length() - 1, 0));
|
||||
return new PbsAst.BoundedLiteralExpr(parseIntOrDefault(raw, 0), span(token.start(), token.end()));
|
||||
}
|
||||
if (match(PbsTokenKind.STRING_LITERAL)) {
|
||||
final var token = previous();
|
||||
return new PbsAst.StringLiteralExpr(unescapeString(token.lexeme()), span(token.start(), token.end()));
|
||||
}
|
||||
if (match(PbsTokenKind.IDENTIFIER)) {
|
||||
final var token = previous();
|
||||
return new PbsAst.IdentifierExpr(token.lexeme(), span(token.start(), token.end()));
|
||||
}
|
||||
if (match(PbsTokenKind.LEFT_PAREN)) {
|
||||
final var open = previous();
|
||||
final var expression = parseExpression();
|
||||
final var close = consume(PbsTokenKind.RIGHT_PAREN, "E_PARSE_EXPECTED_TOKEN", "Expected ')' after grouped expression");
|
||||
return new PbsAst.GroupExpr(expression, span(open.start(), close.end()));
|
||||
}
|
||||
|
||||
final var token = peek();
|
||||
report(token, "E_PARSE_UNEXPECTED_TOKEN", "Unexpected token in expression: " + token.kind());
|
||||
advance();
|
||||
return new PbsAst.IntLiteralExpr(0L, span(token.start(), token.end()));
|
||||
}
|
||||
|
||||
private void synchronizeTopLevel() {
|
||||
while (!isAtEnd()) {
|
||||
if (check(PbsTokenKind.FN) || check(PbsTokenKind.IMPORT)) {
|
||||
return;
|
||||
}
|
||||
if (match(PbsTokenKind.SEMICOLON)) {
|
||||
return;
|
||||
}
|
||||
advance();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean match(final PbsTokenKind... kinds) {
|
||||
for (final var kind : kinds) {
|
||||
if (check(kind)) {
|
||||
advance();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private PbsToken consume(final PbsTokenKind kind, final String code, final String message) {
|
||||
if (check(kind)) {
|
||||
return advance();
|
||||
}
|
||||
final var token = peek();
|
||||
report(token, code, message + ", found " + token.kind());
|
||||
if (!isAtEnd()) {
|
||||
return advance();
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
private boolean check(final PbsTokenKind kind) {
|
||||
if (isAtEnd()) return kind == PbsTokenKind.EOF;
|
||||
return peek().kind() == kind;
|
||||
}
|
||||
|
||||
private PbsToken advance() {
|
||||
if (!isAtEnd()) current++;
|
||||
return previous();
|
||||
}
|
||||
|
||||
private boolean isAtEnd() {
|
||||
return peek().kind() == PbsTokenKind.EOF;
|
||||
}
|
||||
|
||||
private PbsToken peek() {
|
||||
return tokens.get(current);
|
||||
}
|
||||
|
||||
private PbsToken previous() {
|
||||
return tokens.get(Math.max(current - 1, 0));
|
||||
}
|
||||
|
||||
private Span span(final long start, final long end) {
|
||||
return new Span(fileId, start, end);
|
||||
}
|
||||
|
||||
private void report(final PbsToken token, final String code, final String message) {
|
||||
issues.report(builder -> builder
|
||||
.error(true)
|
||||
.message("[PBS:%s] %s at %s:[%d,%d)".formatted(code, message, sourceLabel, token.start(), token.end())));
|
||||
}
|
||||
|
||||
private long parseLongOrDefault(final String text, final long fallback) {
|
||||
try {
|
||||
return Long.parseLong(text);
|
||||
} catch (NumberFormatException ignored) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
private int parseIntOrDefault(final String text, final int fallback) {
|
||||
try {
|
||||
return Integer.parseInt(text);
|
||||
} catch (NumberFormatException ignored) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
private double parseDoubleOrDefault(final String text, final double fallback) {
|
||||
try {
|
||||
return Double.parseDouble(text);
|
||||
} catch (NumberFormatException ignored) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
private String unescapeString(final String lexeme) {
|
||||
if (lexeme.length() < 2) {
|
||||
return "";
|
||||
}
|
||||
final var raw = lexeme.substring(1, lexeme.length() - 1);
|
||||
final var sb = new StringBuilder(raw.length());
|
||||
for (int i = 0; i < raw.length(); i++) {
|
||||
final char c = raw.charAt(i);
|
||||
if (c != '\\' || i + 1 >= raw.length()) {
|
||||
sb.append(c);
|
||||
continue;
|
||||
}
|
||||
final char next = raw.charAt(++i);
|
||||
switch (next) {
|
||||
case 'n' -> sb.append('\n');
|
||||
case 'r' -> sb.append('\r');
|
||||
case 't' -> sb.append('\t');
|
||||
case '"' -> sb.append('"');
|
||||
case '\\' -> sb.append('\\');
|
||||
default -> sb.append(next);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+31
-6
@@ -4,22 +4,47 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import p.studio.compiler.messages.BuildingIssueSink;
|
||||
import p.studio.compiler.messages.FrontendPhaseContext;
|
||||
import p.studio.compiler.models.IRBackend;
|
||||
import p.studio.compiler.models.IRFunction;
|
||||
import p.studio.compiler.pbs.PbsFrontendCompiler;
|
||||
import p.studio.utilities.logs.LogAggregator;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
|
||||
@Slf4j
|
||||
public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
private final PbsFrontendCompiler frontendCompiler = new PbsFrontendCompiler();
|
||||
|
||||
@Override
|
||||
public IRBackend compile(final FrontendPhaseContext ctx, final LogAggregator logs, final BuildingIssueSink issues) {
|
||||
final var functions = new ArrayList<IRFunction>();
|
||||
|
||||
for (final var pId : ctx.stack.reverseTopologicalOrder) {
|
||||
for (final var fId : ctx.fileTable.getFiles(pId)) {
|
||||
final var fileIds = ctx.fileTable
|
||||
.getFiles(pId)
|
||||
.stream()
|
||||
.sorted(Comparator.comparing(fId -> ctx.fileTable.get(fId).getCanonPath().toString()))
|
||||
.toList();
|
||||
|
||||
for (final var fId : fileIds) {
|
||||
final var sourceHandle = ctx.fileTable.get(fId);
|
||||
if (!sourceHandle.getFilename().endsWith(".pbs")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
sourceHandle.readUtf8().ifPresentOrElse(
|
||||
utf8Content -> logs.using(log).info("File content: %s".formatted(utf8Content)),
|
||||
() -> issues.report(builder -> builder
|
||||
.error(true)
|
||||
.message("Failed to read file content: %s".formatted(sourceHandle.toString()))));
|
||||
utf8Content -> functions.addAll(frontendCompiler
|
||||
.compileFile(fId, utf8Content, sourceHandle.getCanonPath().toString(), issues)
|
||||
.stream()
|
||||
.toList()),
|
||||
() -> issues.report(builder -> builder
|
||||
.error(true)
|
||||
.message("Failed to read file content: %s".formatted(sourceHandle.toString()))));
|
||||
}
|
||||
}
|
||||
return new IRBackend();
|
||||
|
||||
logs.using(log).debug("PBS frontend lowered %d function(s) to IR".formatted(functions.size()));
|
||||
return new IRBackend(ReadOnlyList.wrap(functions));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user