From 0cc6be32c6752f8410f90cca85686d8096a8ea8d Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 4 Jun 2019 15:40:02 -0600 Subject: [PATCH] Not-too-hackish pointers. Close #51. --- examples/pointers/main.hank | 14 +++++++++ examples/pointers/test1.hlog | 6 ++++ hank/HInterface.hx | 57 ++++++++++++++++++++++++++++++++++-- hank/Story.hx | 2 +- hank/StoryTestCase.hx | 5 +++- 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 examples/pointers/main.hank create mode 100644 examples/pointers/test1.hlog diff --git a/examples/pointers/main.hank b/examples/pointers/main.hank new file mode 100644 index 0000000..013fb5c --- /dev/null +++ b/examples/pointers/main.hank @@ -0,0 +1,14 @@ +``` +var v1 = 'Variable 1'; +var v2 = 'Variable 2'; +var v3 = 'Variable 3'; +``` + +{v1} +{&v1} +{v2} +~ v2 = &v1; +{v2} +{*v2} +~ *v2 = 'Value changed.'; +{v1} \ No newline at end of file diff --git a/examples/pointers/test1.hlog b/examples/pointers/test1.hlog new file mode 100644 index 0000000..4f4b95f --- /dev/null +++ b/examples/pointers/test1.hlog @@ -0,0 +1,6 @@ +Variable 1 +v1 +Variable 2 +v1 +Variable 1 +Value changed. \ No newline at end of file diff --git a/hank/HInterface.hx b/hank/HInterface.hx index 821046a..fbff4f5 100644 --- a/hank/HInterface.hx +++ b/hank/HInterface.hx @@ -11,6 +11,52 @@ import hscript.Expr; using hank.Extensions; import hank.StoryTree; +class HankInterp extends Interp { + var hInterface: HInterface; + public function new(hInterface: HInterface) { + this.hInterface = hInterface; + super(); + } + public override function expr(e: Expr): Dynamic { + switch (e) { + // pointers are actually just string keys to the Interp's variables. + // TODO Divert target variables are just fully qualified strings that can be passed to divertTo() + case EUnop("&", true, e): + switch (e) { + case EIdent(id): + return id; + default: + throw 'Addressing complex expressions is not implemented'; + } + case EUnop("*", true, e): + switch (e) { + case EIdent(id): + return variables[variables[id]]; + default: + throw 'Dereferencing complex expressions is not implemented'; + } + default: return super.expr(e); + } + + } + + override function assign(e1: Expr, e2: Expr): Dynamic { + var v = expr(e2); + switch (e1) { + case EUnop("*", true, e): + switch (e) { + case EIdent(id): + variables[variables[id]] = v; + default: + throw 'Dereferenced assignment of complex expressions is not implemented.'; + } + return v; + default: + return super.assign(e1, e2); + } + } +} + /** Interface between a Hank story, and its embedded hscript interpreter **/ @@ -18,16 +64,23 @@ import hank.StoryTree; class HInterface { var BOOLEAN_OPS = ['&&', '||', '!']; - var parser: Parser = new Parser(); - var interp: Interp = new Interp(); + var parser: Parser; + var interp: HankInterp; var viewCounts: Map; public function new(storyTree: StoryNode, viewCounts: Map) { + + this.parser = new Parser(); + parser.unops["*"] = false; + parser.unops["&"] = false; + this.interp = new HankInterp(this); + this.interp.variables['_isTruthy'] = isTruthy.bind(viewCounts); this.interp.variables['_valueOf'] = valueOf.bind(viewCounts); this.interp.variables['_resolve'] = resolveInScope.bind(this.interp.variables); this.interp.variables['_resolveField'] = resolveField.bind(this.interp.variables); this.viewCounts = viewCounts; + } static function resolveInScope(variables: Map, name: String): Dynamic { diff --git a/hank/Story.hx b/hank/Story.hx index 8f8b08f..395c352 100644 --- a/hank/Story.hx +++ b/hank/Story.hx @@ -426,7 +426,7 @@ class Story { null; } - trace(typeName); + //trace(typeName); if (typeName != null && insertionHooks.exists(typeName)) { return insertionHooks[typeName](value); } diff --git a/hank/StoryTestCase.hx b/hank/StoryTestCase.hx index 32f48d3..5441709 100644 --- a/hank/StoryTestCase.hx +++ b/hank/StoryTestCase.hx @@ -140,7 +140,7 @@ class StoryTestCase extends utest.Test { var firstColonIdx = line.indexOf(':'); var index = Std.parseInt(line.substr(0, firstColonIdx))-1; var expectedOutput = line.substr(firstColonIdx+1).trim(); - // trace('expecting: ${expectedOutput}'); + // trace('expecting: ${expectedOutput}'); var output = story.choose(index); // trace('got: ${output}'); HankAssert.equals(expectedOutput, output); @@ -148,6 +148,9 @@ class StoryTestCase extends utest.Test { else if (line.length > 0) { // Assert that the story's next frame is HasText(line) // trace('${line} from ${frame}'); + //trace('expecting: ${HasText(line)}'); + //trace('got: $frame'); + //trace(''); HankAssert.equals(HasText(line), frame); }