2016-08-12 18:38:43 +02:00
|
|
|
#ifndef LIBCUBESCRIPT_CS_VM_HH
|
|
|
|
#define LIBCUBESCRIPT_CS_VM_HH
|
|
|
|
|
2016-09-07 22:57:28 +02:00
|
|
|
#include "cubescript/cubescript.hh"
|
2016-08-12 18:38:43 +02:00
|
|
|
|
2017-02-14 18:33:45 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <array>
|
2017-04-23 15:34:45 +02:00
|
|
|
#include <vector>
|
2021-03-17 01:26:16 +01:00
|
|
|
#include <type_traits>
|
2016-08-12 18:38:43 +02:00
|
|
|
|
2021-03-23 01:11:21 +01:00
|
|
|
#include "cs_std.hh"
|
2021-03-22 21:26:05 +01:00
|
|
|
#include "cs_bcode.hh"
|
2021-03-22 22:33:01 +01:00
|
|
|
#include "cs_ident.hh"
|
2016-09-08 23:42:14 +02:00
|
|
|
|
2016-08-12 18:38:43 +02:00
|
|
|
namespace cscript {
|
|
|
|
|
|
|
|
static constexpr int MaxArguments = 25;
|
|
|
|
static constexpr int MaxResults = 7;
|
|
|
|
|
2016-08-24 19:54:24 +02:00
|
|
|
static constexpr int DummyIdx = MaxArguments;
|
|
|
|
static constexpr int NumargsIdx = MaxArguments + 1;
|
|
|
|
static constexpr int DbgaliasIdx = MaxArguments + 2;
|
|
|
|
|
2016-08-30 23:30:40 +02:00
|
|
|
static const int cs_valtypet[] = {
|
2021-03-18 20:55:14 +01:00
|
|
|
CS_VAL_NULL, CS_VAL_INT, CS_VAL_FLOAT, CS_VAL_STRING,
|
|
|
|
CS_VAL_CODE, CS_VAL_IDENT
|
2016-08-30 23:30:40 +02:00
|
|
|
};
|
|
|
|
|
2017-02-13 18:10:40 +01:00
|
|
|
static inline int cs_vtype_to_int(cs_value_type v) {
|
2016-08-30 23:30:40 +02:00
|
|
|
return cs_valtypet[int(v)];
|
|
|
|
}
|
|
|
|
|
2016-09-14 23:24:13 +02:00
|
|
|
struct CsBreakException {
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CsContinueException {
|
|
|
|
};
|
|
|
|
|
2016-09-09 17:43:23 +02:00
|
|
|
template<typename T>
|
2017-01-25 02:09:50 +01:00
|
|
|
constexpr size_t CsTypeStorageSize =
|
2017-01-25 02:10:17 +01:00
|
|
|
(sizeof(T) - 1) / sizeof(uint32_t) + 1;
|
2016-09-09 17:43:23 +02:00
|
|
|
|
2017-02-13 18:10:40 +01:00
|
|
|
struct cs_gen_state {
|
|
|
|
cs_state &cs;
|
|
|
|
cs_gen_state *prevps;
|
2016-09-29 21:21:21 +02:00
|
|
|
bool parsing = true;
|
2021-03-19 22:49:29 +01:00
|
|
|
cs_valbuf<uint32_t> code;
|
2021-03-20 02:26:37 +01:00
|
|
|
char const *source, *send;
|
2017-01-25 02:09:50 +01:00
|
|
|
size_t current_line;
|
2021-03-20 02:26:37 +01:00
|
|
|
std::string_view src_name;
|
2016-08-12 18:38:43 +02:00
|
|
|
|
2017-02-13 18:10:40 +01:00
|
|
|
cs_gen_state() = delete;
|
|
|
|
cs_gen_state(cs_state &csr):
|
2021-03-20 02:26:37 +01:00
|
|
|
cs{csr}, prevps{csr.p_pstate}, code{cs},
|
|
|
|
source{}, send{}, current_line{1}, src_name{}
|
2016-09-29 21:21:21 +02:00
|
|
|
{
|
|
|
|
csr.p_pstate = this;
|
|
|
|
}
|
|
|
|
|
2017-02-13 18:10:40 +01:00
|
|
|
~cs_gen_state() {
|
2016-09-29 21:21:21 +02:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
void done() {
|
|
|
|
if (!parsing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cs.p_pstate = prevps;
|
|
|
|
parsing = false;
|
|
|
|
}
|
2016-08-12 18:38:43 +02:00
|
|
|
|
2021-03-20 02:26:37 +01:00
|
|
|
std::string_view get_str();
|
2021-03-19 22:25:38 +01:00
|
|
|
cs_charbuf get_str_dup();
|
2016-09-20 22:11:40 +02:00
|
|
|
|
2021-03-20 02:26:37 +01:00
|
|
|
std::string_view get_word();
|
2016-09-20 22:11:40 +02:00
|
|
|
|
2021-03-20 02:26:37 +01:00
|
|
|
void gen_str(std::string_view word) {
|
2021-03-17 20:40:32 +01:00
|
|
|
if (word.size() <= 3) {
|
2021-03-18 20:55:14 +01:00
|
|
|
uint32_t op = CS_CODE_VAL_INT | CS_RET_STRING;
|
2017-01-25 02:09:50 +01:00
|
|
|
for (size_t i = 0; i < word.size(); ++i) {
|
2017-04-04 00:28:56 +02:00
|
|
|
op |= uint32_t(
|
|
|
|
static_cast<unsigned char>(word[i])
|
|
|
|
) << ((i + 1) * 8);
|
2016-08-17 05:02:53 +02:00
|
|
|
}
|
2017-01-25 01:18:29 +01:00
|
|
|
code.push_back(op);
|
2016-08-12 18:38:43 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-03-18 20:55:14 +01:00
|
|
|
code.push_back(CS_CODE_VAL | CS_RET_STRING | (word.size() << 8));
|
2017-01-25 02:10:17 +01:00
|
|
|
auto it = reinterpret_cast<uint32_t const *>(word.data());
|
2021-03-19 22:49:29 +01:00
|
|
|
code.append(it, it + (word.size() / sizeof(uint32_t)));
|
2017-01-25 02:10:17 +01:00
|
|
|
size_t esz = word.size() % sizeof(uint32_t);
|
2016-08-12 18:38:43 +02:00
|
|
|
union {
|
2017-01-25 02:10:17 +01:00
|
|
|
char c[sizeof(uint32_t)];
|
|
|
|
uint32_t u;
|
2016-08-12 18:38:43 +02:00
|
|
|
} end;
|
|
|
|
end.u = 0;
|
|
|
|
memcpy(end.c, word.data() + word.size() - esz, esz);
|
2017-01-25 01:18:29 +01:00
|
|
|
code.push_back(end.u);
|
2016-08-12 18:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void gen_str() {
|
2021-03-18 20:55:14 +01:00
|
|
|
code.push_back(CS_CODE_VAL_INT | CS_RET_STRING);
|
2016-08-12 18:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void gen_null() {
|
2021-03-18 20:55:14 +01:00
|
|
|
code.push_back(CS_CODE_VAL_INT | CS_RET_NULL);
|
2016-08-12 18:38:43 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 18:10:40 +01:00
|
|
|
void gen_int(cs_int i = 0) {
|
2016-08-17 05:02:53 +02:00
|
|
|
if (i >= -0x800000 && i <= 0x7FFFFF) {
|
2021-03-18 20:55:14 +01:00
|
|
|
code.push_back(CS_CODE_VAL_INT | CS_RET_INT | (i << 8));
|
2016-08-17 05:02:53 +02:00
|
|
|
} else {
|
2016-09-09 17:43:23 +02:00
|
|
|
union {
|
2017-02-13 18:10:40 +01:00
|
|
|
cs_int i;
|
|
|
|
uint32_t u[CsTypeStorageSize<cs_int>];
|
2016-09-09 17:43:23 +02:00
|
|
|
} c;
|
|
|
|
c.i = i;
|
2021-03-18 20:55:14 +01:00
|
|
|
code.push_back(CS_CODE_VAL | CS_RET_INT);
|
2021-03-19 22:49:29 +01:00
|
|
|
code.append(c.u, c.u + CsTypeStorageSize<cs_int>);
|
2016-08-12 18:38:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 02:26:37 +01:00
|
|
|
void gen_int(std::string_view word);
|
2016-08-12 18:38:43 +02:00
|
|
|
|
2017-02-13 18:10:40 +01:00
|
|
|
void gen_float(cs_float f = 0.0f) {
|
|
|
|
if (cs_int(f) == f && f >= -0x800000 && f <= 0x7FFFFF) {
|
2021-03-18 20:55:14 +01:00
|
|
|
code.push_back(CS_CODE_VAL_INT | CS_RET_FLOAT | (cs_int(f) << 8));
|
2016-08-17 05:02:53 +02:00
|
|
|
} else {
|
2016-08-12 18:38:43 +02:00
|
|
|
union {
|
2017-02-13 18:10:40 +01:00
|
|
|
cs_float f;
|
|
|
|
uint32_t u[CsTypeStorageSize<cs_float>];
|
2016-08-12 18:38:43 +02:00
|
|
|
} c;
|
|
|
|
c.f = f;
|
2021-03-18 20:55:14 +01:00
|
|
|
code.push_back(CS_CODE_VAL | CS_RET_FLOAT);
|
2021-03-19 22:49:29 +01:00
|
|
|
code.append(c.u, c.u + CsTypeStorageSize<cs_float>);
|
2016-08-12 18:38:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 02:26:37 +01:00
|
|
|
void gen_float(std::string_view word);
|
2016-08-12 18:38:43 +02:00
|
|
|
|
2017-02-13 18:10:40 +01:00
|
|
|
void gen_ident(cs_ident *id) {
|
2017-01-25 01:18:29 +01:00
|
|
|
code.push_back(
|
2016-08-18 03:53:51 +02:00
|
|
|
((id->get_index() < MaxArguments)
|
2021-03-18 20:55:14 +01:00
|
|
|
? CS_CODE_IDENT_ARG
|
|
|
|
: CS_CODE_IDENT
|
2016-08-18 03:53:51 +02:00
|
|
|
) | (id->get_index() << 8)
|
2016-08-17 05:02:53 +02:00
|
|
|
);
|
2016-08-12 18:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void gen_ident() {
|
2016-09-12 20:04:59 +02:00
|
|
|
gen_ident(cs.p_state->identmap[DummyIdx]);
|
2016-08-12 18:38:43 +02:00
|
|
|
}
|
|
|
|
|
2021-03-20 02:26:37 +01:00
|
|
|
void gen_ident(std::string_view word) {
|
2016-08-12 18:38:43 +02:00
|
|
|
gen_ident(cs.new_ident(word));
|
|
|
|
}
|
|
|
|
|
2016-08-17 05:02:53 +02:00
|
|
|
void gen_value(
|
2021-03-20 02:26:37 +01:00
|
|
|
int wordtype, std::string_view word = std::string_view(),
|
2016-09-22 01:44:35 +02:00
|
|
|
int line = 0
|
2016-08-17 05:02:53 +02:00
|
|
|
);
|
2016-08-12 18:38:43 +02:00
|
|
|
|
2021-03-20 02:26:37 +01:00
|
|
|
void gen_main(std::string_view s, int ret_type = CS_VAL_ANY);
|
2016-08-12 18:38:43 +02:00
|
|
|
|
2016-09-22 01:19:29 +02:00
|
|
|
void next_char() {
|
2021-03-20 02:26:37 +01:00
|
|
|
if (source == send) {
|
2016-09-22 01:19:29 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-09-22 01:44:35 +02:00
|
|
|
if (*source == '\n') {
|
|
|
|
++current_line;
|
|
|
|
}
|
2021-03-20 02:26:37 +01:00
|
|
|
++source;
|
2016-08-12 18:38:43 +02:00
|
|
|
}
|
|
|
|
|
2017-01-25 02:09:50 +01:00
|
|
|
char current(size_t ahead = 0) {
|
2021-03-20 02:26:37 +01:00
|
|
|
if (std::size_t(send - source) <= ahead) {
|
2016-09-22 01:15:51 +02:00
|
|
|
return '\0';
|
|
|
|
}
|
2016-09-22 00:20:23 +02:00
|
|
|
return source[ahead];
|
2016-08-12 18:38:43 +02:00
|
|
|
}
|
2016-09-22 00:20:23 +02:00
|
|
|
|
2021-03-20 02:26:37 +01:00
|
|
|
std::string_view read_macro_name();
|
2016-09-22 00:20:23 +02:00
|
|
|
|
2021-03-20 02:26:37 +01:00
|
|
|
char skip_until(std::string_view chars);
|
2016-09-22 00:20:23 +02:00
|
|
|
char skip_until(char cf);
|
|
|
|
|
|
|
|
void skip_comments();
|
2016-08-12 18:38:43 +02:00
|
|
|
};
|
|
|
|
|
2021-03-21 18:45:59 +01:00
|
|
|
void bcode_ref(uint32_t *code);
|
|
|
|
void bcode_unref(uint32_t *code);
|
2016-09-02 22:42:10 +02:00
|
|
|
|
|
|
|
template<typename F>
|
2017-02-13 18:10:40 +01:00
|
|
|
static void cs_do_args(cs_state &cs, F body) {
|
2016-09-10 19:54:55 +02:00
|
|
|
if (!cs.p_callstack) {
|
|
|
|
body();
|
|
|
|
return;
|
|
|
|
}
|
2021-03-17 01:26:16 +01:00
|
|
|
cs_valarray<cs_ident_stack, MaxArguments> argstack{cs};
|
2016-09-08 21:30:08 +02:00
|
|
|
int argmask1 = cs.p_callstack->usedargs;
|
2016-09-02 22:42:10 +02:00
|
|
|
for (int i = 0; argmask1; argmask1 >>= 1, ++i) {
|
|
|
|
if (argmask1 & 1) {
|
2021-03-21 02:59:37 +01:00
|
|
|
static_cast<cs_alias_impl *>(cs.p_state->identmap[i])->undo_arg(
|
|
|
|
argstack[i]
|
2016-09-02 22:42:10 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 20:55:14 +01:00
|
|
|
cs_ident_link *prevstack = cs.p_callstack->next;
|
|
|
|
cs_ident_link aliaslink = {
|
2016-09-15 04:30:37 +02:00
|
|
|
cs.p_callstack->id, cs.p_callstack,
|
|
|
|
prevstack ? prevstack->usedargs : ((1 << MaxArguments) - 1),
|
|
|
|
prevstack ? prevstack->argstack : nullptr
|
2016-09-02 22:42:10 +02:00
|
|
|
};
|
2016-09-08 21:30:08 +02:00
|
|
|
cs.p_callstack = &aliaslink;
|
2021-03-23 01:11:21 +01:00
|
|
|
call_with_cleanup(std::move(body), [&]() {
|
2016-09-15 04:30:37 +02:00
|
|
|
if (prevstack) {
|
|
|
|
prevstack->usedargs = aliaslink.usedargs;
|
|
|
|
}
|
2016-09-08 23:42:14 +02:00
|
|
|
cs.p_callstack = aliaslink.next;
|
|
|
|
int argmask2 = cs.p_callstack->usedargs;
|
|
|
|
for (int i = 0; argmask2; argmask2 >>= 1, ++i) {
|
|
|
|
if (argmask2 & 1) {
|
2021-03-21 02:59:37 +01:00
|
|
|
static_cast<cs_alias_impl *>(cs.p_state->identmap[i])->redo_arg(
|
|
|
|
argstack[i]
|
2016-09-08 23:42:14 +02:00
|
|
|
);
|
|
|
|
}
|
2016-09-02 22:42:10 +02:00
|
|
|
}
|
2016-09-08 23:42:14 +02:00
|
|
|
});
|
2016-09-02 22:42:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-12 18:38:43 +02:00
|
|
|
} /* namespace cscript */
|
|
|
|
|
|
|
|
#endif /* LIBCUBESCRIPT_CS_VM_HH */
|