aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-10-22 13:27:31 +0100
committerMichael Williamson <mike@zwobble.org>2023-10-22 13:27:31 +0100
commit296fa6ee9ee63089603c8bb13bb7d3346f85a037 (patch)
tree0754151b4533fd9c9c315ef361879fdc12160cdd
parent5831a308f4d8f9e83821fffff67a2141cc76d26b (diff)
Add explicit visibility
-rw-r--r--examples/01-true/src/True.shed2
-rw-r--r--examples/02-hello-world/src/HelloWorld.shed2
-rw-r--r--examples/03-val/src/HelloWorld.shed2
-rw-r--r--examples/04-val-as-namespace-statement/src/HelloWorld.shed2
-rw-r--r--examples/05-string-add/src/HelloWorld.shed2
-rw-r--r--examples/06-if/src/Counting.shed2
-rw-r--r--examples/07-mutual-recursion/src/MutualRecursion.shed2
-rw-r--r--examples/08-blocks/src/Counting.shed2
-rw-r--r--examples/09-var/src/HelloWorld.shed2
-rw-r--r--examples/10-while/src/Counting.shed2
-rw-r--r--examples/11-tuples/src/HelloWorld.shed2
-rw-r--r--examples/12-named-args/src/HelloWorld.shed2
-rw-r--r--examples/13-target-ignore/src/HelloWorld.shed2
-rw-r--r--examples/14-structs/src/HelloWorld.shed2
-rw-r--r--examples/15-struct-hoisting/src/HelloWorld.shed2
-rw-r--r--examples/16-sums/src/HelloWorld.shed2
-rw-r--r--examples/17-import/src/HelloWorld.shed2
-rw-r--r--examples/17-import/src/Message.shed2
-rw-r--r--src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNode.java12
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/DeclaredVariable.java2
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/DeclaredVariables.java7
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java5
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java10
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java49
24 files changed, 83 insertions, 38 deletions
diff --git a/examples/01-true/src/True.shed b/examples/01-true/src/True.shed
index 2161327a..a7caae19 100644
--- a/examples/01-true/src/True.shed
+++ b/examples/01-true/src/True.shed
@@ -1,2 +1,2 @@
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
}
diff --git a/examples/02-hello-world/src/HelloWorld.shed b/examples/02-hello-world/src/HelloWorld.shed
index e2d8d187..fb5f2385 100644
--- a/examples/02-hello-world/src/HelloWorld.shed
+++ b/examples/02-hello-world/src/HelloWorld.shed
@@ -1,3 +1,3 @@
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
print!("hello, world\n");
}
diff --git a/examples/03-val/src/HelloWorld.shed b/examples/03-val/src/HelloWorld.shed
index b12229b7..794eb242 100644
--- a/examples/03-val/src/HelloWorld.shed
+++ b/examples/03-val/src/HelloWorld.shed
@@ -1,4 +1,4 @@
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
val message = "hello, world\n";
print!(message);
}
diff --git a/examples/04-val-as-namespace-statement/src/HelloWorld.shed b/examples/04-val-as-namespace-statement/src/HelloWorld.shed
index 2e38b501..1ffdcf9f 100644
--- a/examples/04-val-as-namespace-statement/src/HelloWorld.shed
+++ b/examples/04-val-as-namespace-statement/src/HelloWorld.shed
@@ -1,5 +1,5 @@
val message = "hello, world\n";
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
print!(message);
}
diff --git a/examples/05-string-add/src/HelloWorld.shed b/examples/05-string-add/src/HelloWorld.shed
index 07657574..07c2d514 100644
--- a/examples/05-string-add/src/HelloWorld.shed
+++ b/examples/05-string-add/src/HelloWorld.shed
@@ -1,3 +1,3 @@
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
print!("hello, " + "world\n");
}
diff --git a/examples/06-if/src/Counting.shed b/examples/06-if/src/Counting.shed
index 060482b5..fa7d3cbe 100644
--- a/examples/06-if/src/Counting.shed
+++ b/examples/06-if/src/Counting.shed
@@ -8,7 +8,7 @@ fun message(count: Int) -> String {
}
}
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
print!(message(1));
print!(message(2));
print!(message(3));
diff --git a/examples/07-mutual-recursion/src/MutualRecursion.shed b/examples/07-mutual-recursion/src/MutualRecursion.shed
index 86450afe..e72a15fc 100644
--- a/examples/07-mutual-recursion/src/MutualRecursion.shed
+++ b/examples/07-mutual-recursion/src/MutualRecursion.shed
@@ -1,4 +1,4 @@
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
val message = a(5);
print!(message);
}
diff --git a/examples/08-blocks/src/Counting.shed b/examples/08-blocks/src/Counting.shed
index 1aeeb4b3..79496448 100644
--- a/examples/08-blocks/src/Counting.shed
+++ b/examples/08-blocks/src/Counting.shed
@@ -15,7 +15,7 @@ fun message(count: Int) -> String {
message + message2
}
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
print!(message(1));
print!(message(2));
print!(message(3));
diff --git a/examples/09-var/src/HelloWorld.shed b/examples/09-var/src/HelloWorld.shed
index 22f8a4ca..a1bafbf2 100644
--- a/examples/09-var/src/HelloWorld.shed
+++ b/examples/09-var/src/HelloWorld.shed
@@ -1,4 +1,4 @@
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
var message = "hello, ";
assign message = message + "world\n";
print!(message);
diff --git a/examples/10-while/src/Counting.shed b/examples/10-while/src/Counting.shed
index 81d637a2..aa95f536 100644
--- a/examples/10-while/src/Counting.shed
+++ b/examples/10-while/src/Counting.shed
@@ -8,7 +8,7 @@ fun message(count: Int) -> String {
}
}
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
var count = 1;
while (count != 4) {
diff --git a/examples/11-tuples/src/HelloWorld.shed b/examples/11-tuples/src/HelloWorld.shed
index 46c22ba8..ba93f6b2 100644
--- a/examples/11-tuples/src/HelloWorld.shed
+++ b/examples/11-tuples/src/HelloWorld.shed
@@ -1,4 +1,4 @@
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
#(val part1, val part2) = messageParts();
print!(part1);
print!(part2);
diff --git a/examples/12-named-args/src/HelloWorld.shed b/examples/12-named-args/src/HelloWorld.shed
index 5640cffa..411b1ab7 100644
--- a/examples/12-named-args/src/HelloWorld.shed
+++ b/examples/12-named-args/src/HelloWorld.shed
@@ -1,4 +1,4 @@
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
// Args not ordered by name, in different order from params.
printMessage!(.b = ", ", .a = "hello", .c = "world\n");
}
diff --git a/examples/13-target-ignore/src/HelloWorld.shed b/examples/13-target-ignore/src/HelloWorld.shed
index 69dd5c98..002dbd44 100644
--- a/examples/13-target-ignore/src/HelloWorld.shed
+++ b/examples/13-target-ignore/src/HelloWorld.shed
@@ -1,4 +1,4 @@
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
#(val part1, _, _, val part2) = messageParts();
print!(part1);
print!(part2);
diff --git a/examples/14-structs/src/HelloWorld.shed b/examples/14-structs/src/HelloWorld.shed
index a9263f85..94d2cfa4 100644
--- a/examples/14-structs/src/HelloWorld.shed
+++ b/examples/14-structs/src/HelloWorld.shed
@@ -1,4 +1,4 @@
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
val messageParts = getMessageParts();
print!(messageParts.part1);
print!(messageParts.part2);
diff --git a/examples/15-struct-hoisting/src/HelloWorld.shed b/examples/15-struct-hoisting/src/HelloWorld.shed
index d9caa802..5cb6c2e2 100644
--- a/examples/15-struct-hoisting/src/HelloWorld.shed
+++ b/examples/15-struct-hoisting/src/HelloWorld.shed
@@ -1,6 +1,6 @@
val messageParts = MessageParts(.part1 = "hello, ", .part2 = "world\n");
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
print!(messageParts.part1);
print!(messageParts.part2);
}
diff --git a/examples/16-sums/src/HelloWorld.shed b/examples/16-sums/src/HelloWorld.shed
index 3dd7038d..58177d9f 100644
--- a/examples/16-sums/src/HelloWorld.shed
+++ b/examples/16-sums/src/HelloWorld.shed
@@ -4,7 +4,7 @@ struct None() {}
sumtype Option = Some | None;
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
printOption!(Some(.value = "hello, world!"));
printOption!(None());
}
diff --git a/examples/17-import/src/HelloWorld.shed b/examples/17-import/src/HelloWorld.shed
index 202e4948..8cebf604 100644
--- a/examples/17-import/src/HelloWorld.shed
+++ b/examples/17-import/src/HelloWorld.shed
@@ -1,5 +1,5 @@
import Message;
-fun main() ! Io -> Unit {
+pub fun main() ! Io -> Unit {
print!(Message.message);
}
diff --git a/examples/17-import/src/Message.shed b/examples/17-import/src/Message.shed
index 9aeb381c..db03110a 100644
--- a/examples/17-import/src/Message.shed
+++ b/examples/17-import/src/Message.shed
@@ -1 +1 @@
-val message = "hello, world\n";
+pub val message = "hello, world\n";
diff --git a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNode.java b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNode.java
index 65e4f84c..c81a4409 100644
--- a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNode.java
+++ b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNode.java
@@ -121,5 +121,17 @@ public record UntypedFunctionDefinitionNode(
this.body.plus(statement)
);
}
+
+ public Builder withVisibility(Visibility visibility) {
+ return new Builder(
+ this.name,
+ visibility,
+ this.positionalParams,
+ this.namedParams,
+ this.effect,
+ this.returnType,
+ this.body
+ );
+ }
}
}
diff --git a/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariable.java b/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariable.java
index 08d45542..bed7501e 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariable.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariable.java
@@ -1,10 +1,12 @@
package org.shedlang.compiler.typechecker;
import org.shedlang.compiler.sources.Source;
+import org.shedlang.compiler.types.Visibility;
public record DeclaredVariable(
String name,
VariableType variableType,
+ Visibility visibility,
Source source
) {
}
diff --git a/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariables.java b/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariables.java
index 2950b170..f9878a53 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariables.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariables.java
@@ -2,6 +2,7 @@ package org.shedlang.compiler.typechecker;
import org.pcollections.PVector;
import org.shedlang.compiler.ast.untyped.*;
+import org.shedlang.compiler.types.Visibility;
import java.util.List;
@@ -53,6 +54,7 @@ class DeclaredVariables {
return variables.plus(new DeclaredVariable(
import_.variableName(),
VariableType.IMPORT,
+ Visibility.PRIVATE,
import_.source()
));
}
@@ -91,6 +93,7 @@ class DeclaredVariables {
variables.plus(new DeclaredVariable(
functionDefinition.name(),
VariableType.CONSTANT,
+ functionDefinition.visibility(),
functionDefinition.source()
));
@@ -98,6 +101,7 @@ class DeclaredVariables {
variables.plus(new DeclaredVariable(
struct.name(),
VariableType.CONSTANT,
+ Visibility.PRIVATE,
struct.source()
));
@@ -105,6 +109,7 @@ class DeclaredVariables {
variables.plus(new DeclaredVariable(
sumType.name(),
VariableType.CONSTANT,
+ Visibility.PRIVATE,
sumType.source()
));
};
@@ -129,6 +134,7 @@ class DeclaredVariables {
variables.plus(new DeclaredVariable(
var.name(),
VariableType.VAR,
+ Visibility.PRIVATE,
var.source()
));
@@ -136,6 +142,7 @@ class DeclaredVariables {
variables.plus(new DeclaredVariable(
val.name(),
VariableType.VAL,
+ val.visibility(),
val.source()
));
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
index 9040f9df..f4033702 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
@@ -787,7 +787,6 @@ public class TypeChecker {
var declaredVariables = DeclaredVariables.forImports(untyped.imports(), P.vector());
declaredVariables = DeclaredVariables.forNamespaceStatements(untyped.body(), declaredVariables);
- // TODO: explicit exports
var namespaceFinalContext = context;
var fieldTypes = declaredVariables.stream()
@@ -798,6 +797,10 @@ public class TypeChecker {
case VAL -> true;
case VAR -> false;
})
+ .filter(variable -> switch (variable.visibility()) {
+ case PUBLIC -> true;
+ case PRIVATE -> false;
+ })
.map(variable -> Map.entry(
variable.name(),
namespaceFinalContext.compileTimeLookup(variable.name())
diff --git a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java
index dde00bca..c5a27329 100644
--- a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java
+++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java
@@ -195,16 +195,6 @@ public class TypedNodeMatchers {
return TypedNamespaceNodeMatcher.empty();
}
- public static Matcher<TypedNamespaceNode> isTypedNamespaceNode(
- Matcher<Iterable<? extends TypedNamespaceStatementNode>> body,
- Matcher<? super Iterable<? extends String>> fieldNames
- ) {
- return allOf(
- has("body", x -> x.body(), body),
- has("fieldNames", x -> x.fieldNames(), fieldNames)
- );
- }
-
public static Matcher<TypedParamNode> isTypedParamNode(
String name,
Matcher<TypedTypeLevelExpressionNode<?>> type
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java
index bddd7ba6..85515dad 100644
--- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java
+++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java
@@ -29,7 +29,9 @@ public class TypeCheckerNamespaceTests {
assertThat(
result.value(),
- isTypedNamespaceNode(isSequence(), isSequence())
+ isTypedNamespaceNode()
+ .withImports(isSequence())
+ .withBody(isSequence())
);
}
@@ -70,12 +72,9 @@ public class TypeCheckerNamespaceTests {
assertThat(
result.value(),
- isTypedNamespaceNode(
- isSequence(
- isTypedFunctionDefinitionNode().withName("f")
- ),
- isSequence(equalTo("f"))
- )
+ isTypedNamespaceNode().withBody(isSequence(
+ isTypedFunctionDefinitionNode().withName("f")
+ ))
);
}
@@ -493,7 +492,7 @@ public class TypeCheckerNamespaceTests {
}
@Test
- public void typesOfFieldsAreStored() {
+ public void namespaceFieldsIncludesPublicFields() {
var namespaceName = NamespaceName.of("Example");
var untyped = Untyped.namespace(
namespaceName,
@@ -501,6 +500,38 @@ public class TypeCheckerNamespaceTests {
UntypedFunctionDefinitionNode.builder()
.withName("f")
.withReturnType(Untyped.typeLevelReference("Unit"))
+ .withVisibility(Visibility.PRIVATE)
+ .build(),
+ UntypedFunctionDefinitionNode.builder()
+ .withName("g")
+ .withReturnType(Untyped.typeLevelReference("Unit"))
+ .withVisibility(Visibility.PUBLIC)
+ .build()
+ )
+ );
+ var context = TypeCheckerContext.EMPTY
+ .enterGlobal(P.map(Map.entry("Unit", Types.metaType(Types.UNIT))));
+
+ var result = TypeChecker.typeCheckNamespace(untyped, context);
+
+ assertThat(result.value().fieldNames(), isSequence(equalTo("g")));
+ }
+
+ @Test
+ public void namespaceTypeIncludesPublicFields() {
+ var namespaceName = NamespaceName.of("Example");
+ var untyped = Untyped.namespace(
+ namespaceName,
+ List.of(
+ UntypedFunctionDefinitionNode.builder()
+ .withName("f")
+ .withReturnType(Untyped.typeLevelReference("Unit"))
+ .withVisibility(Visibility.PRIVATE)
+ .build(),
+ UntypedFunctionDefinitionNode.builder()
+ .withName("g")
+ .withReturnType(Untyped.typeLevelReference("Unit"))
+ .withVisibility(Visibility.PUBLIC)
.build()
)
);
@@ -515,7 +546,7 @@ public class TypeCheckerNamespaceTests {
assertThat(
namespaceTypeInfo,
equalTo(NamespaceTypeInfo.fromFields(P.orderedMap(
- Map.entry("f", new FunctionType(List.of(), List.of(), Effects.NONE, Types.UNIT))
+ Map.entry("g", new FunctionType(List.of(), List.of(), Effects.NONE, Types.UNIT))
)))
);
}