From 4310c4a044b9d4477b0a15850b32ec25307613a2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Jun 2023 11:26:30 +0100 Subject: Check number of positional args --- .../shedlang/compiler/typechecker/TypeChecker.java | 11 ++++++++++ .../compiler/typechecker/UnexpectedTypeError.java | 25 ---------------------- .../compiler/typechecker/UnknownVariableError.java | 17 --------------- .../typechecker/errors/UnexpectedTypeError.java | 25 ++++++++++++++++++++++ .../typechecker/errors/UnknownVariableError.java | 17 +++++++++++++++ .../errors/WrongNumberOfPositionalArgs.java | 23 ++++++++++++++++++++ 6 files changed, 76 insertions(+), 42 deletions(-) delete mode 100644 src/main/java/org/shedlang/compiler/typechecker/UnexpectedTypeError.java delete mode 100644 src/main/java/org/shedlang/compiler/typechecker/UnknownVariableError.java create mode 100644 src/main/java/org/shedlang/compiler/typechecker/errors/UnexpectedTypeError.java create mode 100644 src/main/java/org/shedlang/compiler/typechecker/errors/UnknownVariableError.java create mode 100644 src/main/java/org/shedlang/compiler/typechecker/errors/WrongNumberOfPositionalArgs.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java index e89bdd61..fee93041 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java @@ -2,6 +2,9 @@ package org.shedlang.compiler.typechecker; import org.shedlang.compiler.ast.typed.*; import org.shedlang.compiler.ast.untyped.*; +import org.shedlang.compiler.typechecker.errors.UnexpectedTypeError; +import org.shedlang.compiler.typechecker.errors.UnknownVariableError; +import org.shedlang.compiler.typechecker.errors.WrongNumberOfPositionalArgs; import org.shedlang.compiler.typechecker.typesets.TypeSets; import org.shedlang.compiler.types.FunctionType; import org.shedlang.compiler.types.Type; @@ -29,6 +32,14 @@ public class TypeChecker { TypeCheckerContext context, FunctionType receiverType ) { + if (args.positional().size() != receiverType.positionalParams().size()) { + throw new WrongNumberOfPositionalArgs( + receiverType.positionalParams().size(), + args.positional().size(), + args.source() + ); + } + var typedPositionalArgs = args.positional().stream() .map(arg -> typeCheckExpression(arg, context)) .toList(); diff --git a/src/main/java/org/shedlang/compiler/typechecker/UnexpectedTypeError.java b/src/main/java/org/shedlang/compiler/typechecker/UnexpectedTypeError.java deleted file mode 100644 index 51cc6652..00000000 --- a/src/main/java/org/shedlang/compiler/typechecker/UnexpectedTypeError.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.shedlang.compiler.typechecker; - -import org.shedlang.compiler.errors.SourceError; -import org.shedlang.compiler.sources.Source; -import org.shedlang.compiler.typechecker.typesets.TypeSet; -import org.shedlang.compiler.types.Type; - -public class UnexpectedTypeError extends SourceError { - private final TypeSet expected; - private final Type actual; - - public UnexpectedTypeError(TypeSet expected, Type actual, Source source) { - super("Expected " + expected.describe() + " but was " + actual.describe(), source); - this.expected = expected; - this.actual = actual; - } - - public TypeSet expected() { - return expected; - } - - public Type actual() { - return actual; - } -} diff --git a/src/main/java/org/shedlang/compiler/typechecker/UnknownVariableError.java b/src/main/java/org/shedlang/compiler/typechecker/UnknownVariableError.java deleted file mode 100644 index 91ab3fae..00000000 --- a/src/main/java/org/shedlang/compiler/typechecker/UnknownVariableError.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.shedlang.compiler.typechecker; - -import org.shedlang.compiler.errors.SourceError; -import org.shedlang.compiler.sources.Source; - -public class UnknownVariableError extends SourceError { - private final String variableName; - - public UnknownVariableError(String variableName, Source source) { - super("Unknown variable: " + variableName, source); - this.variableName = variableName; - } - - public String variableName() { - return variableName; - } -} diff --git a/src/main/java/org/shedlang/compiler/typechecker/errors/UnexpectedTypeError.java b/src/main/java/org/shedlang/compiler/typechecker/errors/UnexpectedTypeError.java new file mode 100644 index 00000000..02355f2a --- /dev/null +++ b/src/main/java/org/shedlang/compiler/typechecker/errors/UnexpectedTypeError.java @@ -0,0 +1,25 @@ +package org.shedlang.compiler.typechecker.errors; + +import org.shedlang.compiler.errors.SourceError; +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.typechecker.typesets.TypeSet; +import org.shedlang.compiler.types.Type; + +public class UnexpectedTypeError extends SourceError { + private final TypeSet expected; + private final Type actual; + + public UnexpectedTypeError(TypeSet expected, Type actual, Source source) { + super("Expected " + expected.describe() + " but was " + actual.describe(), source); + this.expected = expected; + this.actual = actual; + } + + public TypeSet expected() { + return expected; + } + + public Type actual() { + return actual; + } +} diff --git a/src/main/java/org/shedlang/compiler/typechecker/errors/UnknownVariableError.java b/src/main/java/org/shedlang/compiler/typechecker/errors/UnknownVariableError.java new file mode 100644 index 00000000..bc85c18c --- /dev/null +++ b/src/main/java/org/shedlang/compiler/typechecker/errors/UnknownVariableError.java @@ -0,0 +1,17 @@ +package org.shedlang.compiler.typechecker.errors; + +import org.shedlang.compiler.errors.SourceError; +import org.shedlang.compiler.sources.Source; + +public class UnknownVariableError extends SourceError { + private final String variableName; + + public UnknownVariableError(String variableName, Source source) { + super("Unknown variable: " + variableName, source); + this.variableName = variableName; + } + + public String variableName() { + return variableName; + } +} diff --git a/src/main/java/org/shedlang/compiler/typechecker/errors/WrongNumberOfPositionalArgs.java b/src/main/java/org/shedlang/compiler/typechecker/errors/WrongNumberOfPositionalArgs.java new file mode 100644 index 00000000..723c3f7d --- /dev/null +++ b/src/main/java/org/shedlang/compiler/typechecker/errors/WrongNumberOfPositionalArgs.java @@ -0,0 +1,23 @@ +package org.shedlang.compiler.typechecker.errors; + +import org.shedlang.compiler.errors.SourceError; +import org.shedlang.compiler.sources.Source; + +public class WrongNumberOfPositionalArgs extends SourceError { + private final int expected; + private final int actual; + + public WrongNumberOfPositionalArgs(int expected, int actual, Source source) { + super("Expected " + expected + " positional arguments but got " + actual, source); + this.expected = expected; + this.actual = actual; + } + + public int expected() { + return expected; + } + + public int actual() { + return actual; + } +} -- cgit v1.2.3