The mechanical half, ahead of the parser change that needs it. tools/unit-return.py
fills the empty slot with () and rewrites Unit as () wherever a type is spelled --
(Fn [i32] Unit), (Map i32 Unit), a return type written out.
Deciding whether a defn already had a return type is the whole difficulty, and
the script does it the way parse.ml did: is_type_form is transcribed rather than
improved, because being identical to the parser it replaces is what makes the
sweep meaning-preserving. It is re-runnable, so the lanes that branched before
this can have the same pass at merge:
python3 tools/unit-return.py .
python3 tools/unit-return.py --in-strings test/test_flan.ml test/test_acceptance.ml \
test/test_session.ml emacs/test-flan-dev.el emacs/test-flan-mode.el
python3 tools/unit-return.py --raw-ml lib/prelude.ml
python3 tools/unit-return.py --in-html web/index.html
-v logs every defn it saw and what it decided, which is how a sweep of 440 sites
gets reviewed at all. Embedded modes pool a file's type declarations across all
its fragments, because a snippet split across concatenation -- decls ^ "(defn f
[s [u8]] Cursor ...)" -- cannot see the names the other half declared; pooled
names count only in bare-symbol position, for the same reason the prelude's do.
A fragment that cuts off mid-form is skipped rather than guessed at. Five sites
in test_flan.ml still needed a hand, and they are in this commit.
Two things ride along because the sweep needs them: parse.ml reads a lone () as
the return type of a function with no body, which was not a shape the old
optional slot could produce; and the map refusals name () rather than Unit, since
that is now the spelling a caller wrote.
96 lines
5.0 KiB
Plaintext
96 lines
5.0 KiB
Plaintext
;;;; index-of-bytes, trim, the two byte classes, and parse-f64.
|
|
;;;;
|
|
;;;; The search cases are the ones a naive loop gets wrong rather than the
|
|
;;;; ones it gets right: a needle that matches only at the very end, one that
|
|
;;;; matches only at index 0, one whose first byte occurs repeatedly before
|
|
;;;; the real match ("aab" in "aaab"), a needle longer than the haystack
|
|
;;;; (which must answer None and must not trap building the window), the
|
|
;;;; empty needle, and a near-miss that shares every byte but the last.
|
|
;;;;
|
|
;;;; The parse-f64 cases are every shape strtod answers a plausible number
|
|
;;;; for and a caller cannot tell from a real one: "", "abc", "1x", ".",
|
|
;;;; "1e", " 1", "0x10" and "nan". Each must be None.
|
|
|
|
(defn show-idx [o (Option i32)] ()
|
|
(print (match o (Some i) i None -1))
|
|
(print " "))
|
|
|
|
(defn show-bool [b bool] ()
|
|
(print (if b "t" "f")))
|
|
|
|
;; Brackets around the result so an empty trim is visible as [] rather than
|
|
;; as nothing at all — the all-whitespace case is otherwise indistinguishable
|
|
;; from a trim that printed the wrong slice of length zero.
|
|
(defn show-trim [s string] ()
|
|
(print "[")
|
|
(print (trim (bytes s)))
|
|
(print "]"))
|
|
|
|
(defn main [] i32
|
|
(show-idx (index-of-bytes (bytes "hello world") (bytes "world"))) ; 6, at the end
|
|
(show-idx (index-of-bytes (bytes "hello world") (bytes "hello"))) ; 0, at the start
|
|
(show-idx (index-of-bytes (bytes "hello world") (bytes "o w"))) ; 4, in the middle
|
|
(show-idx (index-of-bytes (bytes "banana") (bytes "na"))) ; 2, first of two
|
|
(show-idx (index-of-bytes (bytes "aaab") (bytes "aab"))) ; 1, after false starts
|
|
(println "")
|
|
(show-idx (index-of-bytes (bytes "hello") (bytes "hellp"))) ; -1, last byte differs
|
|
(show-idx (index-of-bytes (bytes "hi") (bytes "hiya"))) ; -1, longer, no trap
|
|
(show-idx (index-of-bytes (bytes "") (bytes "a"))) ; -1, empty haystack
|
|
(show-idx (index-of-bytes (bytes "hello") (bytes ""))) ; 0, empty needle
|
|
(show-idx (index-of-bytes (bytes "") (bytes ""))) ; 0, both empty
|
|
(show-idx (index-of-bytes (bytes "hello") (bytes "hello"))) ; 0, whole string
|
|
(println "")
|
|
|
|
(show-trim " hi ") ; [hi]
|
|
(show-trim "hi") ; [hi] nothing to remove
|
|
(show-trim "\thi\n") ; [hi] tab and newline count
|
|
(show-trim " ") ; [] all whitespace, must not run backwards
|
|
(show-trim "") ; []
|
|
(show-trim " a b ") ; [a b] the inner space survives
|
|
(show-trim " x") ; [x] one-sided
|
|
(show-trim "x ") ; [x]
|
|
(println "")
|
|
|
|
(show-bool (digit? \0)) (show-bool (digit? \9)) (show-bool (digit? \/))
|
|
(show-bool (digit? \:)) (show-bool (digit? \a))
|
|
(println "")
|
|
(show-bool (space? \space)) (show-bool (space? \tab))
|
|
(show-bool (space? \newline)) (show-bool (space? \return))
|
|
(show-bool (space? \a)) (show-bool (space? \0))
|
|
(println "")
|
|
|
|
;; Accepted. The last is the round trip through %g that proves the value and
|
|
;; not merely the acceptance is right.
|
|
(print (match (parse-f64 (bytes "0")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "3.5")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "-3.5")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "+0.25")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "1e3")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "1.5E-2")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "12")) (Some v) v None -999.0))
|
|
(println "")
|
|
;; Refused. Every one of these is a number out of strtod, which is the point.
|
|
(print (match (parse-f64 (bytes "")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "abc")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "1x")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes ".")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "1e")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "1e+")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes " 1")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "1 ")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "0x10")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "nan")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes "+")) (Some v) v None -999.0))
|
|
(println "")
|
|
;; A trailing dot with no fraction is a C float literal and is accepted; a
|
|
;; leading one is too. Both are here because they are the boundary the
|
|
;; digit counter, not the position, decides.
|
|
(print (match (parse-f64 (bytes "1.")) (Some v) v None -999.0)) (print " ")
|
|
(print (match (parse-f64 (bytes ".5")) (Some v) v None -999.0))
|
|
(println "")
|
|
|
|
;; Parsing a trimmed field, which is why both exist.
|
|
(print (match (parse-f64 (trim (bytes " 2.25 "))) (Some v) v None -999.0))
|
|
(println "")
|
|
0)
|