Skip to content

Commit

Permalink
Codegen: proper stubs for literal patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Apr 20, 2024
1 parent a1c8cad commit 42d9858
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/codegen/js/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ export const emitPattern = (pattern: Pattern, ctx: Context, assignVar: string, p
case 'float-literal':
case 'bool-literal':
case 'identifier':
return jsError(pattern.expr.kind)
return emitToken(`${jsError(pattern.expr.kind).value};`)
case 'hole':
return emitToken('')
default:
Expand Down
3 changes: 2 additions & 1 deletion src/codegen/js/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export const jsVariable = (name: string, emit?: EmitNode, pub = false): EmitNode
}

export const jsError = (message?: string): EmitToken => {
return emitToken(`(() => { throw Error(${jsString(message ?? '')}); })()`)
const msg = `codegen error: ${message ?? ''}`
return emitToken(`(() => { throw Error(${jsString(msg)}); })()`)
}

export const emitIntersperse = (
Expand Down
6 changes: 6 additions & 0 deletions src/semantic/exhaust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ const matchPattern = (pattern: PatternExpr, tree: MatchTree, ctx: Context): bool
case 'string-literal':
case 'char-literal':
case 'unary-expr':
case 'string-interpolated':
case 'int-literal':
case 'float-literal':
case 'bool-literal':
case 'identifier':
case 'list-pattern':
case 'operand-expr':
// match the node
return true
Expand Down
26 changes: 13 additions & 13 deletions src/semantic/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ export const checkPattern = (
// TODO: typed hole reporting, Haskell style: https://wiki.haskell.org/GHC/Typed_holes
expr.type = expectedType
break
case 'operand-expr':
if (!refutable) {
addError(ctx, unexpectedRefutablePatternError(ctx, pattern.expr))
break
}
checkOperand(expr.operand, ctx)
expr.type = expr.operand.type
break
case 'list-pattern':
if (!refutable) {
addError(ctx, unexpectedRefutablePatternError(ctx, pattern.expr))
Expand All @@ -63,8 +55,18 @@ export const checkPattern = (
})
expr.type ??= unknownType
break
case 'int-literal':
case 'float-literal':
case 'string-literal':
case 'bool-literal':
if (!refutable) {
addError(ctx, unexpectedRefutablePatternError(ctx, pattern.expr))
break
}
checkOperand(expr, ctx)
break
default:
unreachable()
unreachable(expr.kind)
}

if (pattern.name) {
Expand All @@ -73,10 +75,8 @@ export const checkPattern = (
scope.definitions.set(defKey(nameDef), nameDef)
}

if (expectedType.kind !== 'malleable-type') {
if (!isAssignable(expectedType, expr.type!, ctx)) {
addError(ctx, typeError(ctx, pattern.expr, expr.type!, expectedType))
}
if (!isAssignable(expectedType, expr.type!, ctx)) {
addError(ctx, typeError(ctx, pattern.expr, expr.type!, expectedType))
}
}

Expand Down

0 comments on commit 42d9858

Please sign in to comment.