From bf4f513d4c4d14d7feca5f2a0808bda426190b35 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 8 Dec 2020 23:06:55 -0700 Subject: [PATCH] Solve AOC Day 8 Pt 1 with EmbeddedScript --- src/kiss/EmbeddedScript.hx | 27 ++++++++++++++++++++++++--- src/kiss/FieldForms.hx | 1 - 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/kiss/EmbeddedScript.hx b/src/kiss/EmbeddedScript.hx index 8bc8430..8659580 100644 --- a/src/kiss/EmbeddedScript.hx +++ b/src/kiss/EmbeddedScript.hx @@ -20,6 +20,17 @@ class EmbeddedScript { var instructionPointer = 0; var running = false; + // TODO encapsulate these? + public var breakPoints:Map Bool> = []; + public var onBreak:() -> Void = null; + + public function addBreakPoint(instruction:Int, ?condition:() -> Bool) { + if (condition == null) { + condition = () -> true; + } + breakPoints[instruction] = condition; + } + public function new() {} #if macro @@ -58,7 +69,11 @@ class EmbeddedScript { }), name: "instructions", access: [APrivate], - kind: FVar(null, macro [$a{commandList}]) + kind: FFun({ + ret: Helpers.parseComplexType("Array", null), + args: [], + expr: macro return [$a{commandList}] + }) }); classFields.push({ @@ -73,10 +88,16 @@ class EmbeddedScript { ret: null, args: [], expr: macro { - instructions[instructionPointer](); + instructions()[instructionPointer](); ++instructionPointer; - if (instructionPointer >= instructions.length) + if (breakPoints.exists(instructionPointer) && breakPoints[instructionPointer]()) { running = false; + if (onBreak != null) { + onBreak(); + } + } else if (instructionPointer >= instructions().length) { + running = false; + } } }) }); diff --git a/src/kiss/FieldForms.hx b/src/kiss/FieldForms.hx index 82b04a8..21eba5f 100644 --- a/src/kiss/FieldForms.hx +++ b/src/kiss/FieldForms.hx @@ -40,7 +40,6 @@ class FieldForms { return switch (nameExp.def) { case MetaExp("mut", nameExp): access.remove(AFinal); - trace('ACCESS $access'); fieldAccess(formName, fieldName, nameExp, access); default: if (formName == "defvar" || formName == "defun") {