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 ); } } }