Skip to content

Commit

Permalink
imports are now python style with a twist: forms! import std ffi and …
Browse files Browse the repository at this point in the history
…import local myfile
  • Loading branch information
azenla committed Sep 7, 2023
1 parent 0e4362e commit 81296ee
Show file tree
Hide file tree
Showing 22 changed files with 58 additions and 50 deletions.
6 changes: 3 additions & 3 deletions ast/src/main/ast/pork.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ types:
parent: Declaration
values:
- name: form
type: Symbol?
- name: path
type: StringLiteral
type: Symbol
- name: components
type: List<Symbol>
IntLiteral:
parent: Expression
values:
Expand Down
8 changes: 4 additions & 4 deletions ast/src/main/kotlin/gay/pizza/pork/ast/ImportDeclaration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import kotlinx.serialization.Serializable

@Serializable
@SerialName("importDeclaration")
class ImportDeclaration(val form: Symbol?, val path: StringLiteral) : Declaration() {
class ImportDeclaration(val form: Symbol, val components: List<Symbol>) : Declaration() {
override val type: NodeType = NodeType.ImportDeclaration

override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(form, path)
visitor.visitAll(listOf(form), components)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitImportDeclaration(this)

override fun equals(other: Any?): Boolean {
if (other !is ImportDeclaration) return false
return other.form == form && other.path == path
return other.form == form && other.components == components
}

override fun hashCode(): Int {
var result = form.hashCode()
result = 31 * result + path.hashCode()
result = 31 * result + components.hashCode()
result = 31 * result + type.hashCode()
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class CompilationUnitContext(
}

private fun processImport(import: ImportDeclaration) {
val importLocator = ImportLocator(import.path.text, import.form?.id)
val importPath = import.components.joinToString("/") { it.id } + ".pork"
val importLocator = ImportLocator(import.form.id, importPath)
val evaluationContext = evaluator.context(importLocator)
internalScope.inherit(evaluationContext.externalScope)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/complex/b.pork
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "c.pork"
import local c

export func b() {
c()
Expand Down
2 changes: 1 addition & 1 deletion examples/complex/c.pork
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "a.pork"
import local a

export func c() {
a()
Expand Down
2 changes: 1 addition & 1 deletion examples/complex/d.pork
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "b.pork"
import local b

export func d() {
b()
Expand Down
2 changes: 1 addition & 1 deletion examples/complex/main.pork
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "d.pork"
import local d

export func main() {
d()
Expand Down
2 changes: 1 addition & 1 deletion examples/ffi.pork
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std "ffi/malloc.pork"
import std ffi.malloc

export func main() {
while true {
Expand Down
5 changes: 0 additions & 5 deletions examples/imports.pork

This file was deleted.

1 change: 0 additions & 1 deletion examples/loop.pork
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export func main() {
while true {
println("Hello World")
break
}
}
3 changes: 0 additions & 3 deletions examples/module.pork

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gay.pizza.pork.frontend

class StandardImportSource(override val fileContentSource: ContentSource) : ImportSource {
class DynamicImportSource : ImportSource {
private val providers = mutableMapOf<String,ContentSource>()

override fun provideContentSource(form: String): ContentSource {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package gay.pizza.pork.frontend

data class ImportLocator(val path: String, val form: String? = null)
data class ImportLocator(val form: String, val path: String)
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package gay.pizza.pork.frontend

interface ImportSource {
val fileContentSource: ContentSource

fun provideContentSource(form: String): ContentSource
}
13 changes: 5 additions & 8 deletions frontend/src/main/kotlin/gay/pizza/pork/frontend/World.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class World(val importSource: ImportSource) {
private fun resolveAllImports(unit: CompilationUnit): Set<CompilationUnit> {
val units = mutableSetOf<CompilationUnit>()
for (declaration in unit.declarations.filterIsInstance<ImportDeclaration>()) {
val importLocator = ImportLocator(declaration.path.text, form = declaration.form?.id)
val importPath = declaration.components.joinToString("/") { it.id } + ".pork"
val importLocator = ImportLocator(declaration.form.id, importPath)
val importedUnit = loadOneUnit(importLocator)
units.add(importedUnit)
}
Expand All @@ -45,18 +46,14 @@ class World(val importSource: ImportSource) {
return unit
}

private fun pickContentSource(form: String? = null): ContentSource {
if (form != null) {
return importSource.provideContentSource(form)
}
return importSource.fileContentSource
}
private fun pickContentSource(form: String): ContentSource =
importSource.provideContentSource(form)

fun stableIdentity(
importLocator: ImportLocator,
contentSource: ContentSource = pickContentSource(importLocator.form)
): String {
val formKey = importLocator.form ?: "file"
val formKey = importLocator.form
val stableIdentity = contentSource.stableContentIdentity(importLocator.path)
return "[${formKey}][${stableIdentity}]"
}
Expand Down
22 changes: 15 additions & 7 deletions parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,9 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {

private fun readImportDeclaration(): ImportDeclaration = within {
expect(TokenType.Import)
var form: Symbol? = null
if (peek(TokenType.Symbol)) {
form = readSymbolRaw()
}
ImportDeclaration(form, readStringLiteral())
val form = readSymbolRaw()
val components = oneAndContinuedBy(TokenType.Period) { readSymbolRaw() }
ImportDeclaration(form, components)
}

private fun readFunctionDeclaration(): FunctionDefinition = within {
Expand Down Expand Up @@ -292,11 +290,21 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
): List<T> {
val items = mutableListOf<T>()
while (!peek(peeking)) {
val expression = read()
val item = read()
if (consuming != null) {
next(consuming)
}
items.add(expression)
items.add(item)
}
return items
}

private fun <T> oneAndContinuedBy(separator: TokenType, read: () -> T): List<T> {
val items = mutableListOf<T>()
items.add(read())
while (peek(separator)) {
expect(separator)
items.add(read())
}
return items
}
Expand Down
9 changes: 8 additions & 1 deletion parser/src/main/kotlin/gay/pizza/pork/parser/Printer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {

override fun visitImportDeclaration(node: ImportDeclaration) {
append("import ")
visit(node.path)
visit(node.form)
append(" ")
for ((index, component) in node.components.withIndex()) {
visit(component)
if (index != node.components.size - 1) {
append(".")
}
}
}

override fun visitCompilationUnit(node: CompilationUnit) {
Expand Down
1 change: 1 addition & 0 deletions parser/src/main/kotlin/gay/pizza/pork/parser/TokenType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
RightParentheses(SingleChar(')')),
Negation(SingleChar('!'), Promotion('=', Inequality), OperatorFamily),
Comma(SingleChar(',')),
Period(SingleChar('.')),
False(Keyword("false"), KeywordFamily),
True(Keyword("true"), KeywordFamily),
If(Keyword("if"), KeywordFamily),
Expand Down
7 changes: 5 additions & 2 deletions stdlib/src/main/kotlin/gay/pizza/pork/stdlib/PorkStdlib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object PorkStdlib : ContentSource {
private val stdlibClass = PorkStdlib::class.java

private fun readManifestFiles(): List<String> {
val manifestContent = read("stdlib.manifest")
val manifestContent = read("stdlib.manifest", check = false)
return manifestContent.split("\n").filter { line ->
val trimmed = line.trim()
trimmed.isNotEmpty() && !trimmed.startsWith("#")
Expand All @@ -17,7 +17,10 @@ object PorkStdlib : ContentSource {

val files: List<String> = readManifestFiles()

private fun read(path: String): String {
private fun read(path: String, check: Boolean = true): String {
if (check && !files.contains(path)) {
throw RuntimeException("Stdlib does not contain file '${path}'")
}
val stream = stdlibClass.getResourceAsStream("/pork/stdlib/${path}")
?: throw RuntimeException("Stdlib does not contain file '${path}'")
return String(stream.readAllBytes())
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion stdlib/src/main/pork/stdlib.manifest
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
numbers.pork
ffi/malloc.pork
numerics/operator.pork
11 changes: 6 additions & 5 deletions tool/src/main/kotlin/gay/pizza/pork/tool/Tool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import gay.pizza.pork.evaluator.Evaluator
import gay.pizza.pork.evaluator.Scope
import gay.pizza.pork.frontend.ContentSource
import gay.pizza.pork.frontend.ImportLocator
import gay.pizza.pork.frontend.StandardImportSource
import gay.pizza.pork.frontend.DynamicImportSource
import gay.pizza.pork.frontend.World
import gay.pizza.pork.parser.*
import gay.pizza.pork.stdlib.PorkStdlib
Expand All @@ -33,12 +33,13 @@ abstract class Tool {

fun loadMainFunction(scope: Scope, setupEvaluator: Evaluator.() -> Unit = {}): CallableFunction {
val fileContentSource = createContentSource()
val standardImportSource = StandardImportSource(fileContentSource)
standardImportSource.addContentSource("std", PorkStdlib)
val world = World(standardImportSource)
val dynamicImportSource = DynamicImportSource()
dynamicImportSource.addContentSource("std", PorkStdlib)
dynamicImportSource.addContentSource("local", fileContentSource)
val world = World(dynamicImportSource)
val evaluator = Evaluator(world, scope)
setupEvaluator(evaluator)
val resultingScope = evaluator.evaluate(ImportLocator(rootFilePath()))
val resultingScope = evaluator.evaluate(ImportLocator("local", rootFilePath()))
return resultingScope.value("main") as CallableFunction
}
}

0 comments on commit 81296ee

Please sign in to comment.