diff --git a/packages/ramattra-core/src/compiler/analyzer.ts b/packages/ramattra-core/src/compiler/analyzer.ts index 17d1c73..9b20a02 100644 --- a/packages/ramattra-core/src/compiler/analyzer.ts +++ b/packages/ramattra-core/src/compiler/analyzer.ts @@ -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`); diff --git a/packages/ramattra-core/tests/analyze.test.ts b/packages/ramattra-core/tests/analyze.test.ts new file mode 100644 index 0000000..5d506ea --- /dev/null +++ b/packages/ramattra-core/tests/analyze.test.ts @@ -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"); +}); \ No newline at end of file