aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/test/test/luxc/lang/translation/reference.lux
blob: 1aee7df469074d0b1f5202168e8fc55a4b33db52 (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]
       (control [monad #+ do]
                pipe)
       (data ["e" error]
             [text])
       ["r" math/random]
       [meta]
       (meta [code])
       test)
  (luxc (host ["$" jvm]
              (jvm ["$i" inst]))
        (lang ["ls" synthesis]
              (translation [";T" statement]
                           [";T" eval]
                           [";T" expression]
                           [";T" case]
                           [";T" runtime]))
        ["_;" module])
  (test/luxc common))

(def: nilI $;Inst runtimeT;noneI)

(def: cursorI
  $;Inst
  (|>. ($i;int 3)
       ($i;array runtimeT;$Tuple)
       $i;DUP ($i;int 0) ($i;string "") $i;AASTORE
       $i;DUP ($i;int 1) ($i;long 0) ($i;wrap #$;Long) $i;AASTORE
       $i;DUP ($i;int 2) ($i;long 0) ($i;wrap #$;Long) $i;AASTORE))

(def: empty-metaI
  (|>. ($i;int 2)
       ($i;array runtimeT;$Tuple)
       $i;DUP ($i;int 0) cursorI $i;AASTORE
       $i;DUP ($i;int 1) nilI $i;AASTORE))

(context: "Definitions."
  (<| (times +100)
      (do @
        [module-name (|> (r;text +5) (r;filter (|>. (text;contains? "/") not)))
         def-name (r;text +5)
         def-value r;int
         #let [valueI (|>. ($i;long def-value) ($i;wrap #$;Long))]]
        ($_ seq
            (test "Can refer to definitions."
                  (|> (do meta;Monad<Meta>
                        [_ (_module;with-module +0 module-name
                             (statementT;translate-def def-name Int valueI empty-metaI (' {})))
                         sampleI (expressionT;translate (code;symbol [module-name def-name]))]
                        (evalT;eval sampleI))
                      (meta;run (init-compiler []))
                      (case> (#e;Success valueT)
                             (i.= def-value (:! Int valueT))

                             (#e;Error error)
                             false)))
            ))))

(context: "Variables."
  (<| (times +100)
      (do @
        [register (|> r;nat (:: @ map (n.% +100)))
         value r;int]
        ($_ seq
            (test "Can refer to local variables/registers."
                  (|> (do meta;Monad<Meta>
                        [sampleI (caseT;translate-let expressionT;translate
                                                      register
                                                      (code;int value)
                                                      (` ((~ (code;int (nat-to-int register))))))]
                        (evalT;eval sampleI))
                      (meta;run (init-compiler []))
                      (case> (#e;Success outputT)
                             (i.= value (:! Int outputT))

                             (#e;Error error)
                             false)))
            ))))