From 81ca9ad020a4c7f7d0606b8c887c4c7d15ea5162 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sun, 25 Jul 2021 21:06:31 -0600 Subject: [PATCH] generate main functions for Kiss files --- kiss/src/kiss/Kiss.hx | 25 +++++++++++++++++++------ projects/kiss-vscode/src/Main.kiss | 1 + projects/kiss-vscode/test.sh | 1 + 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/kiss/src/kiss/Kiss.hx b/kiss/src/kiss/Kiss.hx index d4eb7064..4b673deb 100644 --- a/kiss/src/kiss/Kiss.hx +++ b/kiss/src/kiss/Kiss.hx @@ -160,12 +160,25 @@ class Kiss { var topLevelBegin = load(kissFile, k); if (topLevelBegin != null) { - - // TODO this is where an error would be thrown, or main() would be generated from top-level expressions - // TODO There are two ideas for how to handle expressions at the top level of a Kiss file: - // 1. Throw an error, because the top level should only contain field definitions - // 2. Append the expression to the body of an automatically generated main() function - // if that result is non-null, try to make main (but this may cause a duplicate declaration) + // If no main function is defined manually, Kiss expressions at the top of a file will be put in a main function. + // If a main function IS defined, this will result in an error + if (k.fieldDict.exists("main")) { + throw CompileError.fromExp(topLevelBegin, '$kissFile has expressions outside of field definitions, but already defines its own main function.'); + } + var b = topLevelBegin.expBuilder(); + // This doesn't need to be added to the fieldDict because all code generation is done + k.fieldList.push({ + name: "main", + access: [AStatic], + kind: FFun(Helpers.makeFunction( + b.symbol("main"), + false, + b.list([]), + [topLevelBegin], + k, + "function")), + pos: topLevelBegin.macroPos() + }); } k.fieldList; diff --git a/projects/kiss-vscode/src/Main.kiss b/projects/kiss-vscode/src/Main.kiss index fe2d019a..a7ee5c78 100644 --- a/projects/kiss-vscode/src/Main.kiss +++ b/projects/kiss-vscode/src/Main.kiss @@ -134,6 +134,7 @@ (set lastConfigDir (joinPath (userHome) ".kiss-vscode" "lastActiveConfig")) (tryLoadConfig))) +// Manually define main so it will exist when test is not defined: (function :Void main [] (#when test (set builtinConfigDir "config") diff --git a/projects/kiss-vscode/test.sh b/projects/kiss-vscode/test.sh index 91e7b95a..bd743bda 100755 --- a/projects/kiss-vscode/test.sh +++ b/projects/kiss-vscode/test.sh @@ -1,3 +1,4 @@ #! /bin/bash +# Run the build without -D test first, to make sure it works that way too: haxe build.hxml && haxe -D test build.hxml -cmd "node bin/extension.js" \ No newline at end of file