Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
feat: better errors for invalid argument types
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Oct 7, 2023
1 parent 88f41ea commit 51e7319
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ yarn run <tool>
- Basics (numbers, strings, booleans)
- String interpolations
- Array literals/types
- Object literals
- Object literals/types
- Array indexing
- Binary expressions (including compound assignment)
- Unary expressions (including `++` and `--`)
Expand All @@ -87,4 +87,5 @@ yarn run <tool>
- Type aliases (e.x. `type number = int | float`)
- `typeof`, `is`
- `value is in array`
- Type guards (`value is T`, `"property" is in object`)
- Classes
5 changes: 3 additions & 2 deletions src/code-analysis/type-checker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,11 @@ export class TypeChecker implements AST.Visitor.BoundExpression<void>, AST.Visit
this.check(expr.callee);

const type = <FunctionType>expr.callee.type;
const expectedTypes = Array.from(type.parameterTypes.values());
const expectedTypes = Array.from(type.parameterTypes.entries());
for (const arg of expr.args) {
const [parameterName, expectedType] = expectedTypes[expr.args.indexOf(arg)];
this.check(arg);
this.assert(arg, arg.type, expectedTypes[expr.args.indexOf(arg)]);
this.assert(arg, arg.type, expectedType, `Argument type '${arg.type.toString()}' is not assignable to type '${expectedType.toString()}' of parameter '${parameterName}'`);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/runtime/intrinsics/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import Intrinsic from "../values/intrinsic";
export default class Eval extends Intrinsic.Function {
public readonly name = "eval";
public readonly returnType = new SingularType("any");
public readonly argumentTypes = { code: new SingularType("string") };
public readonly argumentTypes = { source: new SingularType("string") };

public call(code: string): ValueType {
public call(source: string): ValueType {
const enclosingResultOutputEnabled = this.interpreter!.host.executionOptions.outputResult;
this.interpreter!.host.executionOptions.outputResult = false;
const result = this.interpreter!.host.doString(code);
const result = this.interpreter!.host.doString(source);
this.interpreter!.host.executionOptions.outputResult = enclosingResultOutputEnabled;
return result;
}
Expand Down

0 comments on commit 51e7319

Please sign in to comment.