Skip to content

Commit 672d00b

Browse files
committed
type_checkers: argument
1 parent 14b291d commit 672d00b

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/type_checkers/argument.cr

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -----------------------------------------------------------------------
2+
# This file is part of MoonScript
3+
#
4+
# MoonSript is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# MoonSript is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with MoonSript. If not, see <https://www.gnu.org/licenses/>.
16+
#
17+
# Copyright (C) 2025 Krisna Pranav, MoonScript Developers
18+
# -----------------------------------------------------------------------
19+
20+
module MoonScript
21+
class TypeChecker
22+
def check(node : Ast::Argument) : Checkable
23+
default =
24+
node.default.try(&->resolve(Ast::Node))
25+
26+
type =
27+
resolve_type(resolve(node.type))
28+
.tap(&.label = node.name.try(&.value))
29+
30+
case {default, type}
31+
in {Checkable, Checkable}
32+
resolved =
33+
Comparer.compare type, default
34+
35+
error! :argument_type_mismatch do
36+
block "The type of the default value of an argument does not " \
37+
"match type"
38+
39+
expected type, default
40+
snippet "The argument is here:", node
41+
end unless resolved
42+
43+
type
44+
in {Nil, Checkable}
45+
type
46+
end
47+
end
48+
end
49+
end

0 commit comments

Comments
 (0)