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
(** Copyright 2025-2026, Ram Prosad Chandra Sutra Dhar *)
(** SPDX-License-Identifier: LGPL-3.0-or-later *)
open EChandraSutraDhar_lib
open Ast
open Parser
open Printf
let parse_test input =
match parse input with
| Ok ast -> printf "%s\n" (show_program ast)
| Error fail -> printf "Ошибка: %s\n" fail
;;
let%expect_test "factorial" =
parse_test "let rec factorial n = if n < 2 then 1 else n * factorial(n - 1);;";
[%expect
{|
[(SValue (true,
((PatVariable "factorial"),
(ExpLambda ([(PatVariable "n")],
(ExpBranch (
(ExpBinOper (LowerThan, (ExpIdent "n"), (ExpConst (ConstInt 2)))),
(ExpConst (ConstInt 1)),
(Some (ExpBinOper (Multiply, (ExpIdent "n"),
(ExpFunction ((ExpIdent "factorial"),
(ExpBinOper (Minus, (ExpIdent "n"),
(ExpConst (ConstInt 1))))
))
)))
))
))),
[]))
]
|}]
;;
let%expect_test "fibonacci" =
parse_test "let rec fibo n = if n < 2 then 1 else fibo(n - 1) + fibo(n - 2) ;;";
[%expect
{|
[(SValue (true,
((PatVariable "fibo"),
(ExpLambda ([(PatVariable "n")],
(ExpBranch (
(ExpBinOper (LowerThan, (ExpIdent "n"), (ExpConst (ConstInt 2)))),
(ExpConst (ConstInt 1)),
(Some (ExpBinOper (Plus,
(ExpFunction ((ExpIdent "fibo"),
(ExpBinOper (Minus, (ExpIdent "n"),
(ExpConst (ConstInt 1))))
)),
(ExpFunction ((ExpIdent "fibo"),
(ExpBinOper (Minus, (ExpIdent "n"),
(ExpConst (ConstInt 2))))
))
)))
))
))),
[]))
]
|}]
;;
let%expect_test "lambda_test" =
parse_test "let add x = fun y -> x + y;;";
[%expect
{|
[(SValue (false,
((PatVariable "add"),
(ExpLambda ([(PatVariable "x")],
(ExpLambda ([(PatVariable "y")],
(ExpBinOper (Plus, (ExpIdent "x"), (ExpIdent "y")))))
))),
[]))
]
|}]
;;
let%expect_test "test_tuple" =
parse_test "let x = (1, 2, true) in x;;";
[%expect
{|
[(SEval
(ExpLet (false,
((PatVariable "x"),
(ExpTuple ((ExpConst (ConstInt 1)), (ExpConst (ConstInt 2)),
[(ExpConst (ConstBool true))]))),
[], (ExpIdent "x"))))
]
|}]
;;
let%expect_test "test_list" =
parse_test "let arr = [1;2;true]";
[%expect
{|
[(SValue (false,
((PatVariable "arr"),
(ExpList
[(ExpConst (ConstInt 1)); (ExpConst (ConstInt 2));
(ExpConst (ConstBool true))])),
[]))
]
|}]
;;
let%expect_test "test_sum_two_args" =
parse_test "let sum x y = x + y";
[%expect
{|
[(SValue (false,
((PatVariable "sum"),
(ExpLambda ([(PatVariable "x"); (PatVariable "y")],
(ExpBinOper (Plus, (ExpIdent "x"), (ExpIdent "y")))))),
[]))
]
|}]
;;
let%expect_test "test_annotate_type_1" =
parse_test "let sum (x:int) (y:int) = x + y";
[%expect
{|
[(SValue (false,
((PatVariable "sum"),
(ExpLambda (
[(PatType ((PatVariable "x"), (TyPrim "int")));
(PatType ((PatVariable "y"), (TyPrim "int")))],
(ExpBinOper (Plus, (ExpIdent "x"), (ExpIdent "y")))))),
[]))
]
|}]
;;
let%expect_test "test_annotate_type_2" =
parse_test "let (a : int list) = [] ";
[%expect
{|
[(SValue (false,
((PatType ((PatVariable "a"), (TyList (TyPrim "int")))), (ExpList [])),
[]))
]
|}]
;;
let%expect_test "test_minus" =
parse_test "-1 -2 - (-1) -(3)";
[%expect
{|
[(SEval
(ExpBinOper (Minus,
(ExpBinOper (Minus,
(ExpBinOper (Minus,
(ExpUnarOper (Negative, (ExpConst (ConstInt 1)))),
(ExpConst (ConstInt 2)))),
(ExpUnarOper (Negative, (ExpConst (ConstInt 1)))))),
(ExpConst (ConstInt 3)))))
]
|}]
;;
let%expect_test "test_unit" =
parse_test "let () = print_int 5";
[%expect
{|
[(SValue (false,
(PatUnit, (ExpFunction ((ExpIdent "print_int"), (ExpConst (ConstInt 5))))),
[]))
]
|}]
;;