language: none support

This commit is contained in:
Alex Zenla 2023-09-12 02:03:41 -04:00
parent ca111f8e4b
commit a6f9a82e5e
Signed by: alex
GPG Key ID: C0780728420EBFE5
12 changed files with 61 additions and 6 deletions

View File

@ -276,6 +276,9 @@ types:
Continue:
parent: Expression
values: []
NoneLiteral:
parent: Expression
values: []
Native:
parent: Node
values:

View File

@ -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
@ -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

View File

@ -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)

View File

@ -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),

View File

@ -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

View File

@ -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)
}

View File

@ -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()
}

View File

@ -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 {

View File

@ -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?>()
@ -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"
@ -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")
}
@ -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")
}

View File

@ -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" +

View File

@ -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)
}

View File

@ -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),