Skip to content

Commit

Permalink
pork: it's got it all, ffi, state machine tokenizer, and better IDE s…
Browse files Browse the repository at this point in the history
…upport
  • Loading branch information
azenla committed Oct 13, 2023
1 parent d355fb3 commit 5078f38
Show file tree
Hide file tree
Showing 58 changed files with 925 additions and 279 deletions.
11 changes: 9 additions & 2 deletions ast/src/main/ast/pork.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,20 @@ types:
type: Block
- name: elseBlock
type: Block?
ImportPath:
parent: Node
referencedElementValue: components
referencedElementType: CompilationUnit
values:
- name: components
type: List<Symbol>
ImportDeclaration:
parent: Declaration
values:
- name: form
type: Symbol
- name: components
type: List<Symbol>
- name: path
type: ImportPath
IntegerLiteral:
parent: Expression
values:
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/graph/types.dot
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ digraph A {
type_FunctionDefinition [shape=box,label="FunctionDefinition"]
type_LetDefinition [shape=box,label="LetDefinition"]
type_If [shape=box,label="If"]
type_ImportPath [shape=box,label="ImportPath"]
type_ImportDeclaration [shape=box,label="ImportDeclaration"]
type_IntegerLiteral [shape=box,label="IntegerLiteral"]
type_LongLiteral [shape=box,label="LongLiteral"]
Expand Down Expand Up @@ -45,6 +46,7 @@ digraph A {
type_Node -> type_Block
type_Node -> type_CompilationUnit
type_Node -> type_ArgumentSpec
type_Node -> type_ImportPath
type_Node -> type_ForInItem
type_Node -> type_Native
type_Expression -> type_LetAssignment
Expand Down Expand Up @@ -98,7 +100,9 @@ digraph A {
type_LetDefinition -> type_Expression [style=dotted]
type_If -> type_Expression [style=dotted]
type_If -> type_Block [style=dotted]
type_ImportPath -> type_Symbol [style=dotted]
type_ImportDeclaration -> type_Symbol [style=dotted]
type_ImportDeclaration -> type_ImportPath [style=dotted]
type_ListLiteral -> type_Expression [style=dotted]
type_Parentheses -> type_Expression [style=dotted]
type_PrefixOperation -> type_PrefixOperator [style=dotted]
Expand Down
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 components: List<Symbol>) : Declaration() {
class ImportDeclaration(val form: Symbol, val path: ImportPath) : Declaration() {
override val type: NodeType = NodeType.ImportDeclaration

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

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.components == components
return other.form == form && other.path == path
}

override fun hashCode(): Int {
var result = form.hashCode()
result = 31 * result + components.hashCode()
result = 31 * result + path.hashCode()
result = 31 * result + type.hashCode()
return result
}
Expand Down
28 changes: 28 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/gen/ImportPath.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast.gen

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("importPath")
class ImportPath(val components: List<Symbol>) : Node() {
override val type: NodeType = NodeType.ImportPath

override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitAll(components)

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

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

override fun hashCode(): Int {
var result = components.hashCode()
result = 31 * result + type.hashCode()
return result
}
}
3 changes: 3 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/gen/NodeCoalescer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class NodeCoalescer(val followChildren: Boolean = true, val handler: (Node) -> U
override fun visitImportDeclaration(node: ImportDeclaration): Unit =
handle(node)

override fun visitImportPath(node: ImportPath): Unit =
handle(node)

override fun visitIndexedBy(node: IndexedBy): Unit =
handle(node)

Expand Down
2 changes: 2 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/gen/NodeParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ interface NodeParser {

fun parseImportDeclaration(): ImportDeclaration

fun parseImportPath(): ImportPath

fun parseIndexedBy(): IndexedBy

fun parseInfixOperation(): InfixOperation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fun NodeParser.parse(type: NodeType): Node =
NodeType.FunctionDefinition -> parseFunctionDefinition()
NodeType.LetDefinition -> parseLetDefinition()
NodeType.If -> parseIf()
NodeType.ImportPath -> parseImportPath()
NodeType.ImportDeclaration -> parseImportDeclaration()
NodeType.IntegerLiteral -> parseIntegerLiteral()
NodeType.LongLiteral -> parseLongLiteral()
Expand Down
1 change: 1 addition & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/gen/NodeType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum class NodeType(val parent: NodeType? = null) {
FunctionDefinition(Definition),
If(Expression),
ImportDeclaration(Declaration),
ImportPath(Node),
IndexedBy(Expression),
InfixOperation(Expression),
IntegerLiteral(Expression),
Expand Down
2 changes: 2 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/gen/NodeVisitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ interface NodeVisitor<T> {

fun visitImportDeclaration(node: ImportDeclaration): T

fun visitImportPath(node: ImportPath): T

fun visitIndexedBy(node: IndexedBy): T

fun visitInfixOperation(node: InfixOperation): T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is FunctionDefinition -> visitFunctionDefinition(node)
is LetDefinition -> visitLetDefinition(node)
is If -> visitIf(node)
is ImportPath -> visitImportPath(node)
is ImportDeclaration -> visitImportDeclaration(node)
is IntegerLiteral -> visitIntegerLiteral(node)
is LongLiteral -> visitLongLiteral(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CompilationUnitContext(
}

private fun processImport(import: ImportDeclaration) {
val importPath = import.components.joinToString("/") { it.id } + ".pork"
val importPath = import.path.components.joinToString("/") { it.id } + ".pork"
val importLocator = ImportLocator(import.form.id, importPath)
val evaluationContext = evaluator.context(importLocator)
internalScope.inherit(evaluationContext.externalScope)
Expand All @@ -67,9 +67,11 @@ class CompilationUnitContext(
companion object {
private val preludeImport = ImportDeclaration(
Symbol("std"),
listOf(
Symbol("lang"),
Symbol("prelude")
ImportPath(
listOf(
Symbol("lang"),
Symbol("prelude")
)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : NodeVisitor<Any> {
topLevelUsedError("ImportDeclaration", "CompilationUnitContext")
}

override fun visitImportPath(node: ImportPath): Any {
topLevelUsedError("ImportPath", "CompilationUnitContext")
}

override fun visitIndexedBy(node: IndexedBy): Any {
val value = node.expression.visit(this)
val index = node.index.visit(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FunctionContext(val compilationUnitContext: CompilationUnitContext, val no
val native = node.native!!
val nativeFunctionProvider =
compilationUnitContext.evaluator.nativeFunctionProvider(native.form.id)
nativeFunctionProvider.provideNativeFunction(native.definitions.map { it.text }, node.arguments)
nativeFunctionProvider.provideNativeFunction(native.definitions.map { it.text }, node.arguments, compilationUnitContext)
}

private val nativeCached by lazy { resolveMaybeNative() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ class InternalNativeProvider(val quiet: Boolean = false) : NativeProvider {
"listInitWith" to CallableFunction(::listInitWith)
)

override fun provideNativeFunction(definitions: List<String>, arguments: List<ArgumentSpec>): CallableFunction {
fun add(name: String, function: CallableFunction) {
functions[name] = function
}

override fun provideNativeFunction(
definitions: List<String>,
arguments: List<ArgumentSpec>,
inside: CompilationUnitContext
): CallableFunction {
val definition = definitions[0]
return functions[definition] ?: throw RuntimeException("Unknown Internal Function: $definition")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package gay.pizza.pork.evaluator
import gay.pizza.pork.ast.gen.ArgumentSpec

interface NativeProvider {
fun provideNativeFunction(definitions: List<String>, arguments: List<ArgumentSpec>): CallableFunction
fun provideNativeFunction(definitions: List<String>, arguments: List<ArgumentSpec>, inside: CompilationUnitContext): CallableFunction
}
24 changes: 20 additions & 4 deletions examples/ffi.pork
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import std ffi.malloc
import std ffi.struct

export let timeval = ffiStructDefine(
"long", "seconds",
"unsigned int", "microseconds"
)

export let timezone = ffiStructDefine(
"int", "minutes_greenwich",
"int", "dst_time"
)

func gettimeofday(value, tz)
native ffi "c" "int gettimeofday(struct timeval*, struct timezone*)"

export func main() {
let pointer = malloc(8192)
println(pointer)
free(pointer)
let time = ffiStructAllocate(timeval)
let zone = ffiStructAllocate(timezone)
let result = gettimeofday(time, zone)
let seconds = ffiStructValue(timeval, "seconds", time)
println("Result:", result)
println("Seconds:", seconds)
}
3 changes: 3 additions & 0 deletions examples/gameoflife/SDL2.pork
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export func SDL_GetModState()

export let SDL_RENDERER_PRESENTVSYNC = 4

export func SDL_PollEvent(event)
native ffi "SDL2" "int SDL_PollEvent(struct SDL_Event*)"

export func SDL_CreateRenderer(window, index, flags)
native ffi "SDL2" "void* SDL_CreateRenderer(void*, int, unsigned int)"

Expand Down
12 changes: 8 additions & 4 deletions ffi/src/main/kotlin/gay/pizza/pork/ffi/FfiMacPlatform.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package gay.pizza.pork.ffi

import java.nio.file.Path
import kotlin.io.path.*

object FfiMacPlatform : FfiPlatform {
private val frameworksDirectories = listOf(
"/Library/Frameworks"
)

override fun findLibrary(name: String): Path? {
override fun findLibrary(name: String): String? {
val frameworksToCheck = frameworksDirectories.map { frameworkDirectory ->
Path("$frameworkDirectory/$name.framework/$name")
}
for (framework in frameworksToCheck) {
if (!framework.exists()) continue
return if (framework.isSymbolicLink()) {
return framework.parent.resolve(framework.readSymbolicLink()).absolute()
return framework.parent.resolve(framework.readSymbolicLink()).absolutePathString()
} else {
framework.absolute()
framework.absolutePathString()
}
}

if (name == "c") {
return "libSystem.dylib"
}

return null
}
}
Loading

0 comments on commit 5078f38

Please sign in to comment.