aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/host/js.lux
blob: 41dc0965ee7a33de964247762d4795ab9eee0c83 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(.module:
  [lux #- or and function]
  (lux (data [text]
             text/format
             (coll [list "list/" Functor<List> Fold<List>]))))

(type: #export JS Text)

(type: #export Expression JS)

(type: #export Statement JS)

(def: #export (number value)
  (-> Frac Expression)
  (%f value))

(def: #export (string value)
  (-> Text Expression)
  (%t value))

(def: #export (apply func args)
  (-> Expression (List Expression) Expression)
  (format func "(" (text.join-with "," args) ")"))

(def: #export (var! name value)
  (-> Text (Maybe Expression) Statement)
  (case value
    #.None
    (format "var " name ";")
    
    (#.Some value)
    (format "var " name " = " value ";")))

(def: #export (set! name value)
  (-> Text Expression Statement)
  (format name " = " value ";"))

(def: #export (if! test then! else!)
  (-> Expression Statement Statement Statement)
  (format "if(" test ") "
          then!
          " else "
          else!))

(def: #export (cond! clauses else!)
  (-> (List [Expression Statement]) Statement Statement)
  (list/fold (.function [[test then!] next!]
               (if! test then! next!))
             else!
             (list.reverse clauses)))

(def: #export (block! statements)
  (-> (List Statement) Statement)
  (format "{" (text.join-with "" statements) "}"))

(def: #export (while! test body)
  (-> Expression (List Statement) Statement)
  (format "while(" test ") " (block! body)))

(def: #export (throw! message)
  (-> Expression Statement)
  (format "throw Error(" message ");"))

(def: #export (return! value)
  (-> Expression Statement)
  (format "return " value ";"))

(def: #export (function name args body)
  (-> Text (List Text) (List Statement) Expression)
  (let [args (format "(" (text.join-with ", " args) ")")
        function (format "function " name args " " (block! body))]
    (format "(" function ")")))

(def: #export (? test then else)
  (-> Expression Expression Expression Expression)
  (format "(" test " ? " then " : " else ")"))

(def: #export (object fields)
  (-> (List [Text Expression]) Expression)
  (format "({"
          (|> fields
              (list/map (.function [[key val]]
                          (format key ": " val)))
              (text.join-with ", "))
          "})"))

(do-template [<name> <op>]
  [(def: #export (<name> param subject)
     (-> Expression Expression Expression)
     (format "(" subject " " <op> " " param ")"))]

  [=       "==="]
  [<       "<"]
  [<=      "<="]
  [>       ">"]
  [>=      ">="]
  [+       "+"]
  [-       "-"]
  [*       "*"]
  [/       "/"]
  [%       "%"]
  )

(do-template [<name> <op>]
  [(def: #export (<name> param subject)
     (-> Expression Expression Expression)
     (format "(" param " " <op> " " subject ")"))]

  [or      "||"]
  [and     "&&"]
  [bit-or  "|"]
  [bit-and "&"]
  )