Pass EmbeddedScript commands the script instance

This commit is contained in:
2020-12-11 17:31:31 -07:00
parent acd1289fe4
commit 219cadd040
9 changed files with 70 additions and 30 deletions

View File

@@ -9,5 +9,8 @@ class BootCodeExample extends EmbeddedScript {}
@:build(kiss.EmbeddedScript.build("src/year2020/BootCodeDSL.kiss", "src/year2020/inputs/day8.txt"))
class BootCodeReal extends EmbeddedScript {}
@:build(kiss.EmbeddedScript.build("src/year2020/BootCodeFixDSL.kiss", "src/year2020/inputs/day8-example.txt"))
class BootCodeFixExample extends EmbeddedScript {}
@:build(kiss.EmbeddedScript.build("src/year2020/BootCodeFixDSL.kiss", "src/year2020/inputs/day8.txt"))
class BootCodeFix extends EmbeddedScript {}

View File

@@ -2,9 +2,9 @@
(defmethod setBreakPoint [] (addBreakPoint instructionPointer))
(defmethod nop [v] (setBreakPoint))
(defmethod acc [v] (setBreakPoint) (set accumulator (+ accumulator v)))
(defmethod jmp [v]
(setBreakPoint)
//(print (+ "jumping " (Std.string v) " from " (Std.string instructionPointer)))
(set instructionPointer (+ instructionPointer (- v 1))))
(defmethod nop [v :Dynamic self] (self.setBreakPoint))
(defmethod acc [v :Dynamic self] (self.setBreakPoint) (set self.accumulator (+ self.accumulator v)))
(defmethod jmp [v :Dynamic self]
(self.setBreakPoint)
(print (+ "jumping " (Std.string v) " from " (Std.string (+ 1 self.instructionPointer))))
(set self.instructionPointer (+ self.instructionPointer (- v 1))))

View File

@@ -4,7 +4,8 @@
`(,(ReaderExp.Symbol
(begin (stream.dropWhitespace) (nextToken stream)))
(,(ReaderExp.Symbol
(begin (stream.dropWhitespace) (stream.expect "+/-" (lambda [] (stream.takeChars 1)))))
0
,(ReaderExp.Symbol
(nextToken stream)))))
(begin (stream.dropWhitespace) (stream.expect "+/-" (lambda [] (stream.takeChars 1)))))
0
,(ReaderExp.Symbol
(nextToken stream)))
self))

View File

@@ -1,6 +1,8 @@
(load "BootCodeCommon.kiss")
(defvar &mut forked false)
(defvar :Map<Int,Bool> instructionsTested (new Map<Int,Bool>))
(defprop &mut forked false)
(defprop &mut forkedAt -1)
(defreadermacro ["jmp" "nop"] [stream]
(let [inst
@@ -13,13 +15,26 @@
arg
(ReaderExp.Symbol (nextToken stream))]
(stream.dropWhitespace)
`(if forked
(,instSymbol (,op 0 ,arg))
(begin
(fork [
(lambda [] (when ,(ReaderExp.Symbol (Std.string (= inst "nop"))) (set forked true)) (jmp (,op 0 ,arg)))
(lambda [] (when ,(ReaderExp.Symbol (Std.string (= inst "jmp"))) (set forked true)) (nop (,op 0 ,arg)))
`(cond
((or self.forked (instructionsTested.exists self.instructionPointer))
(print "can't fork again")
(,instSymbol (,op 0 ,arg) self))
(true
(dictSet instructionsTested self.instructionPointer true)
(self.setBreakPoint)
(self.fork [
(lambda [:Dynamic self]
(print self.accumulator)
(when ,(ReaderExp.Symbol (Std.string (= inst "nop")))
(print (set self.forked true))
(set self.forkedAt self.instructionPointer))
(jmp (,op 0 ,arg) self))
(lambda [:Dynamic self]
(print self.accumulator)
(when ,(ReaderExp.Symbol (Std.string (= inst "jmp")))
(print (set self.forked true))
(set self.forkedAt self.instructionPointer))
(nop (,op 0 ,arg) self))
])))))
// Define the default reader LAST because default readers tend to break everything

View File

@@ -79,14 +79,30 @@
(assert (= 39645 (Bags.totalChildBags "shiny gold" parentMap)))
// Day 8
(print "BootCodeExample")
(let [example (new BootCodeExample)]
(example.setBreakHandler (lambda [example] (assert (= 5 .accumulator example))))
(example.run))
(print "BootCodeReal")
(let [bootCode (new BootCodeReal)]
(bootCode.setBreakHandler (lambda [bootCode] (assert (= 2058 bootCode.accumulator) (assert true))))
(bootCode.setBreakHandler (lambda [bootCode] (assert (= 2058 (print bootCode.accumulator)))))
(bootCode.run))
(print "BootCodeFixExample")
(let [bootCode (new BootCodeFixExample)]
(bootCode.setBreakHandler
(lambda [bootCodeFork]
(if (= bootCodeFork.instructionPointer (bootCodeFork.instructionCount))
(print (+ "answer could be " (Std.string bootCodeFork.accumulator) " forked at " (Std.string bootCodeFork.forkedAt)))
(print "hit an infinite loop"))))
(bootCode.addBreakPoint (bootCode.instructionCount))
(bootCode.run))
(print "BootCodeFixReal")
(let [bootCode (new BootCodeFix)]
(bootCode.setBreakHandler (lambda [:BootCodeFix bootCodeFork] (when (= bootCodeFork.instructionPointer (bootCodeFork.instructionCount)) (print .accumulator (the BootCodeFix bootCodeFork)))))
(bootCode.setBreakHandler
(lambda [bootCodeFork]
(if (= bootCodeFork.instructionPointer (bootCodeFork.instructionCount))
(print (+ "answer could be " (Std.string bootCodeFork.accumulator) " forked at " (Std.string (+ 1 bootCodeFork.forkedAt))))
(print "hit an infinite loop"))))
(bootCode.addBreakPoint (bootCode.instructionCount))
(bootCode.run))