diff options
| author | Michael Williamson <mike@zwobble.org> | 2025-01-12 21:25:32 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2025-01-12 21:25:32 +0000 |
| commit | 6f554d8e265ad2664c97d9d8d6efef2f53e9eb5f (patch) | |
| tree | 8d2453bd70ce1143be425448a16c4a93d8767461 /src/main/java/org/shedlang/compiler/ast | |
| parent | eef0470389d88de6169a6962f14a9d422175e4c2 (diff) | |
Parse native function declarations
Diffstat (limited to 'src/main/java/org/shedlang/compiler/ast')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/ast/untyped/UntypedNamespaceStatementNode.java | 2 | ||||
| -rw-r--r-- | src/main/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNode.java | 104 |
2 files changed, 105 insertions, 1 deletions
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<UntypedTypeLevelExpressionNode> 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 + ); + } + } +} |
