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
No known key found for this signature in database
GPG Key ID: 067B238899B51269
8 changed files with 27 additions and 6 deletions

View File

@ -1,5 +1,7 @@
package gay.pizza.pork.common
import java.util.stream.IntStream
class IndentBuffer(
val buffer: StringBuilder = StringBuilder(),
indent: String = " "
@ -13,4 +15,12 @@ class IndentBuffer(
}
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)
}
fun emitIndented(text: String) {
emitIndent()
emit(text)
}
fun emitIndentedLine(line: String) {
emitIndent()
emitLine(line)

View File

@ -46,7 +46,11 @@ class CompilableSymbol(val compilableSlab: CompilableSlab, val scopeSymbol: Scop
}
val type = if (what is NativeFunctionDescriptor) {
IrDefinitionType.NativeFunction
} else IrDefinitionType.CodeFunction
} else if (scopeSymbol.definition is LetDefinition) {
IrDefinitionType.Variable
} else {
IrDefinitionType.CodeFunction
}
val irCodeElement = irCodeEmitter.visit(what)
val irCodeBlock = if (irCodeElement is IrCodeBlock) {
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>()
for (slab in world.slabs) {
slabs.add(compilableSlabs.of(slab).compiledIrSlab)

View File

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

View File

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

View File

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

View File

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