(module token (data Token (TokBlank) (TokK) (TokS) (TokCommit) (TokI) (TokDot) (TokIota) (TokFlush)) (data TokenRow (TokenRow Int Token Bool Bool)) (data CellError (CellNotBit Int Int) (CellPartial Int Int) (CellNoToken Int Int)) (data CellParse (CellsParsed (List Token)) (CellsFailed CellError)) (defn token-of-cell ((bits Int)) (Option Token) (if (= bits 0) (some (TokBlank)) (if (= bits 1) (some (TokK)) (if (= bits 2) (some (TokS)) (if (= bits 3) (some (TokCommit)) (if (= bits 4) (some (TokI)) (if (= bits 5) (some (TokDot)) (if (= bits 6) (some (TokIota)) (if (= bits 7) (some (TokFlush)) (none)))))))))) (defn token-rows () (List TokenRow) (cons (TokenRow 0 (TokBlank) false false) (cons (TokenRow 1 (TokK) true false) (cons (TokenRow 2 (TokS) true false) (cons (TokenRow 3 (TokCommit) false true) (cons (TokenRow 4 (TokI) true false) (cons (TokenRow 5 (TokDot) false false) (cons (TokenRow 6 (TokIota) true false) (cons (TokenRow 7 (TokFlush) false true) (nil)))))))))) (defn cells-loop ((bits (List Int)) (index Int) (value Int) (width Int) (cells (List Token))) CellParse (match bits ((nil) (if (= width 0) (CellsParsed (reverse cells)) (CellsFailed (CellPartial index width)))) ((cons bit rest) (if (and (>= bit 0) (<= bit 1)) (let ((next-value (+ (* value 2) bit)) (next-width (+ width 1))) (if (= next-width 3) (match (token-of-cell next-value) ((none) (CellsFailed (CellNoToken index next-value))) ((some token) (cells-loop rest (+ index 1) 0 0 (cons token cells)))) (cells-loop rest index next-value next-width cells))) (CellsFailed (CellNotBit index bit)))))) (defn cells-of-bits ((bits (List Int))) CellParse (cells-loop bits 0 0 0 (nil))))