|
| 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::Use) : Checkable |
| 23 | + condition = |
| 24 | + node.condition |
| 25 | + |
| 26 | + provider = |
| 27 | + ast.providers.find(&.name.value.==(node.provider.value)) |
| 28 | + |
| 29 | + error! :use_not_found_provider do |
| 30 | + snippet "could not find a provider:", node.provider |
| 31 | + end unless provider |
| 32 | + |
| 33 | + resolve provider |
| 34 | + |
| 35 | + lookups[node] = {provider, nil} |
| 36 | + |
| 37 | + subscription = |
| 38 | + records.find!(&.name.==(provider.subscription.value)) |
| 39 | + |
| 40 | + record = |
| 41 | + resolve node.data |
| 42 | + |
| 43 | + error! :use_subscription_mismatch do |
| 44 | + block "The subsctipion of provider not matches: " |
| 45 | + expected subscription, record |
| 46 | + snippet "The provider in question is here:", node |
| 47 | + end unless Comparer.compare(record, subscription) |
| 48 | + |
| 49 | + if condition |
| 50 | + condition_type = resolve condition |
| 51 | + |
| 52 | + error! :use_condition_mismatch do |
| 53 | + block do |
| 54 | + text "The expression of the" |
| 55 | + bold "where condition" |
| 56 | + text "must evaluate to a boolean value: " |
| 57 | + end |
| 58 | + |
| 59 | + snippet condition_type |
| 60 | + snippet "The condition in question is here:", condition |
| 61 | + end unless Comparer.compare(condition_type, BOOL) |
| 62 | + end |
| 63 | + |
| 64 | + resolve node.data |
| 65 | + end |
| 66 | + end |
| 67 | +end |
0 commit comments