From f48318bee40b0ff40f32c1d3275da872cd8cf60c Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 21 May 2021 17:27:19 -0600 Subject: [PATCH] [nap] tags example --- kiss/src/kiss/Macros.hx | 1 + projects/nap/build.hxml | 7 ----- projects/nap/example-archive/song1.json | 1 - projects/nap/haxelib.json | 1 - projects/nap/src/nap/Archive.hx | 4 +-- projects/nap/src/nap/Archive.kiss | 22 +++++++------- projects/nap/src/nap/BoolExpInterp.hx | 3 ++ projects/nap/src/nap/BoolExpInterp.kiss | 9 ++++++ projects/nap/src/nap/Entry.hx | 4 +-- projects/nap/src/nap/Event.hx | 7 ----- projects/nap/src/nap/Lib.hx | 9 +++--- projects/nap/src/nap/Lib.kiss | 33 ++++++++++++--------- projects/nap/src/nap/Main.hx | 7 ----- projects/nap/src/nap/Main.kiss | 27 ----------------- projects/nap/src/nap/components/Tag.hx | 3 -- projects/nap/src/nap/components/Tag.kiss | 2 -- projects/nap/src/nap/components/Tags.hx | 3 ++ projects/nap/src/nap/systems/TagSystem.kiss | 6 ++-- projects/nap/src/test/TestMain.hx | 2 ++ projects/nap/src/test/TestMain.kiss | 31 ++++++++++--------- projects/nap/test.hxml | 6 ++++ projects/nap/test.sh | 2 +- 22 files changed, 84 insertions(+), 106 deletions(-) delete mode 100644 projects/nap/build.hxml delete mode 100644 projects/nap/example-archive/song1.json create mode 100644 projects/nap/src/nap/BoolExpInterp.kiss delete mode 100644 projects/nap/src/nap/Event.hx delete mode 100644 projects/nap/src/nap/Main.hx delete mode 100644 projects/nap/src/nap/Main.kiss delete mode 100644 projects/nap/src/nap/components/Tag.hx delete mode 100644 projects/nap/src/nap/components/Tag.kiss create mode 100644 projects/nap/src/nap/components/Tags.hx create mode 100644 projects/nap/test.hxml diff --git a/kiss/src/kiss/Macros.hx b/kiss/src/kiss/Macros.hx index 6ca505e4..6196ea88 100644 --- a/kiss/src/kiss/Macros.hx +++ b/kiss/src/kiss/Macros.hx @@ -553,6 +553,7 @@ class Macros { argList.push(propExp); propertyDefs.push( b.call(b.symbol("defprop"), [propExp])); + // TODO allow &prop &mut or &mut &prop switch (propExp.def) { case TypedExp(_, {pos: _, def: Symbol(name)}): propertySetExps.push( diff --git a/projects/nap/build.hxml b/projects/nap/build.hxml deleted file mode 100644 index dcfc5b21..00000000 --- a/projects/nap/build.hxml +++ /dev/null @@ -1,7 +0,0 @@ --lib uuid --lib kiss --lib hscript --lib tink_json --cp src ---main nap.Main ---interp \ No newline at end of file diff --git a/projects/nap/example-archive/song1.json b/projects/nap/example-archive/song1.json deleted file mode 100644 index b193e724..00000000 --- a/projects/nap/example-archive/song1.json +++ /dev/null @@ -1 +0,0 @@ -{"name":"Villain Alcohol","id":"song1","components":[["Tag", ["song"]]]} \ No newline at end of file diff --git a/projects/nap/haxelib.json b/projects/nap/haxelib.json index 84d7c7c5..337f7ccb 100644 --- a/projects/nap/haxelib.json +++ b/projects/nap/haxelib.json @@ -8,7 +8,6 @@ "releasenote": "It isn't safe to use this library yet.", "contributors": ["NQNStudios"], "classPath": "src/", - "main": "nap.Main", "dependencies": { "kiss": "", "tink_json": "" diff --git a/projects/nap/src/nap/Archive.hx b/projects/nap/src/nap/Archive.hx index 280b825d..5ce0210f 100644 --- a/projects/nap/src/nap/Archive.hx +++ b/projects/nap/src/nap/Archive.hx @@ -1,10 +1,10 @@ package nap; -import kiss.Prelude; import sys.FileSystem; +import kiss.Prelude; import sys.io.File; import tink.Json; -import nap.systems.TagSystem; +import uuid.Uuid; using haxe.io.Path; diff --git a/projects/nap/src/nap/Archive.kiss b/projects/nap/src/nap/Archive.kiss index 54328a2e..a21eccf7 100644 --- a/projects/nap/src/nap/Archive.kiss +++ b/projects/nap/src/nap/Archive.kiss @@ -1,16 +1,18 @@ -(defnew [archiveDir] +(defnew [&prop :String archiveDir + &prop :Array systems] + [:Map entries - (let [entryFiles (FileSystem.readDirectory archiveDir)] - (for file entryFiles =>(file.withoutExtension) ~(the Entry (Json.parse (File.getContent (Path.join [archiveDir file])))))) - :Array systems [ - (the System (new TagSystem "song"))]] + (let [entryFiles (FileSystem.readDirectory (Path.join [archiveDir "entries"]))] + (for file entryFiles =>(file.withoutExtension) ~(the Entry (Json.parse (File.getContent (Path.join [archiveDir "entries" file]))))))] + + // Assign entries to the Systems that care about them (doFor system systems (doFor =>id entry entries (when (system.canProcessEntry entry) (system.entries.push entry))))) -(defmethod :Void handleEvent [:Event e] - (throw "can't handle $e")) - -(defmethod :Void process [] - (doFor system systems (system.process))) \ No newline at end of file +(defun :Entry _newEntry [] + (object + id (Uuid.v4) + components (new Map) + files [])) \ No newline at end of file diff --git a/projects/nap/src/nap/BoolExpInterp.hx b/projects/nap/src/nap/BoolExpInterp.hx index d4380697..4e6a1f8f 100644 --- a/projects/nap/src/nap/BoolExpInterp.hx +++ b/projects/nap/src/nap/BoolExpInterp.hx @@ -1,7 +1,10 @@ package nap; import kiss.KissInterp; +import hscript.Parser; +import kiss.Prelude; +@:build(kiss.Kiss.build()) class BoolExpInterp extends KissInterp { public function new() { super(); diff --git a/projects/nap/src/nap/BoolExpInterp.kiss b/projects/nap/src/nap/BoolExpInterp.kiss new file mode 100644 index 00000000..df53363f --- /dev/null +++ b/projects/nap/src/nap/BoolExpInterp.kiss @@ -0,0 +1,9 @@ +(defun eval [:String expStr :Array activeConditions] + (let [hscriptExp + (.parseString (new Parser) + (Prelude.convertToHScript expStr)) + interp + (new BoolExpInterp)] + (doFor condition activeConditions + (interp.variables.set condition true)) + ?(interp.execute hscriptExp))) \ No newline at end of file diff --git a/projects/nap/src/nap/Entry.hx b/projects/nap/src/nap/Entry.hx index 0b07e28f..528f1926 100644 --- a/projects/nap/src/nap/Entry.hx +++ b/projects/nap/src/nap/Entry.hx @@ -2,6 +2,6 @@ package nap; typedef Entry = { id:String, - name:String, - components:Map> + components:Map, + files:Array }; diff --git a/projects/nap/src/nap/Event.hx b/projects/nap/src/nap/Event.hx deleted file mode 100644 index a6ce264f..00000000 --- a/projects/nap/src/nap/Event.hx +++ /dev/null @@ -1,7 +0,0 @@ -package nap; - -/** - * All types of user input events to the NAP system. Archive handles these, acting as a backend - * for whichever front-end generates the Events. - */ -enum Event {} diff --git a/projects/nap/src/nap/Lib.hx b/projects/nap/src/nap/Lib.hx index b9b31adf..6ab38ebf 100644 --- a/projects/nap/src/nap/Lib.hx +++ b/projects/nap/src/nap/Lib.hx @@ -1,10 +1,11 @@ package nap; -import uuid.Uuid; import kiss.Prelude; -import kiss.Stream; -import hscript.Parser; -import hscript.Interp; +import sys.io.File; +import tink.Json; +import uuid.Uuid; + +using haxe.io.Path; @:build(kiss.Kiss.build()) class Lib {} diff --git a/projects/nap/src/nap/Lib.kiss b/projects/nap/src/nap/Lib.kiss index cdd15e64..770f8f38 100644 --- a/projects/nap/src/nap/Lib.kiss +++ b/projects/nap/src/nap/Lib.kiss @@ -1,15 +1,20 @@ -(defun evalBoolExp [:String expStr :Array activeConditions] - (let [hscriptExp - (.parseString (new Parser) - (Prelude.convertToHScript expStr)) - interp - (new BoolExpInterp)] - (doFor condition activeConditions - (interp.variables.set condition true)) - ?(interp.execute hscriptExp))) +// Lib is its own class because, while it would make sense to group its functions and macros in Archive.kiss, +// other files would not be able to (load "Archive.kiss") for the macro definitions without taking on Archive's constructor. -(defun :Entry newEntry [:String name] - (object - id (Uuid.v4) - name name - components (new Map))) \ No newline at end of file +(defmacro hasComponent [e componentType] + `(.exists .components ,e ,(symbolName componentType))) + +// TODO add to the documentation a hint that macros should use fully qualified paths so macro caller classes don't need to import everything +(defmacro getComponent [archive e componentType] + `(the nap.components ,componentType + (tink.Json.parse + (sys.io.File.getContent + (haxe.io.Path.join [.archiveDir (the nap.Archive ,archive) "components" (+ (dictGet (the Map .components ,e) ,(symbolName componentType)) ".json")]))))) + +(defun tagList [archive e] + (let [t + (getComponent archive e Tags)] + (collect (t.keys)))) + +(defun tagsMatch [archive e tagsBoolExp] + (BoolExpInterp.eval tagsBoolExp (tagList archive e))) \ No newline at end of file diff --git a/projects/nap/src/nap/Main.hx b/projects/nap/src/nap/Main.hx deleted file mode 100644 index 78d48151..00000000 --- a/projects/nap/src/nap/Main.hx +++ /dev/null @@ -1,7 +0,0 @@ -package nap; - -import kiss.Kiss; -import kiss.Prelude; - -@:build(kiss.Kiss.build()) -class Main {} diff --git a/projects/nap/src/nap/Main.kiss b/projects/nap/src/nap/Main.kiss deleted file mode 100644 index 0c43e4f0..00000000 --- a/projects/nap/src/nap/Main.kiss +++ /dev/null @@ -1,27 +0,0 @@ -(defun :Void main [] - ~(Lib.evalBoolExp "true" []) - ~(Lib.evalBoolExp "false" []) - ~(Lib.evalBoolExp "flag" []) - ~(Lib.evalBoolExp "flag" ["flag"]) - ~(Lib.evalBoolExp "(and flag false)" ["flag"]) - ~(Lib.evalBoolExp "(or flag otherFlag)" ["otherFlag"]) - //trace(error); - (let [archiveDir - // TODO optional flags like --cache will complicate this way of handling args - (.shift (Sys.args)) - archive - (new Archive archiveDir)] - // TODO run a front-end -- could be a test frontend that sends predetermined list of events - (archive.process))) - - - - /* - (let [e (new Entry "name")] - (dictSet e.components "f" [5]) - (dictSet e.components "b" [(object c "d")]) - (let [s (haxe.Json.stringify e) - :Entry e2 (haxe.Json.parse s)] - // can't call Entry methods on e2 - (print s)))) - */ \ No newline at end of file diff --git a/projects/nap/src/nap/components/Tag.hx b/projects/nap/src/nap/components/Tag.hx deleted file mode 100644 index 48cfe484..00000000 --- a/projects/nap/src/nap/components/Tag.hx +++ /dev/null @@ -1,3 +0,0 @@ -package nap.components; - -typedef Tag = String; diff --git a/projects/nap/src/nap/components/Tag.kiss b/projects/nap/src/nap/components/Tag.kiss deleted file mode 100644 index 493cdc44..00000000 --- a/projects/nap/src/nap/components/Tag.kiss +++ /dev/null @@ -1,2 +0,0 @@ -(defun :Array getTags [:Entry e] - (or (dictGet e.components "Tag") [])) \ No newline at end of file diff --git a/projects/nap/src/nap/components/Tags.hx b/projects/nap/src/nap/components/Tags.hx new file mode 100644 index 00000000..a082b1f0 --- /dev/null +++ b/projects/nap/src/nap/components/Tags.hx @@ -0,0 +1,3 @@ +package nap.components; + +typedef Tags = Map; diff --git a/projects/nap/src/nap/systems/TagSystem.kiss b/projects/nap/src/nap/systems/TagSystem.kiss index 227760a7..9d8d9a54 100644 --- a/projects/nap/src/nap/systems/TagSystem.kiss +++ b/projects/nap/src/nap/systems/TagSystem.kiss @@ -1,6 +1,4 @@ -(load "../components/Tag.kiss") - (defnew [&prop :String tagFilterString] []) -(defmethod &override :Bool canProcessEntry [:Entry e] - (Lib.evalBoolExp tagFilterString (getTags e))) \ No newline at end of file +(defmethod &override :Bool canProcessEntry [:Entry e] (print false)) + // (Lib.evalBoolExp tagFilterString (getTags e))) \ No newline at end of file diff --git a/projects/nap/src/test/TestMain.hx b/projects/nap/src/test/TestMain.hx index e2631b39..37438f38 100644 --- a/projects/nap/src/test/TestMain.hx +++ b/projects/nap/src/test/TestMain.hx @@ -4,6 +4,8 @@ import kiss.Kiss; import kiss.Prelude; import nap.BoolExpInterp; import nap.Archive; +import nap.Lib; +import nap.components.*; @:build(kiss.Kiss.build()) class TestMain {} diff --git a/projects/nap/src/test/TestMain.kiss b/projects/nap/src/test/TestMain.kiss index 28477b12..271ba5e0 100644 --- a/projects/nap/src/test/TestMain.kiss +++ b/projects/nap/src/test/TestMain.kiss @@ -1,3 +1,7 @@ +// TODO external programs need to be able to find and (load) this path to get the macros: +(load "../nap/Lib.kiss") + + (defun :Void main [] (assert (BoolExpInterp.eval "true" [])) (assert !(BoolExpInterp.eval "false" [])) @@ -9,17 +13,16 @@ (let [systems [] archive - (new Archive "src/test/example-archive" systems)] - (print "TODO"))) - - - - /* - (let [e (new Entry "name")] - (dictSet e.components "f" [5]) - (dictSet e.components "b" [(object c "d")]) - (let [s (haxe.Json.stringify e) - :Entry e2 (haxe.Json.parse s)] - // can't call Entry methods on e2 - (print s)))) - */ \ No newline at end of file + (new Archive "src/test/example-archive" systems) + song1 + (dictGet archive.entries "song1") + song2 + (dictGet archive.entries "song2")] + + (assert (hasComponent song1 Tags)) + (assert (hasComponent song2 Tags)) + (assert (tagsMatch archive song1 "(and song western)")) + (assert !(tagsMatch archive song1 "(and song religious)")) + (assert (tagsMatch archive song2 "(and song religious)")) + (assert !(tagsMatch archive song2 "(and song western)")) + )) \ No newline at end of file diff --git a/projects/nap/test.hxml b/projects/nap/test.hxml new file mode 100644 index 00000000..53281b50 --- /dev/null +++ b/projects/nap/test.hxml @@ -0,0 +1,6 @@ +-D test +-lib kiss +-lib nap +-cp test +--main test.TestMain +--interp \ No newline at end of file diff --git a/projects/nap/test.sh b/projects/nap/test.sh index 05e25e95..67438b8a 100644 --- a/projects/nap/test.sh +++ b/projects/nap/test.sh @@ -1,4 +1,4 @@ #! /bin/bash haxelib dev nap . -haxelib run nap example-archive \ No newline at end of file +haxe test.hxml \ No newline at end of file