mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 13:11:32 +00:00
add a standard library, and introduce formed imports (import std "myfile.pork")
This commit is contained in:
13
stdlib/build.gradle.kts
Normal file
13
stdlib/build.gradle.kts
Normal file
@ -0,0 +1,13 @@
|
||||
plugins {
|
||||
id("gay.pizza.pork.module")
|
||||
}
|
||||
|
||||
tasks.processResources {
|
||||
from("src/main/pork") {
|
||||
into("pork/stdlib")
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":frontend"))
|
||||
}
|
33
stdlib/src/main/kotlin/gay/pizza/pork/stdlib/PorkStdlib.kt
Normal file
33
stdlib/src/main/kotlin/gay/pizza/pork/stdlib/PorkStdlib.kt
Normal file
@ -0,0 +1,33 @@
|
||||
package gay.pizza.pork.stdlib
|
||||
|
||||
import gay.pizza.pork.frontend.ContentSource
|
||||
import gay.pizza.pork.parser.CharSource
|
||||
import gay.pizza.pork.parser.StringCharSource
|
||||
|
||||
object PorkStdlib : ContentSource {
|
||||
private val stdlibClass = PorkStdlib::class.java
|
||||
|
||||
private fun readManifestFiles(): List<String> {
|
||||
val manifestContent = read("stdlib.manifest")
|
||||
return manifestContent.split("\n").filter { line ->
|
||||
val trimmed = line.trim()
|
||||
trimmed.isNotEmpty() && !trimmed.startsWith("#")
|
||||
}
|
||||
}
|
||||
|
||||
val files: List<String> = readManifestFiles()
|
||||
|
||||
private fun read(path: String): String {
|
||||
val stream = stdlibClass.getResourceAsStream("/pork/stdlib/${path}")
|
||||
?: throw RuntimeException("Stdlib does not contain file '${path}'")
|
||||
return String(stream.readAllBytes())
|
||||
}
|
||||
|
||||
override fun loadAsCharSource(path: String): CharSource {
|
||||
return StringCharSource(read(path))
|
||||
}
|
||||
|
||||
override fun stableContentIdentity(path: String): String {
|
||||
return path
|
||||
}
|
||||
}
|
5
stdlib/src/main/pork/ffi/malloc.pork
Normal file
5
stdlib/src/main/pork/ffi/malloc.pork
Normal file
@ -0,0 +1,5 @@
|
||||
export func malloc(size)
|
||||
native ffi "c:malloc:void*"
|
||||
|
||||
export func free(pointer)
|
||||
native ffi "c:free:void"
|
15
stdlib/src/main/pork/numbers.pork
Normal file
15
stdlib/src/main/pork/numbers.pork
Normal file
@ -0,0 +1,15 @@
|
||||
export func add(a, b) {
|
||||
a + b
|
||||
}
|
||||
|
||||
export func subtract(a, b) {
|
||||
a - b
|
||||
}
|
||||
|
||||
export func multiply(a, b) {
|
||||
a * b
|
||||
}
|
||||
|
||||
export func divide(a, b) {
|
||||
a / b
|
||||
}
|
1
stdlib/src/main/pork/stdlib.manifest
Normal file
1
stdlib/src/main/pork/stdlib.manifest
Normal file
@ -0,0 +1 @@
|
||||
numbers.pork
|
Reference in New Issue
Block a user