rework some bits of the compiler to work more appropriately

This commit is contained in:
Alex Zenla
2025-07-16 23:14:25 -07:00
parent 17b1aa44c0
commit a48fac4581
8 changed files with 27 additions and 6 deletions

View File

@ -1,5 +1,7 @@
package gay.pizza.pork.common package gay.pizza.pork.common
import java.util.stream.IntStream
class IndentBuffer( class IndentBuffer(
val buffer: StringBuilder = StringBuilder(), val buffer: StringBuilder = StringBuilder(),
indent: String = " " indent: String = " "
@ -13,4 +15,12 @@ class IndentBuffer(
} }
override fun toString(): String = buffer.toString() override fun toString(): String = buffer.toString()
override fun chars(): IntStream {
return buffer.chars()
}
override fun codePoints(): IntStream {
return buffer.codePoints()
}
} }

View File

@ -11,6 +11,11 @@ abstract class IndentTracked(val indent: String) {
emit(indentLevelText) emit(indentLevelText)
} }
fun emitIndented(text: String) {
emitIndent()
emit(text)
}
fun emitIndentedLine(line: String) { fun emitIndentedLine(line: String) {
emitIndent() emitIndent()
emitLine(line) emitLine(line)

View File

@ -46,7 +46,11 @@ class CompilableSymbol(val compilableSlab: CompilableSlab, val scopeSymbol: Scop
} }
val type = if (what is NativeFunctionDescriptor) { val type = if (what is NativeFunctionDescriptor) {
IrDefinitionType.NativeFunction IrDefinitionType.NativeFunction
} else IrDefinitionType.CodeFunction } else if (scopeSymbol.definition is LetDefinition) {
IrDefinitionType.Variable
} else {
IrDefinitionType.CodeFunction
}
val irCodeElement = irCodeEmitter.visit(what) val irCodeElement = irCodeEmitter.visit(what)
val irCodeBlock = if (irCodeElement is IrCodeBlock) { val irCodeBlock = if (irCodeElement is IrCodeBlock) {
irCodeElement irCodeElement

View File

@ -42,7 +42,9 @@ class Compiler(val world: World) {
} }
} }
fun compileIrWorld(): IrWorld { fun compileIrWorld(entryPointSymbol: CompilableSymbol): IrWorld {
val usedSymbolSet = mutableSetOf<CompilableSymbol>()
contributeCompiledSymbols(usedSymbolSet, entryPointSymbol.scopeSymbol, entryPointSymbol)
val slabs = mutableListOf<IrSlab>() val slabs = mutableListOf<IrSlab>()
for (slab in world.slabs) { for (slab in world.slabs) {
slabs.add(compilableSlabs.of(slab).compiledIrSlab) slabs.add(compilableSlabs.of(slab).compiledIrSlab)

View File

@ -8,6 +8,6 @@ func fib(n) {
} }
export func main() { export func main() {
let result = fib(20) let result = fib(28)
println(result) println(result)
} }

View File

@ -18,5 +18,5 @@ include(
":ffi", ":ffi",
":tool", ":tool",
":minimal", ":minimal",
":support:pork-idea" ":support:pork-idea",
) )

View File

@ -39,7 +39,7 @@ class CompileCommand : CliktCommand("compile") {
val compiledSlab = compiler.compilableSlabs.of(slab) val compiledSlab = compiler.compilableSlabs.of(slab)
val compiledMain = compiledSlab.resolve(Symbol("main")) val compiledMain = compiledSlab.resolve(Symbol("main"))
?: throw RuntimeException("'main' function not found.") ?: throw RuntimeException("'main' function not found.")
val irWorld = compiler.compileIrWorld() val irWorld = compiler.compileIrWorld(compiledMain)
val compiledWorld = compiler.compile(compiledMain) val compiledWorld = compiler.compile(compiledMain)
if (showIrCode) { if (showIrCode) {
printCompiledIr(irWorld) printCompiledIr(irWorld)

View File

@ -16,7 +16,7 @@ class RootCommand : CliktCommand("pork") {
AttributeCommand(), AttributeCommand(),
ScopeAnalysisCommand(), ScopeAnalysisCommand(),
CopyStdlibCommand(), CopyStdlibCommand(),
CompileCommand() CompileCommand(),
) )
} }