Skip to content

Commit

Permalink
Move combinators from scanner to stream
Browse files Browse the repository at this point in the history
  • Loading branch information
phischu committed Dec 18, 2024
1 parent dfcad1a commit 00c64d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
26 changes: 0 additions & 26 deletions libraries/common/scanner.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,6 @@ def read[A](): A / { Scan[A], stop } = {
def scanner[A] { scanner: () => Unit / Scan[A] }: Unit / read[A] =
returning::scanner[A, Unit]{scanner}


def many[A] { action: () => A / stop }: Unit / emit[A] =
exhaustively { do emit(action()) }

def some[A] { action: () => A / stop }: Unit / { emit[A], stop } = {
do emit(action())
many { action() }
}

def optionally { program: () => Unit / stop }: Bool =
returning::optionally[Unit]{program}.isDefined

def attempt[R] { program: () => R / stop } { fallback: () => R }: R =
try {
program()
} with stop {
fallback()
}

def expect(message: String) { scanner: () => Unit / stop }: Unit / Exception[WrongFormat] =
returning::expect[Unit](message){scanner}

Expand Down Expand Up @@ -161,13 +142,6 @@ def scanner[A, R] { scanner: () => R / Scan[A] }: R / read[A] = {
}
}

def optionally[R] { program: () => R / stop }: Option[R] =
try {
Some(program())
} with stop {
None()
}

def expect[R](message: String) { scanner: () => R / stop }: R / Exception[WrongFormat] =
attempt { scanner() } { wrongFormat(message) }

Expand Down
25 changes: 25 additions & 0 deletions libraries/common/stream.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ def exhaustively { program: () => Unit / stop }: Unit =
()
}

def many[A] { action: () => A / stop }: Unit / emit[A] =
exhaustively { do emit(action()) }

def some[A] { action: () => A / stop }: Unit / { emit[A], stop } = {
do emit(action())
many { action() }
}

def optionally { program: () => Unit / stop }: Bool =
returning::optionally[Unit]{program}.isDefined

def attempt[R] { program: () => R / stop } { fallback: () => R }: R =
try {
program()
} with stop {
fallback()
}


/// In Effekt lower bounds are inclusive and upper bounds are exclusive
record Indexed[A](index: Int, value: A)
Expand Down Expand Up @@ -348,6 +366,13 @@ def boundary[R] { program: () => R / stop }: Option[R] =
None()
}

def optionally[R] { program: () => R / stop }: Option[R] =
try {
Some(program())
} with stop {
None()
}

def index[A, R] { stream: () => R / emit[A] }: R / emit[Indexed[A]] = {
var i = 0;
try {
Expand Down

0 comments on commit 00c64d3

Please sign in to comment.