ArkScript
A small, fast, functional and scripting language for video games
TypeChecker.hpp
Go to the documentation of this file.
1/**
2 * @file TypeChecker.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief
5 * @version 1.1
6 * @date 2022-01-16
7 *
8 * @copyright Copyright (c) 2022-2024
9 *
10 */
11
12#ifndef INCLUDE_ARK_TYPECHECKER_HPP
13#define INCLUDE_ARK_TYPECHECKER_HPP
14
15#include <string>
16#include <vector>
17
18#include <Ark/VM/Value.hpp>
19
20namespace Ark::types
21{
22 namespace details
23 {
24 template <typename T, typename... Ts>
25 using AllSame = std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...>>;
26
27 template <int I>
28 [[nodiscard]] bool checkN(const std::vector<Value>& args)
29 {
30 return I >= args.size();
31 }
32
33 template <int I, typename T, typename... Ts>
34 [[nodiscard]] bool checkN(const std::vector<Value>& args, T type, Ts... xs)
35 {
36 if (I >= args.size() || (type != ValueType::Any && args[I].valueType() != type))
37 return false;
38 return checkN<I + 1>(args, xs...);
39 }
40 }
41
42 /**
43 * @brief Helper to see if a builtin has been given a wanted set of types
44 *
45 * @tparam Ts Variadic argument list composed of ValueTypes
46 * @param args arguments passed to the function
47 * @param types accepted types
48 * @return true if the contract is respected
49 * @return false otherwise
50 */
51 template <typename... Ts, typename = details::AllSame<ValueType, Ts...>>
52 [[nodiscard]] bool check(const std::vector<Value>& args, Ts... types)
53 {
54 if (sizeof...(types) != args.size())
55 return false;
56 return details::checkN<0>(args, types...);
57 }
58
59 /**
60 * @brief A type definition within a contract
61 *
62 */
64 {
65 std::string_view name;
66 std::vector<ValueType> types;
68
69 Typedef(const std::string_view& type_name, const ValueType type, const bool is_variadic = false) :
70 name(type_name), types { type }, variadic(is_variadic)
71 {}
72
73 Typedef(const std::string_view& type_name, const std::vector<ValueType>& type_list, const bool is_variadic = false) :
74 name(type_name), types(type_list), variadic(is_variadic)
75 {}
76 };
77
78 /**
79 * @brief A contract is a list of typed arguments that a function can follow
80 *
81 */
83 {
84 std::vector<Typedef> arguments;
85 };
86
87 /**
88 * @brief Generate an error message based on a given set of types contracts provided argument list
89 *
90 * @param funcname ArkScript name of the function
91 * @param contracts types contracts the function can follow
92 * @param args provided argument list
93 */
94 ARK_API void generateError [[noreturn]] (const std::string_view& funcname, const std::vector<Contract>& contracts, const std::vector<Value>& args);
95}
96
97#endif
#define ARK_API
Definition Module.hpp:28
std::enable_if_t< std::conjunction_v< std::is_same< T, Ts >... > > AllSame
bool checkN(const std::vector< Value > &args)
ARK_API void generateError(const std::string_view &funcname, const std::vector< Contract > &contracts, const std::vector< Value > &args)
Generate an error message based on a given set of types contracts provided argument list.
bool check(const std::vector< Value > &args, Ts... types)
Helper to see if a builtin has been given a wanted set of types.
ValueType
Definition Value.hpp:33
@ Any
Used only for typechecking.
A contract is a list of typed arguments that a function can follow.
std::vector< Typedef > arguments
A type definition within a contract.
std::string_view name
Typedef(const std::string_view &type_name, const std::vector< ValueType > &type_list, const bool is_variadic=false)
Typedef(const std::string_view &type_name, const ValueType type, const bool is_variadic=false)
std::vector< ValueType > types