From a48fac4581fae6003b6c753627668e3e89a8081e Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Wed, 16 Jul 2025 23:14:25 -0700 Subject: [PATCH] rework some bits of the compiler to work more appropriately --- .../main/kotlin/gay/pizza/pork/common/IndentBuffer.kt | 10 ++++++++++ .../main/kotlin/gay/pizza/pork/common/IndentTracked.kt | 5 +++++ .../kotlin/gay/pizza/pork/compiler/CompilableSymbol.kt | 6 +++++- .../main/kotlin/gay/pizza/pork/compiler/Compiler.kt | 4 +++- examples/fib.pork | 2 +- settings.gradle.kts | 2 +- .../main/kotlin/gay/pizza/pork/tool/CompileCommand.kt | 2 +- .../src/main/kotlin/gay/pizza/pork/tool/RootCommand.kt | 2 +- 8 files changed, 27 insertions(+), 6 deletions(-) diff --git a/common/src/main/kotlin/gay/pizza/pork/common/IndentBuffer.kt b/common/src/main/kotlin/gay/pizza/pork/common/IndentBuffer.kt index 4790d36..168eb4d 100644 --- a/common/src/main/kotlin/gay/pizza/pork/common/IndentBuffer.kt +++ b/common/src/main/kotlin/gay/pizza/pork/common/IndentBuffer.kt @@ -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() + } } diff --git a/common/src/main/kotlin/gay/pizza/pork/common/IndentTracked.kt b/common/src/main/kotlin/gay/pizza/pork/common/IndentTracked.kt index 03c86bc..828969f 100644 --- a/common/src/main/kotlin/gay/pizza/pork/common/IndentTracked.kt +++ b/common/src/main/kotlin/gay/pizza/pork/common/IndentTracked.kt @@ -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) diff --git a/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSymbol.kt b/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSymbol.kt index d27fabb..a010627 100644 --- a/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSymbol.kt +++ b/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSymbol.kt @@ -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 diff --git a/compiler/src/main/kotlin/gay/pizza/pork/compiler/Compiler.kt b/compiler/src/main/kotlin/gay/pizza/pork/compiler/Compiler.kt index 336cb27..b673e1b 100644 --- a/compiler/src/main/kotlin/gay/pizza/pork/compiler/Compiler.kt +++ b/compiler/src/main/kotlin/gay/pizza/pork/compiler/Compiler.kt @@ -42,7 +42,9 @@ class Compiler(val world: World) { } } - fun compileIrWorld(): IrWorld { + fun compileIrWorld(entryPointSymbol: CompilableSymbol): IrWorld { + val usedSymbolSet = mutableSetOf() + contributeCompiledSymbols(usedSymbolSet, entryPointSymbol.scopeSymbol, entryPointSymbol) val slabs = mutableListOf() for (slab in world.slabs) { slabs.add(compilableSlabs.of(slab).compiledIrSlab) diff --git a/examples/fib.pork b/examples/fib.pork index 0655100..609d729 100644 --- a/examples/fib.pork +++ b/examples/fib.pork @@ -8,6 +8,6 @@ func fib(n) { } export func main() { - let result = fib(20) + let result = fib(28) println(result) } diff --git a/settings.gradle.kts b/settings.gradle.kts index 8d07785..03f6397 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -18,5 +18,5 @@ include( ":ffi", ":tool", ":minimal", - ":support:pork-idea" + ":support:pork-idea", ) diff --git a/tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt b/tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt index e13c0e0..ec0553d 100644 --- a/tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt +++ b/tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt @@ -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) diff --git a/tool/src/main/kotlin/gay/pizza/pork/tool/RootCommand.kt b/tool/src/main/kotlin/gay/pizza/pork/tool/RootCommand.kt index 044f5b1..5832294 100644 --- a/tool/src/main/kotlin/gay/pizza/pork/tool/RootCommand.kt +++ b/tool/src/main/kotlin/gay/pizza/pork/tool/RootCommand.kt @@ -16,7 +16,7 @@ class RootCommand : CliktCommand("pork") { AttributeCommand(), ScopeAnalysisCommand(), CopyStdlibCommand(), - CompileCommand() + CompileCommand(), ) }