Merge loops/haskell into architecture: Phase 17 — import decls, type annotations, typecheck 15/15
This commit is contained in:
@@ -210,6 +210,7 @@
|
||||
:op (nth node 1)
|
||||
(hk-desugar (nth node 2))
|
||||
(hk-desugar (nth node 3))))
|
||||
((= tag "type-ann") (hk-desugar (nth node 1)))
|
||||
((= tag "neg") (list :neg (hk-desugar (nth node 1))))
|
||||
((= tag "if")
|
||||
(list
|
||||
|
||||
@@ -275,38 +275,47 @@
|
||||
(list :sect-right op-name expr-e))))))
|
||||
(:else
|
||||
(let
|
||||
((first-e (hk-parse-expr-inner))
|
||||
(items (list))
|
||||
(is-tuple false))
|
||||
(append! items first-e)
|
||||
(define
|
||||
hk-tup-loop
|
||||
(fn
|
||||
()
|
||||
(when
|
||||
(hk-match? "comma" nil)
|
||||
(do
|
||||
(hk-advance!)
|
||||
(set! is-tuple true)
|
||||
(append! items (hk-parse-expr-inner))
|
||||
(hk-tup-loop)))))
|
||||
(hk-tup-loop)
|
||||
((first-e (hk-parse-expr-inner)))
|
||||
(cond
|
||||
((hk-match? "rparen" nil)
|
||||
((hk-match? "reservedop" "::")
|
||||
(do
|
||||
(hk-advance!)
|
||||
(if is-tuple (list :tuple items) first-e)))
|
||||
(let
|
||||
((ann-type (hk-parse-type)))
|
||||
(hk-expect! "rparen" nil)
|
||||
(list :type-ann first-e ann-type))))
|
||||
(:else
|
||||
(let
|
||||
((op-info2 (hk-section-op-info)))
|
||||
((items (list)) (is-tuple false))
|
||||
(append! items first-e)
|
||||
(define
|
||||
hk-tup-loop
|
||||
(fn
|
||||
()
|
||||
(when
|
||||
(hk-match? "comma" nil)
|
||||
(do
|
||||
(hk-advance!)
|
||||
(set! is-tuple true)
|
||||
(append! items (hk-parse-expr-inner))
|
||||
(hk-tup-loop)))))
|
||||
(hk-tup-loop)
|
||||
(cond
|
||||
((and (not (nil? op-info2)) (not is-tuple) (let ((after2 (hk-peek-at (get op-info2 "len")))) (and (not (nil? after2)) (= (get after2 "type") "rparen"))))
|
||||
(let
|
||||
((op-name (get op-info2 "name")))
|
||||
(hk-consume-op!)
|
||||
((hk-match? "rparen" nil)
|
||||
(do
|
||||
(hk-advance!)
|
||||
(list :sect-left op-name first-e)))
|
||||
(:else (hk-err "expected ')' after expression"))))))))))))))
|
||||
(if is-tuple (list :tuple items) first-e)))
|
||||
(:else
|
||||
(let
|
||||
((op-info2 (hk-section-op-info)))
|
||||
(cond
|
||||
((and (not (nil? op-info2)) (not is-tuple) (let ((after2 (hk-peek-at (get op-info2 "len")))) (and (not (nil? after2)) (= (get after2 "type") "rparen"))))
|
||||
(let
|
||||
((op-name (get op-info2 "name")))
|
||||
(hk-consume-op!)
|
||||
(hk-advance!)
|
||||
(list :sect-left op-name first-e)))
|
||||
(:else (hk-err "expected ')' after expression")))))))))))))))))
|
||||
(define
|
||||
hk-comp-qual-is-gen?
|
||||
(fn
|
||||
@@ -1724,10 +1733,18 @@
|
||||
(= (hk-peek-type) "eof")
|
||||
(hk-match? "vrbrace" nil)
|
||||
(hk-match? "rbrace" nil))))
|
||||
(define
|
||||
hk-body-step
|
||||
(fn
|
||||
()
|
||||
(cond
|
||||
((hk-match? "reserved" "import")
|
||||
(append! imports (hk-parse-import)))
|
||||
(:else (append! decls (hk-parse-decl))))))
|
||||
(when
|
||||
(not (hk-body-at-end?))
|
||||
(do
|
||||
(append! decls (hk-parse-decl))
|
||||
(hk-body-step)
|
||||
(define
|
||||
hk-body-loop
|
||||
(fn
|
||||
@@ -1738,7 +1755,7 @@
|
||||
(hk-advance!)
|
||||
(when
|
||||
(not (hk-body-at-end?))
|
||||
(append! decls (hk-parse-decl)))
|
||||
(hk-body-step))
|
||||
(hk-body-loop)))))
|
||||
(hk-body-loop)))
|
||||
(list imports decls))))
|
||||
|
||||
102
lib/haskell/tests/parse-extras.sx
Normal file
102
lib/haskell/tests/parse-extras.sx
Normal file
@@ -0,0 +1,102 @@
|
||||
;; Phase 17 — parser polish unit tests.
|
||||
|
||||
(hk-test
|
||||
"type-ann: literal int annotated"
|
||||
(hk-deep-force (hk-run "main = (42 :: Int)"))
|
||||
42)
|
||||
|
||||
(hk-test
|
||||
"type-ann: arithmetic annotated"
|
||||
(hk-deep-force (hk-run "main = (1 + 2 :: Int)"))
|
||||
3)
|
||||
|
||||
(hk-test
|
||||
"type-ann: function arg annotated"
|
||||
(hk-deep-force
|
||||
(hk-run "f x = x + 1\nmain = f (1 :: Int)"))
|
||||
2)
|
||||
|
||||
(hk-test
|
||||
"type-ann: string annotated"
|
||||
(hk-deep-force (hk-run "main = (\"hi\" :: String)"))
|
||||
"hi")
|
||||
|
||||
(hk-test
|
||||
"type-ann: bool annotated"
|
||||
(hk-deep-force (hk-run "main = (True :: Bool)"))
|
||||
(list "True"))
|
||||
|
||||
(hk-test
|
||||
"type-ann: tuple annotated"
|
||||
(hk-deep-force (hk-run "main = ((1, 2) :: (Int, Int))"))
|
||||
(list "Tuple" 1 2))
|
||||
|
||||
(hk-test
|
||||
"type-ann: nested annotation in arithmetic"
|
||||
(hk-deep-force (hk-run "main = (1 :: Int) + (2 :: Int)"))
|
||||
3)
|
||||
|
||||
(hk-test
|
||||
"type-ann: function-typed annotation passes through eval"
|
||||
(hk-deep-force
|
||||
(hk-run "main = let f = ((\\x -> x + 1) :: Int -> Int) in f 5"))
|
||||
6)
|
||||
|
||||
(hk-test
|
||||
"no regression: plain parens still work"
|
||||
(hk-deep-force (hk-run "main = (5)"))
|
||||
5)
|
||||
|
||||
(hk-test
|
||||
"no regression: 3-tuple still works"
|
||||
(hk-deep-force (hk-run "main = (1, 2, 3)"))
|
||||
(list "Tuple" 1 2 3))
|
||||
|
||||
(hk-test
|
||||
"no regression: section-left still works"
|
||||
(hk-deep-force (hk-run "main = (3 +) 4"))
|
||||
7)
|
||||
|
||||
(hk-test
|
||||
"no regression: section-right still works"
|
||||
(hk-deep-force (hk-run "main = (+ 3) 4"))
|
||||
7)
|
||||
|
||||
(hk-test
|
||||
"import: still works as the very first decl"
|
||||
(hk-deep-force
|
||||
(hk-run "import qualified Data.IORef as I
|
||||
main = do { r <- I.newIORef 7; I.readIORef r }"))
|
||||
(list "IO" 7))
|
||||
|
||||
(hk-test
|
||||
"import: between decls — after main"
|
||||
(hk-deep-force
|
||||
(hk-run "main = do { r <- I.newIORef 11; I.readIORef r }
|
||||
import qualified Data.IORef as I"))
|
||||
(list "IO" 11))
|
||||
|
||||
(hk-test
|
||||
"import: between two decls — uses helper after import"
|
||||
(hk-deep-force
|
||||
(hk-run "f x = x + 100
|
||||
import qualified Data.IORef as I
|
||||
main = do { r <- I.newIORef 5; I.modifyIORef r f; I.readIORef r }"))
|
||||
(list "IO" 105))
|
||||
|
||||
(hk-test
|
||||
"import: two imports in different positions"
|
||||
(hk-deep-force
|
||||
(hk-run "import qualified Data.IORef as I
|
||||
helper x = x * 2
|
||||
import qualified Data.Map as M
|
||||
main = do { r <- I.newIORef (helper 21); I.readIORef r }"))
|
||||
(list "IO" 42))
|
||||
|
||||
(hk-test
|
||||
"import: unqualified, mid-file"
|
||||
(hk-deep-force
|
||||
(hk-run "go x = x
|
||||
import Data.IORef
|
||||
main = go 9"))
|
||||
9)
|
||||
@@ -16,15 +16,18 @@
|
||||
true)))
|
||||
|
||||
;; ─── Valid programs pass through ─────────────────────────────────────────────
|
||||
(hk-test "typed ok: simple arithmetic" (hk-run-typed "main = 1 + 2") 3)
|
||||
(hk-test "typed ok: simple arithmetic"
|
||||
(hk-deep-force (hk-run-typed "main = 1 + 2")) 3)
|
||||
|
||||
(hk-test "typed ok: boolean" (hk-run-typed "main = True") (list "True"))
|
||||
(hk-test "typed ok: boolean"
|
||||
(hk-deep-force (hk-run-typed "main = True")) (list "True"))
|
||||
|
||||
(hk-test "typed ok: let binding" (hk-run-typed "main = let x = 1 in x + 2") 3)
|
||||
(hk-test "typed ok: let binding"
|
||||
(hk-deep-force (hk-run-typed "main = let x = 1 in x + 2")) 3)
|
||||
|
||||
(hk-test
|
||||
"typed ok: two independent fns"
|
||||
(hk-run-typed "f x = x + 1\nmain = f 5")
|
||||
(hk-deep-force (hk-run-typed "f x = x + 1\nmain = f 5"))
|
||||
6)
|
||||
|
||||
;; ─── Untypeable programs are rejected ────────────────────────────────────────
|
||||
@@ -76,7 +79,7 @@
|
||||
|
||||
(hk-test
|
||||
"run-typed sig ok: Int declared matches"
|
||||
(hk-run-typed "main :: Int\nmain = 1 + 2")
|
||||
(hk-deep-force (hk-run-typed "main :: Int\nmain = 1 + 2"))
|
||||
3)
|
||||
|
||||
{:fails hk-test-fails :pass hk-test-pass :fail hk-test-fail}
|
||||
Reference in New Issue
Block a user