Skip to content

Commit

Permalink
language: none support
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Sep 12, 2023
1 parent ca111f8 commit a6f9a82
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ast/src/main/ast/pork.yml
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ types:
Continue:
parent: Expression
values: []
NoneLiteral:
parent: Expression
values: []
Native:
parent: Node
values:
Expand Down
2 changes: 2 additions & 0 deletions ast/src/main/graph/types.dot
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ digraph A {
type_ForIn [shape=box,label="ForIn"]
type_Break [shape=box,label="Break"]
type_Continue [shape=box,label="Continue"]
type_NoneLiteral [shape=box,label="NoneLiteral"]
type_Native [shape=box,label="Native"]
type_Node -> type_Expression
type_Node -> type_Symbol
Expand Down Expand Up @@ -62,6 +63,7 @@ digraph A {
type_Expression -> type_ForIn
type_Expression -> type_Break
type_Expression -> type_Continue
type_Expression -> type_NoneLiteral
type_Definition -> type_FunctionDefinition
type_Definition -> type_LetDefinition
type_Declaration -> type_ImportDeclaration
Expand Down
3 changes: 3 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/NodeCoalescer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitNative(node: Native): Unit =
handle(node)

override fun visitNoneLiteral(node: NoneLiteral): Unit =
handle(node)

override fun visitParentheses(node: Parentheses): Unit =
handle(node)

Expand Down
1 change: 1 addition & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/NodeType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum class NodeType(val parent: NodeType? = null) {
ListLiteral(Expression),
LongLiteral(Expression),
Native(Node),
NoneLiteral(Expression),
Parentheses(Expression),
PrefixOperation(Expression),
SetAssignment(Expression),
Expand Down
2 changes: 2 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface NodeVisitor<T> {

fun visitNative(node: Native): T

fun visitNoneLiteral(node: NoneLiteral): T

fun visitParentheses(node: Parentheses): T

fun visitPrefixOperation(node: PrefixOperation): T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is ForIn -> visitForIn(node)
is Break -> visitBreak(node)
is Continue -> visitContinue(node)
is NoneLiteral -> visitNoneLiteral(node)
is Native -> visitNative(node)
}

Expand Down
22 changes: 22 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/NoneLiteral.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

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

@Serializable
@SerialName("noneLiteral")
class NoneLiteral : Expression() {
override val type: NodeType = NodeType.NoneLiteral

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

override fun equals(other: Any?): Boolean {
if (other !is NoneLiteral) return false
return true
}

override fun hashCode(): Int =
31 * type.hashCode()
}
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
topLevelUsedError("Native", "FunctionContext")
}

override fun visitNoneLiteral(node: NoneLiteral): Any = None

override fun visitContinue(node: Continue): Any = ContinueMarker

private inline fun <T> scoped(block: () -> T): T {
Expand Down
19 changes: 14 additions & 5 deletions ffi/src/main/kotlin/gay/pizza/pork/ffi/JnaNativeProvider.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package gay.pizza.pork.ffi

import com.sun.jna.Function
import com.sun.jna.NativeLibrary
import com.sun.jna.Pointer
import gay.pizza.pork.ast.ArgumentSpec
import gay.pizza.pork.evaluator.CallableFunction
import gay.pizza.pork.evaluator.NativeProvider
import gay.pizza.pork.evaluator.None

class JnaNativeProvider : NativeProvider {
override fun provideNativeFunction(definition: String, arguments: List<ArgumentSpec>): CallableFunction {
val functionDefinition = FfiFunctionDefinition.parse(definition)
val function = Function.getFunction(functionDefinition.library, functionDefinition.function)
val library = NativeLibrary.getInstance(functionDefinition.library)
val function = library.getFunction(functionDefinition.function)
?: throw RuntimeException("Failed to find function ${functionDefinition.function} in library ${functionDefinition.library}")
return CallableFunction { functionArgs ->
val ffiArgs = mutableListOf<Any?>()
Expand Down Expand Up @@ -38,7 +41,7 @@ class JnaNativeProvider : NativeProvider {
"void" -> function.invokeVoid(values)
"char*" -> function.invokeString(values, false)
else -> throw RuntimeException("Unsupported ffi return type: $type")
}
} ?: None

private fun rewriteType(type: String): String = when (type) {
"size_t" -> "long"
Expand All @@ -55,7 +58,13 @@ class JnaNativeProvider : NativeProvider {
"double" -> numberConvert(type, value) { toDouble() }
"float" -> numberConvert(type, value) { toFloat() }
"char*" -> notNullConvert(type, value) { toString() }
"void*" -> nullableConvert(value) { this as Pointer }
"void*" -> nullableConvert(value) {
if (value is Long) {
Pointer(value)
} else {
value as Pointer
}
}
else -> throw RuntimeException("Unsupported ffi type: $type")
}

Expand All @@ -67,14 +76,14 @@ class JnaNativeProvider : NativeProvider {
}

private fun <T> nullableConvert(value: Any?, into: Any.() -> T): T? {
if (value == null) {
if (value == null || value == None) {
return null
}
return into(value)
}

private fun <T> numberConvert(type: String, value: Any?, into: Number.() -> T): T {
if (value == null) {
if (value == null || value == None) {
throw RuntimeException("Null values cannot be used for converting to numeric type $type")
}

Expand Down
5 changes: 5 additions & 0 deletions parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
Continue()
}

TokenType.None -> {
expect(TokenType.None)
NoneLiteral()
}

else -> {
throw ParseError(
"Failed to parse token: ${token.type} '${token.text}' as" +
Expand Down
4 changes: 4 additions & 0 deletions parser/src/main/kotlin/gay/pizza/pork/parser/Printer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
visit(node.definition)
}

override fun visitNoneLiteral(node: NoneLiteral) {
append("none")
}

override fun visitSymbol(node: Symbol) {
append(node.id)
}
Expand Down
3 changes: 2 additions & 1 deletion parser/src/main/kotlin/gay/pizza/pork/parser/TokenType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
StringLiteral(StringLiteralFamily),
Equality(OperatorFamily),
Inequality(ManyChars("!="), OperatorFamily),
ExclaimationPoint(SingleChar('!'), Promotion('=', Inequality)),
ExclamationPoint(SingleChar('!'), Promotion('=', Inequality)),
None(ManyChars("None"), KeywordFamily),
Equals(SingleChar('='), Promotion('=', Equality)),
PlusPlus(ManyChars("++"), OperatorFamily),
MinusMinus(ManyChars("--"), OperatorFamily),
Expand Down

0 comments on commit a6f9a82

Please sign in to comment.