The checked-arithmetic helpers every generated module carries no longer test __TINYC__.
838 lines
33 KiB
C
838 lines
33 KiB
C
/* Generated by zi2c from compiler_text.zi. */
|
|
#define ZIRAN_COMPILER_TEXT_H_SOURCE 1
|
|
#include <stdbool.h>
|
|
#include "zir_bounds.h"
|
|
#include "zir_string.h"
|
|
#include "compiler_text.h"
|
|
#ifndef ZIRAN_HELPER_ZiDivU64
|
|
#define ZIRAN_HELPER_ZiDivU64
|
|
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__OPTIMIZE__)
|
|
#define ZiDivU64(a, b) __extension__ ({ uint64_t __zi_a = (a), __zi_b = (b); if(!__zi_b) abort(); __zi_a / __zi_b; })
|
|
#else
|
|
static inline uint64_t ZiDivU64(uint64_t a, uint64_t b) {
|
|
if(!b) abort();
|
|
return a / b;
|
|
}
|
|
#endif
|
|
#endif
|
|
#ifndef ZIRAN_MAYBE_UNUSED
|
|
#if defined(__GNUC__) || defined(__clang__)
|
|
#define ZIRAN_MAYBE_UNUSED __attribute__((unused))
|
|
#else
|
|
#define ZIRAN_MAYBE_UNUSED
|
|
#endif
|
|
#endif
|
|
static uint8_t compiler_text_Byte(String source, int64_t at) ZIRAN_MAYBE_UNUSED;
|
|
static int64_t compiler_text_AfterQuote(String source, int64_t at) ZIRAN_MAYBE_UNUSED;
|
|
static int64_t compiler_text_EscapeByte(uint8_t byte, bool c_string, Slice output) ZIRAN_MAYBE_UNUSED;
|
|
static String Operators[16] = {{"==", sizeof("==") - 1}, {"!=", sizeof("!=") - 1}, {"<=", sizeof("<=") - 1}, {">=", sizeof(">=") - 1}, {"<<", sizeof("<<") - 1}, {">>", sizeof(">>") - 1}, {"+", sizeof("+") - 1}, {"-", sizeof("-") - 1}, {"*", sizeof("*") - 1}, {"/", sizeof("/") - 1}, {"%", sizeof("%") - 1}, {"<", sizeof("<") - 1}, {">", sizeof(">") - 1}, {"&", sizeof("&") - 1}, {"|", sizeof("|") - 1}, {"^", sizeof("^") - 1}};
|
|
static String Procedures[16] = {{"operator_equal", sizeof("operator_equal") - 1}, {"operator_not_equal", sizeof("operator_not_equal") - 1}, {"operator_less_equal", sizeof("operator_less_equal") - 1}, {"operator_greater_equal", sizeof("operator_greater_equal") - 1}, {"operator_shift_left", sizeof("operator_shift_left") - 1}, {"operator_shift_right", sizeof("operator_shift_right") - 1}, {"operator_add", sizeof("operator_add") - 1}, {"operator_subtract", sizeof("operator_subtract") - 1}, {"operator_multiply", sizeof("operator_multiply") - 1}, {"operator_divide", sizeof("operator_divide") - 1}, {"operator_modulo", sizeof("operator_modulo") - 1}, {"operator_less", sizeof("operator_less") - 1}, {"operator_greater", sizeof("operator_greater") - 1}, {"operator_bit_and", sizeof("operator_bit_and") - 1}, {"operator_bit_or", sizeof("operator_bit_or") - 1}, {"operator_bit_xor", sizeof("operator_bit_xor") - 1}};
|
|
|
|
int32_t
|
|
compiler_text_NameDistance(String a, String b, int32_t limit)
|
|
{
|
|
if (limit < 0 || limit >= 128) {
|
|
return 128;
|
|
}
|
|
bool value_0 = (int64_t)(a).length >= 128LL || (int64_t)(b).length >= 128LL || (int64_t)((uint64_t)((int64_t)(a).length) - (uint64_t)((int64_t)(b).length)) > (int64_t)(limit);
|
|
if (value_0 || (int64_t)((uint64_t)((int64_t)(b).length) - (uint64_t)((int64_t)(a).length)) > (int64_t)(limit)) {
|
|
return (int32_t)((uint32_t)limit + 1u);
|
|
}
|
|
int32_t previous[128] = {0};
|
|
int32_t current[128] = {0};
|
|
{
|
|
int64_t range_first_8 = 0LL;
|
|
int64_t range_last_8 = (int64_t)(b).length;
|
|
int64_t j = range_first_8;
|
|
if (j <= range_last_8) for (;;) {
|
|
int32_t* value_1 = &(previous[j]);
|
|
(*value_1) = (int32_t)(j);
|
|
if (j == range_last_8) {
|
|
break;
|
|
}
|
|
j = (int64_t)((uint64_t)j + UINT64_C(1));
|
|
}
|
|
}
|
|
int64_t i = 1LL;
|
|
while (i <= (int64_t)(a).length) {
|
|
int32_t best = (int32_t)(i);
|
|
int32_t* value_2 = &(current[0]);
|
|
(*value_2) = best;
|
|
int64_t j = 1LL;
|
|
while (j <= (int64_t)(b).length) {
|
|
bool value_3 = (uint8_t)a.data[((int64_t)((uint64_t)i - UINT64_C(1)))] != (uint8_t)b.data[((int64_t)((uint64_t)j - UINT64_C(1)))];
|
|
int32_t value_4 = 0;
|
|
if (value_3) {
|
|
value_4 = 1;
|
|
}
|
|
int32_t substitute = (int32_t)((uint32_t)((previous)[((int64_t)((uint64_t)j - UINT64_C(1)))]) + (uint32_t)value_4);
|
|
int32_t remove = (int32_t)((uint32_t)((previous)[j]) + 1u);
|
|
int32_t insert = (int32_t)((uint32_t)((current)[((int64_t)((uint64_t)j - UINT64_C(1)))]) + 1u);
|
|
int32_t value = substitute;
|
|
if (remove < value) {
|
|
value = remove;
|
|
}
|
|
if (insert < value) {
|
|
value = insert;
|
|
}
|
|
int32_t* value_5 = &(current[j]);
|
|
(*value_5) = value;
|
|
if (value < best) {
|
|
best = value;
|
|
}
|
|
j = (int64_t)((uint64_t)j + UINT64_C(1));
|
|
}
|
|
if (best > limit) {
|
|
return (int32_t)((uint32_t)limit + 1u);
|
|
}
|
|
{
|
|
int64_t range_first_36 = 0LL;
|
|
int64_t range_last_36 = (int64_t)(b).length;
|
|
int64_t j = range_first_36;
|
|
if (j <= range_last_36) for (;;) {
|
|
int32_t* value_6 = &(previous[j]);
|
|
(*value_6) = (current)[j];
|
|
if (j == range_last_36) {
|
|
break;
|
|
}
|
|
j = (int64_t)((uint64_t)j + UINT64_C(1));
|
|
}
|
|
}
|
|
i = (int64_t)((uint64_t)i + UINT64_C(1));
|
|
}
|
|
int32_t distance = (previous)[(int64_t)(b).length];
|
|
if (distance > limit) {
|
|
return (int32_t)((uint32_t)limit + 1u);
|
|
}
|
|
return distance;
|
|
}
|
|
|
|
bool
|
|
compiler_text_SpaceByte(uint8_t byte)
|
|
{
|
|
return byte == 32 || (byte >= 9 && byte <= 13);
|
|
}
|
|
|
|
bool
|
|
compiler_text_IdentifierByte(uint8_t byte)
|
|
{
|
|
return (byte >= 65 && byte <= 90) || (byte >= 97 && byte <= 122) || (byte >= 48 && byte <= 57) || byte == 95;
|
|
}
|
|
|
|
int64_t
|
|
compiler_text_SkipSpace(String source, int64_t at, bool inline_only)
|
|
{
|
|
while (at < (int64_t)(source).length) {
|
|
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
|
|
if (inline_only) {
|
|
if (byte != 32 && byte != 9) {
|
|
break;
|
|
}
|
|
} else {
|
|
uint8_t value_0 = byte;
|
|
bool value_1 = false;
|
|
{
|
|
uint8_t byte = value_0;
|
|
value_1 = byte == 32 || (byte >= 9 && byte <= 13);
|
|
}
|
|
if (!value_1) {
|
|
break;
|
|
}
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
return at;
|
|
}
|
|
|
|
SourceRange
|
|
compiler_text_TrimRange(String source)
|
|
{
|
|
int64_t begin = compiler_text_SkipSpace(source, 0LL, false);
|
|
int64_t end = (int64_t)(source).length;
|
|
while (true) {
|
|
bool value_0 = end > begin;
|
|
if (value_0) {
|
|
uint8_t value_1 = (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)end - UINT64_C(1))));
|
|
bool value_2 = false;
|
|
{
|
|
uint8_t byte = value_1;
|
|
value_2 = byte == 32 || (byte >= 9 && byte <= 13);
|
|
}
|
|
value_0 = value_2;
|
|
}
|
|
if (!value_0) { break; }
|
|
end = (int64_t)((uint64_t)end - UINT64_C(1));
|
|
}
|
|
return (SourceRange){.begin = begin, .end = end};
|
|
}
|
|
|
|
LiteralBits
|
|
compiler_text_IntegerLiteral(String source)
|
|
{
|
|
if ((int64_t)(source).length == 0LL || (uint8_t)ZIRAN_INDEX(source.data, source.length, 0) < 48 || (uint8_t)ZIRAN_INDEX(source.data, source.length, 0) > 57) {
|
|
return (LiteralBits){0};
|
|
}
|
|
uint64_t base = 10ULL;
|
|
int64_t at = 0LL;
|
|
if ((uint8_t)source.data[0] == 48 && (int64_t)(source).length > 1LL) {
|
|
base = 8ULL;
|
|
at = 1LL;
|
|
if ((uint8_t)source.data[1] == 120 || (uint8_t)source.data[1] == 88) {
|
|
base = 16ULL;
|
|
at = 2LL;
|
|
} else if ((uint8_t)source.data[1] == 98 || (uint8_t)source.data[1] == 66) {
|
|
base = 2ULL;
|
|
at = 2LL;
|
|
}
|
|
}
|
|
bool digits = false;
|
|
uint64_t value = 0ULL;
|
|
while (at < (int64_t)(source).length) {
|
|
uint8_t byte = (uint8_t)source.data[at];
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
if (byte == 95) {
|
|
continue;
|
|
}
|
|
uint8_t value_0 = byte;
|
|
int32_t value_1 = 0;
|
|
{
|
|
uint8_t byte = value_0;
|
|
if (byte >= 48 && byte <= 57) {
|
|
value_1 = (int32_t)((uint32_t)(byte) - 48u);
|
|
goto value_2;
|
|
}
|
|
if (byte >= 97 && byte <= 102) {
|
|
value_1 = (int32_t)(((uint32_t)(byte) - 97u) + 10u);
|
|
goto value_2;
|
|
}
|
|
if (byte >= 65 && byte <= 70) {
|
|
value_1 = (int32_t)(((uint32_t)(byte) - 65u) + 10u);
|
|
goto value_2;
|
|
}
|
|
value_1 = -1;
|
|
value_2: ;
|
|
}
|
|
int32_t digit = value_1;
|
|
if (digit < 0 || (uint64_t)(digit) >= base) {
|
|
return (LiteralBits){0};
|
|
}
|
|
uint64_t value_3 = ZiDivU64(((uint64_t)(UINT64_C(18446744073709551615) - (uint64_t)(digit))), base);
|
|
if (value > value_3) {
|
|
return (LiteralBits){0};
|
|
}
|
|
value = (uint64_t)(((uint64_t)value * (uint64_t)base) + (uint64_t)(digit));
|
|
digits = true;
|
|
}
|
|
if (!digits && base != 8ULL) {
|
|
return (LiteralBits){0};
|
|
}
|
|
return (LiteralBits){.valid = true, .value = value};
|
|
}
|
|
|
|
int64_t
|
|
compiler_text_BlockHeaderEnd(String source)
|
|
{
|
|
int64_t end = (int64_t)(source).length;
|
|
while (true) {
|
|
bool value_0 = end > 0LL;
|
|
if (value_0) {
|
|
uint8_t value_1 = (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)end - UINT64_C(1))));
|
|
bool value_2 = false;
|
|
{
|
|
uint8_t byte = value_1;
|
|
value_2 = byte == 32 || (byte >= 9 && byte <= 13);
|
|
}
|
|
value_0 = value_2;
|
|
}
|
|
if (!value_0) { break; }
|
|
end = (int64_t)((uint64_t)end - UINT64_C(1));
|
|
}
|
|
if (end > 0LL && (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)end - UINT64_C(1)))) == 123) {
|
|
end = (int64_t)((uint64_t)end - UINT64_C(1));
|
|
}
|
|
while (true) {
|
|
bool value_4 = end > 0LL;
|
|
if (value_4) {
|
|
uint8_t value_5 = (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)end - UINT64_C(1))));
|
|
bool value_6 = false;
|
|
{
|
|
uint8_t byte = value_5;
|
|
value_6 = byte == 32 || (byte >= 9 && byte <= 13);
|
|
}
|
|
value_4 = value_6;
|
|
}
|
|
if (!value_4) { break; }
|
|
end = (int64_t)((uint64_t)end - UINT64_C(1));
|
|
}
|
|
return end;
|
|
}
|
|
|
|
int64_t
|
|
compiler_text_CamelIdentifier(String source, Slice output)
|
|
{
|
|
if ((int64_t)(output).length == 0LL) {
|
|
return 0LL;
|
|
}
|
|
int64_t used = 0LL;
|
|
bool upper = true;
|
|
for (int64_t it_index = 0LL; it_index < (int64_t)(source).length; it_index++) {
|
|
uint8_t byte = (uint8_t)source.data[it_index];
|
|
if ((int64_t)((uint64_t)used + UINT64_C(1)) >= (int64_t)(output).length) {
|
|
break;
|
|
}
|
|
if ((byte >= 65 && byte <= 90) || (byte >= 97 && byte <= 122) || (byte >= 48 && byte <= 57)) {
|
|
if (upper && byte >= 97 && byte <= 122) {
|
|
byte = (uint8_t)((uint32_t)byte - 32u);
|
|
}
|
|
uint8_t* value_0 = &(((uint8_t *)output.data)[SliceIndex(output, (int64_t)used)]);
|
|
(*value_0) = byte;
|
|
used = (int64_t)((uint64_t)used + UINT64_C(1));
|
|
upper = false;
|
|
} else {
|
|
upper = true;
|
|
}
|
|
}
|
|
if (used == 0LL && (int64_t)((uint64_t)used + UINT64_C(1)) < (int64_t)(output).length) {
|
|
uint8_t* value_1 = &(((uint8_t *)output.data)[SliceIndex(output, (int64_t)used)]);
|
|
(*value_1) = 88;
|
|
used = (int64_t)((uint64_t)used + UINT64_C(1));
|
|
}
|
|
if (used > 0LL && ((uint8_t *)output.data)[0] >= 48 && ((uint8_t *)output.data)[0] <= 57 && (int64_t)((uint64_t)used + UINT64_C(1)) < (int64_t)(output).length) {
|
|
int64_t index = used;
|
|
while (index > 0LL) {
|
|
uint8_t* value_2 = &(((uint8_t *)output.data)[SliceIndex(output, (int64_t)index)]);
|
|
uint8_t value_3 = ((uint8_t *)output.data)[SliceIndex(output, (int64_t)((int64_t)((uint64_t)index - UINT64_C(1))))];
|
|
(*value_2) = value_3;
|
|
index = (int64_t)((uint64_t)index - UINT64_C(1));
|
|
}
|
|
uint8_t* value_4 = &(((uint8_t *)output.data)[0]);
|
|
(*value_4) = 77;
|
|
used = (int64_t)((uint64_t)used + UINT64_C(1));
|
|
}
|
|
uint8_t* value_5 = &(((uint8_t *)output.data)[SliceIndex(output, (int64_t)used)]);
|
|
(*value_5) = 0;
|
|
return used;
|
|
}
|
|
|
|
int64_t
|
|
compiler_text_GoFieldIdentifier(String source, Slice output)
|
|
{
|
|
int64_t used = compiler_text_CamelIdentifier(source, output);
|
|
String replacement = StringLiteral("");
|
|
{
|
|
Slice value_0 = output;
|
|
Slice value_1 = SliceRange(value_0, (int64_t)0LL, (int64_t)used, sizeof(uint8_t));
|
|
String name = StringView((const char *)(value_1).data, (size_t)(value_1).length);
|
|
if (StringEqual(name, StringLiteral("Id"))) {
|
|
replacement = StringLiteral("ID");
|
|
} else if (StringEqual(name, StringLiteral("FocusId"))) {
|
|
replacement = StringLiteral("FocusID");
|
|
} else if (StringEqual(name, StringLiteral("MenuId"))) {
|
|
replacement = StringLiteral("MenuID");
|
|
} else if (StringEqual(name, StringLiteral("SelectedId"))) {
|
|
replacement = StringLiteral("SelectedID");
|
|
} else if (StringEqual(name, StringLiteral("ActivatedId"))) {
|
|
replacement = StringLiteral("ActivatedID");
|
|
} else if (StringEqual(name, StringLiteral("CanonicalUrl"))) {
|
|
replacement = StringLiteral("CanonicalURL");
|
|
}
|
|
}
|
|
if ((int64_t)(replacement).length == 0LL) {
|
|
return used;
|
|
}
|
|
{
|
|
int64_t range_first_26 = 0LL;
|
|
int64_t range_last_26 = (int64_t)((uint64_t)used - UINT64_C(1));
|
|
int64_t index = range_first_26;
|
|
if (index <= range_last_26) for (;;) {
|
|
uint8_t* value_2 = &(((uint8_t *)output.data)[SliceIndex(output, (int64_t)index)]);
|
|
(*value_2) = (uint8_t)ZIRAN_INDEX(replacement.data, replacement.length, index);
|
|
if (index == range_last_26) {
|
|
break;
|
|
}
|
|
index = (int64_t)((uint64_t)index + UINT64_C(1));
|
|
}
|
|
}
|
|
return used;
|
|
}
|
|
|
|
int64_t
|
|
compiler_text_EscapeCString(String source, Slice output)
|
|
{
|
|
if ((int64_t)(output).length == 0LL) {
|
|
return 0LL;
|
|
}
|
|
int64_t used = 0LL;
|
|
for (int64_t it_index = 0LL; it_index < (int64_t)(source).length; it_index++) {
|
|
uint8_t byte = (uint8_t)source.data[it_index];
|
|
if ((int64_t)((uint64_t)used + UINT64_C(1)) >= (int64_t)(output).length) {
|
|
break;
|
|
}
|
|
uint8_t escaped[4] = {0};
|
|
Slice value_0 = {escaped, 4};
|
|
int64_t count = compiler_text_EscapeByte(byte, true, value_0);
|
|
{
|
|
int64_t range_first_10 = 0LL;
|
|
int64_t range_last_10 = (int64_t)((uint64_t)count - UINT64_C(1));
|
|
int64_t index = range_first_10;
|
|
if (index <= range_last_10) for (;;) {
|
|
if ((int64_t)((uint64_t)used + UINT64_C(1)) < (int64_t)(output).length) {
|
|
uint8_t* value_1 = &(((uint8_t *)output.data)[SliceIndex(output, (int64_t)used)]);
|
|
(*value_1) = (escaped)[index];
|
|
used = (int64_t)((uint64_t)used + UINT64_C(1));
|
|
}
|
|
if (index == range_last_10) {
|
|
break;
|
|
}
|
|
index = (int64_t)((uint64_t)index + UINT64_C(1));
|
|
}
|
|
}
|
|
}
|
|
uint8_t* value_2 = &(((uint8_t *)output.data)[SliceIndex(output, (int64_t)used)]);
|
|
(*value_2) = 0;
|
|
return used;
|
|
}
|
|
|
|
SourcePart
|
|
compiler_text_NextSourcePart(String source, int64_t at)
|
|
{
|
|
if (at < 0LL || at > (int64_t)(source).length) {
|
|
return (SourcePart){.begin = 0LL, .end = 0LL, .next = -1LL};
|
|
}
|
|
int64_t start = at;
|
|
int32_t depth = 0;
|
|
while (at < (int64_t)(source).length) {
|
|
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
|
|
if (byte == 34 || byte == 39) {
|
|
at = compiler_text_AfterQuote(source, at);
|
|
} else if (byte == 44 && depth == 0) {
|
|
break;
|
|
} else {
|
|
if (byte == 40 || byte == 91 || byte == 123) {
|
|
depth = (int32_t)((uint32_t)depth + 1u);
|
|
} else if (byte == 41 || byte == 93 || byte == 125) {
|
|
if (depth > 0) {
|
|
depth = (int32_t)((uint32_t)depth - 1u);
|
|
}
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
}
|
|
int64_t end = at;
|
|
int64_t next = -1LL;
|
|
if (at < (int64_t)(source).length) {
|
|
next = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
return (SourcePart){.begin = start, .end = end, .next = next};
|
|
}
|
|
|
|
int64_t
|
|
compiler_text_TopLevelAssignment(String source)
|
|
{
|
|
int64_t at = 0LL;
|
|
int32_t depth = 0;
|
|
while (at < (int64_t)(source).length) {
|
|
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
|
|
if (byte == 34 || byte == 39) {
|
|
at = compiler_text_AfterQuote(source, at);
|
|
} else {
|
|
if (byte == 40 || byte == 91 || byte == 123) {
|
|
depth = (int32_t)((uint32_t)depth + 1u);
|
|
} else if (byte == 41 || byte == 93 || byte == 125) {
|
|
if (depth > 0) {
|
|
depth = (int32_t)((uint32_t)depth - 1u);
|
|
}
|
|
} else {
|
|
bool value_0 = byte == 61 && depth == 0;
|
|
if (value_0) {
|
|
String value_1 = source;
|
|
int64_t value_2 = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
uint8_t value_3 = 0;
|
|
{
|
|
String source = value_1;
|
|
int64_t at = value_2;
|
|
if (at < 0LL || at >= (int64_t)(source).length) {
|
|
value_3 = 0;
|
|
goto value_4;
|
|
}
|
|
value_3 = (uint8_t)source.data[at];
|
|
value_4: ;
|
|
}
|
|
value_0 = (value_3 != 61);
|
|
}
|
|
bool value_5 = value_0;
|
|
if (value_5) {
|
|
bool value_6 = at == 0LL;
|
|
if (!value_6) {
|
|
bool value_7 = (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)at - UINT64_C(1)))) != 61 && (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)at - UINT64_C(1)))) != 33;
|
|
bool value_8 = value_7 && (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)at - UINT64_C(1)))) != 60;
|
|
bool value_9 = value_8 && (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)at - UINT64_C(1)))) != 62;
|
|
value_6 = value_9;
|
|
}
|
|
value_5 = value_6;
|
|
}
|
|
if (value_5) {
|
|
return at;
|
|
}
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
}
|
|
return -1LL;
|
|
}
|
|
|
|
DecodedLiteral
|
|
compiler_text_DecodeString(String source, Slice output)
|
|
{
|
|
if ((int64_t)(source).length < 2LL || (uint8_t)ZIRAN_INDEX(source.data, source.length, 0) != 34 || (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)((int64_t)(source).length) - UINT64_C(1)))) != 34) {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
int64_t used = 0LL;
|
|
int32_t remaining = 0;
|
|
uint32_t scalar = 0;
|
|
uint32_t minimum = 0;
|
|
int64_t at = 1LL;
|
|
while ((int64_t)((uint64_t)at + UINT64_C(1)) < (int64_t)(source).length) {
|
|
uint32_t value = (uint32_t)((uint8_t)ZIRAN_INDEX(source.data, source.length, at));
|
|
bool unicode_escape = false;
|
|
if (value == 92) {
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
if ((int64_t)((uint64_t)at + UINT64_C(1)) >= (int64_t)(source).length) {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
value = (uint32_t)((uint8_t)ZIRAN_INDEX(source.data, source.length, at));
|
|
String escapes = StringLiteral("0abfnrtv\\\"");
|
|
uint8_t values[10] = {0, 7, 8, 12, 10, 13, 9, 11, 92, 34};
|
|
bool found = false;
|
|
{
|
|
int64_t range_first_20 = 0LL;
|
|
int64_t range_last_20 = (int64_t)((uint64_t)((int64_t)(escapes).length) - UINT64_C(1));
|
|
int64_t index = range_first_20;
|
|
if (index <= range_last_20) for (;;) {
|
|
if (value == (uint32_t)((uint8_t)escapes.data[index])) {
|
|
value = (uint32_t)(ZIRAN_INDEX(values, 10, index));
|
|
found = true;
|
|
break;
|
|
}
|
|
if (index == range_last_20) {
|
|
break;
|
|
}
|
|
index = (int64_t)((uint64_t)index + UINT64_C(1));
|
|
}
|
|
}
|
|
if (!found) {
|
|
if (value != 120 && value != 117 && value != 85) {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
unicode_escape = value != 120;
|
|
int32_t digits = 2;
|
|
if (value == 117) {
|
|
digits = 4;
|
|
} else if (value == 85) {
|
|
digits = 8;
|
|
}
|
|
value = 0;
|
|
{
|
|
int32_t range_first_40 = 0;
|
|
int32_t range_last_40 = (int32_t)((uint32_t)digits - 1u);
|
|
int32_t index = range_first_40;
|
|
if (index <= range_last_40) for (;;) {
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
if ((int64_t)((uint64_t)at + UINT64_C(1)) >= (int64_t)(source).length) {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
uint8_t value_0 = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
|
|
int32_t value_1 = 0;
|
|
{
|
|
uint8_t byte = value_0;
|
|
if (byte >= 48 && byte <= 57) {
|
|
value_1 = (int32_t)((uint32_t)(byte) - 48u);
|
|
goto value_2;
|
|
}
|
|
if (byte >= 97 && byte <= 102) {
|
|
value_1 = (int32_t)(((uint32_t)(byte) - 97u) + 10u);
|
|
goto value_2;
|
|
}
|
|
if (byte >= 65 && byte <= 70) {
|
|
value_1 = (int32_t)(((uint32_t)(byte) - 65u) + 10u);
|
|
goto value_2;
|
|
}
|
|
value_1 = -1;
|
|
value_2: ;
|
|
}
|
|
int32_t digit = value_1;
|
|
if (digit < 0) {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
value = (uint32_t)(((uint32_t)value * 16u) + (uint32_t)(digit));
|
|
if (index == range_last_40) {
|
|
break;
|
|
}
|
|
index = (int32_t)((uint32_t)index + 1u);
|
|
}
|
|
}
|
|
if (value > 1114111 || (value >= 55296 && value <= 57343)) {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
}
|
|
} else if (value == 34) {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
uint8_t bytes[4] = {0};
|
|
uint8_t* value_3 = &(bytes[0]);
|
|
(*value_3) = (uint8_t)(value);
|
|
int32_t count = 1;
|
|
if (unicode_escape && value >= 128) {
|
|
if (value < 2048) {
|
|
uint8_t* value_4 = &(bytes[0]);
|
|
(*value_4) = (uint8_t)(((uint32_t)(192u | (uint32_t)((value >> 6)))));
|
|
uint8_t* value_5 = &(bytes[1]);
|
|
(*value_5) = (uint8_t)(((uint32_t)(128u | ((uint32_t)value & 63u))));
|
|
count = 2;
|
|
} else if (value < 65536) {
|
|
uint8_t* value_6 = &(bytes[0]);
|
|
(*value_6) = (uint8_t)(((uint32_t)(224u | (uint32_t)((value >> 12)))));
|
|
uint8_t* value_7 = &(bytes[1]);
|
|
(*value_7) = (uint8_t)(((uint32_t)(128u | ((uint32_t)((value >> 6)) & 63u))));
|
|
uint8_t* value_8 = &(bytes[2]);
|
|
(*value_8) = (uint8_t)(((uint32_t)(128u | ((uint32_t)value & 63u))));
|
|
count = 3;
|
|
} else {
|
|
uint8_t* value_9 = &(bytes[0]);
|
|
(*value_9) = (uint8_t)(((uint32_t)(240u | (uint32_t)((value >> 18)))));
|
|
uint8_t* value_10 = &(bytes[1]);
|
|
(*value_10) = (uint8_t)(((uint32_t)(128u | ((uint32_t)((value >> 12)) & 63u))));
|
|
uint8_t* value_11 = &(bytes[2]);
|
|
(*value_11) = (uint8_t)(((uint32_t)(128u | ((uint32_t)((value >> 6)) & 63u))));
|
|
uint8_t* value_12 = &(bytes[3]);
|
|
(*value_12) = (uint8_t)(((uint32_t)(128u | ((uint32_t)value & 63u))));
|
|
count = 4;
|
|
}
|
|
}
|
|
{
|
|
int32_t range_first_82 = 0;
|
|
int32_t range_last_82 = (int32_t)((uint32_t)count - 1u);
|
|
int32_t index = range_first_82;
|
|
if (index <= range_last_82) for (;;) {
|
|
uint8_t byte = (bytes)[index];
|
|
if (remaining > 0) {
|
|
if (((uint8_t)((uint32_t)byte & 192u)) != 128) {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
scalar = (uint32_t)(((uint32_t)scalar << 6) | (uint32_t)(((uint8_t)((uint32_t)byte & 63u))));
|
|
remaining = (int32_t)((uint32_t)remaining - 1u);
|
|
if (remaining == 0 && (scalar < minimum || scalar > 1114111 || (scalar >= 55296 && scalar <= 57343))) {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
} else if (byte >= 128) {
|
|
if (byte >= 194 && byte <= 223) {
|
|
remaining = 1;
|
|
scalar = (uint32_t)(((uint8_t)((uint32_t)byte & 31u)));
|
|
minimum = 128;
|
|
} else if (byte >= 224 && byte <= 239) {
|
|
remaining = 2;
|
|
scalar = (uint32_t)(((uint8_t)((uint32_t)byte & 15u)));
|
|
minimum = 2048;
|
|
} else if (byte >= 240 && byte <= 244) {
|
|
remaining = 3;
|
|
scalar = (uint32_t)(((uint8_t)((uint32_t)byte & 7u)));
|
|
minimum = 65536;
|
|
} else {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
}
|
|
if (used >= (int64_t)(output).length) {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
uint8_t* value_13 = &(((uint8_t *)output.data)[SliceIndex(output, (int64_t)used)]);
|
|
(*value_13) = byte;
|
|
used = (int64_t)((uint64_t)used + UINT64_C(1));
|
|
if (index == range_last_82) {
|
|
break;
|
|
}
|
|
index = (int32_t)((uint32_t)index + 1u);
|
|
}
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
if (remaining != 0) {
|
|
return (DecodedLiteral){.valid = false, .count = 0LL};
|
|
}
|
|
return (DecodedLiteral){.valid = true, .count = used};
|
|
}
|
|
|
|
FormatStep
|
|
compiler_text_NextPrintPiece(Slice bytes, int64_t at, Slice output)
|
|
{
|
|
if (at < 0LL || at > (int64_t)(bytes).length) {
|
|
return (FormatStep){.valid = false, .argument = false, .present = false, .next = at};
|
|
}
|
|
if (at == (int64_t)(bytes).length) {
|
|
return (FormatStep){.valid = true, .argument = false, .present = false, .next = at};
|
|
}
|
|
if (((uint8_t *)bytes.data)[SliceIndex(bytes, (int64_t)at)] == 37 && ((int64_t)((uint64_t)at + UINT64_C(1)) >= (int64_t)(bytes).length || ((uint8_t *)bytes.data)[SliceIndex(bytes, (int64_t)((int64_t)((uint64_t)at + UINT64_C(1))))] != 37)) {
|
|
if ((int64_t)(output).length > 0LL) {
|
|
uint8_t* value_0 = &(((uint8_t *)output.data)[0]);
|
|
(*value_0) = 0;
|
|
}
|
|
return (FormatStep){.valid = true, .argument = true, .present = true, .next = (int64_t)((uint64_t)at + UINT64_C(1))};
|
|
}
|
|
if ((int64_t)(output).length < 3LL) {
|
|
return (FormatStep){.valid = false, .argument = false, .present = false, .next = at};
|
|
}
|
|
uint8_t* value_1 = &(((uint8_t *)output.data)[0]);
|
|
(*value_1) = 34;
|
|
int64_t used = 1LL;
|
|
while (at < (int64_t)(bytes).length) {
|
|
uint8_t byte = ((uint8_t *)bytes.data)[SliceIndex(bytes, (int64_t)at)];
|
|
if (byte == 37) {
|
|
if ((int64_t)((uint64_t)at + UINT64_C(1)) >= (int64_t)(bytes).length || ((uint8_t *)bytes.data)[SliceIndex(bytes, (int64_t)((int64_t)((uint64_t)at + UINT64_C(1))))] != 37) {
|
|
break;
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
uint8_t escaped[4] = {0};
|
|
Slice value_2 = {escaped, 4};
|
|
int64_t count = compiler_text_EscapeByte(byte, false, value_2);
|
|
if ((int64_t)(((uint64_t)used + (uint64_t)count) + UINT64_C(2)) > (int64_t)(output).length) {
|
|
return (FormatStep){.valid = false, .argument = false, .present = false, .next = at};
|
|
}
|
|
{
|
|
int64_t range_first_30 = 0LL;
|
|
int64_t range_last_30 = (int64_t)((uint64_t)count - UINT64_C(1));
|
|
int64_t index = range_first_30;
|
|
if (index <= range_last_30) for (;;) {
|
|
uint8_t* value_3 = &(((uint8_t *)output.data)[SliceIndex(output, (int64_t)used)]);
|
|
(*value_3) = (escaped)[index];
|
|
used = (int64_t)((uint64_t)used + UINT64_C(1));
|
|
if (index == range_last_30) {
|
|
break;
|
|
}
|
|
index = (int64_t)((uint64_t)index + UINT64_C(1));
|
|
}
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
uint8_t* value_4 = &(((uint8_t *)output.data)[SliceIndex(output, (int64_t)used)]);
|
|
(*value_4) = 34;
|
|
uint8_t* value_5 = &(((uint8_t *)output.data)[SliceIndex(output, (int64_t)((int64_t)((uint64_t)used + UINT64_C(1))))]);
|
|
(*value_5) = 0;
|
|
return (FormatStep){.valid = true, .argument = false, .present = true, .next = at};
|
|
}
|
|
|
|
String
|
|
compiler_text_OperatorProcedure(String op)
|
|
{
|
|
for (int64_t index = 0LL; index <= 15LL; index++) {
|
|
if (StringEqual(op, (Operators)[index])) {
|
|
return (Procedures)[index];
|
|
}
|
|
}
|
|
return StringLiteral("");
|
|
}
|
|
|
|
int64_t
|
|
compiler_text_OperatorTokenLength(String source)
|
|
{
|
|
for (int64_t index = 0LL; index <= 15LL; index++) {
|
|
String op = (Operators)[index];
|
|
if ((int64_t)(source).length >= (int64_t)(op).length && StringEqual(StringRange(source, (int64_t)0LL, (int64_t)(int64_t)(op).length), op)) {
|
|
return (int64_t)(op).length;
|
|
}
|
|
}
|
|
return 0LL;
|
|
}
|
|
|
|
String
|
|
compiler_text_ProcedureOperator(String name)
|
|
{
|
|
for (int64_t index = 0LL; index <= 15LL; index++) {
|
|
if (StringEqual(name, (Procedures)[index])) {
|
|
return (Operators)[index];
|
|
}
|
|
}
|
|
return StringLiteral("");
|
|
}
|
|
|
|
int32_t
|
|
compiler_text_HexDigit(uint8_t byte)
|
|
{
|
|
if (byte >= 48 && byte <= 57) {
|
|
return (int32_t)((uint32_t)(byte) - 48u);
|
|
}
|
|
if (byte >= 97 && byte <= 102) {
|
|
return (int32_t)(((uint32_t)(byte) - 97u) + 10u);
|
|
}
|
|
if (byte >= 65 && byte <= 70) {
|
|
return (int32_t)(((uint32_t)(byte) - 65u) + 10u);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static uint8_t
|
|
compiler_text_Byte(String source, int64_t at)
|
|
{
|
|
if (at < 0LL || at >= (int64_t)(source).length) {
|
|
return 0;
|
|
}
|
|
return (uint8_t)source.data[at];
|
|
}
|
|
|
|
static int64_t
|
|
compiler_text_AfterQuote(String source, int64_t at)
|
|
{
|
|
uint8_t quote = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
while (at < (int64_t)(source).length) {
|
|
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
if (byte == quote) {
|
|
break;
|
|
}
|
|
if (byte == 92 && at < (int64_t)(source).length) {
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
}
|
|
return at;
|
|
}
|
|
|
|
static int64_t
|
|
compiler_text_EscapeByte(uint8_t byte, bool c_string, Slice output)
|
|
{
|
|
uint8_t* value_0 = &(((uint8_t *)output.data)[0]);
|
|
(*value_0) = 92;
|
|
if (byte == 34 || byte == 92) {
|
|
uint8_t* value_1 = &(((uint8_t *)output.data)[1]);
|
|
(*value_1) = byte;
|
|
return 2LL;
|
|
}
|
|
if (byte == 10) {
|
|
uint8_t* value_2 = &(((uint8_t *)output.data)[1]);
|
|
(*value_2) = 110;
|
|
return 2LL;
|
|
}
|
|
if (byte == 9) {
|
|
uint8_t* value_3 = &(((uint8_t *)output.data)[1]);
|
|
(*value_3) = 116;
|
|
return 2LL;
|
|
}
|
|
if (byte == 13 && c_string) {
|
|
uint8_t* value_4 = &(((uint8_t *)output.data)[1]);
|
|
(*value_4) = 114;
|
|
return 2LL;
|
|
}
|
|
if (byte < 32 || byte == 127) {
|
|
String hex = StringLiteral("0123456789abcdef");
|
|
uint8_t* value_5 = &(((uint8_t *)output.data)[1]);
|
|
(*value_5) = 120;
|
|
uint8_t* value_6 = &(((uint8_t *)output.data)[2]);
|
|
(*value_6) = (uint8_t)ZIRAN_INDEX(hex.data, hex.length, ((uint8_t)(byte >> 4)));
|
|
uint8_t* value_7 = &(((uint8_t *)output.data)[3]);
|
|
(*value_7) = (uint8_t)ZIRAN_INDEX(hex.data, hex.length, ((uint8_t)((uint32_t)byte & 15u)));
|
|
return 4LL;
|
|
}
|
|
uint8_t* value_8 = &(((uint8_t *)output.data)[0]);
|
|
(*value_8) = byte;
|
|
return 1LL;
|
|
}
|