Skip to content

Commit

Permalink
Fix array indexing solver check
Browse files Browse the repository at this point in the history
Had the arguments flipped
  • Loading branch information
DvvCz committed Aug 30, 2023
1 parent 8055fa7 commit ac98024
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/ramattra-core/src/compiler/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export function analyze(src: string): IREvent[] {
} else if (kind == "index") {
const [obj, index] = [analyzeExpr(expr.data[1]), analyzeExpr(expr.data[2])];

if (!solver.satisfies(obj.type, array(any)))
expr.throw(`Cannot index non array type`);
if (!solver.satisfies(array(any), obj.type))
expr.throw(`Cannot index type of ${reprType(obj.type)}`);

if (!solver.satisfies(index.type, number))
expr.throw(`Can only index an array with a number`);
Expand Down
11 changes: 11 additions & 0 deletions packages/ramattra-core/tests/analyze.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { analyze } from "../src/compiler/analyzer.js";
import { expect, it } from "vitest";

it("should allow indexing arrays", () => {
expect(() => analyze(`
event client(ply) {
let arr = [1, 2, 3];
let element = arr[1];
}
`)).not.toThrowError("Cannot index type of");
});

0 comments on commit ac98024

Please sign in to comment.