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 0.4
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 bool checkN([[maybe_unused]] const std::vector<Value>& args)
29 {
30 return I >= args.size();
31 }
32
33 template <int I, typename T, typename... Ts>
34 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 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 name, const ValueType type, const bool variadic = false) :
70 name(name), types { type }, variadic(variadic)
71 {}
72
73 Typedef(const std::string_view name, const std::vector<ValueType>& types, const bool variadic = false) :
74 name(name), types(types), variadic(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]] (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
Definition: TypeChecker.hpp:25
bool checkN(const std::vector< Value > &args)
Definition: TypeChecker.hpp:28
bool check(const std::vector< Value > &args, Ts... types)
Helper to see if a builtin has been given a wanted set of types.
Definition: TypeChecker.hpp:52
ARK_API void generateError(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.
Definition: TypeChecker.cpp:80
ValueType
Definition: Value.hpp:34
@ Any
Used only for typechecking.
A contract is a list of typed arguments that a function can follow.
Definition: TypeChecker.hpp:83
std::vector< Typedef > arguments
Definition: TypeChecker.hpp:84
A type definition within a contract.
Definition: TypeChecker.hpp:64
std::string_view name
Definition: TypeChecker.hpp:65
std::vector< ValueType > types
Definition: TypeChecker.hpp:66
Typedef(const std::string_view name, const ValueType type, const bool variadic=false)
Definition: TypeChecker.hpp:69
Typedef(const std::string_view name, const std::vector< ValueType > &types, const bool variadic=false)
Definition: TypeChecker.hpp:73