aboutsummaryrefslogtreecommitdiff
path: root/test/Language.hs
blob: 84c72aea031ff61744a40c799354ce9e1aca5432 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
module Language where

import qualified Ast as A
import Compiler
import Control.Monad.State (evalState)
import Data.Foldable (find)
import qualified Error as E
import Lexer (scan)
import Parser (parse)
import Test.HUnit
import Text.Parsec (runParser)
import Text.Parsec.Pos (newPos)

assertAst :: String -> [A.Expr] -> Assertion
assertAst input expected = do
  r <- return $ runParser (scan parse) () "test" input
  case r of
    Left e -> assertFailure $ show e
    Right ast -> assertEqual "" expected ast

expectError :: String -> E.ErrorType -> Assertion
expectError input etyp = do
  r <- return $ runParser (scan parse) () "test" input
  case r of
    Left e -> assertFailure $ show e
    Right ast -> do
      res <- return $ evalState (compileAll ast) startState
      case res of
        Left e -> case (find (\(E.Error t _ _) -> t == etyp) e) of
          Just _ -> return ()
          Nothing -> assertFailure $ "expected " ++ show etyp ++ " didn't happen, got instead:\n" ++ unlines (map (\(E.Error t _ _) -> show t) e)
        Right _ -> assertFailure "expected error, didn't happen"

testCase1 =
  TestLabel "parse module" $
    TestCase $
      assertAst "module main" [A.Module "main" $ newPos "test" 1 1]

testCase2 =
  TestLabel "parse binary number" $
    TestCase $
      assertAst
        "module main\n0b100000;"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Num 32 $ newPos "test" 2 1
        ]

testCase3 =
  TestLabel
    "parse a function"
    $ TestCase $
      assertAst
        "module main\n\
        \def fn() { }"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Func "fn" [] Nothing [] False False $ newPos "test" 2 1
        ]

testCase4 =
  TestLabel
    "parse a function with parameters"
    $ TestCase $
      assertAst
        "module main\n\
        \def fn(a: u8) { }"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Func "fn" [("a", A.Type "u8", True, newPos "test" 2 8)] Nothing [] False False $ newPos "test" 2 1
        ]

testCase5 =
  TestLabel
    "parse a function with return value"
    $ TestCase $
      assertAst
        "module main\n\
        \def fn(): u8 {\n\
        \return 1; }"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Func "fn" [] (Just $ A.Type "u8") [A.Return (Just $ A.Num 1 $ newPos "test" 3 8) $ newPos "test" 3 1] False False $ newPos "test" 2 1
        ]

testCase6 =
  TestLabel
    "parse a function call"
    $ TestCase $
      assertAst
        "module main\n\
        \def fn() { }\n\
        \fn();"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Func "fn" [] Nothing [] False False $ newPos "test" 2 1,
          A.Call (A.Variable "fn" $ newPos "test" 3 1) [] $ newPos "test" 3 1
        ]

testCase7 =
  TestLabel
    "parse a function call with arguments"
    $ TestCase $
      assertAst
        "module main\n\
        \def fn(a: u8) { }\n\
        \fn(10);"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Func "fn" [("a", A.Type "u8", True, newPos "test" 2 8)] Nothing [] False False $ newPos "test" 2 1,
          A.Call (A.Variable "fn" $ newPos "test" 3 1) [A.Num 10 $ newPos "test" 3 4] $ newPos "test" 3 1
        ]

testCase8 =
  TestLabel
    "parse empty return on a function"
    $ TestCase $
      assertAst
        "module main\n\
        \def fn() {\n\
        \return; }"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Func "fn" [] Nothing [A.Return Nothing $ newPos "test" 3 1] False False $ newPos "test" 2 1
        ]

testCase9 =
  TestLabel
    "parse a recursive function"
    $ TestCase $
      assertAst
        "module main\n\
        \def fn() {\n\
        \fn(); }"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Func "fn" [] Nothing [A.Call (A.Variable "fn" $ newPos "test" 3 1) [] $ newPos "test" 3 1] False False $ newPos "test" 2 1
        ]

testCase10 =
  TestLabel
    "parse a function with a function parameter"
    $ TestCase $
      assertAst
        "module main\n\
        \def fn1() { }\n\
        \def fn2(f: ()) {\n\
        \f(); }\n\
        \fn2(fn1);"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Func "fn1" [] Nothing [] False False $ newPos "test" 2 1,
          A.Func
            "fn2"
            [("f", A.FuncType [] Nothing, True, newPos "test" 3 9)]
            Nothing
            [ A.Call (A.Variable "f" $ newPos "test" 4 1) [] $ newPos "test" 4 1
            ]
            False
            False
            $ newPos "test" 3 1,
          A.Call (A.Variable "fn2" $ newPos "test" 5 1) [A.Variable "fn1" $ newPos "test" 5 5] $ newPos "test" 5 1
        ]

