From 6f554d8e265ad2664c97d9d8d6efef2f53e9eb5f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 12 Jan 2025 21:25:32 +0000 Subject: Parse native function declarations --- .../ast/untyped/UntypedNamespaceStatementNode.java | 2 +- .../UntypedNativeFunctionDeclarationNode.java | 104 +++++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNode.java (limited to 'src/main/java/org/shedlang/compiler/ast') diff --git a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedNamespaceStatementNode.java b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedNamespaceStatementNode.java index 8d94d5b8..c4981b68 100644 --- a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedNamespaceStatementNode.java +++ b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedNamespaceStatementNode.java @@ -1,4 +1,4 @@ package org.shedlang.compiler.ast.untyped; -public sealed interface UntypedNamespaceStatementNode extends UntypedNode permits UntypedAssignNode, UntypedEffectDefinitionNode, UntypedFunctionDefinitionNode, UntypedStructDefinitionNode, UntypedSumTypeDefinitionNode { +public sealed interface UntypedNamespaceStatementNode extends UntypedNode permits UntypedAssignNode, UntypedEffectDefinitionNode, UntypedFunctionDefinitionNode, UntypedNativeFunctionDeclarationNode, UntypedStructDefinitionNode, UntypedSumTypeDefinitionNode { } diff --git a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNode.java b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNode.java new file mode 100644 index 00000000..989d6044 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNode.java @@ -0,0 +1,104 @@ +package org.shedlang.compiler.ast.untyped; + +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.sources.Sources; +import org.shedlang.compiler.types.Visibility; + +import java.util.Optional; + +public record UntypedNativeFunctionDeclarationNode( + String name, + Visibility visibility, + UntypedFunctionSignatureNode signature, + Source source +) implements UntypedNamespaceStatementNode, UntypedVariableDeclarationNode { + public static Builder builder() { + return new Builder( + "f", + Visibility.PRIVATE, + UntypedFunctionSignatureNode.builder() + ); + } + + public record Builder( + String name, + Visibility visibility, + UntypedFunctionSignatureNode.Builder signature + ) { + public UntypedNativeFunctionDeclarationNode build() { + return new UntypedNativeFunctionDeclarationNode( + name, + visibility, + signature.build(), + Sources.NULL + ); + } + + public Builder withName(String name) { + return new Builder( + name, + this.visibility, + this.signature + ); + } + + public Builder addPositionalParam(String name, UntypedTypeLevelExpressionNode type) { + return addPositionalParam(name, Optional.of(type)); + } + + public Builder addPositionalParam( + String name, + Optional type + ) { + return new Builder( + this.name, + this.visibility, + this.signature.addPositionalParam(name, type) + ); + } + + public Builder addNamedParam(String name, UntypedTypeLevelExpressionNode type) { + return new Builder( + this.name, + this.visibility, + this.signature.addNamedParam(name, type) + ); + } + + public Builder withEffect(UntypedTypeLevelExpressionNode effect) { + return new Builder( + this.name, + this.visibility, + this.signature.withEffect(effect) + ); + } + + public Builder withReturnTypeUnit() { + return withReturnType(new UntypedTypeLevelReferenceNode("Unit", Sources.NULL)); + } + + public Builder withReturnType(UntypedTypeLevelExpressionNode returnType) { + return new Builder( + this.name, + this.visibility, + this.signature.withReturnType(returnType) + ); + } + + public Builder withoutReturnType() { + return new Builder( + this.name, + this.visibility, + this.signature.withoutReturnType() + ); + } + + public Builder withVisibility(Visibility visibility) { + return new Builder( + this.name, + visibility, + this.signature + ); + } + } +} -- cgit v1.2.3