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
(** Copyright 2026, Dmitrii Kuznetsov *)

(** SPDX-License-Identifier: LGPL-3.0-or-later *)

open C_sharp_strange_lib.Typecheck
open C_sharp_strange_lib.Parser
open C_sharp_strange_lib.Ast
open C_sharp_strange_lib.Common

let show_wrap = function
  | Some (Program x) ->
    (match typecheck x with
     | _, Result.Ok _ -> Format.print_string "Ok!\n"
     | _, Result.Error e -> Format.printf "%a\n%!" pp_error e)
  | _ -> Format.print_string "Parsing error\n"
;;

let print_tc p str = show_wrap (parse_option p str)
let test_typecheck = print_tc parse_prog

let%expect_test "Factorial" =
  test_typecheck
    {|
    class Program {
      int Fac(int num) {
        if (num == 1) {
          return 1;
        }
        else 
        {
          return num * Fac(num - 1);
        }
      }
      public static int Main() {
        return Fac(5);
      }
    } |};
  [%expect
    {|
    Ok! |}]
;;

let%expect_test "Wrong factorial" =
  test_typecheck
    {|
    class Program {
      int Fac(int num) {
        if (num == 1) {
          return "one";
        }
      }
    } |};
  [%expect
    {|
    (TCError (OtherError "Returned type does not match the function type")) |}]
;;

let%expect_test "Already declared variable" =
  test_typecheck
    {| 
  class Program {
    int a = 5;
    int b = 9;
    int a = 9;
  } |};
  [%expect
    {|
    (TCError (OtherError "This variable is already declared")) |}]
;;

let%expect_test "Invalid value" =
  test_typecheck
    {|
    class Program {
      public static int Main() {
        int a;
        int b = a -1 + 4;
        return b;
      }
    } |};
  [%expect
    {|
  (TCError (OtherError "Variable not found: a"))|}]
;;

let%expect_test "Checking fields" =
  test_typecheck
    {| 
  class Program {
    int b = 9;
    int c = b * 67;
    int a = (50 % 2) + b - c;
    bool r = (a != b * c) || (a >= b) && (a == c +90);
    string s = "ok";
    char h = 'a';

    void M() {
      a = 5;
      r = s != "kkkk" && (190%22 == 100 * -2/5);
    }
  } |};
  [%expect
    {|
    Ok! |}]
;;

let%expect_test "String + int" =
  test_typecheck
    {| 
  class Program {
    string a = "5";
    int c = 9 + a;
  } |};
  [%expect
    {|
    (TCError TypeMismatch) |}]
;;

let%expect_test "While" =
  test_typecheck
    {| 
  class Program {
    public static int Main() {
      int count = 0;
      bool b = true;
      while(true) {
        if (count != 2) {
          count = count + 1;
          b = b && false;
        }
        else if (b == false){
          return -1;
        }
        else {
          return 0;
        }
      }
    }
  } |};
  [%expect
    {|
    Ok! |}]
;;

let%expect_test "For" =
  test_typecheck
    {| 
  class Program {
    int n = 10;
    int count = 7% 2*67;
    public static int Main() {
      for (int i = 0; i < n; i=i+1) {
        for (int j = 1;;) {
          for (;j != n; j = j + 2) {
            for (;;) {
              count = count + i + j;
            }
          }
        }
      }
      return count;
    }
  } |};
  [%expect
    {|
    Ok! |}]
;;

let%expect_test "Wrong main" =
  test_typecheck
    {| 
  class Program {
    public async void Main() {}
  }
  |};
  [%expect
    {|
    (TCError
       (OtherError "Main must be static, non-async, no params, return int/void")) |}]
;;

let%expect_test "Already declared function" =
  test_typecheck
    {| 
  class Program {
    void Test() {}
    int a = 9;
    void Test() {}
  } |};
  [%expect
    {|
    (TCError (OtherError "This variable is already declared")) |}]
;;

let%expect_test "Function type mismatch" =
  test_typecheck
    {| 
  class Program {
    public void a(int n, int m){
      return n+m;
    }
  }|};
  [%expect
    {|
    (TCError TypeMismatch) |}]
;;

let%expect_test "Factorial with writeline" =
  test_typecheck
    {|
    class Program {
      int Fac(int num) {
        if (num == 1) {
          return 1;
        }
        else 
        {
          return num * Fac(num - 1);
        }
      }
      public static int Main() {
       int result = Fac(5);
       System.Console.WriteLine(result);
        return result;
      }
    } |};
  [%expect
    {|
    Ok! |}]
;;