Skip to content

Commit 1d9fc7b

Browse files
committed
fix: handling all conversions inside the code so that it doesn't throw any warnings
1 parent 18c3a97 commit 1d9fc7b

28 files changed

+297
-254
lines changed

include/Ark/Compiler/AST/Optimizer.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* @file Optimizer.hpp
33
* @author Alexandre Plateau ([email protected])
44
* @brief Optimizes a given ArkScript AST
5-
* @version 0.4
6-
* @date 2020-10-27
5+
* @version 1.0
6+
* @date 2024-07-09
77
*
8-
* @copyright Copyright (c) 2020-2021
8+
* @copyright Copyright (c) 2020-2024
99
*
1010
*/
1111

@@ -32,8 +32,9 @@ namespace Ark::internal
3232
/**
3333
* @brief Construct a new Optimizer
3434
*
35+
* @param debug level of debug
3536
*/
36-
explicit Optimizer(uint16_t options) noexcept;
37+
explicit Optimizer(unsigned debug) noexcept;
3738

3839
/**
3940
* @brief Send the AST to the optimizer, then run the different optimization strategies on it
@@ -51,7 +52,7 @@ namespace Ark::internal
5152

5253
private:
5354
Node m_ast;
54-
uint16_t m_options;
55+
unsigned m_debug;
5556
std::unordered_map<std::string, unsigned> m_sym_appearances;
5657

5758
/**
@@ -74,7 +75,7 @@ namespace Ark::internal
7475
* @param node
7576
* @param func
7677
*/
77-
void runOnGlobalScopeVars(Node& node, const std::function<void(Node&, Node&, int)>& func);
78+
void runOnGlobalScopeVars(Node& node, const std::function<void(Node&, Node&, std::size_t)>& func);
7879

7980
/**
8081
* @brief Count the occurrences of each symbol in the AST, recursively

include/Ark/Compiler/Compiler.hpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ namespace Ark
6060
friend class Welder;
6161

6262
private:
63+
struct Page
64+
{
65+
std::size_t index;
66+
bool is_temp;
67+
};
68+
6369
// tables: symbols, values, plugins and codes
6470
std::vector<internal::Node> m_symbols;
6571
std::vector<std::string> m_defined_symbols;
@@ -86,27 +92,27 @@ namespace Ark
8692
/**
8793
* @brief helper functions to get a temp or finalized code page
8894
*
89-
* @param i page index, if negative, refers to a temporary code page
95+
* @param page page descriptor
9096
* @return std::vector<internal::Word>&
9197
*/
92-
std::vector<internal::Word>& page(const int i) noexcept
98+
std::vector<internal::Word>& page(const Page page) noexcept
9399
{
94-
if (i >= 0)
95-
return m_code_pages[i];
96-
return m_temp_pages[-i - 1];
100+
if (!page.is_temp)
101+
return m_code_pages[page.index];
102+
return m_temp_pages[page.index];
97103
}
98104

99105
/**
100106
* @brief helper functions to get a temp or finalized code page
101107
*
102-
* @param i page index, if negative, refers to a temporary code page
108+
* @param page page descriptor
103109
* @return std::vector<internal::Word>*
104110
*/
105-
std::vector<internal::Word>* page_ptr(const int i) noexcept
111+
std::vector<internal::Word>* page_ptr(const Page page) noexcept
106112
{
107-
if (i >= 0)
108-
return &m_code_pages[i];
109-
return &m_temp_pages[-i - 1];
113+
if (!page.is_temp)
114+
return &m_code_pages[page.index];
115+
return &m_temp_pages[page.index];
110116
}
111117

