diff --git a/examples/subsections/main.hank b/examples/subsections/main.hank index 480537e..9634250 100644 --- a/examples/subsections/main.hank +++ b/examples/subsections/main.hank @@ -11,7 +11,7 @@ Fourth output. = one -First output. +-(gather) First output. -> two diff --git a/hank/HInterface.hx b/hank/HInterface.hx index 5f408b3..0297f40 100644 --- a/hank/HInterface.hx +++ b/hank/HInterface.hx @@ -1,7 +1,7 @@ package hank; using Reflect; -import Type; +using Type; import hscript.Parser; import hscript.Interp; @@ -18,33 +18,43 @@ class HInterface { var parser: Parser = new Parser(); var interp: Interp = new Interp(); + var viewCounts: Map; public function new(storyTree: StoryNode, viewCounts: Map) { this.interp.variables['_isTruthy'] = isTruthy; - this.interp.variables['_resolve'] = resolve.bind(this.interp.variables, viewCounts, storyTree); - this.interp.variables['_resolveField'] = resolve.bind(this.interp.variables, viewCounts); + this.interp.variables['_resolve'] = resolve.bind(this.interp.variables, storyTree); + this.interp.variables['_resolveField'] = resolve.bind(this.interp.variables); + this.viewCounts = viewCounts; } - static function resolve(variables: Map, viewCounts: Map,container: Dynamic, name: String): Dynamic { + static function isStoryNode(o: Dynamic) { + var type = o.typeof(); + switch (type) { + case TClass(c): + if (c.getClassName() == 'hank.StoryNode') { + return true; + } + default: + } + return false; + } + + static function resolve(variables: Map, container: Dynamic, name: String): Dynamic { if (variables.exists(name)) { return variables[name]; - } else { + } else if (isStoryNode(container)) { // If the variable is a StoryNode, don't return the node, return its viewCount - var type = Type.typeof(container); - switch (type) { - case TClass(c): - trace(c); - var node: StoryNode = cast(container, StoryNode); - switch (node.resolve(name)) { - case Some(node): - return viewCounts[node]; - case None: - throw 'Cannot resolve ${name}'; - } - default: - return container.field(name); + var node: StoryNode = cast(container, StoryNode); + switch (node.resolve(name)) { + case Some(node): + return node; + case None: + throw 'Cannot resolve ${name}'; } } + else { + return container.field(name); + } } static function isTruthy(v: Dynamic) { @@ -75,11 +85,15 @@ class HInterface { var expr = parser.parseString(h); expr = transmute(expr); var val = interp.expr(expr); - if (val == null) { + + return Std.string(if (val == null) { throw 'Expression ${h} evaluated to null'; + } else if (isStoryNode(val)) { + var node: StoryNode = cast(val, StoryNode); + viewCounts[node]; } else { - return Std.string(val); - } + val; + }); } /** diff --git a/tests/HInterfaceTest.hx b/tests/HInterfaceTest.hx index 12d981f..834a839 100644 --- a/tests/HInterfaceTest.hx +++ b/tests/HInterfaceTest.hx @@ -29,6 +29,7 @@ class HInterfaceTest extends utest.Test { viewCounts[storyTree.resolve("start").unwrap()] = 5; viewCounts[storyTree.resolve("start").unwrap().resolve("one").unwrap()] = 2; + viewCounts[storyTree.resolve("start").unwrap().resolve("one").unwrap().resolve("gather").unwrap()] = 7; hInterface = new HInterface(storyTree, viewCounts); } @@ -39,7 +40,8 @@ class HInterfaceTest extends utest.Test { function testViewCount() { assertExpr('start', 5); - //assertExpr('start.one', 5); + assertExpr('start.one', 2); + assertExpr('start.one.gather', 7); } function testClassInstanceDotAccess() {