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 * @date 2022-01-16
6 *
7 * @copyright Copyright (c) 2022-2025
8 *
9 */
10
11#ifndef INCLUDE_ARK_TYPECHECKER_HPP
12#define INCLUDE_ARK_TYPECHECKER_HPP
13
14#include <string>
15#include <vector>
16#include <ostream>
17#include <sstream>
18
19#include <Ark/Exceptions.hpp>
20#include <Ark/VM/Value.hpp>
21
22namespace Ark::types
23{
24 namespace details
25 {
26 template <typename T, typename... Ts>
27 using AllSame = std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...>>;
28
29 template <int I>
30 [[nodiscard]] bool checkN(const std::vector<Value>& args)
31 {
32 return I >= args.size();
33 }
34
35 template <int I, typename T, typename... Ts>
36 [[nodiscard]] bool checkN(const std::vector<Value>& args, T type, Ts... xs)
37 {
38 if (I >= args.size() || (type != ValueType::Any && args[I].valueType() != type))
39 return false;
40 return checkN<I + 1>(args, xs...);
41 }
42 }
43
44 /**
45 * @brief Helper to see if a builtin has been given a wanted set of types
46 *
47 * @tparam Ts Variadic argument list composed of ValueTypes
48 * @param args arguments passed to the function
49 * @param types accepted types
50 * @return true if the contract is respected
51 * @return false otherwise
52 */
53 template <typename... Ts, typename = details::AllSame<ValueType, Ts...>>
54 [[nodiscard]] bool check(const std::vector<Value>& args, Ts... types)
55 {
56 if (sizeof...(types) != args.size())
57 return false;
58 return details::checkN<0>(args, types...);
59 }
60
61 /**
62 * @brief A type definition within a contract
63 *
64 */
66 {
67 std::string name;
68 std::vector<ValueType> types;
70
71 Typedef(const std::string& type_name, const ValueType type, const bool is_variadic = false) :
72 name(type_name), types { type }, variadic(is_variadic)
73 {}
74
75 Typedef(const std::string& type_name, const std::vector<ValueType>& type_list, const bool is_variadic = false) :
76 name(type_name), types(type_list), variadic(is_variadic)
77 {}
78 };
79
80 /**
81 * @brief A contract is a list of typed arguments that a function can follow
82 *
83 */
85 {
86 std::vector<Typedef> arguments;
87 };
88
89 /**
90 * @brief Generate an error message based on a given set of types contracts provided argument list
91 *
92 * @param funcname ArkScript name of the function
93 * @param contracts types contracts the function can follow
94 * @param args provided argument list
95 * @param os output stream, default to cout
96 * @param colorize enable output colorizing
97 */
99 const std::string_view& funcname,
100 const std::vector<Contract>& contracts,
101 const std::vector<Value>& args,
102 std::ostream& os = std::cout,
103 bool colorize = true);
104
106 {
107 public:
108 TypeCheckingError(std::string&& funcname, const std::vector<Contract>& contracts, const std::vector<Value>& args) :
109 Error("TypeCheckingError"),
110 m_funcname(std::move(funcname)),
111 m_contracts(contracts),
112 m_passed_args(args)
113 {}
114
115 [[nodiscard]] std::string details(const bool colorize) const override
116 {
117 std::stringstream stream;
118 generateError(m_funcname, m_contracts, m_passed_args, stream, colorize);
119 return stream.str();
120 }
121
122 private:
123 std::string m_funcname;
124 std::vector<Contract> m_contracts;
125 std::vector<Value> m_passed_args;
126 };
127}
128
129#endif
ArkScript homemade exceptions.
#define ARK_API
Definition Module.hpp:28
Default value type handled by the virtual machine.
std::vector< Contract > m_contracts
std::string details(const bool colorize) const override
std::vector< Value > m_passed_args
TypeCheckingError(std::string &&funcname, const std::vector< Contract > &contracts, const std::vector< Value > &args)
std::enable_if_t< std::conjunction_v< std::is_same< T, Ts >... > > AllSame
bool checkN(const std::vector< Value > &args)
bool check(const std::vector< Value > &args, Ts... types)
Helper to see if a builtin has been given a wanted set of types.
ARK_API void generateError(const std::string_view &funcname, const std::vector< Contract > &contracts, const std::vector< Value > &args, std::ostream &os=std::cout, bool colorize=true)
Generate an error message based on a given set of types contracts provided argument list.
ValueType
Definition Value.hpp:32
@ 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.
Typedef(const std::string &type_name, const std::vector< ValueType > &type_list, const bool is_variadic=false)
Typedef(const std::string &type_name, const ValueType type, const bool is_variadic=false)
std::vector< ValueType > types