112118
/**
@@ -210,16 +216,16 @@ namespace Ark
210216
* @param is_terminal
211217
* @param var_name
212218
*/
213-
void compileExpression(const internal::Node& x, int p, bool is_result_unused, bool is_terminal, const std::string& var_name = "");
214-
215-
void compileSymbol(const internal::Node& x, int p, bool is_result_unused);
216-
void compileSpecific(const internal::Node& c0, const internal::Node& x, int p, bool is_result_unused);
217-
void compileIf(const internal::Node& x, int p, bool is_result_unused, bool is_terminal, const std::string& var_name);
218-
void compileFunction(const internal::Node& x, int p, bool is_result_unused, const std::string& var_name);
219-
void compileLetMutSet(internal::Keyword n, const internal::Node& x, int p);
220-
void compileWhile(const internal::Node& x, int p);
221-
void compilePluginImport(const internal::Node& x, int p);
222-
void handleCalls(const internal::Node& x, int p, bool is_result_unused, bool is_terminal, const std::string& var_name);
219+
void compileExpression(const internal::Node& x, Page p, bool is_result_unused, bool is_terminal, const std::string& var_name = "");
220+
221+
void compileSymbol(const internal::Node& x, Page p, bool is_result_unused);
222+
void compileSpecific(const internal::Node& c0, const internal::Node& x, Page p, bool is_result_unused);
223+
void compileIf(const internal::Node& x, Page p, bool is_result_unused, bool is_terminal, const std::string& var_name);
224+
void compileFunction(const internal::Node& x, Page p, bool is_result_unused, const std::string& var_name);
225+
void compileLetMutSet(internal::Keyword n, const internal::Node& x, Page p);
226+
void compileWhile(const internal::Node& x, Page p);
227+
void compilePluginImport(const internal::Node& x, Page p);
228+
void handleCalls(const internal::Node& x, Page p, bool is_result_unused, bool is_terminal, const std::string& var_name);
223229

224230
/**
225231
* @brief Register a given node in the symbol table

include/Ark/Compiler/ValTableElem.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ namespace Ark::internal
4040
ValTableElemType type;
4141

4242
// Numbers
43-
explicit ValTableElem(double value) noexcept;
44-
explicit ValTableElem(long value) noexcept;
43+
explicit ValTableElem(double num) noexcept;
44+
explicit ValTableElem(long num) noexcept;
4545
// Strings
46-
explicit ValTableElem(const std::string& value) noexcept;
46+
explicit ValTableElem(const std::string& str) noexcept;
4747
// automatic handling (Number/String/Function)
48-
explicit ValTableElem(const Node& v) noexcept;
48+
explicit ValTableElem(const Node& node) noexcept;
4949
// Functions
50-
explicit ValTableElem(std::size_t value) noexcept;
50+
explicit ValTableElem(std::size_t page) noexcept;
5151

5252
bool operator==(const ValTableElem& A) const noexcept;
5353
};

include/Ark/Files.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* @file Files.hpp
33
* @author Alexandre Plateau ([email protected])
44
* @brief Lots of utilities about the filesystem
5-
* @version 0.2
6-
* @date 2021-11-25
5+
* @version 0.3
6+
* @date 2024-07-09
77
*
88
* @copyright Copyright (c) 2021-2024
99
*
@@ -62,16 +62,16 @@ namespace Ark::Utils
6262
if (!ifs.good())
6363
return std::vector<uint8_t> {};
6464

65-
const std::size_t pos = ifs.tellg();
65+
const auto pos = ifs.tellg();
6666
// reserve appropriate number of bytes
67-
std::vector<char> temp(pos);
67+
std::vector<char> temp(static_cast<std::size_t>(pos));
6868
ifs.seekg(0, std::ios::beg);
6969
ifs.read(&temp[0], pos);
7070
ifs.close();
7171

72-
auto bytecode = std::vector<uint8_t>(pos);
72+
auto bytecode = std::vector<uint8_t>(static_cast<std::size_t>(pos));
7373
// TODO would it be faster to memcpy?
74-
for (std::size_t i = 0; i < pos; ++i)
74+
for (std::size_t i = 0; i < static_cast<std::size_t>(pos); ++i)
7575
bytecode[i] = static_cast<uint8_t>(temp[i]);
7676
return bytecode;
7777
}

include/Ark/Utils.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* @file Utils.hpp
33
* @author Alexandre Plateau ([email protected])
44
* @brief Lots of utilities about string, filesystem and more
5-
* @version 0.4
6-
* @date 2020-10-27
5+
* @version 1.0
6+
* @date 2024-07-09
77
*
88
* @copyright Copyright (c) 2020-2024
99
*
@@ -65,9 +65,9 @@ namespace Ark::Utils
6565
*
6666
* @param str1
6767
* @param str2
68-
* @return int
68+
* @return std::size_t
6969
*/
70-
int levenshteinDistance(const std::string& str1, const std::string& str2);
70+
std::size_t levenshteinDistance(const std::string& str1, const std::string& str2);
7171
}
7272

