Skip to content

Commit 204e2ba

Browse files
- upgraded compiler-utils lib
- added TypeCheckContext as arg to all type check functions
1 parent 4cb247f commit 204e2ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+762
-428
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
},
6767
"dependencies": {
6868
"@helios-lang/codec-utils": "^0.3.4",
69-
"@helios-lang/compiler-utils": "^0.5.15",
69+
"@helios-lang/compiler-utils": "^0.5.17",
7070
"@helios-lang/ir": "0.3.9",
7171
"@helios-lang/type-utils": "^0.3.0",
7272
"@helios-lang/uplc": "^0.7.17"

pnpm-lock.yaml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/expressions/AnyTypeExpr.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { AnyType } from "../typecheck/index.js"
33
import { Expr } from "./Expr.js"
44

55
/**
6-
* @typedef {import("@helios-lang/compiler-utils").Site} Site
6+
* @import { Site } from "@helios-lang/compiler-utils"
7+
* @import { TypeCheckContext } from "../index.js"
78
* @typedef {import("../typecheck/index.js").EvalEntity} EvalEntity
89
*/
910

@@ -19,10 +20,11 @@ export class AnyTypeExpr extends Expr {
1920
}
2021

2122
/**
22-
* @param {Scope} scope
23+
* @param {TypeCheckContext} _ctx
24+
* @param {Scope} _scope
2325
* @returns {EvalEntity}
2426
*/
25-
evalInternal(scope) {
27+
evalInternal(_ctx, _scope) {
2628
return new AnyType()
2729
}
2830

src/expressions/AnyValueExpr.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { AnyEntity } from "../typecheck/index.js"
55
import { Expr } from "./Expr.js"
66

77
/**
8-
* @typedef {import("@helios-lang/compiler-utils").Site} Site
9-
* @typedef {import("@helios-lang/ir").SourceMappedStringI} SourceMappedStringI
8+
* @import { Site } from "@helios-lang/compiler-utils"
9+
* @import { SourceMappedStringI } from "@helios-lang/ir"
10+
* @import { TypeCheckContext } from "../index.js"
1011
* @typedef {import("../typecheck/index.js").Instance} Instance
1112
*/
1213

@@ -22,10 +23,11 @@ export class AnyValueExpr extends Expr {
2223
}
2324

2425
/**
26+
* @param {TypeCheckContext} ctx
2527
* @param {Scope} _scope
2628
* @returns {Instance}
2729
*/
28-
evalInternal(_scope) {
30+
evalInternal(ctx, _scope) {
2931
return new AnyEntity()
3032
}
3133

src/expressions/AssignExpr.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import { Expr } from "./Expr.js"
1111
import { PathExpr } from "./PathExpr.js"
1212

1313
/**
14-
* @typedef {import("@helios-lang/compiler-utils").Site} Site
15-
* @typedef {import("@helios-lang/ir").SourceMappedStringI} SourceMappedStringI
14+
* @import { Site } from "@helios-lang/compiler-utils"
15+
* @import { SourceMappedStringI } from "@helios-lang/ir"
16+
* @import { TypeCheckContext } from "../index.js"
1617
* @typedef {import("../typecheck/index.js").EvalEntity} EvalEntity
1718
*/
1819

@@ -47,24 +48,26 @@ export class AssignExpr extends ChainExpr {
4748
}
4849

4950
/**
51+
* @param {TypeCheckContext} ctx
5052
* @param {Scope} scope
5153
* @returns {EvalEntity}
5254
*/
53-
evalInternal(scope) {
55+
evalInternal(ctx, scope) {
5456
const subScope = new Scope(scope, scope.allowShadowing)
5557

56-
let upstreamVal = this.upstreamExpr.eval(scope)
58+
let upstreamVal = this.upstreamExpr.eval(ctx, scope)
5759

5860
if (upstreamVal && upstreamVal.asTyped) {
5961
if (new VoidType().isBaseOf(upstreamVal.asTyped.type)) {
60-
throw makeTypeError(
62+
ctx.errors.type(
6163
this.upstreamExpr.site,
6264
"can't assign to unit type"
6365
)
6466
}
6567

6668
if (this._nameType.hasType() || this._nameType.isTuple()) {
6769
this._nameType.evalInAssignExpr(
70+
ctx,
6871
subScope,
6972
expectDefined(upstreamVal.asTyped.type.asType),
7073
0
@@ -90,13 +93,14 @@ export class AssignExpr extends ChainExpr {
9093
}
9194
} else if (this._nameType.hasType()) {
9295
// this is the fallback case if the upstream has itself a typeerror
93-
this._nameType.evalInAssignExpr(subScope, undefined, 0)
96+
this._nameType.evalInAssignExpr(ctx, subScope, undefined, 0)
9497
} else {
95-
throw makeTypeError(this.upstreamExpr.site, "rhs isn't an instance")
98+
ctx.errors.type(this.upstreamExpr.site, "rhs isn't an instance")
99+
96100
subScope.set(this._nameType.name, new DataEntity(new AnyType()))
97101
}
98102

99-
const downstreamVal = this.downstreamExpr.eval(subScope)
103+
const downstreamVal = this.downstreamExpr.eval(ctx, subScope)
100104

101105
subScope.assertAllUsed()
102106

src/expressions/BinaryExpr.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { expectDefined } from "@helios-lang/type-utils"
44
import { TAB, ToIRContext } from "../codegen/index.js"
55
import { Scope } from "../scopes/index.js"
66
import { Expr } from "./Expr.js"
7+
import { AnyType, DataEntity } from "../typecheck/common.js"
78

89
/**
910
* @import { SymbolToken, Word } from "@helios-lang/compiler-utils"
10-
* @typedef {import("@helios-lang/ir").SourceMappedStringI} SourceMappedStringI
11+
* @import { SourceMappedStringI } from "@helios-lang/ir"
12+
* @import { TypeCheckContext } from "../index.js"
1113
* @typedef {import("../typecheck/index.js").EvalEntity} EvalEntity
1214
*/
1315

@@ -140,27 +142,32 @@ export class BinaryExpr extends Expr {
140142
}
141143

142144
/**
145+
* @param {TypeCheckContext} ctx
143146
* @param {Scope} scope
144147
* @returns {EvalEntity}
145148
*/
146-
evalInternal(scope) {
147-
const a_ = this._a.eval(scope)
148-
const b_ = this._b.eval(scope)
149+
evalInternal(ctx, scope) {
150+
const a_ = this._a.eval(ctx, scope)
151+
const b_ = this._b.eval(ctx, scope)
149152

150153
const a = a_.asInstance
151154
if (!a) {
152-
throw makeTypeError(
155+
ctx.errors.type(
153156
this._a.site,
154157
`lhs of ${this._op.toString()} not an instance`
155158
)
159+
160+
return new DataEntity(new AnyType())
156161
}
157162

158163
const b = b_.asInstance
159164
if (!b) {
160-
throw makeTypeError(
165+
ctx.errors.type(
161166
this._b.site,
162167
`rhs of ${this._op.toString()} not an instance`
163168
)
169+
170+
return new DataEntity(new AnyType())
164171
}
165172

166173
for (let swap of this.isCommutative() ? [false, true] : [false]) {
@@ -190,10 +197,12 @@ export class BinaryExpr extends Expr {
190197
}
191198
}
192199

193-
throw makeTypeError(
200+
ctx.errors.type(
194201
this.site,
195202
`'${a.type.toString()} ${this._op.toString()} ${b.type.toString()}' undefined`
196203
)
204+
205+
return new DataEntity(new AnyType())
197206
}
198207

199208
/**

src/expressions/CallArgExpr.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Expr } from "./Expr.js"
44

55
/**
66
* @import { Site, Token, Word } from "@helios-lang/compiler-utils"
7+
* @import { TypeCheckContext } from "../index.js"
78
* @typedef {import("../typecheck/index.js").EvalEntity} EvalEntity
89
*/
910

@@ -16,7 +17,7 @@ import { Expr } from "./Expr.js"
1617
* isNamed(): boolean
1718
* isLiteral(): boolean
1819
* toString(): string
19-
* eval(scope: Scope): EvalEntity
20+
* eval(ctx: TypeCheckContext, scope: Scope): EvalEntity
2021
* }} CallArgExprI
2122
*/
2223

@@ -102,10 +103,11 @@ export class CallArgExpr {
102103
}
103104

104105
/**
106+
* @param {TypeCheckContext} ctx
105107
* @param {Scope} scope
106108
* @returns {EvalEntity}
107109
*/
108-
eval(scope) {
109-
return this._valueExpr.eval(scope)
110+
eval(ctx, scope) {
111+
return this._valueExpr.eval(ctx, scope)
110112
}
111113
}

src/expressions/CallExpr.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { expectDefined } from "@helios-lang/type-utils"
44
import { ToIRContext } from "../codegen/index.js"
55
import { Scope } from "../scopes/index.js"
66
import {
7+
AnyType,
8+
DataEntity,
79
FuncType,
810
IntType,
911
ParametricFunc,
@@ -18,7 +20,8 @@ import { PathExpr } from "./PathExpr.js"
1820

1921
/**
2022
* @import { Site } from "@helios-lang/compiler-utils"
21-
* @typedef {import("@helios-lang/ir").SourceMappedStringI} SourceMappedStringI
23+
* @import { SourceMappedStringI } from "@helios-lang/ir"
24+
* @import { TypeCheckContext } from "../index.js"
2225
* @typedef {import("../typecheck/index.js").EvalEntity} EvalEntity
2326
* @typedef {import("../typecheck/index.js").Func} Func
2427
* @typedef {import("../typecheck/index.js").Type} Type
@@ -120,19 +123,21 @@ export class CallExpr extends Expr {
120123
}
121124

122125
/**
126+
* @param {TypeCheckContext} ctx
123127
* @param {Scope} scope
124128
* @returns {EvalEntity}
125129
*/
126-
evalInternal(scope) {
127-
const fnVal = this._fnExpr.eval(scope)
130+
evalInternal(ctx, scope) {
131+
const fnVal = this._fnExpr.eval(ctx, scope)
128132

129133
const argVals = this._argExprs.map((ae, i) => {
130-
const av_ = ae.eval(scope)
134+
const av_ = ae.eval(ctx, scope)
131135

132136
const av = av_.asTyped
133137

134138
if (!av) {
135-
throw makeTypeError(ae.site, `arg ${i + 1} not an instance`)
139+
ctx.errors.type(ae.site, `arg ${i + 1} not an instance`)
140+
return new DataEntity(new AnyType())
136141
}
137142

138143
return av
@@ -192,11 +197,12 @@ export class CallExpr extends Expr {
192197
viableCasts
193198
)
194199
} else {
195-
throw makeTypeError(
200+
ctx.errors.type(
196201
this._fnExpr.site,
197-
198202
`unable to call ${fnVal.toString()} (returned by ${this._fnExpr.toString()})`
199203
)
204+
205+
return new DataEntity(new AnyType())
200206
}
201207
}
202208

src/expressions/ChainExpr.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import { ErrorType, VoidType } from "../typecheck/index.js"
66
import { Expr } from "./Expr.js"
77

88
/**
9-
* @typedef {import("@helios-lang/compiler-utils").Site} Site
10-
* @typedef {import("@helios-lang/ir").SourceMappedStringI} SourceMappedStringI
9+
* @import { Site } from "@helios-lang/compiler-utils"
10+
* @import { SourceMappedStringI } from "@helios-lang/ir"
11+
* @import { TypeCheckContext } from "../index.js"
1112
* @typedef {import("../typecheck/index.js").EvalEntity} EvalEntity
1213
*/
1314

@@ -43,36 +44,34 @@ export class ChainExpr extends Expr {
4344
}
4445

4546
/**
47+
* @param {TypeCheckContext} ctx
4648
* @param {Scope} scope
4749
* @returns {EvalEntity}
4850
*/
49-
evalInternal(scope) {
50-
const upstreamVal_ = this.upstreamExpr.eval(scope)
51+
evalInternal(ctx, scope) {
52+
const upstreamVal_ = this.upstreamExpr.eval(ctx, scope)
5153

5254
if (upstreamVal_) {
5355
const upstreamVal = upstreamVal_.asTyped
5456

5557
if (!upstreamVal) {
56-
throw makeTypeError(
57-
this.upstreamExpr.site,
58-
"upstream isn't typed"
59-
)
58+
ctx.errors.type(this.upstreamExpr.site, "upstream isn't typed")
6059
} else {
6160
if (new ErrorType().isBaseOf(upstreamVal.type)) {
62-
throw makeTypeError(
61+
ctx.errors.type(
6362
this.downstreamExpr.site,
6463
"unreachable code (upstream always throws error)"
6564
)
6665
} else if (!new VoidType().isBaseOf(upstreamVal.type)) {
67-
throw makeTypeError(
66+
ctx.errors.type(
6867
this.upstreamExpr.site,
6968
"unexpected return value (hint: use '='"
7069
)
7170
}
7271
}
7372
}
7473

75-
return this.downstreamExpr.eval(scope)
74+
return this.downstreamExpr.eval(ctx, scope)
7675
}
7776

7877
/**

0 commit comments

Comments
 (0)