Skip to content

Commit

Permalink
refactor: prelude.parser.number
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Sep 22, 2024
1 parent 3ddc301 commit 8d49c02
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 12 deletions.
4 changes: 1 addition & 3 deletions 2015/day16.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package y2015.day16

import prelude.*
import prelude.parser.*

import cats.parse.Parser
import cats.parse.Parser.*
import cats.parse.Rfc5234.{digit, alpha}

type Clue = Map[String, Int]

val number: Parser[Int] =
digit.rep.string.map(_.toInt)

val ident: Parser[String] =
alpha.rep.string

Expand Down
5 changes: 2 additions & 3 deletions 2015/day6.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package y2015.day6

import prelude.*
import prelude.parser.*

import utils.*
import java.awt.image.BufferedImage

Expand Down Expand Up @@ -51,9 +53,6 @@ class Grid[T](
import cats.parse.Parser.*
import cats.parse.Rfc5234.{digit, sp}

val number: Parser[Int] =
digit.rep.string.map(_.toInt)

val coord: Parser[Point] =
(number <* char(',')) ~ number map Point.apply

Expand Down
9 changes: 5 additions & 4 deletions 2015/day7.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package y2015.day7

import scala.collection.mutable.HashMap
import prelude.*
import prelude.parser.*
import utils.uint.*
import utils.*

Expand All @@ -24,8 +25,8 @@ object Parser {

def keyword(s: String): Parser[Unit] = string(s).surroundedBy(sp.rep0)

val number: Parser[UInt16] = digit.rep.string.map(x => UInt16(x.toInt))
val signal: Parser[Expr] = number.map(Expr.Emit.apply)
val u16: Parser[UInt16] = number.map(UInt16.apply)
val signal: Parser[Expr] = u16.map(Expr.Emit.apply)
val literal: Parser[String] = charIn('a' to 'z').rep.string
val wire: Parser[Expr] = literal.map(Expr.Var.apply)
val value: Parser[Expr] = signal | wire
Expand All @@ -35,9 +36,9 @@ object Parser {
val and = ((value <* keyword("AND")) ~ value).map(Expr.And.apply) ~ to
val or = ((value <* keyword("OR")) ~ value).map(Expr.Or.apply) ~ to
val lshift =
((value <* keyword("LSHIFT")) ~ number).map(Expr.LShift.apply) ~ to
((value <* keyword("LSHIFT")) ~ u16).map(Expr.LShift.apply) ~ to
val rshift =
((value <* keyword("RSHIFT")) ~ number).map(Expr.RShift.apply) ~ to
((value <* keyword("RSHIFT")) ~ u16).map(Expr.RShift.apply) ~ to
val not = (keyword("NOT") *> value).map(Expr.Not.apply) ~ to
val ops =
oneOf(List(connection, and, or, lshift, rshift, not).map(_.backtrack))
Expand Down
2 changes: 1 addition & 1 deletion 2015/day7.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Day7Tests extends FunSuite:
import Parser.*

test("atoms"):
assertEquals(number.parse("123"), Right("", UInt16(123)))
assertEquals(u16.parse("123"), Right("", UInt16(123)))
assertEquals(signal.parse("123"), Right("", Expr.Emit(UInt16(123))))
assertEquals(wire.parse("abc"), Right("", Expr.Var("abc")))

Expand Down
2 changes: 1 addition & 1 deletion 2015/day9.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package y2015.day9

import prelude.*
import prelude.parser.*

import cats.parse.Parser
import cats.parse.Parser.*
import cats.parse.Rfc5234.alpha

import y2015.day6.number
import y2015.day7.Parser.keyword

val city: Parser[String] = alpha.rep.string
Expand Down
7 changes: 7 additions & 0 deletions prelude/parser.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package prelude.parser

import cats.parse.Parser
import cats.parse.Rfc5234.{digit, alpha}

val number: Parser[Int] =
digit.rep.string.map(_.toInt)

0 comments on commit 8d49c02

Please sign in to comment.