7373
#endif

include/Ark/VM/ExecutionContext.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace Ark::internal
3232
static inline unsigned Count = 0;
3333

3434
const bool primary; ///< Tells if the current ExecutionContext is the primary one or not
35-
int ip = 0; ///< Instruction pointer
35+
std::size_t ip = 0; ///< Instruction pointer
3636
std::size_t pp = 0; ///< Page pointer
3737
uint16_t sp = 0; ///< Stack pointer
3838
uint16_t fc = 0; ///< Frame count

include/Ark/VM/Plugin.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#if defined(ARK_OS_WINDOWS)
1818
// do not include winsock.h
1919
# define WIN32_LEAN_AND_MEAN
20+
# define NOMINMAX
2021
# include <Windows.h>
2122
#elif defined(ARK_OS_LINUX)
2223
# include <dlfcn.h>

include/Ark/VM/VM.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <array>
1616
#include <vector>
1717
#include <string>
18+
#include <utility>
1819
#include <cinttypes>
1920
#include <unordered_map>
2021
#include <algorithm>
@@ -298,9 +299,9 @@ namespace Ark
298299
* @brief Function called when the CALL instruction is met in the bytecode
299300
*
300301
* @param context
301-
* @param argc_ number of arguments already sent, default to -1 if it needs to search for them by itself
302+
* @param argc number of arguments already sent
302303
*/
303-
inline void call(internal::ExecutionContext& context, int16_t argc_ = -1);
304+
inline void call(internal::ExecutionContext& context, uint16_t argc);
304305
};
305306

306307
#include "inline/VM.inl"

include/Ark/VM/Value.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ namespace Ark
209209
return A.constList().empty();
210210

211211
case ValueType::Number:
212-
return !A.number();
212+
return A.number() == 0.0;
213213

214214
case ValueType::String:
215215
return A.string().empty();

include/Ark/VM/inline/VM.inl

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@ Value VM::call(const std::string& name, Args&&... args)
2020
push(*it2, context);
2121

2222
// find function object and push it if it's a pageaddr/closure
23-
const uint16_t id = static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it));
24-
Value* var = findNearestVariable(id, context);
25-
if (var != nullptr)
23+
if (const auto dist = std::distance(m_state.m_symbols.begin(), it); std::cmp_less(dist, std::numeric_limits<uint16_t>::max()))
2624
{
27-
if (!var->isFunction())
28-
throwVMError(ErrorKind::Type, "Can't call '" + name + "': it isn't a Function but a " + types_to_str[static_cast<std::size_t>(var->valueType())]);
25+
const uint16_t id = static_cast<uint16_t>(dist);
26+
Value* var = findNearestVariable(id, context);
27+
if (var != nullptr)
28+
{
29+
if (!var->isFunction())
30+
throwVMError(ErrorKind::Type, "Can't call '" + name + "': it isn't a Function but a " + types_to_str[static_cast<std::size_t>(var->valueType())]);
2931

30-
push(Value(var), context);
31-
context.last_symbol = id;
32+
push(Value(var), context);
33+
context.last_symbol = id;
34+
}
35+
else
36+
throwVMError(ErrorKind::Scope, "Couldn't find variable " + name);
3237
}
33-
else
34-
throwVMError(ErrorKind::Scope, "Couldn't find variable " + name);
3538

3639
const std::size_t frames_count = context.fc;
3740
// call it
@@ -59,7 +62,7 @@ Value VM::resolve(const Value* val, Args&&... args)
5962
if (!val->isFunction())
6063
throw TypeError("Value::resolve couldn't resolve a non-function");
6164

62-
const int ip = context.ip;
65+
const std::size_t ip = context.ip;
6366
const std::size_t pp = context.pp;
6467

6568
// convert and push arguments in reverse order
@@ -71,7 +74,7 @@ Value VM::resolve(const Value* val, Args&&... args)
7174

7275
const std::size_t frames_count = context.fc;
7376
// call it
74-
call(context, static_cast<int16_t>(sizeof...(Args)));
77+
call(context, static_cast<uint16_t>(sizeof...(Args)));
7578
// reset instruction pointer, otherwise the safeRun method will start at ip = -1
7679
// without doing context.ip++ as intended (done right after the call() in the loop, but here
7780
// we start outside this loop)
@@ -90,10 +93,10 @@ Value VM::resolve(const Value* val, Args&&... args)
9093