testCase11 =
  TestLabel
    "parse a function with a function parameter (lambda)"
    $ TestCase $
      assertAst
        "module main\n\
        \def fn(f: ()) {\n\
        \f(); }\n\
        \fn(() { });"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Func
            "fn"
            [("f", A.FuncType [] Nothing, True, newPos "test" 2 8)]
            Nothing
            [A.Call (A.Variable "f" $ newPos "test" 3 1) [] $ newPos "test" 3 1]
            False
            False
            $ newPos "test" 2 1,
          A.Call (A.Variable "fn" $ newPos "test" 4 1) [A.Func "lambda@4,4" [] Nothing [] True True $ newPos "test" 4 4] $ newPos "test" 4 1
        ]

testCase12 =
  TestLabel "parse a call to lambda" $
    TestCase $
      assertAst
        "module main\n\
        \() { }();"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Call (A.Func "lambda@2,1" [] Nothing [] True True $ newPos "test" 2 1) [] $ newPos "test" 2 1
        ]

testCase13 =
  TestLabel "parse a variable declaration" $
    TestCase $
      assertAst
        "module main\n\
        \var a: u8 = 10;"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Var "a" (A.Type "u8") (A.Num 10 $ newPos "test" 2 13) False $ newPos "test" 2 5
        ]

testCase14 =
  TestLabel "parse a private variable declaration" $
    TestCase $
      assertAst
        "module main\n\
        \private var a: u8 = 10;"
        [ A.Module "main" $ newPos "test" 1 1,
          A.Var "a" (A.Type "u8") (A.Num 10 $ newPos "test" 2 21) True $ newPos "test" 2 13
        ]

-- test errors

testCaseE1 =
  TestLabel "invalid return value (empty return)" $
    TestCase $
      expectError
        "module main\n\
        \def fn(): u8 { return; }"
        E.TypeError

testCaseE2 =
  TestLabel "invalid return value" $
    TestCase $
      expectError
        "module main\n\
        \def fn(): u16 { return 1; }"
        E.TypeError

testCaseE3 =
  TestLabel "return without function" $
    TestCase $
      expectError
        "module main\n\
        \return;"
        E.UnexpectedReturn

testCaseE4 =
  TestLabel "symbol already defined" $
    TestCase $
      expectError
        "module main\n\
        \def fn() { }\n\
        \def fn() { }"
        E.AlreadyDefined

testCaseE5 =
  TestLabel "parameter already defined" $
    TestCase $
      expectError
        "module main\n\
        \def fn(a: u8, a: u8) { }\n"
        E.AlreadyDefined

testCaseE6 =
  TestLabel "call on non callable" $
    TestCase $
      expectError
        "module main\n\
        \def fn(a: u8): u8 { return a(); }\n"
        E.NonCallable

testCaseE7 =
  TestLabel "undefined variable" $
    TestCase $
      expectError
        "module main\n\
        \def fn(a: u8): u8 { return undef; }\n"
        E.Undefined

testCaseE8 =
  TestLabel "lambdas can use local variables only" $
    TestCase $
      expectError
        "module main\n\
        \def fn(a: u8): () -> u8 {\n\
        \return (): u8 { return a; };\n\
        \}\n"
        E.Undefined

testCaseE9 =
  TestLabel "undefined type in function parameters" $
    TestCase $
      expectError
        "module main\n\
        \def fn(a: undef): bool {\n\
        \return true;\n\
        \}\n"
        E.UndefinedType

testCaseE10 =
  TestLabel "undefined type in function return type" $
    TestCase $
      expectError
        "module main\n\
        \def fn(): undef {\n\
        \return true;\n\
        \}\n"
        E.UndefinedType

testCaseE11 =
  TestLabel "already defined variable" $
    TestCase $
      expectError
        "module main\n\
        \var a: u8 = 1; var a: u8 = 1;\n"
        E.AlreadyDefined

testCaseE12 =
  TestLabel "undefined type in variable declaration" $
    TestCase $
      expectError
        "module main\n\
        \var a: undef = 1;\n"
        E.UndefinedType

testCaseE13 =
  TestLabel "type mismatch in variable declaration" $
    TestCase $
      expectError
        "module main\n\
        \var a: bool = 0;\n"
        E.TypeError

testCaseE14 =
  TestLabel "type mismatch in variable declaration (lambda)" $
    TestCase $
      expectError
        "module main\n\
        \var a: bool = (a:u8): u8 { return a; };\n"
        E.TypeError

language =
  [ testCase2,
    testCase3,
    testCase4,
    testCase5,
    testCase6,
    testCase7,
    testCase8,
    testCase9,
    testCase10,
    testCase11,
    testCase12,
    testCase13,
    testCase14,
    testCaseE1,
    testCaseE2,
    testCaseE3,
    testCaseE4,
    testCaseE5,
    testCaseE6,
    testCaseE7,
    testCaseE8,
    testCaseE9,
    testCaseE10,
    testCaseE11,
    testCaseE12,
    testCaseE13,
    testCaseE14
  ]