(loadFrom [haxelib] [file]) macro

This commit is contained in:
2021-06-13 23:20:03 -06:00
parent 102fdfcd38
commit 82d1f3405a
3 changed files with 34 additions and 6 deletions

View File

@@ -124,9 +124,12 @@ class Kiss {
}); });
} }
public static function load(kissFile:String, k:KissState) { public static function load(kissFile:String, k:KissState, ?loadingDirectory:String) {
k.loadedFiles[kissFile] = true; if (loadingDirectory == null)
var stream = Stream.fromFile(Path.join([k.loadingDirectory, kissFile])); loadingDirectory = k.loadingDirectory;
var fullPath = Path.join([loadingDirectory, kissFile]);
k.loadedFiles[fullPath] = true;
var stream = Stream.fromFile(fullPath);
Reader.readAndProcess(stream, k, (nextExp) -> { Reader.readAndProcess(stream, k, (nextExp) -> {
#if test #if test
Sys.println(nextExp.def.toString()); Sys.println(nextExp.def.toString());
@@ -134,7 +137,9 @@ class Kiss {
var expr = readerExpToHaxeExpr(nextExp, k); var expr = readerExpToHaxeExpr(nextExp, k);
// if non-null, stuff it in main() // 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
}); });
} }

View File

@@ -6,10 +6,12 @@ import kiss.Reader;
import kiss.ReaderExp; import kiss.ReaderExp;
import kiss.Kiss; import kiss.Kiss;
import kiss.CompileError; import kiss.CompileError;
import sys.io.Process;
using kiss.Kiss; using kiss.Kiss;
using kiss.Reader; using kiss.Reader;
using kiss.Helpers; using kiss.Helpers;
using StringTools;
// Macros generate new Kiss reader expressions from the arguments of their call expression. // Macros generate new Kiss reader expressions from the arguments of their call expression.
typedef MacroFunction = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> Null<ReaderExp>; typedef MacroFunction = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> Null<ReaderExp>;
@@ -19,14 +21,34 @@ class Macros {
var macros:Map<String, MacroFunction> = []; var macros:Map<String, MacroFunction> = [];
macros["load"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> { macros["load"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(1, 1, "(load \"[file]\")"); wholeExp.checkNumArgs(1, 1, '(load "[file]")');
switch (args[0].def) { switch (args[0].def) {
case StrExp(otherKissFile): case StrExp(otherKissFile):
if (!k.loadedFiles.exists(otherKissFile)) { if (!k.loadedFiles.exists(otherKissFile)) {
Kiss.load(otherKissFile, k); Kiss.load(otherKissFile, k);
} }
default: default:
throw CompileError.fromExp(args[0], "only argument to load should be a string literal"); throw CompileError.fromExp(args[0], "only argument to load should be a string literal of a .kiss file path");
}
null;
};
macros["loadFrom"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(2, 2, '(loadFrom "[haxelib name]" "[file]")');
var libPath = switch (args[0].def) {
case StrExp(libName):
new Process("haxelib", ["libpath", libName]).stdout.readAll().toString().trim();
default:
throw CompileError.fromExp(args[0], "first argument to loadFrom should be a string literal of a haxe library's name");
};
switch (args[1].def) {
case StrExp(otherKissFile):
if (!k.loadedFiles.exists(otherKissFile)) {
Kiss.load(otherKissFile, k, libPath);
}
default:
throw CompileError.fromExp(args[1], "second argument to loadFrom should be a string literal of a .kiss file path");
} }
null; null;
}; };

View File

@@ -77,6 +77,7 @@ class Main {
static function newProject(args:Array<String>) { static function newProject(args:Array<String>) {
var name = promptFor("name"); var name = promptFor("name");
// TODO put the prompted description in a README.md
var pkg = name.replace("-", "_"); var pkg = name.replace("-", "_");
var haxelibJson = { var haxelibJson = {
"name": name, "name": name,