aboutsummaryrefslogtreecommitdiff
path: root/test/Language.hs
blob: ea387d83623dafa57501c5ff95254c89d669f6b2 (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
module Language where

import qualified Ast as A
import Compiler
import Lexer (scan)
import Parser (parse)
import Test.HUnit
import Text.Parsec (runParser)
import Text.Parsec.Pos (newPos)

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

testCase = TestCase $ do
  assertAst "parse module" "module main" [A.Module "main" $ newPos "test" 1 1]
  assertAst
    "parse a function"
    "module main\n\
    \def fn() { }"
    [ A.Module "main" $ newPos "test" 1 1,
      A.Func "fn" [] Nothing [] False False $ newPos "test" 2 1
    ]
  assertAst
    "parse a function with parameters"
    "module main\n\
    \def fn(a: u8) { }"
    [ A.Module "main" $ newPos "test" 1 1,
      A.Func "fn" [("a", A.Type "u8", newPos "test" 2 8)] Nothing [] False False $ newPos "test" 2 1
    ]
  assertAst
    "parse a function with return value"
    "module main\n\
    \def fn(): u8 { }"
    [ A.Module "main" $ newPos "test" 1 1,
      A.Func "fn" [] (Just $ A.Type "u8") [] False False $ newPos "test" 2 1
    ]
  assertAst
    "parse a function call"
    "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.Var "fn" $ newPos "test" 3 1) [] $ newPos "test" 3 1
    ]
  assertAst
    "parse a function call with arguments"
    "module main\n\
    \def fn(a: u8) { }\n\
    \fn(10);"
    [ A.Module "main" $ newPos "test" 1 1,
      A.Func "fn" [("a", A.Type "u8", newPos "test" 2 8)] Nothing [] False False $ newPos "test" 2 1,
      A.Call (A.Var "fn" $ newPos "test" 3 1) [A.Num 10 $ newPos "test" 3 4] $ newPos "test" 3 1
    ]

language = [testCase]