aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/test/test/luxc/lang/translation/primitive.lux
blob: 1f5552bcee4becb8fe710432ecc01ac61801263b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(.module:
  lux
  (lux [io #+ IO]
       (control [monad #+ do]
                pipe)
       (data text/format
             ["e" error]
             [bool "bool/" Eq<Bool>]
             [text "text/" Eq<Text>])
       ["r" math/random]
       [macro]
       (macro [code])
       test)
  (luxc [lang]
        (lang [".L" host]
              ["ls" synthesis]
              (translation (jvm [".T_jvm" expression]
                                [".T_jvm" runtime]
                                [".T_jvm" eval])
                           (js [".T_js" expression]
                               [".T_js" runtime]
                               [".T_js" eval]))))
  (test/luxc common))

(def: (spec translate-expression eval translate-runtime init)
  (All [a]
    (-> (-> ls.Synthesis (Meta a))
        (-> a (Meta Top))
        (Meta Top)
        (IO Compiler)
        Test))
  (do r.Monad<Random>
    [%bool% r.bool
     %nat% r.nat
     %int% r.int
     %deg% r.deg
     %frac% r.frac
     %text% (r.text +5)]
    (`` ($_ seq
            (test "Can translate unit."
                  (|> (do macro.Monad<Meta>
                        [_ translate-runtime
                         sampleO (translate-expression (' []))]
                        (eval sampleO))
                      (lang.with-current-module "")
                      (macro.run (io.run init))
                      (case> (#e.Success valueT)
                             (is hostL.unit (:! Text valueT))

                             _
                             false)))
            (~~ (do-template [<desc> <type> <synthesis> <sample> <test>]
                  [(test (format "Can translate " <desc> ".")
                         (|> (do macro.Monad<Meta>
                               [_ translate-runtime
                                sampleO (translate-expression (<synthesis> <sample>))]
                               (eval sampleO))
                             (lang.with-current-module "")
                             (macro.run (io.run init))
                             (case> (#e.Success valueT)
                                    (<test> <sample> (:! <type> valueT))

                                    (#e.Error error)
                                    false)))]

                  ["bool" Bool code.bool %bool% bool/=]
                  ["nat"  Nat  code.nat  %nat%  n/=]
                  ["int"  Int  code.int  %int%  i/=]
                  ["deg"  Deg  code.deg  %deg%  d/=]
                  ["frac" Frac code.frac %frac% f/=]
                  ["text" Text code.text %text% text/=]))
            ))))

(context: "[JVM] Primitives."
  (<| (times +100)
      (spec expressionT_jvm.translate evalT_jvm.eval runtimeT_jvm.translate init-jvm)))

(context: "[JS] Primitives."
  (<| (times +100)
      (spec expressionT_js.translate evalT_js.eval runtimeT_js.translate init-js)))