about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--315slice.subx14
-rw-r--r--400.mu5
-rw-r--r--apps/advent2020/4b.mu20
3 files changed, 39 insertions, 0 deletions
diff --git a/315slice.subx b/315slice.subx
new file mode 100644
index 00000000..0a348550
--- /dev/null
+++ b/315slice.subx
@@ -0,0 +1,14 @@
+== code
+
+# variant of slice-to-string intended to be called from Mu
+# Mu doesn't yet expose allocation-descriptors
+_slice-to-string:  # in: (addr slice), out: (addr handle array byte)
+    # . prologue
+    55/push-ebp
+    89/<- %ebp 4/r32/esp
+    #
+    (slice-to-string Heap *(ebp+8) *(ebp+0xc))
+    # . epilogue
+    89/<- %esp 5/r32/ebp
+    5d/pop-to-ebp
+    c3/return
diff --git a/400.mu b/400.mu
index 23e3ce2d..d766a8f6 100644
--- a/400.mu
+++ b/400.mu
@@ -94,7 +94,9 @@ sig slice-equal? s: (addr slice), p: (addr array byte) -> _/eax: boolean
 sig slice-starts-with? s: (addr slice), head: (addr array byte) -> _/eax: boolean
 sig write-slice out: (addr stream byte), s: (addr slice)
 sig write-slice-buffered out: (addr buffered-file), s: (addr slice)
+# bad name alert
 sig slice-to-string ad: (addr allocation-descriptor), in: (addr slice), out: (addr handle array byte)
+sig _slice-to-string in: (addr slice), out: (addr handle array byte)
 sig next-token in: (addr stream byte), delimiter: byte, out: (addr slice)
 sig next-token-from-slice start: (addr byte), end: (addr byte), delimiter: byte, out: (addr slice)
 sig skip-chars-matching in: (addr stream byte), delimiter: byte
@@ -113,6 +115,9 @@ sig write-stream-data f: (addr buffered-file), s: (addr stream byte)
 sig write-int32-decimal out: (addr stream byte), n: int
 sig is-decimal-digit? c: grapheme -> _/eax: boolean
 sig to-decimal-digit in: grapheme -> _/eax: int
+# bad name alert
+# next-word really tokenizes
+# next-raw-word really reads whitespace-separated words
 sig next-word line: (addr stream byte), out: (addr slice)  # skips '#' comments
 sig next-raw-word line: (addr stream byte), out: (addr slice)  # does not skip '#' comments
 sig has-metadata? word: (addr slice), s: (addr string) -> _/eax: boolean
diff --git a/apps/advent2020/4b.mu b/apps/advent2020/4b.mu
index d4a3496e..ffeddb32 100644
--- a/apps/advent2020/4b.mu
+++ b/apps/advent2020/4b.mu
@@ -157,6 +157,26 @@ fn main -> _/ebx: int {
         var pid?/eax: boolean <- slice-equal? key-slice, "pid"
         compare pid?, 0  # false
         break-if-=
+        # convert val
+        var s: (handle array byte)
+        var s2: (addr handle array byte) <- address s
+        _slice-to-string val-slice, s2
+        # check length
+        var len/eax: int <- length s2
+        compare len, 9
+        {
+          break-if-=
+          curr-passport-field-count <- copy 8
+        }
+        # check valid decimal int
+        # parse-decimal-int-from-slice currently returns 0 on invalid parse,
+        # which isn't ideal but suffices for our purposes
+        var val/eax: int <- parse-decimal-int-from-slice val-slice
+        compare val, 0
+        {
+          break-if->
+          curr-passport-field-count <- copy 8
+        }
       }
       loop
     }