implement knots.hank up-to-date

This commit is contained in:
2020-10-03 11:44:50 -06:00
parent 11fb50c2d2
commit af29fca070
4 changed files with 28 additions and 72 deletions

View File

@@ -1,6 +1,5 @@
package hank;
import hiss.HissRepl;
import hiss.HissReader;
import hiss.HissTools;
import hiss.StaticFiles;
@@ -10,7 +9,7 @@ class Demo implements StoryTeller {
StaticFiles.compileWithAll("examples");
// TODO ask the user to choose an example
new Story("examples/rc-bank-heist.hank", new Demo()).run();
new Story("examples/knots.hank", new Demo()).run();
}
public function new() {

View File

@@ -1,12 +1,13 @@
package hank;
import hiss.HTypes;
import hiss.HissRepl;
import hiss.CCInterp;
import hiss.StaticFiles;
using hiss.HissTools;
class Story {
var teller: StoryTeller;
var hissRepl: HissRepl;
var interp: CCInterp;
var storyScript: String;
public function new(storyScript: String, storyTeller: StoryTeller) {
@@ -17,27 +18,19 @@ class Story {
teller = storyTeller;
hissRepl = new HissRepl();
interp = new CCInterp();
hissRepl.interp.set("*handle-output*", Function(Haxe(Fixed, function(text: HValue) {
storyTeller.handleOutput(text.toString());
return Nil;
}, "*handle-output*")));
interp.importFunction(storyTeller.handleOutput, "*handle-output*");
hissRepl.load("hanklib.hiss");
interp.load("hanklib.hiss");
}
public function run() {
hissRepl.load("reader-macros.hiss");
interp.load("reader-macros.hiss");
hissRepl.interp.print(Atom(String("For debug purposes, it reads as:")));
hissRepl.interp.print(hissRepl.readAll(StaticFiles.getContent(storyScript)));
hissRepl.interp.print(Atom(String("")));
String("For debug purposes, it reads as:").print();
interp.readAll(StaticFiles.getContent(storyScript)).print();
switch (hissRepl.load(storyScript)) {
case Signal(Error(s)):
throw s;
default:
}
interp.load(storyScript);
}
}

View File

@@ -1,5 +1,5 @@
(defun output (&rest parts)
(*handle-output* (funcall + (map parts eval-output-part))))
(*handle-output* (apply + (map parts eval-output-part))))
(defun eval-output-part (part)
(cond

View File

@@ -1,55 +1,19 @@
/*
['INCLUDE ' => include],
['<-' => thread],
['->' => divert],
['===' => knot],
['==' => knot],
['=' => stitch],
['~' => haxeLine],
['```' => haxeBlock],
['-' => gather],
['*' => choice],
['+' => choice],
['#' => tag]];
(def-reader-macro "->" (start stream)
`(funcall ,(read-symbol "" stream)))
[] starts choice conditional text
{ } starts a Hiss insertion -- usually for variables
( ) starts a Hiss funcall insertion -- equivalent to {()}
< > starts an alt expression
*/
(def-reader-macro "==" (start stream)
(let (knot-name
(read-symbol "" stream)
_
(HStream:take-line stream)
knot-body
(read-all (first (HStream:take-until stream ["=="] nil nil t))))
// Define the knot's function at READ-TIME
(eval `(defun ,knot-name () ,@knot-body))
nil))
// ( ) at the start of an output is a label
(defun read-output (start stream)
`(output ,(HStream:take-line stream "rl")))
(defmacro expect (stream function &rest args)
`(get (call ,stream ,function ,@args) output))
(setq *hank-inline-terminators* '(
"->"
"["
"{"
"("
"<"))
(defun read-output (_ stream _)
;(print "reading output")
;(print "RIGHT BELOW:")
;(print (call stream peek-all))
(list 'output (expect stream take-until (list "\n") t)))
(defun read-divert (_ stream _)
;(print "reading divert")
(call stream drop-whitespace)
(list 'divert (expect stream take-until-whitespace)))
(defun hank-read-mode ()
(setq *hiss-readtable* (copy-readtable))
; TODO set other read macros
(set-macro-string "->" read-divert)
(set-default-read-function read-output))
(defun hiss-read-mode ()
(set-readtable *hiss-readtable*)
(set-default-read-function read-symbol))
(hank-read-mode)
(set-default-read-function read-output)