9194
inline Value VM::resolve(internal::ExecutionContext* context, std::vector<Value>& n)
9295
{
93-
if (!n[0].isFunction())
96+
if (!n[0].isFunction()) // TODO use fmt
9497
throw TypeError("VM::resolve couldn't resolve a non-function (" + types_to_str[static_cast<std::size_t>(n[0].valueType())] + ")");
9598

96-
const int ip = context->ip;
99+
const std::size_t ip = context->ip;
97100
const std::size_t pp = context->pp;
98101

99102
// convert and push arguments in reverse order
@@ -103,7 +106,7 @@ inline Value VM::resolve(internal::ExecutionContext* context, std::vector<Value>
103106

104107
const std::size_t frames_count = context->fc;
105108
// call it
106-
call(*context, static_cast<int16_t>(n.size() - 1));
109+
call(*context, static_cast<uint16_t>(n.size() - 1));
107110
// reset instruction pointer, otherwise the safeRun method will start at ip = -1
108111
// without doing context.ip++ as intended (done right after the call() in the loop, but here
109112
// we start outside this loop)
@@ -190,22 +193,24 @@ inline void VM::swapStackForFunCall(const uint16_t argc, internal::ExecutionCont
190193

191194
default: // 2 or more elements
192195
{
193-
const int16_t first = context.sp - argc;
196+
const auto first = static_cast<int16_t>(context.sp - argc);
194197
// move first argument to the very end
195-
context.stack[context.sp + 1] = context.stack[first + 0];
198+
context.stack[context.sp + 1] = context.stack[static_cast<std::size_t>(first + 0)];
196199
// move second argument right before the last one
197-
context.stack[context.sp + 0] = context.stack[first + 1];
200+
context.stack[context.sp + 0] = context.stack[static_cast<std::size_t>(first + 1)];
198201
// move the rest, if any
199-
int16_t x = 2;
200-
const int16_t stop = ((argc % 2 == 0) ? argc : (argc - 1)) / 2;
202+
uint16_t x = 2;
203+
const uint16_t stop = ((argc % 2 == 0) ? argc : (argc - 1)) / 2;
201204
while (x <= stop)
202205
{
203-
// destination , origin
204-
std::swap(context.stack[context.sp - x + 1], context.stack[first + x]);
206+
// destination, origin
207+
std::swap(
208+
context.stack[static_cast<std::size_t>(context.sp - x + 1)],
209+
context.stack[static_cast<std::size_t>(first + x)]);
205210
++x;
206211
}
207-
context.stack[first + 0] = Value(static_cast<PageAddr_t>(context.pp));
208-
context.stack[first + 1] = Value(ValueType::InstPtr, static_cast<PageAddr_t>(context.ip));
212+
context.stack[static_cast<std::size_t>(first + 0)] = Value(static_cast<PageAddr_t>(context.pp));
213+
context.stack[static_cast<std::size_t>(first + 1)] = Value(ValueType::InstPtr, static_cast<PageAddr_t>(context.ip));
209214
context.sp += 2;
210215
break;
211216
}
@@ -234,7 +239,7 @@ inline void VM::returnFromFuncCall(internal::ExecutionContext& context)
234239
context.locals.pop_back();
235240
}
236241

237-
inline void VM::call(internal::ExecutionContext& context, const int16_t argc_)
242+
inline void VM::call(internal::ExecutionContext& context, const uint16_t argc)
238243
{
239244
/*
240245
Argument: number of arguments when calling the function
@@ -245,18 +250,6 @@ inline void VM::call(internal::ExecutionContext& context, const int16_t argc_)
245250
*/
246251
using namespace internal;
247252

248-
uint16_t argc = 0;
249-
250-
// handling calls from C++ code
251-
if (argc_ <= -1) [[unlikely]]
252-
{
253-
++context.ip;
254-
argc = (static_cast<uint16_t>(m_state.m_pages[context.pp][context.ip]) << 8) + static_cast<uint16_t>(m_state.m_pages[context.pp][context.ip + 1]);
255-
++context.ip;
256-
}
257-
else
258-
argc = argc_;
259-
260253
Value function = *popAndResolveAsPtr(context);
261254
context.stacked_closure_scopes.emplace_back(nullptr);
262255

0 commit comments

Comments
 (0)