Skip to content

Commit

Permalink
Added machine and event data types and include files command line opt…
Browse files Browse the repository at this point in the history
…ions.
  • Loading branch information
clnhlzmn committed Mar 21, 2021
1 parent b19af66 commit 0720cb8
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 24 deletions.
11 changes: 10 additions & 1 deletion makina-compiler/src/xyz/colinholzman/makina/CliOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ class CliOptions {
@Option(names = ["-o", "--output"], description = ["generated files output directory"])
var outputDir: String? = null

@Option(names = ["-m", "--machine-data"], description = ["data type of machine context"])
var machineDataType: String = "void *"

@Option(names = ["-e", "--event-data"], description = ["data type of event context"])
var eventDataType: String = "void *"

@Option(names = ["-i", "--include"], description = ["files to include in generated code"])
var includes: List<String> = emptyList()

@Parameters(description = ["one ore more input files"])
lateinit var files: List<String>
var files: List<String> = emptyList()

}
13 changes: 10 additions & 3 deletions makina-compiler/src/xyz/colinholzman/makina/CodeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package xyz.colinholzman.makina

import java.io.PrintWriter

class CodeGenerator(val machine: Machine) {
class CodeGenerator(val machine: Machine,
val machineDataType: String = "void *",
val eventDataType: String = "void *",
val includes: List<String> = emptyList()) {

private val machineStructName = "struct ${machine.id}"
private val machineEventName = "struct ${machine.id}_event"
Expand All @@ -14,6 +17,10 @@ class CodeGenerator(val machine: Machine) {
println("#ifndef ${machine.id.toUpperCase()}_H")
println("#define ${machine.id.toUpperCase()}_H")
println()
for (include in includes) {
println("#include \"$include\"")
}
println()
println("$machineStructName;")
println("$machineEventName;")
println()
Expand All @@ -28,13 +35,13 @@ class CodeGenerator(val machine: Machine) {
println("};")
println()
println("$machineEventName {")
println("\t${eventDataType} ctx;")
println("\t$machineEventIdName id;")
println("\tvoid *ctx;")
println("};")
println()
println("$machineStructName {")
println("\tint (*state)($machineStructName *, $machineEventName *);")
println("\tvoid *ctx;")
println("\t${machineDataType} ctx;")
println("};")
println()
println("int ${machine.id}_init($machineStructName *);")
Expand Down
3 changes: 2 additions & 1 deletion makina-compiler/src/xyz/colinholzman/makina/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ fun main(args: Array<String>) {
val headerPath = Paths.get(outputDirectoryPath, "${machine.id}.h").toString()
val sourcePath = Paths.get(outputDirectoryPath, "${machine.id}.c").toString()

val generator = CodeGenerator(machine)
val generator = CodeGenerator(machine, options.machineDataType,
options.eventDataType, options.includes)

val headerStream = ByteArrayOutputStream()
val implementationStream = ByteArrayOutputStream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ internal class CodeGeneratorTest {
int main (void) {
output = fopen("test_output.txt", "w+");
struct test instance;
test_init(&instance); //s1_entry, s1_s2_entry
test_process_event(&instance, &(struct test_event) {test_event_e1}); //s1_s2_e1_guard, s1_s2_exit, s1_exit, s2_entry, s2_s1_entry
test_process_event(&instance, &(struct test_event) {test_event_e2}); //...
test_process_event(&instance, &(struct test_event) {test_event_e1}); //s2_s1_exit, s2_exit, s2_s1_e1_action, s1_entry, s1_s2_entry
test_init(&instance); //s1_entry, s1_s2_entry
test_process_event(&instance, &(struct test_event) {.id = test_event_e1}); //s1_s2_e1_guard, s1_s2_exit, s1_exit, s2_entry, s2_s1_entry
test_process_event(&instance, &(struct test_event) {.id = test_event_e2}); //...
test_process_event(&instance, &(struct test_event) {.id = test_event_e1}); //s2_s1_exit, s2_exit, s2_s1_e1_action, s1_entry, s1_s2_entry
s1_s2_e1_guard_value = 1;
test_process_event(&instance, &(struct test_event) {test_event_e1}); //s1_s2_e1_guard, s1_s2_e1_action
test_process_event(&instance, &(struct test_event) {test_event_e2}); //s1_s2_exit, s1_exit, s2_entry, s2_s3_entry
test_process_event(&instance, &(struct test_event) {test_event_e2}); //s2_s3_exit, s2_exit, s1_entry, s1_s2_entry
test_process_event(&instance, &(struct test_event) {.id = test_event_e1}); //s1_s2_e1_guard, s1_s2_e1_action
test_process_event(&instance, &(struct test_event) {.id = test_event_e2}); //s1_s2_exit, s1_exit, s2_entry, s2_s3_entry
test_process_event(&instance, &(struct test_event) {.id = test_event_e2}); //s2_s3_exit, s2_exit, s1_entry, s1_s2_entry
fclose(output);
return 0;
}
Expand Down Expand Up @@ -165,7 +165,6 @@ internal class CodeGeneratorTest {
//invoke Makina
main(arrayOf(testSourceFile.absolutePath))


val makinaOutputFilePath = testDir.resolve("test.c").toString()
val compiledExecutablePath = testDir.resolve("test.exe").toString()

Expand Down
69 changes: 58 additions & 11 deletions makina-compiler/test/xyz/colinholzman/makina/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,79 @@ class MainTest {

@Test
fun main() {
val sourceFile = File.createTempFile("makina", ".mkna")
val name = "foo"
val sourceFile = File.createTempFile("makina", ".mkna")
sourceFile.writeText("machine $name; state bar {}")
main(arrayOf(sourceFile.absolutePath))

val header = File(Paths.get(sourceFile.parent, "$name.h").toUri())
val implementation = File(Paths.get(sourceFile.parent, "$name.c").toUri())

if (header.exists()) header.deleteRecursively()
if (implementation.exists()) implementation.deleteRecursively()

main(arrayOf(sourceFile.absolutePath))
assert(header.exists())
assert(implementation.exists())
}

@Test
fun outputDirectory() {
val sourceFile = File.createTempFile("makina", ".mkna")
val name = "foo"
val sourceFile = File.createTempFile("makina", ".mkna")
sourceFile.writeText("machine $name; state bar {}")

val outputDirectoryName = "test"
val outputDir = Paths.get(sourceFile.parent, outputDirectoryName).toString()
val outputDirFile = File(outputDir)
if (outputDirFile.exists()) {
outputDirFile.deleteRecursively()
}
main(arrayOf(sourceFile.absolutePath, "-o", outputDir))
val header = File(Paths.get(outputDir, "$name.h").toUri())
val implementation = File(Paths.get(outputDir, "$name.c").toUri())
val outputDirPath = Paths.get(sourceFile.parent, outputDirectoryName).toString()

val header = File(Paths.get(outputDirPath, "$name.h").toUri())
val implementation = File(Paths.get(outputDirPath, "$name.c").toUri())

if (header.exists()) header.deleteRecursively()
if (implementation.exists()) implementation.deleteRecursively()

val outputDir = File(outputDirPath)
if (outputDir.exists()) outputDir.deleteRecursively()

main(arrayOf(sourceFile.absolutePath, "-o", outputDirPath))

assert(header.exists())
assert(implementation.exists())
}

@Test
fun dataTypes() {
val name = "foo"
val sourceFile = File.createTempFile("makina", ".mkna")
sourceFile.writeText("machine $name; state bar {}")

val header = File(Paths.get(sourceFile.parent, "$name.h").toUri())
if (header.exists()) header.deleteRecursively()

val machineDataType = "struct machine_data"
val eventDataType = "struct event_data"
main(arrayOf(sourceFile.absolutePath, "-m", machineDataType, "-e", eventDataType))

val headerContent = header.readBytes().toString(Charsets.UTF_8)
assert(headerContent.contains("$machineDataType ctx;"))
assert(headerContent.contains("$eventDataType ctx;"))
}

@Test
fun includes() {
val name = "foo"
val sourceFile = File.createTempFile("makina", ".mkna")
sourceFile.writeText("machine $name; state bar {}")

val header = File(Paths.get(sourceFile.parent, "$name.h").toUri())
if (header.exists()) header.deleteRecursively()

val include1 = "../some_header.h"
val include2 = "another_header.h"
main(arrayOf(sourceFile.absolutePath, "-i", include1, "-i", include2))

val headerContent = header.readBytes().toString(Charsets.UTF_8)
assert(headerContent.contains("#include \"$include1\""))
assert(headerContent.contains("#include \"$include2\""))
}

}

0 comments on commit 0720cb8

Please sign in to comment.