Skip to content

Commit

Permalink
tokenizer: remove TokenStream, use sequences instead
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Oct 17, 2023
1 parent 15f5f31 commit e3bfa3f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 52 deletions.
17 changes: 10 additions & 7 deletions minimal/src/main/kotlin/gay/pizza/pork/minimal/Tool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ abstract class Tool {
fun parse(attribution: NodeAttribution = DiscardNodeAttribution): CompilationUnit =
Parser(tokenize(), attribution).parseCompilationUnit()

fun highlight(scheme: HighlightScheme): List<Highlight> =
Highlighter(scheme).highlight(tokenize().stream())
fun highlight(scheme: HighlightScheme): Sequence<Highlight> =
Highlighter(scheme).highlight(tokenize())

fun reprint(): String = buildString { visit(Printer(this)) }

Expand All @@ -44,6 +44,13 @@ abstract class Tool {
return resultingScope.value("main") as CallableFunction
}

fun loadMainFunctionStandard(scope: Scope, quiet: Boolean = false): CallableFunction =
loadMainFunction(scope, setupEvaluator = {
addNativeProvider("internal", InternalNativeProvider(quiet = quiet))
addNativeProvider("ffi", FfiNativeProvider())
addNativeProvider("java", JavaNativeProvider())
})

fun buildWorld(): World {
val fileContentSource = createContentSource()
val dynamicImportSource = DynamicImportSource()
Expand All @@ -54,11 +61,7 @@ abstract class Tool {
}

fun run(scope: Scope, quiet: Boolean = false) {
val main = loadMainFunction(scope, setupEvaluator = {
addNativeProvider("internal", InternalNativeProvider(quiet = quiet))
addNativeProvider("ffi", FfiNativeProvider())
addNativeProvider("java", JavaNativeProvider())
})
val main = loadMainFunctionStandard(scope, quiet = quiet)
main.call(emptyList(), CallStack())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gay.pizza.pork.tokenizer

class Highlighter(val scheme: HighlightScheme) {
fun highlight(stream: TokenStream): List<Highlight> =
stream.tokens.map { scheme.highlight(it) }
fun highlight(source: TokenSource): Sequence<Highlight> =
source.sequence().map { scheme.highlight(it) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gay.pizza.pork.tokenizer

class ListTokenSource(val tokens: List<Token>) : TokenSource {
private var index = 0

override val currentIndex: Int
get() = index

override fun next(): Token {
if (index == tokens.size) {
return tokens.last()
}
val char = tokens[index]
index++
return char
}

override fun peek(): Token {
if (index == tokens.size) {
return tokens.last()
}
return tokens[index]
}

override fun peekTypeAhead(ahead: Int): TokenType {
val calculated = index + ahead
if (calculated >= tokens.size) {
return tokens.last().type
}
return tokens[calculated].type
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,17 @@ interface TokenSource : PeekableSource<Token> {
return tokens
}

fun stream(): TokenStream = TokenStream(consumeAllRemainingTokens())
fun sequence(): Sequence<Token> {
var endFlag = false
return generateSequence {
if (endFlag) {
return@generateSequence null
}
val token = next()
if (token.type == TokenType.EndOfFile) {
endFlag = true
token
} else token
}
}
}

This file was deleted.

This file was deleted.

6 changes: 1 addition & 5 deletions tool/src/main/kotlin/gay/pizza/pork/tool/RunCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ class RunCommand : CliktCommand(help = "Run Program", name = "run") {
override fun run() {
val tool = FileTool(PlatformFsProvider.resolve(path))
val scope = Scope.root()
val main = tool.loadMainFunction(scope, setupEvaluator = {
addNativeProvider("internal", InternalNativeProvider(quiet = quiet))
addNativeProvider("ffi", FfiNativeProvider())
addNativeProvider("java", JavaNativeProvider())
})
val main = tool.loadMainFunctionStandard(scope, quiet = quiet)

if (dumpScope) {
val functionContext = main as FunctionContext
Expand Down

0 comments on commit e3bfa3f

Please sign in to comment.