Skip to content

Commit

Permalink
Codegen: complex con patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Apr 23, 2024
1 parent a297fda commit 0446a33
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/codegen/js/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,14 @@ export const emitPatternExprCondition = (patternExpr: PatternExpr, ctx: Context,
switch (patternExpr.kind) {
case 'con-pattern': {
const variantName = patternExpr.identifier.names.at(-1)!.value
const cond = `${sVar}.$noisVariant===${jsString(variantName)}`
// TODO: nested patterns
return cond
const conds = [
`${sVar}.$noisVariant===${jsString(variantName)}`,
...patternExpr.fieldPatterns.flatMap(f => {
if (!f.pattern) return []
return [emitPatternExprCondition(f.pattern.expr, ctx, `${sVar}.value.${f.name.value}`)]
})
]
return conds.join('&&')
}
case 'list-pattern':
const conds = [
Expand Down
19 changes: 19 additions & 0 deletions src/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,25 @@ pub fn main() {
_ { false }
}
println(a.trace())
}`
}
const res = run(await compile(files))
expect(res.stderr.toString()).toEqual('')
expect(res.stdout.toString()).toEqual('true\n')
})

it('con-pattern', async () => {
const files = {
'mod.no': `\
type Foo { A(), B(b: Int) }
pub fn main() {
let a = match B(6) {
A() { false }
B(b: 5) { false }
B(b) { true }
_ { false }
}
println(a.trace())
}`
}
const res = run(await compile(files))
Expand Down
2 changes: 1 addition & 1 deletion src/semantic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const checkModule = (module: Module, ctx: Context): void => {
}
ctx.moduleStack.push(module)

const resolvedRefs = []
const resolvedRefs: VirtualUseExpr[] = []
for (const useExpr of module.references!) {
const ref = checkUseExpr(useExpr, ctx)
if (!ref) continue
Expand Down

0 comments on commit 0446a33

Please sign in to comment.