aboutsummaryrefslogtreecommitdiff
path: root/test/Language.hs
blob: c8040358195111d43069a80d4c311a50a2106cbb (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
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 expected ast

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

testCase2 =
  TestCase $
    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
      ]

testCase3 =
  TestCase $
    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
      ]

testCase4 =
  TestCase $
    assertAst
      "parse a function with return value"
      "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
      ]

testCase5 =
  TestCase $
    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
      ]

testCase6 =
  TestCase $
    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
      ]

testCase7 =
  TestCase $
    assertAst
      "parse empty return on a function"
      "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
      ]

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

testCase9 =
  TestCase $
    assertAst
      "parse a function with a function parameter"
      "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, newPos "test" 3 9)]
          Nothing
          [ A.Call (A.Var "f" $ newPos "test" 4 1) [] $ newPos "test" 4 1
          ]
          False
          False
          $ newPos "test" 3 1,
        A.Call (A.Var "fn2" $ newPos "test" 5 1) [A.Var "fn1" $ newPos "test" 5 5] $ newPos "test" 5 1
      ]

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

testCase11 =
  TestCase $
    assertAst
      "parse a call to lambda"
      "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
      ]

language = [testCase1, testCase2, testCase3, testCase4, testCase5, testCase6, testCase7, testCase8, testCase9, testCase10, testCase11]