aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/config/ConfigChecker.java
blob: d6bb1973c5b1b4396c07d885f1a76be582055805 (plain)
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
package org.shedlang.compiler.config;

import org.shedlang.compiler.config.errors.ConfigError;
import org.shedlang.compiler.sources.Sources;
import org.shedlang.compiler.typechecker.TypeInfos;
import org.shedlang.compiler.types.Effects;
import org.shedlang.compiler.types.FunctionType;
import org.shedlang.compiler.types.Types;

import java.util.List;
import java.util.Optional;

import static org.shedlang.compiler.config.MainFunction.MAIN_FUNCTION_NAME;

public class ConfigChecker {
    private ConfigChecker() {
    }

    public static void configCheck(ProjectConfig projectConfig, TypeInfos typeInfos) {
        for (var binConfig : projectConfig.bins()) {
            configCheckBin(binConfig, typeInfos);
        }
    }

    static void configCheckBin(BinConfig binConfig, TypeInfos typeInfos) {
        // TODO: use source from config
        var namespaceTypeInfo = typeInfos.namespaceTypeInfo(binConfig.mainNamespace())
            .orElseThrow(() -> new ConfigError(
                "Namespace " + binConfig.mainNamespace() + " not found",
                Sources.NULL
            ));

        var mainType = namespaceTypeInfo.fieldType(MAIN_FUNCTION_NAME)
            .orElseThrow(() -> new ConfigError(
                "Namespace " + binConfig.mainNamespace() + " has no field " + MAIN_FUNCTION_NAME,
                Sources.NULL
            ));

        var expectedMainType = new FunctionType(
            Optional.empty(),
            List.of(),
            List.of(),
            Effects.IO,
            Types.UNIT
        );

        if (!mainType.equals(expectedMainType)) {
            throw new ConfigError(
                MAIN_FUNCTION_NAME + " function should have type " + expectedMainType.describe() +
                    ", but " + binConfig.mainNamespace() + "." + MAIN_FUNCTION_NAME +
                    " has type " + mainType.describe(),
                Sources.NULL
            );
        }
    }
}