2017-06-20 21:21:39 +02:00
|
|
|
#include <cubescript/cubescript.hh>
|
2021-03-24 02:21:27 +01:00
|
|
|
#include "cs_gen.hh"
|
2021-03-23 01:45:35 +01:00
|
|
|
#include "cs_std.hh"
|
2021-03-23 22:17:25 +01:00
|
|
|
#include "cs_parser.hh"
|
2016-08-14 17:14:10 +02:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2017-02-09 20:59:14 +01:00
|
|
|
#include <limits>
|
2021-03-20 06:18:31 +01:00
|
|
|
#include <iterator>
|
2016-08-14 17:14:10 +02:00
|
|
|
|
2021-03-23 23:32:25 +01:00
|
|
|
namespace cubescript {
|
2016-08-14 17:14:10 +02:00
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
std::string_view codegen_state::get_str() {
|
2017-01-25 02:09:50 +01:00
|
|
|
size_t nl;
|
2021-03-20 02:26:37 +01:00
|
|
|
char const *beg = source;
|
2021-03-23 23:29:32 +01:00
|
|
|
source = parse_string(
|
2021-03-26 02:29:54 +01:00
|
|
|
*ts.pstate, std::string_view{source, std::size_t(send - source)}, nl
|
2021-03-20 02:26:37 +01:00
|
|
|
);
|
2016-10-15 21:15:19 +02:00
|
|
|
current_line += nl - 1;
|
2021-03-20 02:26:37 +01:00
|
|
|
auto ret = std::string_view{beg, std::size_t(source - beg)};
|
|
|
|
return ret.substr(1, ret.size() - 2);
|
2016-09-20 22:11:40 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
charbuf codegen_state::get_str_dup() {
|
2021-04-04 05:36:19 +02:00
|
|
|
charbuf buf{ts};
|
2021-03-23 23:29:32 +01:00
|
|
|
unescape_string(std::back_inserter(buf), get_str());
|
2021-03-20 06:18:31 +01:00
|
|
|
return buf;
|
2016-09-20 22:11:40 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
std::string_view codegen_state::read_macro_name() {
|
2021-03-20 02:26:37 +01:00
|
|
|
char const *op = source;
|
2016-09-22 00:20:23 +02:00
|
|
|
char c = current();
|
|
|
|
if (!isalpha(c) && (c != '_')) {
|
2021-03-20 02:26:37 +01:00
|
|
|
return std::string_view{};
|
2016-09-22 00:20:23 +02:00
|
|
|
}
|
|
|
|
for (; isalnum(c) || (c == '_'); c = current()) {
|
|
|
|
next_char();
|
|
|
|
}
|
2021-03-20 02:26:37 +01:00
|
|
|
return std::string_view{op, std::size_t(source - op)};
|
2016-09-22 00:20:23 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
char codegen_state::skip_until(std::string_view chars) {
|
2016-09-22 00:20:23 +02:00
|
|
|
char c = current();
|
2021-03-20 02:26:37 +01:00
|
|
|
while (c && (chars.find(c) == std::string_view::npos)) {
|
2016-09-22 00:20:23 +02:00
|
|
|
next_char();
|
|
|
|
c = current();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
char codegen_state::skip_until(char cf) {
|
2016-09-22 00:20:23 +02:00
|
|
|
char c = current();
|
|
|
|
while (c && (c != cf)) {
|
|
|
|
next_char();
|
|
|
|
c = current();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
static bool is_hspace(char c) {
|
2016-09-22 00:20:23 +02:00
|
|
|
return (c == ' ') || (c == '\t') || (c == '\r');
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
void codegen_state::skip_comments() {
|
2016-08-14 17:14:10 +02:00
|
|
|
for (;;) {
|
2021-03-23 23:29:32 +01:00
|
|
|
for (char c = current(); is_hspace(c); c = current()) {
|
2016-09-22 00:20:23 +02:00
|
|
|
next_char();
|
|
|
|
}
|
2016-09-23 20:04:29 +02:00
|
|
|
if (current() == '\\') {
|
|
|
|
char c = current(1);
|
|
|
|
if ((c != '\r') && (c != '\n')) {
|
2021-04-05 04:10:39 +02:00
|
|
|
throw error{*ts.pstate, "invalid line break"};
|
2016-09-23 20:04:29 +02:00
|
|
|
}
|
|
|
|
/* skip backslash */
|
|
|
|
next_char();
|
|
|
|
/* skip CR or LF */
|
|
|
|
next_char();
|
|
|
|
/* when CR, try also skipping LF; covers \r, \n, \r\n */
|
|
|
|
if ((c == '\r') && (current(1) == '\n')) {
|
|
|
|
next_char();
|
|
|
|
}
|
|
|
|
/* skip whitespace on new line */
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-22 00:20:23 +02:00
|
|
|
if ((current() != '/') || (current(1) != '/')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (current() != '\n') {
|
|
|
|
next_char();
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
std::string_view codegen_state::get_word() {
|
2021-03-20 02:26:37 +01:00
|
|
|
char const *beg = source;
|
2021-03-23 23:29:32 +01:00
|
|
|
source = parse_word(
|
2021-03-26 02:29:54 +01:00
|
|
|
*ts.pstate, std::string_view{source, std::size_t(send - source)}
|
2021-03-20 02:26:37 +01:00
|
|
|
);
|
|
|
|
return std::string_view{beg, std::size_t(source - beg)};
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
static inline int ret_code(int type, int def = 0) {
|
|
|
|
if (type >= VAL_ANY) {
|
2021-03-28 17:07:04 +02:00
|
|
|
return def;
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
return type << BC_INST_RET;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:25:12 +02:00
|
|
|
static void compilestatements(
|
2021-03-28 01:28:31 +01:00
|
|
|
codegen_state &gs, int rettype, int brak = '\0'
|
2016-08-17 02:25:12 +02:00
|
|
|
);
|
2021-03-20 02:26:37 +01:00
|
|
|
static inline std::pair<std::string_view, size_t> compileblock(
|
2021-03-23 23:29:32 +01:00
|
|
|
codegen_state &gs, std::string_view p, size_t line,
|
|
|
|
int rettype = BC_RET_NULL, int brak = '\0'
|
2016-08-17 02:25:12 +02:00
|
|
|
);
|
2016-08-14 17:14:10 +02:00
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
void codegen_state::gen_int(std::string_view word) {
|
2021-03-23 01:45:35 +01:00
|
|
|
gen_int(parse_int(word));
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
void codegen_state::gen_float(std::string_view word) {
|
2021-03-23 01:45:35 +01:00
|
|
|
gen_float(parse_float(word));
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
void codegen_state::gen_value(int wordtype, std::string_view word, int line) {
|
2016-08-14 17:14:10 +02:00
|
|
|
switch (wordtype) {
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_ANY:
|
2016-08-17 02:25:12 +02:00
|
|
|
if (!word.empty()) {
|
|
|
|
gen_str(word);
|
|
|
|
} else {
|
|
|
|
gen_null();
|
|
|
|
}
|
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_STRING:
|
2016-08-14 17:14:10 +02:00
|
|
|
gen_str(word);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_FLOAT:
|
2016-08-17 02:25:12 +02:00
|
|
|
gen_float(word);
|
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_INT:
|
2016-08-17 02:25:12 +02:00
|
|
|
gen_int(word);
|
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_COND:
|
2016-08-17 02:25:12 +02:00
|
|
|
if (!word.empty()) {
|
2016-09-22 01:44:35 +02:00
|
|
|
compileblock(*this, word, line);
|
2016-08-17 02:25:12 +02:00
|
|
|
} else {
|
|
|
|
gen_null();
|
|
|
|
}
|
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_CODE:
|
2016-09-22 01:44:35 +02:00
|
|
|
compileblock(*this, word, line);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_IDENT:
|
2016-08-17 02:25:12 +02:00
|
|
|
gen_ident(word);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
static inline void compileblock(codegen_state &gs) {
|
|
|
|
gs.code.push_back(BC_INST_EMPTY);
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
|
2021-03-20 02:26:37 +01:00
|
|
|
static inline std::pair<std::string_view, size_t> compileblock(
|
2021-03-23 23:29:32 +01:00
|
|
|
codegen_state &gs, std::string_view p, size_t line, int rettype, int brak
|
2016-08-17 02:25:12 +02:00
|
|
|
) {
|
2017-01-25 02:09:50 +01:00
|
|
|
size_t start = gs.code.size();
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_BLOCK);
|
2021-03-31 02:21:32 +02:00
|
|
|
gs.code.push_back(BC_INST_OFFSET | std::uint32_t((start + 2) << 8));
|
2017-01-25 02:09:50 +01:00
|
|
|
size_t retline = line;
|
2021-03-20 02:26:37 +01:00
|
|
|
if (!p.empty()) {
|
|
|
|
char const *op = gs.source, *oe = gs.send;
|
2017-01-25 02:09:50 +01:00
|
|
|
size_t oldline = gs.current_line;
|
2021-03-20 02:26:37 +01:00
|
|
|
gs.source = p.data();
|
|
|
|
gs.send = p.data() + p.size();
|
2016-09-22 01:44:35 +02:00
|
|
|
gs.current_line = line;
|
2021-03-23 23:29:32 +01:00
|
|
|
compilestatements(gs, VAL_ANY, brak);
|
2021-03-20 02:26:37 +01:00
|
|
|
p = std::string_view{gs.source, std::size_t(gs.send - gs.source)};
|
2016-09-22 01:44:35 +02:00
|
|
|
retline = gs.current_line;
|
2016-08-14 17:14:10 +02:00
|
|
|
gs.source = op;
|
2021-03-20 02:26:37 +01:00
|
|
|
gs.send = oe;
|
2016-09-22 01:44:35 +02:00
|
|
|
gs.current_line = oldline;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
if (gs.code.size() > start + 2) {
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_EXIT | rettype);
|
2017-01-25 02:10:17 +01:00
|
|
|
gs.code[start] |= uint32_t(gs.code.size() - (start + 1)) << 8;
|
2016-08-14 17:14:10 +02:00
|
|
|
} else {
|
|
|
|
gs.code.resize(start);
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_EMPTY | rettype);
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2017-01-28 23:04:10 +01:00
|
|
|
return std::make_pair(p, retline);
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
static inline void compileunescapestr(codegen_state &gs) {
|
2016-09-20 22:24:56 +02:00
|
|
|
auto str = gs.get_str();
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_VAL | BC_RET_STRING);
|
2016-08-17 02:25:12 +02:00
|
|
|
gs.code.reserve(
|
2017-01-25 02:10:17 +01:00
|
|
|
gs.code.size() + str.size() / sizeof(uint32_t) + 1
|
2016-08-17 02:25:12 +02:00
|
|
|
);
|
2017-01-25 02:10:17 +01:00
|
|
|
size_t bufs = (gs.code.capacity() - gs.code.size()) * sizeof(uint32_t);
|
2021-03-26 02:29:54 +01:00
|
|
|
auto alloc = std_allocator<char>{gs.ts.istate};
|
2021-03-21 06:13:45 +01:00
|
|
|
auto *buf = alloc.allocate(bufs + 1);
|
2021-03-23 23:29:32 +01:00
|
|
|
char *wbuf = unescape_string(&buf[0], str);
|
2021-03-20 06:18:31 +01:00
|
|
|
memset(
|
|
|
|
&buf[wbuf - buf], 0,
|
|
|
|
sizeof(uint32_t) - (wbuf - buf) % sizeof(uint32_t)
|
|
|
|
);
|
|
|
|
gs.code.back() |= (wbuf - buf) << 8;
|
2017-01-25 01:18:29 +01:00
|
|
|
uint32_t *ubuf = reinterpret_cast<uint32_t *>(buf);
|
2021-03-20 06:18:31 +01:00
|
|
|
gs.code.append(ubuf, ubuf + ((wbuf - buf) / sizeof(uint32_t) + 1));
|
2021-03-21 06:13:45 +01:00
|
|
|
alloc.deallocate(buf, bufs + 1);
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:25:12 +02:00
|
|
|
static bool compilearg(
|
2021-03-28 01:28:31 +01:00
|
|
|
codegen_state &gs, int wordtype, charbuf *word = nullptr
|
2016-08-17 02:25:12 +02:00
|
|
|
);
|
2016-08-14 17:14:10 +02:00
|
|
|
|
2021-03-28 01:28:31 +01:00
|
|
|
static void compilelookup(codegen_state &gs, int ltype) {
|
2021-04-04 05:36:19 +02:00
|
|
|
charbuf lookup{gs.ts};
|
2016-08-14 17:14:10 +02:00
|
|
|
gs.next_char();
|
|
|
|
switch (gs.current()) {
|
2016-08-17 02:25:12 +02:00
|
|
|
case '(':
|
|
|
|
case '[':
|
2021-03-28 01:28:31 +01:00
|
|
|
if (!compilearg(gs, VAL_STRING)) {
|
2016-08-17 02:25:12 +02:00
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '$':
|
2021-03-28 01:28:31 +01:00
|
|
|
compilelookup(gs, VAL_STRING);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
case '\"':
|
2016-09-20 22:11:40 +02:00
|
|
|
lookup = gs.get_str_dup();
|
2021-03-19 22:25:38 +01:00
|
|
|
lookup.push_back('\0');
|
2016-08-17 02:25:12 +02:00
|
|
|
goto lookupid;
|
|
|
|
default: {
|
2021-03-19 22:25:38 +01:00
|
|
|
lookup.append(gs.get_word());
|
2016-09-20 22:11:40 +02:00
|
|
|
if (lookup.empty()) goto invalid;
|
2021-03-19 22:25:38 +01:00
|
|
|
lookup.push_back('\0');
|
2016-08-14 17:14:10 +02:00
|
|
|
lookupid:
|
2021-04-05 19:52:13 +02:00
|
|
|
ident &id = gs.ts.istate->new_ident(
|
2021-04-03 06:16:43 +02:00
|
|
|
*gs.ts.pstate, lookup.str_term(), IDENT_FLAG_UNKNOWN
|
2021-03-28 00:38:41 +01:00
|
|
|
);
|
2021-04-05 19:52:13 +02:00
|
|
|
switch (id.get_type()) {
|
|
|
|
case ident_type::IVAR:
|
|
|
|
gs.code.push_back(
|
|
|
|
BC_INST_IVAR | ret_code(ltype, BC_RET_INT) |
|
|
|
|
(id.get_index() << 8)
|
|
|
|
);
|
|
|
|
switch (ltype) {
|
|
|
|
case VAL_POP:
|
|
|
|
gs.code.pop_back();
|
|
|
|
break;
|
|
|
|
case VAL_CODE:
|
|
|
|
gs.code.push_back(BC_INST_COMPILE);
|
|
|
|
break;
|
|
|
|
case VAL_IDENT:
|
|
|
|
gs.code.push_back(BC_INST_IDENT_U);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case ident_type::FVAR:
|
|
|
|
gs.code.push_back(
|
|
|
|
BC_INST_FVAR | ret_code(ltype, BC_RET_FLOAT) |
|
|
|
|
(id.get_index() << 8)
|
|
|
|
);
|
|
|
|
switch (ltype) {
|
|
|
|
case VAL_POP:
|
|
|
|
gs.code.pop_back();
|
|
|
|
break;
|
|
|
|
case VAL_CODE:
|
|
|
|
gs.code.push_back(BC_INST_COMPILE);
|
|
|
|
break;
|
|
|
|
case VAL_IDENT:
|
|
|
|
gs.code.push_back(BC_INST_IDENT_U);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case ident_type::SVAR:
|
|
|
|
switch (ltype) {
|
|
|
|
case VAL_POP:
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
gs.code.push_back(
|
|
|
|
BC_INST_SVAR | ret_code(ltype, BC_RET_STRING) |
|
|
|
|
(id.get_index() << 8)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
case ident_type::ALIAS:
|
|
|
|
switch (ltype) {
|
|
|
|
case VAL_POP:
|
|
|
|
return;
|
|
|
|
case VAL_COND:
|
|
|
|
gs.code.push_back(
|
|
|
|
BC_INST_LOOKUP | (id.get_index() << 8)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
gs.code.push_back(
|
|
|
|
BC_INST_LOOKUP |
|
|
|
|
ret_code(ltype, BC_RET_STRING) |
|
|
|
|
(id.get_index() << 8)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
case ident_type::COMMAND: {
|
|
|
|
std::uint32_t comtype = BC_INST_COM, numargs = 0;
|
|
|
|
auto fmt = static_cast<command_impl &>(id).get_args();
|
|
|
|
for (char c: fmt) {
|
|
|
|
switch (c) {
|
|
|
|
case 's':
|
|
|
|
gs.gen_str(std::string_view{});
|
|
|
|
numargs++;
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-05 19:52:13 +02:00
|
|
|
case 'i':
|
|
|
|
gs.gen_int();
|
|
|
|
numargs++;
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-05 19:52:13 +02:00
|
|
|
case 'b':
|
|
|
|
gs.gen_int(std::numeric_limits<integer_type>::min());
|
|
|
|
numargs++;
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-05 19:52:13 +02:00
|
|
|
case 'f':
|
|
|
|
gs.gen_float();
|
|
|
|
numargs++;
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-05 19:52:13 +02:00
|
|
|
case 'F':
|
|
|
|
gs.code.push_back(BC_INST_DUP | BC_RET_FLOAT);
|
|
|
|
numargs++;
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-05 19:52:13 +02:00
|
|
|
case 'E':
|
|
|
|
case 't':
|
|
|
|
gs.gen_null();
|
|
|
|
numargs++;
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-05 19:52:13 +02:00
|
|
|
case 'e':
|
|
|
|
compileblock(gs);
|
|
|
|
numargs++;
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-05 19:52:13 +02:00
|
|
|
case 'r':
|
|
|
|
gs.gen_ident();
|
|
|
|
numargs++;
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-05 19:52:13 +02:00
|
|
|
case '$':
|
|
|
|
gs.gen_ident(id);
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
gs.gen_int(-1);
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
comtype = BC_INST_COM_C;
|
|
|
|
goto compilecomv;
|
|
|
|
case 'V':
|
|
|
|
comtype = BC_INST_COM_V;
|
|
|
|
goto compilecomv;
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2021-04-05 19:52:13 +02:00
|
|
|
gs.code.push_back(
|
|
|
|
comtype | ret_code(ltype) | (id.get_index() << 8)
|
|
|
|
);
|
|
|
|
gs.code.push_back(
|
|
|
|
BC_INST_RESULT_ARG | ret_code(ltype)
|
|
|
|
);
|
|
|
|
goto done;
|
|
|
|
compilecomv:
|
|
|
|
gs.code.push_back(
|
|
|
|
comtype | ret_code(ltype) | (id.get_index() << 8)
|
|
|
|
);
|
|
|
|
gs.code.push_back(numargs);
|
|
|
|
gs.code.push_back(
|
|
|
|
BC_INST_RESULT_ARG | ret_code(ltype)
|
|
|
|
);
|
|
|
|
goto done;
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2021-04-05 19:52:13 +02:00
|
|
|
default:
|
|
|
|
goto invalid;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2021-03-19 22:25:38 +01:00
|
|
|
gs.gen_str(lookup.str_term());
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
switch (ltype) {
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_COND:
|
2021-03-28 17:07:04 +02:00
|
|
|
gs.code.push_back(BC_INST_LOOKUP_U);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
default:
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_LOOKUP_U | ret_code(ltype));
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
done:
|
|
|
|
switch (ltype) {
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_POP:
|
|
|
|
gs.code.push_back(BC_INST_POP);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_CODE:
|
|
|
|
gs.code.push_back(BC_INST_COMPILE);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_COND:
|
|
|
|
gs.code.push_back(BC_INST_COND);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_IDENT:
|
|
|
|
gs.code.push_back(BC_INST_IDENT_U);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
invalid:
|
|
|
|
switch (ltype) {
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_POP:
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_NULL:
|
|
|
|
case VAL_ANY:
|
|
|
|
case VAL_WORD:
|
|
|
|
case VAL_COND:
|
2016-08-17 02:25:12 +02:00
|
|
|
gs.gen_null();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
gs.gen_value(ltype);
|
|
|
|
break;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
static bool compileblockstr(codegen_state &gs, char const *str, char const *send) {
|
2021-03-31 02:21:32 +02:00
|
|
|
std::size_t startc = gs.code.size();
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_VAL | BC_RET_STRING);
|
2021-03-20 02:26:37 +01:00
|
|
|
gs.code.reserve(gs.code.size() + (send - str) / sizeof(uint32_t) + 1);
|
2021-03-26 02:29:54 +01:00
|
|
|
auto alloc = std_allocator<char>{gs.ts.istate};
|
2021-03-21 06:13:45 +01:00
|
|
|
auto asz = ((send - str) / sizeof(uint32_t) + 1) * sizeof(uint32_t);
|
|
|
|
char *buf = alloc.allocate(asz);
|
2021-03-31 02:21:32 +02:00
|
|
|
std::size_t len = 0;
|
2021-03-20 23:59:38 +01:00
|
|
|
while (str < send) {
|
2021-03-20 02:26:37 +01:00
|
|
|
std::string_view chrs{"\r/\"@]"};
|
2021-03-20 23:59:38 +01:00
|
|
|
char const *orig = str;
|
|
|
|
str = std::find_first_of(str, send, chrs.begin(), chrs.end());
|
|
|
|
memcpy(&buf[len], orig, str - orig);
|
|
|
|
len += (str - orig);
|
|
|
|
if (str == send) {
|
2016-08-14 17:14:10 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2021-03-20 23:59:38 +01:00
|
|
|
switch (*str) {
|
2016-08-17 02:25:12 +02:00
|
|
|
case '\r':
|
2021-03-20 23:59:38 +01:00
|
|
|
++str;
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
case '\"': {
|
2021-03-20 23:59:38 +01:00
|
|
|
char const *start = str;
|
2021-03-23 23:29:32 +01:00
|
|
|
str = parse_string(
|
2021-03-26 02:29:54 +01:00
|
|
|
*gs.ts.pstate, std::string_view{str, send}
|
2021-03-20 02:26:37 +01:00
|
|
|
);
|
2021-03-20 23:59:38 +01:00
|
|
|
memcpy(&buf[len], start, std::size_t(str - start));
|
|
|
|
len += (str - start);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2016-08-17 02:25:12 +02:00
|
|
|
case '/':
|
2021-03-20 23:59:38 +01:00
|
|
|
if (((str + 1) != send) && str[1] == '/') {
|
|
|
|
str = std::find(str, send, '\n');
|
2016-08-17 02:25:12 +02:00
|
|
|
} else {
|
2021-03-20 23:59:38 +01:00
|
|
|
buf[len++] = *str++;
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '@':
|
|
|
|
case ']':
|
2021-03-20 23:59:38 +01:00
|
|
|
if (str < send) {
|
|
|
|
buf[len++] = *str++;
|
|
|
|
} else {
|
|
|
|
goto done;
|
|
|
|
}
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
2017-01-25 02:10:17 +01:00
|
|
|
memset(&buf[len], '\0', sizeof(uint32_t) - len % sizeof(uint32_t));
|
2017-01-25 01:18:29 +01:00
|
|
|
uint32_t *ubuf = reinterpret_cast<uint32_t *>(buf);
|
2021-03-19 22:49:29 +01:00
|
|
|
gs.code.append(ubuf, ubuf + (len / sizeof(uint32_t) + 1));
|
2016-08-14 17:14:10 +02:00
|
|
|
gs.code[startc] |= len << 8;
|
2021-03-21 06:13:45 +01:00
|
|
|
alloc.deallocate(buf, asz);
|
2016-08-14 17:14:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-28 01:28:31 +01:00
|
|
|
static bool compileblocksub(codegen_state &gs) {
|
2021-04-04 05:36:19 +02:00
|
|
|
charbuf lookup{gs.ts};
|
2016-08-14 17:14:10 +02:00
|
|
|
switch (gs.current()) {
|
2016-08-17 02:25:12 +02:00
|
|
|
case '(':
|
2021-03-28 01:28:31 +01:00
|
|
|
if (!compilearg(gs, VAL_ANY)) {
|
2016-08-17 02:25:12 +02:00
|
|
|
return false;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
case '[':
|
2021-03-28 01:28:31 +01:00
|
|
|
if (!compilearg(gs, VAL_STRING)) {
|
2016-08-17 02:25:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-28 17:07:04 +02:00
|
|
|
gs.code.push_back(BC_INST_LOOKUP_U);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
case '\"':
|
2016-09-20 22:11:40 +02:00
|
|
|
lookup = gs.get_str_dup();
|
2021-03-19 22:25:38 +01:00
|
|
|
lookup.push_back('\0');
|
2016-08-17 02:25:12 +02:00
|
|
|
goto lookupid;
|
|
|
|
default: {
|
2021-03-19 22:25:38 +01:00
|
|
|
lookup.append(gs.read_macro_name());
|
2016-09-20 22:11:40 +02:00
|
|
|
if (lookup.empty()) {
|
2016-08-17 02:25:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-19 22:25:38 +01:00
|
|
|
lookup.push_back('\0');
|
2016-08-17 02:25:12 +02:00
|
|
|
lookupid:
|
2021-04-05 19:52:13 +02:00
|
|
|
ident &id = gs.ts.istate->new_ident(
|
2021-04-03 06:16:43 +02:00
|
|
|
*gs.ts.pstate, lookup.str_term(), IDENT_FLAG_UNKNOWN
|
2021-03-28 00:38:41 +01:00
|
|
|
);
|
2021-04-05 19:52:13 +02:00
|
|
|
switch (id.get_type()) {
|
|
|
|
case ident_type::IVAR:
|
|
|
|
gs.code.push_back(BC_INST_IVAR | (id.get_index() << 8));
|
|
|
|
goto done;
|
|
|
|
case ident_type::FVAR:
|
|
|
|
gs.code.push_back(BC_INST_FVAR | (id.get_index() << 8));
|
|
|
|
goto done;
|
|
|
|
case ident_type::SVAR:
|
|
|
|
gs.code.push_back(BC_INST_SVAR | (id.get_index() << 8));
|
|
|
|
goto done;
|
|
|
|
case ident_type::ALIAS:
|
|
|
|
gs.code.push_back(
|
|
|
|
BC_INST_LOOKUP | (id.get_index() << 8)
|
|
|
|
);
|
|
|
|
goto done;
|
|
|
|
default:
|
|
|
|
break;
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2021-03-19 22:25:38 +01:00
|
|
|
gs.gen_str(lookup.str_term());
|
2021-03-28 17:07:04 +02:00
|
|
|
gs.code.push_back(BC_INST_LOOKUP_U);
|
2016-08-14 17:14:10 +02:00
|
|
|
done:
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-28 01:28:31 +01:00
|
|
|
static void compileblockmain(codegen_state &gs, int wordtype) {
|
2021-03-20 02:26:37 +01:00
|
|
|
char const *start = gs.source;
|
2017-01-25 02:09:50 +01:00
|
|
|
size_t curline = gs.current_line;
|
2016-08-14 17:14:10 +02:00
|
|
|
int concs = 0;
|
|
|
|
for (int brak = 1; brak;) {
|
2016-09-22 00:20:23 +02:00
|
|
|
switch (gs.skip_until("@\"/[]")) {
|
2016-08-17 02:25:12 +02:00
|
|
|
case '\0':
|
2021-04-05 04:10:39 +02:00
|
|
|
throw error{*gs.ts.pstate, "missing \"]\""};
|
2016-09-10 15:01:49 +02:00
|
|
|
return;
|
2016-08-17 02:25:12 +02:00
|
|
|
case '\"':
|
2016-09-20 22:24:56 +02:00
|
|
|
gs.get_str();
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
case '/':
|
2016-09-20 22:24:56 +02:00
|
|
|
gs.next_char();
|
2016-08-17 02:25:12 +02:00
|
|
|
if (gs.current() == '/') {
|
2016-09-22 00:20:23 +02:00
|
|
|
gs.skip_until('\n');
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '[':
|
2016-09-20 22:24:56 +02:00
|
|
|
gs.next_char();
|
2016-08-17 02:25:12 +02:00
|
|
|
brak++;
|
|
|
|
break;
|
|
|
|
case ']':
|
2016-09-20 22:24:56 +02:00
|
|
|
gs.next_char();
|
2016-08-17 02:25:12 +02:00
|
|
|
brak--;
|
|
|
|
break;
|
|
|
|
case '@': {
|
2021-03-20 02:26:37 +01:00
|
|
|
char const *esc = gs.source;
|
2016-09-22 00:40:33 +02:00
|
|
|
int level = 0;
|
2016-08-17 02:25:12 +02:00
|
|
|
while (gs.current() == '@') {
|
2016-09-22 00:40:33 +02:00
|
|
|
++level;
|
2016-08-17 02:25:12 +02:00
|
|
|
gs.next_char();
|
|
|
|
}
|
|
|
|
if (brak > level) {
|
|
|
|
continue;
|
|
|
|
} else if (brak < level) {
|
2021-04-05 04:10:39 +02:00
|
|
|
throw error{*gs.ts.pstate, "too many @s"};
|
2016-09-10 15:01:49 +02:00
|
|
|
return;
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2021-03-20 02:26:37 +01:00
|
|
|
if (compileblockstr(gs, start, esc)) {
|
2016-08-17 02:25:12 +02:00
|
|
|
concs++;
|
|
|
|
}
|
2021-03-28 01:28:31 +01:00
|
|
|
if (compileblocksub(gs)) {
|
2016-08-17 02:25:12 +02:00
|
|
|
concs++;
|
|
|
|
}
|
|
|
|
if (concs) {
|
2021-03-20 02:26:37 +01:00
|
|
|
start = gs.source;
|
2016-09-22 01:44:35 +02:00
|
|
|
curline = gs.current_line;
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
|
|
|
break;
|
2016-09-22 01:07:43 +02:00
|
|
|
}
|
2016-09-22 00:40:33 +02:00
|
|
|
default:
|
|
|
|
gs.next_char();
|
|
|
|
break;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-20 02:26:37 +01:00
|
|
|
if (gs.source - 1 > start) {
|
2016-08-17 02:25:12 +02:00
|
|
|
if (!concs) {
|
|
|
|
switch (wordtype) {
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_POP:
|
2016-08-17 02:25:12 +02:00
|
|
|
return;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_CODE:
|
|
|
|
case VAL_COND: {
|
2021-03-20 02:26:37 +01:00
|
|
|
auto ret = compileblock(gs, std::string_view{
|
|
|
|
start, std::size_t(gs.send - start)
|
2021-03-23 23:29:32 +01:00
|
|
|
}, curline, BC_RET_NULL, ']');
|
2021-03-20 02:26:37 +01:00
|
|
|
gs.source = ret.first.data();
|
|
|
|
gs.send = ret.first.data() + ret.first.size();
|
2016-09-22 01:44:35 +02:00
|
|
|
gs.current_line = ret.second;
|
2016-08-17 02:25:12 +02:00
|
|
|
return;
|
2016-09-22 01:44:35 +02:00
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_IDENT:
|
2021-03-20 02:26:37 +01:00
|
|
|
gs.gen_ident(std::string_view{
|
|
|
|
start, std::size_t((gs.source - 1) - start)
|
|
|
|
});
|
2016-08-17 02:25:12 +02:00
|
|
|
return;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2021-03-20 02:26:37 +01:00
|
|
|
compileblockstr(gs, start, gs.source - 1);
|
2016-08-17 02:25:12 +02:00
|
|
|
if (concs > 1) {
|
|
|
|
concs++;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (concs) {
|
2021-03-28 01:28:31 +01:00
|
|
|
gs.code.push_back(BC_INST_CONC_W | ret_code(wordtype) | (concs << 8));
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2016-08-17 02:25:12 +02:00
|
|
|
switch (wordtype) {
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_POP:
|
2021-03-20 02:26:37 +01:00
|
|
|
if (concs || gs.source - 1 > start) {
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_POP);
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_COND:
|
2021-03-20 02:26:37 +01:00
|
|
|
if (!concs && gs.source - 1 <= start) {
|
2016-08-17 02:25:12 +02:00
|
|
|
gs.gen_null();
|
|
|
|
} else {
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_COND);
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_CODE:
|
2021-03-20 02:26:37 +01:00
|
|
|
if (!concs && gs.source - 1 <= start) {
|
2016-08-17 02:25:12 +02:00
|
|
|
compileblock(gs);
|
|
|
|
} else {
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_COMPILE);
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_IDENT:
|
2021-03-20 02:26:37 +01:00
|
|
|
if (!concs && gs.source - 1 <= start) {
|
2016-08-17 02:25:12 +02:00
|
|
|
gs.gen_ident();
|
|
|
|
} else {
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_IDENT_U);
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_STRING:
|
|
|
|
case VAL_NULL:
|
|
|
|
case VAL_ANY:
|
|
|
|
case VAL_WORD:
|
2021-03-20 02:26:37 +01:00
|
|
|
if (!concs && gs.source - 1 <= start) {
|
2016-08-17 02:25:12 +02:00
|
|
|
gs.gen_str();
|
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
break;
|
2016-08-17 02:25:12 +02:00
|
|
|
default:
|
|
|
|
if (!concs) {
|
2021-03-20 02:26:37 +01:00
|
|
|
if (gs.source - 1 <= start) {
|
2016-08-17 02:25:12 +02:00
|
|
|
gs.gen_value(wordtype);
|
|
|
|
} else {
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_FORCE | (wordtype << BC_INST_RET));
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
break;
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool compilearg(
|
2021-03-28 01:28:31 +01:00
|
|
|
codegen_state &gs, int wordtype, charbuf *word
|
2016-08-17 02:25:12 +02:00
|
|
|
) {
|
2016-09-22 00:20:23 +02:00
|
|
|
gs.skip_comments();
|
2016-08-17 02:25:12 +02:00
|
|
|
switch (gs.current()) {
|
|
|
|
case '\"':
|
|
|
|
switch (wordtype) {
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_POP:
|
2016-09-20 22:24:56 +02:00
|
|
|
gs.get_str();
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_COND: {
|
2017-01-25 02:09:50 +01:00
|
|
|
size_t line = gs.current_line;
|
2016-09-20 22:11:40 +02:00
|
|
|
auto s = gs.get_str_dup();
|
|
|
|
if (!s.empty()) {
|
2021-03-19 22:25:38 +01:00
|
|
|
s.push_back('\0');
|
|
|
|
compileblock(gs, s.str_term(), line);
|
2016-08-17 02:25:12 +02:00
|
|
|
} else {
|
|
|
|
gs.gen_null();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_CODE: {
|
2016-09-20 22:11:40 +02:00
|
|
|
auto s = gs.get_str_dup();
|
2021-03-19 22:25:38 +01:00
|
|
|
s.push_back('\0');
|
|
|
|
compileblock(gs, s.str_term(), gs.current_line);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_WORD:
|
2016-09-20 22:11:40 +02:00
|
|
|
if (word) {
|
2021-03-31 01:10:58 +02:00
|
|
|
*word = gs.get_str_dup();
|
2016-09-20 22:11:40 +02:00
|
|
|
}
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_ANY:
|
|
|
|
case VAL_STRING:
|
2016-08-17 02:25:12 +02:00
|
|
|
compileunescapestr(gs);
|
|
|
|
break;
|
|
|
|
default: {
|
2021-03-31 02:21:32 +02:00
|
|
|
int line = int(gs.current_line);
|
2016-09-20 22:11:40 +02:00
|
|
|
auto s = gs.get_str_dup();
|
2021-03-19 22:25:38 +01:00
|
|
|
s.push_back('\0');
|
|
|
|
gs.gen_value(wordtype, s.str_term(), line);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
return true;
|
2016-08-17 02:25:12 +02:00
|
|
|
case '$':
|
2021-03-28 01:28:31 +01:00
|
|
|
compilelookup(gs, wordtype);
|
2016-08-14 17:14:10 +02:00
|
|
|
return true;
|
2021-03-28 01:28:31 +01:00
|
|
|
case '(': {
|
2016-08-17 02:25:12 +02:00
|
|
|
gs.next_char();
|
2021-03-28 01:28:31 +01:00
|
|
|
std::size_t start = gs.code.size();
|
|
|
|
compilestatements(gs, VAL_ANY, ')');
|
|
|
|
if (gs.code.size() > start) {
|
|
|
|
gs.code.push_back(BC_INST_RESULT_ARG | ret_code(wordtype));
|
2016-08-17 02:25:12 +02:00
|
|
|
} else {
|
2021-03-28 01:28:31 +01:00
|
|
|
gs.gen_value(wordtype);
|
|
|
|
return true;
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
|
|
|
switch (wordtype) {
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_POP:
|
|
|
|
gs.code.push_back(BC_INST_POP);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_COND:
|
|
|
|
gs.code.push_back(BC_INST_COND);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_CODE:
|
|
|
|
gs.code.push_back(BC_INST_COMPILE);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_IDENT:
|
|
|
|
gs.code.push_back(BC_INST_IDENT_U);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
return true;
|
2021-03-28 01:28:31 +01:00
|
|
|
}
|
2016-08-17 02:25:12 +02:00
|
|
|
case '[':
|
|
|
|
gs.next_char();
|
2021-03-28 01:28:31 +01:00
|
|
|
compileblockmain(gs, wordtype);
|
2016-08-17 02:25:12 +02:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
switch (wordtype) {
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_POP: {
|
2016-09-20 22:24:56 +02:00
|
|
|
return !gs.get_word().empty();
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_COND: {
|
2017-01-25 02:09:50 +01:00
|
|
|
size_t line = gs.current_line;
|
2016-09-20 22:11:40 +02:00
|
|
|
auto s = gs.get_word();
|
|
|
|
if (s.empty()) {
|
2016-08-17 02:25:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-22 01:44:35 +02:00
|
|
|
compileblock(gs, s, line);
|
2016-08-17 02:25:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_CODE: {
|
2017-01-25 02:09:50 +01:00
|
|
|
size_t line = gs.current_line;
|
2016-09-20 22:11:40 +02:00
|
|
|
auto s = gs.get_word();
|
|
|
|
if (s.empty()) {
|
2016-08-17 02:25:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-22 01:44:35 +02:00
|
|
|
compileblock(gs, s, line);
|
2016-08-17 02:25:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_WORD: {
|
2016-09-20 22:11:40 +02:00
|
|
|
auto w = gs.get_word();
|
|
|
|
if (word) {
|
2021-03-19 22:25:38 +01:00
|
|
|
word->clear();
|
|
|
|
word->append(w);
|
2016-09-20 22:11:40 +02:00
|
|
|
}
|
|
|
|
return !w.empty();
|
|
|
|
}
|
2016-08-17 02:25:12 +02:00
|
|
|
default: {
|
2021-03-31 02:21:32 +02:00
|
|
|
int line = int(gs.current_line);
|
2016-09-20 22:11:40 +02:00
|
|
|
auto s = gs.get_word();
|
|
|
|
if (s.empty()) {
|
2016-08-17 02:25:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-22 01:44:35 +02:00
|
|
|
gs.gen_value(wordtype, s, line);
|
2016-08-17 02:25:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-17 22:32:32 +02:00
|
|
|
static void compile_cmd(
|
2021-04-05 19:52:13 +02:00
|
|
|
codegen_state &gs, command_impl *id, ident &self, bool &more, int rettype,
|
2021-04-03 03:14:52 +02:00
|
|
|
std::uint32_t limit = 0
|
2016-09-17 22:32:32 +02:00
|
|
|
) {
|
2021-04-03 03:14:52 +02:00
|
|
|
std::uint32_t comtype = BC_INST_COM, numargs = 0, numcargs = 0, fakeargs = 0;
|
2016-09-17 22:32:32 +02:00
|
|
|
bool rep = false;
|
|
|
|
auto fmt = id->get_args();
|
2021-03-20 02:26:37 +01:00
|
|
|
for (auto it = fmt.begin(); it != fmt.end(); ++it) {
|
|
|
|
switch (*it) {
|
2021-03-17 20:40:32 +01:00
|
|
|
case 's': /* string */
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_STRING);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
2021-04-03 03:14:52 +02:00
|
|
|
if (!more || (limit && (numcargs >= limit))) {
|
2016-09-17 22:32:32 +02:00
|
|
|
if (rep) {
|
|
|
|
break;
|
|
|
|
}
|
2021-03-20 02:26:37 +01:00
|
|
|
gs.gen_str(std::string_view{});
|
2016-09-17 22:32:32 +02:00
|
|
|
fakeargs++;
|
2021-03-20 02:26:37 +01:00
|
|
|
} else if ((it + 1) == fmt.end()) {
|
2016-09-17 22:32:32 +02:00
|
|
|
int numconc = 1;
|
2021-03-31 02:21:32 +02:00
|
|
|
for (;;) {
|
|
|
|
more = compilearg(gs, VAL_STRING);
|
|
|
|
if (!more) {
|
|
|
|
break;
|
|
|
|
}
|
2016-09-17 22:32:32 +02:00
|
|
|
numconc++;
|
|
|
|
}
|
|
|
|
if (numconc > 1) {
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_CONC | BC_RET_STRING | (numconc << 8));
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
numargs++;
|
2021-04-03 03:14:52 +02:00
|
|
|
numcargs++;
|
2016-09-17 22:32:32 +02:00
|
|
|
break;
|
2021-03-17 20:40:32 +01:00
|
|
|
case 'i': /* integer */
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_INT);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
2021-04-03 03:14:52 +02:00
|
|
|
if (!more || (limit && (numcargs >= limit))) {
|
2016-09-17 22:32:32 +02:00
|
|
|
if (rep) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gs.gen_int();
|
|
|
|
fakeargs++;
|
|
|
|
}
|
|
|
|
numargs++;
|
2021-04-03 03:14:52 +02:00
|
|
|
numcargs++;
|
2016-09-17 22:32:32 +02:00
|
|
|
break;
|
2021-03-17 20:40:32 +01:00
|
|
|
case 'b': /* integer, INT_MIN default */
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_INT);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
2021-04-03 03:14:52 +02:00
|
|
|
if (!more || (limit && (numcargs >= limit))) {
|
2016-09-17 22:32:32 +02:00
|
|
|
if (rep) {
|
|
|
|
break;
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.gen_int(std::numeric_limits<integer_type>::min());
|
2016-09-17 22:32:32 +02:00
|
|
|
fakeargs++;
|
|
|
|
}
|
|
|
|
numargs++;
|
2021-04-03 03:14:52 +02:00
|
|
|
numcargs++;
|
2016-09-17 22:32:32 +02:00
|
|
|
break;
|
2021-03-17 20:40:32 +01:00
|
|
|
case 'f': /* float */
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_FLOAT);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
2021-04-03 03:14:52 +02:00
|
|
|
if (!more || (limit && (numcargs >= limit))) {
|
2016-09-17 22:32:32 +02:00
|
|
|
if (rep) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gs.gen_float();
|
|
|
|
fakeargs++;
|
|
|
|
}
|
|
|
|
numargs++;
|
2021-04-03 03:14:52 +02:00
|
|
|
numcargs++;
|
2016-09-17 22:32:32 +02:00
|
|
|
break;
|
2021-03-17 20:40:32 +01:00
|
|
|
case 'F': /* float, prev-argument default */
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_FLOAT);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
2021-04-03 03:14:52 +02:00
|
|
|
if (!more || (limit && (numcargs >= limit))) {
|
2016-09-17 22:32:32 +02:00
|
|
|
if (rep) {
|
|
|
|
break;
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_DUP | BC_RET_FLOAT);
|
2016-09-17 22:32:32 +02:00
|
|
|
fakeargs++;
|
|
|
|
}
|
|
|
|
numargs++;
|
2021-04-03 03:14:52 +02:00
|
|
|
numcargs++;
|
2016-09-17 22:32:32 +02:00
|
|
|
break;
|
2021-03-17 20:40:32 +01:00
|
|
|
case 't': /* any arg */
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_ANY);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
2021-04-03 03:14:52 +02:00
|
|
|
if (!more || (limit && (numcargs >= limit))) {
|
2016-09-17 22:32:32 +02:00
|
|
|
if (rep) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gs.gen_null();
|
|
|
|
fakeargs++;
|
|
|
|
}
|
|
|
|
numargs++;
|
2021-04-03 03:14:52 +02:00
|
|
|
numcargs++;
|
2016-09-17 22:32:32 +02:00
|
|
|
break;
|
2021-03-17 20:40:32 +01:00
|
|
|
case 'E': /* condition */
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_COND);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
2021-04-03 03:14:52 +02:00
|
|
|
if (!more || (limit && (numcargs >= limit))) {
|
2016-09-17 22:32:32 +02:00
|
|
|
if (rep) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gs.gen_null();
|
|
|
|
fakeargs++;
|
|
|
|
}
|
|
|
|
numargs++;
|
2021-04-03 03:14:52 +02:00
|
|
|
numcargs++;
|
2016-09-17 22:32:32 +02:00
|
|
|
break;
|
2021-03-17 20:40:32 +01:00
|
|
|
case 'e': /* code */
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_CODE);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
2021-04-03 03:14:52 +02:00
|
|
|
if (!more || (limit && (numcargs >= limit))) {
|
2016-09-17 22:32:32 +02:00
|
|
|
if (rep) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
compileblock(gs);
|
|
|
|
fakeargs++;
|
|
|
|
}
|
|
|
|
numargs++;
|
2021-04-03 03:14:52 +02:00
|
|
|
numcargs++;
|
2016-09-17 22:32:32 +02:00
|
|
|
break;
|
2021-03-17 20:40:32 +01:00
|
|
|
case 'r': /* ident */
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_IDENT);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
2021-04-03 03:14:52 +02:00
|
|
|
if (!more || (limit && (numcargs >= limit))) {
|
2016-09-17 22:32:32 +02:00
|
|
|
if (rep) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gs.gen_ident();
|
|
|
|
fakeargs++;
|
|
|
|
}
|
|
|
|
numargs++;
|
2021-04-03 03:14:52 +02:00
|
|
|
numcargs++;
|
2016-09-17 22:32:32 +02:00
|
|
|
break;
|
2021-03-17 20:40:32 +01:00
|
|
|
case '$': /* self */
|
2021-04-03 03:14:52 +02:00
|
|
|
gs.gen_ident(self);
|
2016-09-17 22:32:32 +02:00
|
|
|
numargs++;
|
|
|
|
break;
|
2021-03-17 20:40:32 +01:00
|
|
|
case 'N': /* number of arguments */
|
2016-09-17 22:32:32 +02:00
|
|
|
gs.gen_int(numargs - fakeargs);
|
|
|
|
numargs++;
|
|
|
|
break;
|
2021-03-17 20:40:32 +01:00
|
|
|
case 'C': /* concatenated string */
|
2021-03-23 23:29:32 +01:00
|
|
|
comtype = BC_INST_COM_C;
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-31 02:21:32 +02:00
|
|
|
for (;;) {
|
|
|
|
more = compilearg(gs, VAL_ANY);
|
2021-04-03 03:14:52 +02:00
|
|
|
if (!more || (limit && (numcargs >= limit))) {
|
2021-03-31 02:21:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-09-17 22:32:32 +02:00
|
|
|
numargs++;
|
2021-04-03 03:14:52 +02:00
|
|
|
numcargs++;
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
goto compilecomv;
|
2021-03-17 20:40:32 +01:00
|
|
|
case 'V': /* varargs */
|
2021-03-23 23:29:32 +01:00
|
|
|
comtype = BC_INST_COM_V;
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-31 02:21:32 +02:00
|
|
|
for(;;) {
|
|
|
|
more = compilearg(gs, VAL_ANY);
|
2021-04-03 03:14:52 +02:00
|
|
|
if (!more || (limit && (numcargs >= limit))) {
|
2021-03-31 02:21:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-09-17 22:32:32 +02:00
|
|
|
numargs++;
|
2021-04-03 03:14:52 +02:00
|
|
|
numcargs++;
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
goto compilecomv;
|
2021-03-17 20:40:32 +01:00
|
|
|
case '1': /* vararg repetition */
|
2016-09-17 22:32:32 +02:00
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
2021-04-03 03:14:52 +02:00
|
|
|
if (more && (!limit || (numcargs < limit))) {
|
2021-03-20 02:26:37 +01:00
|
|
|
int numrep = *it - '0' + 1;
|
|
|
|
it -= numrep;
|
2016-09-17 22:32:32 +02:00
|
|
|
rep = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(comtype | ret_code(rettype) | (id->get_index() << 8));
|
2016-09-17 22:32:32 +02:00
|
|
|
return;
|
|
|
|
compilecomv:
|
2017-01-25 01:18:29 +01:00
|
|
|
gs.code.push_back(
|
2021-03-30 02:08:25 +02:00
|
|
|
comtype | ret_code(rettype) | (id->get_index() << 8)
|
2016-09-17 22:32:32 +02:00
|
|
|
);
|
2021-03-30 02:08:25 +02:00
|
|
|
gs.code.push_back(numargs);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
|
|
|
|
2021-03-28 01:28:31 +01:00
|
|
|
static void compile_alias(codegen_state &gs, alias *id, bool &more) {
|
2021-03-30 02:08:25 +02:00
|
|
|
std::uint32_t numargs = 0;
|
2021-03-31 02:21:32 +02:00
|
|
|
for (;;) {
|
|
|
|
more = compilearg(gs, VAL_ANY);
|
|
|
|
if (!more) {
|
|
|
|
break;
|
|
|
|
}
|
2016-09-17 22:32:32 +02:00
|
|
|
++numargs;
|
|
|
|
}
|
2017-01-25 01:18:29 +01:00
|
|
|
gs.code.push_back(
|
2021-03-30 02:08:25 +02:00
|
|
|
BC_INST_CALL | (id->get_index() << 8)
|
2016-09-17 22:32:32 +02:00
|
|
|
);
|
2021-03-30 02:08:25 +02:00
|
|
|
gs.code.push_back(numargs);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
|
|
|
|
2021-03-28 01:28:31 +01:00
|
|
|
static void compile_local(codegen_state &gs, bool &more) {
|
2021-03-30 02:08:25 +02:00
|
|
|
std::uint32_t numargs = 0;
|
2016-09-17 22:32:32 +02:00
|
|
|
if (more) {
|
2021-03-31 02:21:32 +02:00
|
|
|
for (;;) {
|
|
|
|
more = compilearg(gs, VAL_IDENT);
|
|
|
|
if (!more) {
|
|
|
|
break;
|
|
|
|
}
|
2016-09-17 22:32:32 +02:00
|
|
|
numargs++;
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_LOCAL | (numargs << 8));
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void compile_do(
|
2021-03-28 01:28:31 +01:00
|
|
|
codegen_state &gs, bool &more, int rettype, int opcode
|
2016-09-17 22:32:32 +02:00
|
|
|
) {
|
|
|
|
if (more) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_CODE);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back((more ? opcode : BC_INST_NULL) | ret_code(rettype));
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void compile_if(
|
2021-03-28 01:28:31 +01:00
|
|
|
codegen_state &gs, ident *id, bool &more, int rettype
|
2016-09-17 22:32:32 +02:00
|
|
|
) {
|
|
|
|
if (more) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_ANY);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
|
|
|
if (!more) {
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_NULL | ret_code(rettype));
|
2016-09-17 22:32:32 +02:00
|
|
|
} else {
|
2021-03-31 02:21:32 +02:00
|
|
|
std::size_t start1 = gs.code.size();
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_CODE);
|
2016-09-17 22:32:32 +02:00
|
|
|
if (!more) {
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_POP);
|
|
|
|
gs.code.push_back(BC_INST_NULL | ret_code(rettype));
|
2016-09-17 22:32:32 +02:00
|
|
|
} else {
|
2021-03-31 02:21:32 +02:00
|
|
|
std::size_t start2 = gs.code.size();
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_CODE);
|
2021-03-31 02:21:32 +02:00
|
|
|
std::uint32_t inst1 = gs.code[start1];
|
|
|
|
std::uint32_t op1 = inst1 & ~BC_INST_RET_MASK;
|
|
|
|
auto len1 = std::uint32_t(start2 - (start1 + 1));
|
2016-09-17 22:32:32 +02:00
|
|
|
if (!more) {
|
2021-03-23 23:29:32 +01:00
|
|
|
if (op1 == (BC_INST_BLOCK | (len1 << 8))) {
|
|
|
|
gs.code[start1] = (len1 << 8) | BC_INST_JUMP_B | BC_INST_FLAG_FALSE;
|
|
|
|
gs.code[start1 + 1] = BC_INST_ENTER_RESULT;
|
2016-09-17 22:32:32 +02:00
|
|
|
gs.code[start1 + len1] = (
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code[start1 + len1] & ~BC_INST_RET_MASK
|
|
|
|
) | ret_code(rettype);
|
2016-09-17 22:32:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
compileblock(gs);
|
|
|
|
} else {
|
2021-03-31 02:21:32 +02:00
|
|
|
std::uint32_t inst2 = gs.code[start2];
|
|
|
|
std::uint32_t op2 = inst2 & ~BC_INST_RET_MASK;
|
|
|
|
auto len2 = std::uint32_t(gs.code.size() - (start2 + 1));
|
2021-03-23 23:29:32 +01:00
|
|
|
if (op2 == (BC_INST_BLOCK | (len2 << 8))) {
|
|
|
|
if (op1 == (BC_INST_BLOCK | (len1 << 8))) {
|
2021-03-31 02:21:32 +02:00
|
|
|
gs.code[start1] = (std::uint32_t(start2 - start1) << 8)
|
2021-03-23 23:29:32 +01:00
|
|
|
| BC_INST_JUMP_B | BC_INST_FLAG_FALSE;
|
|
|
|
gs.code[start1 + 1] = BC_INST_ENTER_RESULT;
|
2016-09-17 22:32:32 +02:00
|
|
|
gs.code[start1 + len1] = (
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code[start1 + len1] & ~BC_INST_RET_MASK
|
|
|
|
) | ret_code(rettype);
|
|
|
|
gs.code[start2] = (len2 << 8) | BC_INST_JUMP;
|
|
|
|
gs.code[start2 + 1] = BC_INST_ENTER_RESULT;
|
2016-09-17 22:32:32 +02:00
|
|
|
gs.code[start2 + len2] = (
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code[start2 + len2] & ~BC_INST_RET_MASK
|
|
|
|
) | ret_code(rettype);
|
2016-09-17 22:32:32 +02:00
|
|
|
return;
|
2021-03-23 23:29:32 +01:00
|
|
|
} else if (op1 == (BC_INST_EMPTY | (len1 << 8))) {
|
|
|
|
gs.code[start1] = BC_INST_NULL | (inst2 & BC_INST_RET_MASK);
|
|
|
|
gs.code[start2] = (len2 << 8) | BC_INST_JUMP_B | BC_INST_FLAG_TRUE;
|
|
|
|
gs.code[start2 + 1] = BC_INST_ENTER_RESULT;
|
2016-09-17 22:32:32 +02:00
|
|
|
gs.code[start2 + len2] = (
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code[start2 + len2] & ~BC_INST_RET_MASK
|
|
|
|
) | ret_code(rettype);
|
2016-09-17 22:32:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_COM | ret_code(rettype) | (id->get_index() << 8));
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void compile_and_or(
|
2021-03-28 01:28:31 +01:00
|
|
|
codegen_state &gs, ident *id, bool &more, int rettype
|
2016-09-17 22:32:32 +02:00
|
|
|
) {
|
2021-03-30 02:08:25 +02:00
|
|
|
std::uint32_t numargs = 0;
|
2016-09-17 22:32:32 +02:00
|
|
|
if (more) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_COND);
|
2016-09-17 22:32:32 +02:00
|
|
|
}
|
|
|
|
if (!more) {
|
2017-01-25 01:18:29 +01:00
|
|
|
gs.code.push_back(
|
2021-03-22 22:33:01 +01:00
|
|
|
((id->get_raw_type() == ID_AND)
|
2021-03-23 23:29:32 +01:00
|
|
|
? BC_INST_TRUE : BC_INST_FALSE) | ret_code(rettype)
|
2016-09-17 22:32:32 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
numargs++;
|
2021-03-31 02:21:32 +02:00
|
|
|
std::size_t start = gs.code.size(), end = start;
|
|
|
|
for (;;) {
|
|
|
|
more = compilearg(gs, VAL_COND);
|
|
|
|
if (!more) {
|
|
|
|
break;
|
|
|
|
}
|
2016-09-17 22:32:32 +02:00
|
|
|
numargs++;
|
2021-03-23 23:29:32 +01:00
|
|
|
if ((gs.code[end] & ~BC_INST_RET_MASK) != (
|
|
|
|
BC_INST_BLOCK | (uint32_t(gs.code.size() - (end + 1)) << 8)
|
2016-09-17 22:32:32 +02:00
|
|
|
)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end = gs.code.size();
|
|
|
|
}
|
|
|
|
if (more) {
|
2021-03-31 02:21:32 +02:00
|
|
|
for (;;) {
|
|
|
|
more = compilearg(gs, VAL_COND);
|
|
|
|
if (!more) {
|
|
|
|
break;
|
|
|
|
}
|
2016-09-17 22:32:32 +02:00
|
|
|
numargs++;
|
|
|
|
}
|
2017-01-25 01:18:29 +01:00
|
|
|
gs.code.push_back(
|
2021-03-30 02:08:25 +02:00
|
|
|
BC_INST_COM_V | ret_code(rettype) | (id->get_index() << 8)
|
2016-09-17 22:32:32 +02:00
|
|
|
);
|
2021-03-30 02:08:25 +02:00
|
|
|
gs.code.push_back(numargs);
|
2016-09-17 22:32:32 +02:00
|
|
|
} else {
|
2021-03-31 02:21:32 +02:00
|
|
|
std::uint32_t op = (id->get_raw_type() == ID_AND)
|
2021-03-23 23:29:32 +01:00
|
|
|
? (BC_INST_JUMP_RESULT | BC_INST_FLAG_FALSE)
|
|
|
|
: (BC_INST_JUMP_RESULT | BC_INST_FLAG_TRUE);
|
2017-01-25 01:18:29 +01:00
|
|
|
gs.code.push_back(op);
|
2016-09-17 22:32:32 +02:00
|
|
|
end = gs.code.size();
|
|
|
|
while ((start + 1) < end) {
|
2017-01-25 02:10:17 +01:00
|
|
|
uint32_t len = gs.code[start] >> 8;
|
2021-03-31 02:21:32 +02:00
|
|
|
gs.code[start] = std::uint32_t((end - (start + 1)) << 8) | op;
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code[start + 1] = BC_INST_ENTER;
|
2016-09-17 22:32:32 +02:00
|
|
|
gs.code[start + len] = (
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code[start + len] & ~BC_INST_RET_MASK
|
|
|
|
) | ret_code(rettype);
|
2016-09-17 22:32:32 +02:00
|
|
|
start += len + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 01:28:31 +01:00
|
|
|
static void compilestatements(codegen_state &gs, int rettype, int brak) {
|
2021-04-04 05:36:19 +02:00
|
|
|
charbuf idname{gs.ts};
|
2016-08-14 17:14:10 +02:00
|
|
|
for (;;) {
|
2016-09-22 00:20:23 +02:00
|
|
|
gs.skip_comments();
|
2016-09-20 22:11:40 +02:00
|
|
|
idname.clear();
|
2017-01-25 02:09:50 +01:00
|
|
|
size_t curline = gs.current_line;
|
2021-03-28 01:28:31 +01:00
|
|
|
bool more = compilearg(gs, VAL_WORD, &idname);
|
2016-08-17 02:25:12 +02:00
|
|
|
if (!more) {
|
|
|
|
goto endstatement;
|
|
|
|
}
|
2016-09-22 00:20:23 +02:00
|
|
|
gs.skip_comments();
|
2016-08-17 02:25:12 +02:00
|
|
|
if (gs.current() == '=') {
|
2016-09-22 00:42:34 +02:00
|
|
|
switch (gs.current(1)) {
|
2016-08-17 02:25:12 +02:00
|
|
|
case '/':
|
2016-09-22 00:42:34 +02:00
|
|
|
if (gs.current(2) != '/') {
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-11-06 01:07:53 +01:00
|
|
|
[[fallthrough]];
|
2016-08-17 02:25:12 +02:00
|
|
|
case ';':
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
case '\0':
|
|
|
|
gs.next_char();
|
2016-09-20 22:11:40 +02:00
|
|
|
if (!idname.empty()) {
|
2021-03-19 22:25:38 +01:00
|
|
|
idname.push_back('\0');
|
2021-04-05 19:52:13 +02:00
|
|
|
ident &id = gs.ts.istate->new_ident(
|
2021-04-03 06:16:43 +02:00
|
|
|
*gs.ts.pstate, idname.str_term(), IDENT_FLAG_UNKNOWN
|
2021-03-28 00:38:41 +01:00
|
|
|
);
|
2021-04-05 19:52:13 +02:00
|
|
|
switch (id.get_type()) {
|
|
|
|
case ident_type::ALIAS:
|
|
|
|
more = compilearg(gs, VAL_ANY);
|
|
|
|
if (!more) {
|
|
|
|
gs.gen_str();
|
2021-04-03 03:14:52 +02:00
|
|
|
}
|
2021-04-05 19:52:13 +02:00
|
|
|
gs.code.push_back(
|
|
|
|
BC_INST_ALIAS | (id.get_index() << 8)
|
|
|
|
);
|
|
|
|
goto endstatement;
|
|
|
|
case ident_type::IVAR: {
|
|
|
|
auto *hid = gs.ts.istate->cmd_ivar;
|
|
|
|
compile_cmd(
|
|
|
|
gs, static_cast<command_impl *>(hid),
|
|
|
|
id, more, rettype, 1
|
|
|
|
);
|
|
|
|
goto endstatement;
|
|
|
|
}
|
|
|
|
case ident_type::FVAR: {
|
|
|
|
auto *hid = gs.ts.istate->cmd_fvar;
|
|
|
|
compile_cmd(
|
|
|
|
gs, static_cast<command_impl *>(hid),
|
|
|
|
id, more, rettype, 1
|
|
|
|
);
|
|
|
|
goto endstatement;
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2021-04-05 19:52:13 +02:00
|
|
|
case ident_type::SVAR: {
|
|
|
|
auto *hid = gs.ts.istate->cmd_svar;
|
|
|
|
compile_cmd(
|
|
|
|
gs, static_cast<command_impl *>(hid),
|
|
|
|
id, more, rettype, 1
|
|
|
|
);
|
|
|
|
goto endstatement;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2021-03-19 22:25:38 +01:00
|
|
|
gs.gen_str(idname.str_term());
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
more = compilearg(gs, VAL_ANY);
|
2016-08-17 02:25:12 +02:00
|
|
|
if (!more) {
|
|
|
|
gs.gen_str();
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_ALIAS_U);
|
2016-08-17 02:25:12 +02:00
|
|
|
goto endstatement;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2016-09-20 22:11:40 +02:00
|
|
|
if (idname.empty()) {
|
2016-08-14 17:14:10 +02:00
|
|
|
noid:
|
2021-03-30 02:08:25 +02:00
|
|
|
std::uint32_t numargs = 0;
|
2021-03-31 02:21:32 +02:00
|
|
|
for (;;) {
|
|
|
|
more = compilearg(gs, VAL_ANY);
|
|
|
|
if (!more) {
|
|
|
|
break;
|
|
|
|
}
|
2016-08-17 02:25:12 +02:00
|
|
|
++numargs;
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_CALL_U | (numargs << 8));
|
2016-08-14 17:14:10 +02:00
|
|
|
} else {
|
2021-03-19 22:25:38 +01:00
|
|
|
idname.push_back('\0');
|
2021-03-26 02:29:54 +01:00
|
|
|
ident *id = gs.ts.pstate->get_ident(idname.str_term());
|
2016-08-14 17:14:10 +02:00
|
|
|
if (!id) {
|
2021-03-23 22:17:25 +01:00
|
|
|
if (is_valid_name(idname.str_term())) {
|
2021-03-19 22:25:38 +01:00
|
|
|
gs.gen_str(idname.str_term());
|
2016-08-14 17:14:10 +02:00
|
|
|
goto noid;
|
|
|
|
}
|
|
|
|
switch (rettype) {
|
2021-03-23 23:29:32 +01:00
|
|
|
case VAL_ANY: {
|
2021-03-20 02:26:37 +01:00
|
|
|
std::string_view end = idname.str_term();
|
2021-03-23 23:29:32 +01:00
|
|
|
integer_type val = parse_int(end, &end);
|
2016-08-17 02:25:12 +02:00
|
|
|
if (!end.empty()) {
|
2021-03-19 22:25:38 +01:00
|
|
|
gs.gen_str(idname.str_term());
|
2016-08-17 02:25:12 +02:00
|
|
|
} else {
|
|
|
|
gs.gen_int(val);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2021-03-31 02:21:32 +02:00
|
|
|
gs.gen_value(rettype, idname.str_term(), int(curline));
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_RESULT);
|
2016-08-17 02:25:12 +02:00
|
|
|
} else {
|
2021-03-21 02:41:04 +01:00
|
|
|
switch (id->get_raw_type()) {
|
2021-03-22 22:33:01 +01:00
|
|
|
case ID_ALIAS:
|
2016-09-17 22:32:32 +02:00
|
|
|
compile_alias(
|
2021-03-28 01:28:31 +01:00
|
|
|
gs, static_cast<alias *>(id), more
|
2016-08-17 02:25:12 +02:00
|
|
|
);
|
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
case ID_COMMAND:
|
2016-09-17 22:32:32 +02:00
|
|
|
compile_cmd(
|
2021-04-05 19:52:13 +02:00
|
|
|
gs, static_cast<command_impl *>(id), *id, more,
|
2021-03-28 01:28:31 +01:00
|
|
|
rettype
|
2016-08-17 02:25:12 +02:00
|
|
|
);
|
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
case ID_LOCAL:
|
2021-03-28 01:28:31 +01:00
|
|
|
compile_local(gs, more);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
case ID_DO:
|
2021-03-28 01:28:31 +01:00
|
|
|
compile_do(gs, more, rettype, BC_INST_DO);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
case ID_DOARGS:
|
2021-03-28 01:28:31 +01:00
|
|
|
compile_do(gs, more, rettype, BC_INST_DO_ARGS);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
case ID_IF:
|
2021-03-28 01:28:31 +01:00
|
|
|
compile_if(gs, id, more, rettype);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
case ID_BREAK:
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_BREAK | BC_INST_FLAG_FALSE);
|
2016-09-15 02:12:22 +02:00
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
case ID_CONTINUE:
|
2021-03-23 23:29:32 +01:00
|
|
|
gs.code.push_back(BC_INST_BREAK | BC_INST_FLAG_TRUE);
|
2016-09-15 02:12:22 +02:00
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
case ID_RESULT:
|
2016-08-17 02:25:12 +02:00
|
|
|
if (more) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_ANY);
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2017-01-25 01:18:29 +01:00
|
|
|
gs.code.push_back(
|
2021-03-23 23:29:32 +01:00
|
|
|
(more ? BC_INST_RESULT : BC_INST_NULL) |
|
|
|
|
ret_code(rettype)
|
2016-08-17 02:25:12 +02:00
|
|
|
);
|
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
case ID_NOT:
|
2016-08-17 02:25:12 +02:00
|
|
|
if (more) {
|
2021-03-28 01:28:31 +01:00
|
|
|
more = compilearg(gs, VAL_ANY);
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2017-01-25 01:18:29 +01:00
|
|
|
gs.code.push_back(
|
2021-03-23 23:29:32 +01:00
|
|
|
(more ? BC_INST_NOT : BC_INST_TRUE) | ret_code(rettype)
|
2016-08-17 02:25:12 +02:00
|
|
|
);
|
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
case ID_AND:
|
|
|
|
case ID_OR:
|
2021-03-28 01:28:31 +01:00
|
|
|
compile_and_or(gs, id, more, rettype);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-03 03:14:52 +02:00
|
|
|
case ID_IVAR: {
|
2021-04-04 06:47:17 +02:00
|
|
|
auto *hid = gs.ts.istate->cmd_ivar;
|
2021-04-03 03:14:52 +02:00
|
|
|
compile_cmd(
|
|
|
|
gs, static_cast<command_impl *>(hid),
|
2021-04-05 19:52:13 +02:00
|
|
|
*id, more, rettype
|
2021-04-03 03:14:52 +02:00
|
|
|
);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-03 03:14:52 +02:00
|
|
|
}
|
|
|
|
case ID_FVAR: {
|
2021-04-04 06:47:17 +02:00
|
|
|
auto *hid = gs.ts.istate->cmd_fvar;
|
2021-04-03 03:14:52 +02:00
|
|
|
compile_cmd(
|
|
|
|
gs, static_cast<command_impl *>(hid),
|
2021-04-05 19:52:13 +02:00
|
|
|
*id, more, rettype
|
2021-04-03 03:14:52 +02:00
|
|
|
);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-03 03:14:52 +02:00
|
|
|
}
|
|
|
|
case ID_SVAR: {
|
2021-04-04 06:47:17 +02:00
|
|
|
auto *hid = gs.ts.istate->cmd_svar;
|
2021-04-03 03:14:52 +02:00
|
|
|
compile_cmd(
|
|
|
|
gs, static_cast<command_impl *>(hid),
|
2021-04-05 19:52:13 +02:00
|
|
|
*id, more, rettype
|
2021-04-03 03:14:52 +02:00
|
|
|
);
|
2016-08-17 02:25:12 +02:00
|
|
|
break;
|
2021-04-03 03:14:52 +02:00
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
endstatement:
|
2016-08-17 02:25:12 +02:00
|
|
|
if (more) {
|
2021-03-23 23:29:32 +01:00
|
|
|
while (compilearg(gs, VAL_POP));
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
2016-09-22 00:20:23 +02:00
|
|
|
switch (gs.skip_until(")];/\n")) {
|
2016-08-17 02:25:12 +02:00
|
|
|
case '\0':
|
2016-09-22 00:20:23 +02:00
|
|
|
if (gs.current() != brak) {
|
2021-04-05 04:10:39 +02:00
|
|
|
throw error{*gs.ts.pstate, "missing \"%c\"", char(brak)};
|
2016-09-10 15:01:49 +02:00
|
|
|
return;
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
case ')':
|
|
|
|
case ']':
|
2016-09-22 00:20:23 +02:00
|
|
|
if (gs.current() == brak) {
|
|
|
|
gs.next_char();
|
2016-08-17 02:25:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-04-05 04:10:39 +02:00
|
|
|
throw error{*gs.ts.pstate, "unexpected \"%c\"", gs.current()};
|
2016-09-10 15:01:49 +02:00
|
|
|
return;
|
2016-08-17 02:25:12 +02:00
|
|
|
case '/':
|
2016-09-22 00:20:23 +02:00
|
|
|
gs.next_char();
|
2016-08-17 02:25:12 +02:00
|
|
|
if (gs.current() == '/') {
|
2016-09-22 00:20:23 +02:00
|
|
|
gs.skip_until('\n');
|
2016-08-17 02:25:12 +02:00
|
|
|
}
|
|
|
|
goto endstatement;
|
2016-09-22 00:20:23 +02:00
|
|
|
default:
|
|
|
|
gs.next_char();
|
|
|
|
break;
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
void codegen_state::gen_main(std::string_view s, int ret_type) {
|
2021-03-20 02:26:37 +01:00
|
|
|
source = s.data();
|
|
|
|
send = s.data() + s.size();
|
2021-03-23 23:29:32 +01:00
|
|
|
code.push_back(BC_INST_START);
|
|
|
|
compilestatements(*this, VAL_ANY);
|
|
|
|
code.push_back(BC_INST_EXIT | ((ret_type < VAL_ANY) ? (ret_type << BC_INST_RET) : 0));
|
2016-08-14 17:14:10 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:32:25 +01:00
|
|
|
} /* namespace cubescript */
|