diff --git a/releases.md b/releases.md index c0cde3b8..33c33e7b 100644 --- a/releases.md +++ b/releases.md @@ -2,7 +2,8 @@ ## Features -- Added new syntax for literal big ints and big decimals: `100000N`, `100000.30M` +- Added new syntax for literal big ints and big decimals: `100000N`, `100000.30M` +- Added support for big ints and big decimals in pattern matching # 0.2.0 diff --git a/src/main/kotlin/novah/ast/Desugar.kt b/src/main/kotlin/novah/ast/Desugar.kt index 4f57b27d..9ddf5a86 100644 --- a/src/main/kotlin/novah/ast/Desugar.kt +++ b/src/main/kotlin/novah/ast/Desugar.kt @@ -535,6 +535,8 @@ class Desugar(private val smod: SModule, private val typeChecker: Typechecker) { is SLiteralPattern.Int64Literal -> LiteralPattern.Int64Literal(e.desugar(locals) as Expr.Int64) is SLiteralPattern.Float32Literal -> LiteralPattern.Float32Literal(e.desugar(locals) as Expr.Float32) is SLiteralPattern.Float64Literal -> LiteralPattern.Float64Literal(e.desugar(locals) as Expr.Float64) + is SLiteralPattern.BigintLiteral -> LiteralPattern.BigintLiteral(e.desugar(locals) as Expr.Bigint) + is SLiteralPattern.BigdecPattern -> LiteralPattern.BigdecLiteral(e.desugar(locals) as Expr.Bigdec) } private fun SLetDef.DefBind.desugar(locals: List, tvars: Map): LetDef { diff --git a/src/main/kotlin/novah/ast/canonical/CanonicalAST.kt b/src/main/kotlin/novah/ast/canonical/CanonicalAST.kt index 6794c895..c2bab7be 100644 --- a/src/main/kotlin/novah/ast/canonical/CanonicalAST.kt +++ b/src/main/kotlin/novah/ast/canonical/CanonicalAST.kt @@ -259,6 +259,8 @@ sealed class LiteralPattern(open val e: Expr) { data class Int64Literal(override val e: Expr.Int64) : LiteralPattern(e) data class Float32Literal(override val e: Expr.Float32) : LiteralPattern(e) data class Float64Literal(override val e: Expr.Float64) : LiteralPattern(e) + data class BigintLiteral(override val e: Expr.Bigint) : LiteralPattern(e) + data class BigdecLiteral(override val e: Expr.Bigdec) : LiteralPattern(e) } fun Pattern.show(): String = when (this) { @@ -285,6 +287,8 @@ fun LiteralPattern.show(): String = when (this) { is LiteralPattern.Int64Literal -> e.v.toString() is LiteralPattern.Float32Literal -> e.v.toString() is LiteralPattern.Float64Literal -> e.v.toString() + is LiteralPattern.BigintLiteral -> e.v.toString() + is LiteralPattern.BigdecLiteral -> e.v.toString() } data class Metadata(val data: Expr.RecordExtend) { diff --git a/src/main/kotlin/novah/ast/source/SourceAST.kt b/src/main/kotlin/novah/ast/source/SourceAST.kt index ebf85622..21d56d6d 100644 --- a/src/main/kotlin/novah/ast/source/SourceAST.kt +++ b/src/main/kotlin/novah/ast/source/SourceAST.kt @@ -307,6 +307,8 @@ sealed class LiteralPattern { data class Int64Literal(val e: Expr.Int64) : LiteralPattern() data class Float32Literal(val e: Expr.Float32) : LiteralPattern() data class Float64Literal(val e: Expr.Float64) : LiteralPattern() + data class BigintLiteral(val e: Expr.Bigint) : LiteralPattern() + data class BigdecPattern(val e: Expr.Bigdec) : LiteralPattern() } typealias Row = Type diff --git a/src/main/kotlin/novah/formatter/Formatter.kt b/src/main/kotlin/novah/formatter/Formatter.kt index 9b5d63ac..d360c534 100644 --- a/src/main/kotlin/novah/formatter/Formatter.kt +++ b/src/main/kotlin/novah/formatter/Formatter.kt @@ -303,6 +303,8 @@ class Formatter { is LiteralPattern.Int64Literal -> show(p.e) is LiteralPattern.Float32Literal -> show(p.e) is LiteralPattern.Float64Literal -> show(p.e) + is LiteralPattern.BigintLiteral -> show(p.e) + is LiteralPattern.BigdecPattern -> show(p.e) } private fun show(l: LetDef, isFor: Boolean = false): String { diff --git a/src/main/kotlin/novah/frontend/Parser.kt b/src/main/kotlin/novah/frontend/Parser.kt index 64b7fbc9..00f360a0 100644 --- a/src/main/kotlin/novah/frontend/Parser.kt +++ b/src/main/kotlin/novah/frontend/Parser.kt @@ -965,6 +965,14 @@ class Parser( iter.next() Pattern.LiteralP(LiteralPattern.Float64Literal(Expr.Float64(tk.value.v, tk.value.text)), tk.span) } + is BigintT -> { + iter.next() + Pattern.LiteralP(LiteralPattern.BigintLiteral(Expr.Bigint(tk.value.v, tk.value.text)), tk.span) + } + is BigdecT -> { + iter.next() + Pattern.LiteralP(LiteralPattern.BigdecPattern(Expr.Bigdec(tk.value.v, tk.value.text)), tk.span) + } is CharT -> { iter.next() Pattern.LiteralP(LiteralPattern.CharLiteral(Expr.CharE(tk.value.c, tk.value.raw)), tk.span) diff --git a/src/main/kotlin/novah/frontend/matching/PatternMatchingCompiler.kt b/src/main/kotlin/novah/frontend/matching/PatternMatchingCompiler.kt index 0d793012..d0530d02 100644 --- a/src/main/kotlin/novah/frontend/matching/PatternMatchingCompiler.kt +++ b/src/main/kotlin/novah/frontend/matching/PatternMatchingCompiler.kt @@ -231,6 +231,8 @@ class PatternMatchingCompiler(private val ctorCache: MutableMap is LiteralPattern.Int64Literal -> mkPrimCtor(l.e.v.toString()) is LiteralPattern.Float32Literal -> mkPrimCtor(l.e.v.toString()) is LiteralPattern.Float64Literal -> mkPrimCtor(l.e.v.toString()) + is LiteralPattern.BigintLiteral -> mkPrimCtor(l.e.v.toString()) + is LiteralPattern.BigdecLiteral -> mkPrimCtor(l.e.v.toString()) } Pat.PCon(con) } diff --git a/src/main/kotlin/novah/optimize/Optimizer.kt b/src/main/kotlin/novah/optimize/Optimizer.kt index 4e74250d..1b696f07 100644 --- a/src/main/kotlin/novah/optimize/Optimizer.kt +++ b/src/main/kotlin/novah/optimize/Optimizer.kt @@ -531,10 +531,19 @@ class Optimizer(private val ast: CModule, private val ctorCache: MutableMap PatternResult(tru) is Pattern.LiteralP -> { val lit = p.lit.e.convert(locals) - if (p.lit is LiteralPattern.StringLiteral) { - PatternResult(Expr.NativeMethod(eqString, lit, listOf(exp), boolType, exp.span)) - } else { - PatternResult(Expr.OperatorApp("==", listOf(lit, exp), boolType, exp.span)) + when (p.lit) { + is LiteralPattern.StringLiteral -> { + PatternResult(Expr.NativeMethod(eqString, lit, listOf(exp), boolType, exp.span)) + } + is LiteralPattern.BigintLiteral -> { + PatternResult(Expr.NativeMethod(bigIntEquals, lit, listOf(exp), boolType, exp.span)) + } + is LiteralPattern.BigdecLiteral -> { + PatternResult(Expr.NativeMethod(bigDecEquals, lit, listOf(exp), boolType, exp.span)) + } + else -> { + PatternResult(Expr.OperatorApp("==", listOf(lit, exp), boolType, exp.span)) + } } } is Pattern.Regex -> { @@ -817,6 +826,8 @@ class Optimizer(private val ast: CModule, private val ctorCache: MutableMap