Skip to content

Commit 91038fb

Browse files
committed
type_checkers: add required type checkers
1 parent d8204cc commit 91038fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+3142
-1956
lines changed

src/type_checkers/access.cr

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
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-
201
module MoonScript
212
class TypeChecker
223
def unwind_access(node : Ast::Access, stack = [] of Ast::Variable) : Array(Ast::Variable)
234
case item = node.expression
245
when Ast::Access
256
stack.unshift(item.field)
267
unwind_access(item, stack)
27-
when Ast::Variable
8+
when Ast::Variable # Value
289
stack.unshift(item)
2910
end
3011

@@ -60,6 +41,10 @@ module MoonScript
6041
end
6142
end
6243

44+
# Maybe.Just -> Type Variant
45+
# Maybe.isJust -> Entity function
46+
# Html.Event.ADD -> Entity Constant
47+
# (() { { a: "B" }}).a -> Record access
6348
def check(node : Ast::Access) : Checkable
6449
possibility =
6550
case variable = node.expression
@@ -76,6 +61,7 @@ module MoonScript
7661
variable.value
7762
end
7863

64+
# Type variant
7965
if possibility
8066
entity = scope.resolve(possibility, node).try(&.node)
8167

@@ -91,12 +77,13 @@ module MoonScript
9177
else
9278
error! :type_variant_missing do
9379
snippet "I was looking for a variant of a type:", node.field
94-
snippet "The type in question:", parent
80+
snippet "The type in question is here:", parent
9581
end unless entity
9682
end
9783
end
9884
end
9985

86+
# Constant & entities
10087
if entity
10188
if entity && possibility[0].ascii_uppercase?
10289
if target_node = scope.resolve(node.field.value, entity).try(&.node)
@@ -116,7 +103,7 @@ module MoonScript
116103
error! :access_not_record do
117104
snippet "You are trying to access a field on an entity which is not " \
118105
"a record:", target
119-
snippet "The access in question is: ", node
106+
snippet "The access in question is here:", node
120107
end unless target.is_a?(Record)
121108

122109
new_target = target.fields[node.field.value]?

src/type_checkers/argument.cr

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
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-
201
module MoonScript
212
class TypeChecker
223
def check(node : Ast::Argument) : Checkable
@@ -34,10 +15,10 @@ module MoonScript
3415

3516
error! :argument_type_mismatch do
3617
block "The type of the default value of an argument does not " \
37-
"match type"
18+
"match its type annotation."
3819

3920
expected type, default
40-
snippet "The argument is here:", node
21+
snippet "The argument in question is here:", node
4122
end unless resolved
4223

4324
type
@@ -46,4 +27,4 @@ module MoonScript
4627
end
4728
end
4829
end
49-
end
30+
end

src/type_checkers/arguments.cr

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
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-
201
module MoonScript
212
class TypeChecker
223
def check_arguments(arguments : Array(Ast::Argument))
@@ -33,7 +14,7 @@ module MoonScript
3314
block do
3415
text "The argument"
3516
bold %("#{name}")
36-
text "is declared."
17+
text "is declared after one that had a default value."
3718
end
3819

3920
block "Arguments that come after ones that have a default value must also have a default value."
@@ -51,9 +32,9 @@ module MoonScript
5132
end
5233

5334
snippet "It is declared here:", other
54-
snippet "duplication is declared here:", argument
35+
snippet "It is also declared here:", argument
5536
end if other
5637
end
5738
end
5839
end
59-
end
40+
end

src/type_checkers/array_literal.cr

Lines changed: 54 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,59 @@
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-
201
module MoonScript
21-
class TypeChecker
22-
def check(node : Ast::ArrayLiteral) : Checkable
23-
defined_type =
24-
node.type.try do |type|
25-
Type.new("Array", [resolve(type).as(Checkable)])
26-
end
27-
28-
if node.items.empty?
29-
defined_type || Type.new("Array", [Variable.new("a").as(Checkable)])
2+
class TypeChecker
3+
def check(node : Ast::ArrayLiteral) : Checkable
4+
defined_type =
5+
node.type.try do |type|
6+
Type.new("Array", [resolve(type).as(Checkable)])
7+
end
8+
9+
if node.items.empty?
10+
defined_type || Type.new("Array", [Variable.new("a").as(Checkable)])
11+
else
12+
first =
13+
resolve node.items.first
14+
15+
rest =
16+
node.items[1..node.items.size]
17+
18+
rest.each_with_index do |item, index|
19+
type = resolve item
20+
21+
return error! :array_not_matches do
22+
block do
23+
text "The"
24+
bold "#{ordinal(index + 2)} item"
25+
text "of an array does not match the type of the 1st item."
26+
end
27+
28+
snippet "I was expecting the type of the 1st item:", first
29+
snippet "Instead it is:", type
30+
snippet "The item in question is here:", item
31+
end unless Comparer.compare(type, first)
32+
end
33+
34+
inferred_type =
35+
Comparer.normalize(Type.new("Array", [first]))
36+
37+
if defined_type
38+
final_type =
39+
Comparer.compare(inferred_type, defined_type)
40+
41+
error! :array_not_matches_defined_type do
42+
block do
43+
text "The"
44+
bold "defined type"
45+
text "of an array does not match the type of its items."
46+
end
47+
48+
expected defined_type, inferred_type
49+
snippet "The array in question is here:", node.type.not_nil!
50+
end unless final_type
51+
52+
final_type
3053
else
31-
first =
32-
resolve node.items.first
33-
34-
rest =
35-
node.items[1..node.items.size]
36-
37-
rest.each_with_index do |item, index|
38-
type = resolve item
39-
40-
return error! :array_not_matches do
41-
block do
42-
text "The"
43-
bold "#{ordinal(index + 2)} item"
44-
text "of an array does not match the type of the 1st item."
45-
end
46-
47-
snippet "I was expecting the type of the 1st item:", first
48-
snippet "Instead it is:", type
49-
snippet "The item in question is here:", item
50-
end unless Comparer.compare(type, first)
51-
end
52-
53-
inferred_type =
54-
Comparer.normalize(Type.new("Array", [first]))
55-
56-
if defined_type
57-
final_type =
58-
Comparer.compare(inferred_type, defined_type)
59-
60-
error! :array_not_matches_defined_type do
61-
block do
62-
text "The"
63-
bold "defined type"
64-
text "of an array does not matches"
65-
end
66-
67-
expected defined_type, inferred_type
68-
snippet "The array in question is here:", node.type.not_nil!
69-
end unless final_type
70-
71-
final_type
72-
else
73-
inferred_type
74-
end
54+
inferred_type
7555
end
7656
end
7757
end
78-
end
58+
end
59+
end

src/type_checkers/await.cr

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,19 @@
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-
201
module MoonScript
21-
class TypeChecker
22-
def check(node : Ast::Await) : Checkable
23-
type =
24-
resolve node.body
25-
26-
if block = self.block
27-
async.add(block)
28-
end
29-
30-
case type.name
31-
when "Promise", "Deferred"
32-
type.parameters.first
33-
else
34-
type
35-
end
2+
class TypeChecker
3+
def check(node : Ast::Await) : Checkable
4+
type =
5+
resolve node.body
6+
7+
if block = self.block
8+
async.add(block)
9+
end
10+
11+
case type.name
12+
when "Promise", "Deferred"
13+
type.parameters.first
14+
else
15+
type
3616
end
3717
end
38-
end
18+
end
19+
end

0 commit comments

Comments
 (0)