396 lines
18 KiB
C
396 lines
18 KiB
C
/* Generated by zi2c from compiler_expression.zi. */
|
|
#define ZIRAN_COMPILER_EXPRESSION_H_SOURCE 1
|
|
#include <stdbool.h>
|
|
#include "zir_bounds.h"
|
|
#include "zir_string.h"
|
|
#include "compiler_expression.h"
|
|
#ifndef ZIRAN_MAYBE_UNUSED
|
|
#if defined(__GNUC__) || defined(__clang__)
|
|
#define ZIRAN_MAYBE_UNUSED __attribute__((unused))
|
|
#else
|
|
#define ZIRAN_MAYBE_UNUSED
|
|
#endif
|
|
#endif
|
|
static String compiler_expression_Trim(String source) ZIRAN_MAYBE_UNUSED;
|
|
static String compiler_expression_SkipSpace(String source) ZIRAN_MAYBE_UNUSED;
|
|
static int64_t compiler_expression_FindByte(String source, uint8_t wanted) ZIRAN_MAYBE_UNUSED;
|
|
static int64_t compiler_expression_FindPair(String source, String wanted) ZIRAN_MAYBE_UNUSED;
|
|
static bool compiler_expression_WordAt(String source, int64_t at, String word) ZIRAN_MAYBE_UNUSED;
|
|
static bool compiler_expression_AssignmentOperator(String operator) ZIRAN_MAYBE_UNUSED;
|
|
|
|
int32_t
|
|
compiler_expression_BinaryPrecedence(String operator)
|
|
{
|
|
if (StringEqual(operator, StringLiteral("||"))) {
|
|
return 2;
|
|
}
|
|
if (StringEqual(operator, StringLiteral("&&"))) {
|
|
return 3;
|
|
}
|
|
if (StringEqual(operator, StringLiteral("|"))) {
|
|
return 4;
|
|
}
|
|
if (StringEqual(operator, StringLiteral("^"))) {
|
|
return 5;
|
|
}
|
|
if (StringEqual(operator, StringLiteral("&"))) {
|
|
return 6;
|
|
}
|
|
if (StringEqual(operator, StringLiteral("==")) || StringEqual(operator, StringLiteral("!="))) {
|
|
return 7;
|
|
}
|
|
bool value_0 = StringEqual(operator, StringLiteral("<")) || StringEqual(operator, StringLiteral("<=")) || StringEqual(operator, StringLiteral(">"));
|
|
if (value_0 || StringEqual(operator, StringLiteral(">="))) {
|
|
return 8;
|
|
}
|
|
if (StringEqual(operator, StringLiteral("<<")) || StringEqual(operator, StringLiteral(">>"))) {
|
|
return 9;
|
|
}
|
|
if (StringEqual(operator, StringLiteral("+")) || StringEqual(operator, StringLiteral("-"))) {
|
|
return 10;
|
|
}
|
|
if (StringEqual(operator, StringLiteral("*")) || StringEqual(operator, StringLiteral("/")) || StringEqual(operator, StringLiteral("%"))) {
|
|
return 11;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int32_t
|
|
compiler_expression_CharacterByte(String literal)
|
|
{
|
|
bool value_0 = (int64_t)(literal).length == 3LL && (uint8_t)ZIRAN_INDEX(literal.data, literal.length, 0) == 34 && (uint8_t)ZIRAN_INDEX(literal.data, literal.length, 2) == 34;
|
|
if (value_0 && (uint8_t)ZIRAN_INDEX(literal.data, literal.length, 1) < 128) {
|
|
return (int32_t)((uint8_t)literal.data[1]);
|
|
}
|
|
bool value_1 = (int64_t)(literal).length == 4LL && (uint8_t)ZIRAN_INDEX(literal.data, literal.length, 0) == 34 && (uint8_t)ZIRAN_INDEX(literal.data, literal.length, 1) == 92;
|
|
if (value_1 && (uint8_t)ZIRAN_INDEX(literal.data, literal.length, 3) == 34) {
|
|
uint8_t escape = (uint8_t)literal.data[2];
|
|
if (escape == 48) {
|
|
return 0;
|
|
}
|
|
if (escape == 110) {
|
|
return 10;
|
|
}
|
|
if (escape == 114) {
|
|
return 13;
|
|
}
|
|
if (escape == 116) {
|
|
return 9;
|
|
}
|
|
if (escape == 34) {
|
|
return 34;
|
|
}
|
|
if (escape == 92) {
|
|
return 92;
|
|
}
|
|
return -1;
|
|
}
|
|
bool value_2 = (int64_t)(literal).length == 6LL && (uint8_t)ZIRAN_INDEX(literal.data, literal.length, 0) == 34 && (uint8_t)ZIRAN_INDEX(literal.data, literal.length, 1) == 92;
|
|
if (value_2 && (uint8_t)ZIRAN_INDEX(literal.data, literal.length, 2) == 120 && (uint8_t)ZIRAN_INDEX(literal.data, literal.length, 5) == 34) {
|
|
int32_t high = compiler_text_HexDigit((uint8_t)literal.data[3]);
|
|
int32_t low = compiler_text_HexDigit((uint8_t)literal.data[4]);
|
|
if (high >= 0 && low >= 0) {
|
|
return (int32_t)(((uint32_t)high * 16u) + (uint32_t)low);
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
StatementExpression
|
|
compiler_expression_StatementParts(String source, StatementKind kind)
|
|
{
|
|
StatementExpression result = {0};
|
|
bool* value_0 = &(result.is_else);
|
|
bool value_1 = kind == ((StatementKind)(6));
|
|
if (value_1) {
|
|
String value_2 = source;
|
|
String value_3 = StringLiteral("else");
|
|
bool value_4 = false;
|
|
{
|
|
String source = value_2;
|
|
int64_t at = 0LL;
|
|
String word = value_3;
|
|
bool value_6 = at >= 0LL && (int64_t)(word).length <= (int64_t)((uint64_t)((int64_t)(source).length) - (uint64_t)at);
|
|
bool value_7 = value_6 && StringEqual(StringRange(source, (int64_t)at, (int64_t)((int64_t)((uint64_t)at + (uint64_t)((int64_t)(word).length)))), word);
|
|
bool value_8 = value_7;
|
|
value_4 = value_8 && ((int64_t)((uint64_t)at + (uint64_t)((int64_t)(word).length)) == (int64_t)(source).length || compiler_text_SpaceByte((uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)at + (uint64_t)((int64_t)(word).length))))));
|
|
}
|
|
value_1 = value_4;
|
|
}
|
|
(*value_0) = value_1;
|
|
if (kind == ((StatementKind)(3))) {
|
|
int64_t colon = compiler_expression_FindByte(source, 58);
|
|
if (colon >= 0LL) {
|
|
bool* value_9 = &(result.has_declaration);
|
|
(*value_9) = true;
|
|
String* value_10 = &(result.name);
|
|
String value_11 = StringRange(source, (int64_t)0LL, (int64_t)colon);
|
|
String value_12 = StringView(NULL, 0);
|
|
{
|
|
String source = value_11;
|
|
SourceRange range = compiler_text_TrimRange(source);
|
|
value_12 = StringRange(source, (int64_t)range.begin, (int64_t)range.end);
|
|
}
|
|
(*value_10) = value_12;
|
|
String type = StringRange(source, (int64_t)((int64_t)((uint64_t)colon + UINT64_C(1))), (int64_t)source.length);
|
|
int64_t equals = compiler_expression_FindByte(type, 61);
|
|
if (equals >= 0LL) {
|
|
bool* value_14 = &(result.has_value);
|
|
(*value_14) = true;
|
|
String* value_15 = &(result.value);
|
|
(*value_15) = StringRange(type, (int64_t)((int64_t)((uint64_t)equals + UINT64_C(1))), (int64_t)type.length);
|
|
type = StringRange(type, (int64_t)0LL, (int64_t)equals);
|
|
}
|
|
String value_16 = StringView(NULL, 0);
|
|
{
|
|
String source = type;
|
|
SourceRange range = compiler_text_TrimRange(source);
|
|
value_16 = StringRange(source, (int64_t)range.begin, (int64_t)range.end);
|
|
}
|
|
type = value_16;
|
|
if ((int64_t)(type).length > 0LL && (uint8_t)ZIRAN_INDEX(type.data, type.length, ((int64_t)((uint64_t)((int64_t)(type).length) - UINT64_C(1)))) == 59) {
|
|
String value_18 = StringRange(type, (int64_t)0LL, (int64_t)((int64_t)((uint64_t)((int64_t)(type).length) - UINT64_C(1))));
|
|
String value_19 = StringView(NULL, 0);
|
|
{
|
|
String source = value_18;
|
|
SourceRange range = compiler_text_TrimRange(source);
|
|
value_19 = StringRange(source, (int64_t)range.begin, (int64_t)range.end);
|
|
}
|
|
type = value_19;
|
|
}
|
|
String* value_21 = &(result.type);
|
|
(*value_21) = type;
|
|
int64_t modifier = compiler_expression_FindByte(type, 35);
|
|
if (modifier >= 0LL) {
|
|
StatementExpressionError* value_22 = &(result.error);
|
|
(*value_22) = StatementExpressionError_Modifier;
|
|
String* value_23 = &(result.modifier);
|
|
(*value_23) = StringRange(type, (int64_t)modifier, (int64_t)type.length);
|
|
return result;
|
|
}
|
|
}
|
|
} else if (kind == ((StatementKind)(4))) {
|
|
int64_t at = 0LL;
|
|
while (at < (int64_t)(source).length) {
|
|
Scan token = compiler_scan_NextToken(source, at, 1, 1);
|
|
if (((TokenKind)(token.kind)) == ((TokenKind)(0))) {
|
|
break;
|
|
}
|
|
String operator = StringRange(source, (int64_t)token.begin, (int64_t)token.end);
|
|
String value_24 = operator;
|
|
bool value_25 = false;
|
|
{
|
|
String operator = value_24;
|
|
bool value_27 = StringEqual(operator, StringLiteral("=")) || StringEqual(operator, StringLiteral("+=")) || StringEqual(operator, StringLiteral("-="));
|
|
bool value_28 = value_27 || StringEqual(operator, StringLiteral("*=")) || StringEqual(operator, StringLiteral("/="));
|
|
bool value_29 = value_28 || StringEqual(operator, StringLiteral("%=")) || StringEqual(operator, StringLiteral("&="));
|
|
bool value_30 = value_29 || StringEqual(operator, StringLiteral("|=")) || StringEqual(operator, StringLiteral("^="));
|
|
value_25 = value_30 || StringEqual(operator, StringLiteral("<<=")) || StringEqual(operator, StringLiteral(">>="));
|
|
}
|
|
if (value_25) {
|
|
String* value_31 = &(result.assignment);
|
|
(*value_31) = operator;
|
|
String* value_32 = &(result.lhs);
|
|
(*value_32) = StringRange(source, (int64_t)0LL, (int64_t)token.begin);
|
|
String* value_33 = &(result.value);
|
|
(*value_33) = StringRange(source, (int64_t)token.end, (int64_t)source.length);
|
|
bool* value_34 = &(result.has_value);
|
|
(*value_34) = true;
|
|
break;
|
|
}
|
|
at = token.end;
|
|
}
|
|
} else if (kind == ((StatementKind)(10)) || kind == ((StatementKind)(14))) {
|
|
String keyword = StringLiteral("return");
|
|
if (kind == ((StatementKind)(14))) {
|
|
keyword = StringLiteral("unused");
|
|
}
|
|
if ((int64_t)(source).length >= (int64_t)(keyword).length) {
|
|
String* value_35 = &(result.value);
|
|
(*value_35) = StringRange(source, (int64_t)(int64_t)(keyword).length, (int64_t)source.length);
|
|
bool* value_36 = &(result.has_value);
|
|
(*value_36) = true;
|
|
}
|
|
} else if (kind == ((StatementKind)(5))) {
|
|
String* value_37 = &(result.value);
|
|
(*value_37) = source;
|
|
bool* value_38 = &(result.has_value);
|
|
(*value_38) = true;
|
|
} else if (kind == ((StatementKind)(16))) {
|
|
if ((int64_t)(source).length >= 2LL) {
|
|
String value_39 = StringRange(source, (int64_t)2LL, (int64_t)source.length);
|
|
String value_40 = StringView(NULL, 0);
|
|
{
|
|
String source = value_39;
|
|
String value_42 = source;
|
|
SourceRange value_43 = compiler_text_TrimRange(source);
|
|
value_40 = StringRange(value_42, (int64_t)value_43.begin, (int64_t)value_42.length);
|
|
}
|
|
String condition = value_40;
|
|
bool value_44 = (int64_t)(condition).length > 9LL && StringEqual(StringRange(condition, (int64_t)0LL, (int64_t)9LL), StringLiteral("#complete"));
|
|
bool value_45 = value_44;
|
|
if (value_45 && compiler_text_SpaceByte((uint8_t)ZIRAN_INDEX(condition.data, condition.length, 9))) {
|
|
String value_46 = StringRange(condition, (int64_t)9LL, (int64_t)condition.length);
|
|
String value_47 = StringView(NULL, 0);
|
|
{
|
|
String source = value_46;
|
|
String value_49 = source;
|
|
SourceRange value_50 = compiler_text_TrimRange(source);
|
|
value_47 = StringRange(value_49, (int64_t)value_50.begin, (int64_t)value_49.length);
|
|
}
|
|
condition = value_47;
|
|
}
|
|
int64_t equals = compiler_expression_FindPair(condition, StringLiteral("=="));
|
|
if (equals >= 0LL) {
|
|
String value_51 = StringRange(condition, (int64_t)0LL, (int64_t)equals);
|
|
String value_52 = StringView(NULL, 0);
|
|
{
|
|
String source = value_51;
|
|
SourceRange range = compiler_text_TrimRange(source);
|
|
value_52 = StringRange(source, (int64_t)range.begin, (int64_t)range.end);
|
|
}
|
|
condition = value_52;
|
|
}
|
|
String* value_54 = &(result.value);
|
|
(*value_54) = condition;
|
|
bool* value_55 = &(result.has_value);
|
|
(*value_55) = true;
|
|
}
|
|
} else if (kind == ((StatementKind)(7)) || kind == ((StatementKind)(6))) {
|
|
String value_56 = source;
|
|
int64_t value_57 = compiler_text_BlockHeaderEnd(source);
|
|
String condition = StringRange(value_56, (int64_t)0LL, (int64_t)value_57);
|
|
if ((int64_t)(condition).length >= 4LL && StringEqual(StringRange(condition, (int64_t)0LL, (int64_t)4LL), StringLiteral("else"))) {
|
|
String value_58 = StringRange(condition, (int64_t)4LL, (int64_t)condition.length);
|
|
String value_59 = StringView(NULL, 0);
|
|
{
|
|
String source = value_58;
|
|
String value_61 = source;
|
|
SourceRange value_62 = compiler_text_TrimRange(source);
|
|
value_59 = StringRange(value_61, (int64_t)value_62.begin, (int64_t)value_61.length);
|
|
}
|
|
condition = value_59;
|
|
}
|
|
int64_t at = 0LL;
|
|
while (true) {
|
|
bool value_63 = at < (int64_t)(condition).length;
|
|
if (value_63) {
|
|
bool value_64 = compiler_text_SpaceByte((uint8_t)ZIRAN_INDEX(condition.data, condition.length, at));
|
|
value_63 = !value_64;
|
|
}
|
|
if (!(value_63 && (uint8_t)ZIRAN_INDEX(condition.data, condition.length, at) != 40)) { break; }
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
String* value_65 = &(result.value);
|
|
(*value_65) = StringRange(condition, (int64_t)at, (int64_t)condition.length);
|
|
bool* value_66 = &(result.has_value);
|
|
(*value_66) = true;
|
|
}
|
|
bool value_67 = result.has_value;
|
|
if (value_67) {
|
|
String value_68 = result.value;
|
|
String value_69 = StringView(NULL, 0);
|
|
{
|
|
String source = value_68;
|
|
String value_71 = source;
|
|
SourceRange value_72 = compiler_text_TrimRange(source);
|
|
value_69 = StringRange(value_71, (int64_t)value_72.begin, (int64_t)value_71.length);
|
|
}
|
|
value_67 = (StringEqual(value_69, StringLiteral(";")));
|
|
}
|
|
if (value_67) {
|
|
bool* value_73 = &(result.has_value);
|
|
(*value_73) = false;
|
|
}
|
|
if (result.has_value && kind == ((StatementKind)(3)) && (int64_t)(result.type).length > 0LL && (uint8_t)ZIRAN_INDEX(result.type.data, result.type.length, 0) == 91) {
|
|
String value_74 = result.value;
|
|
String value_75 = StringView(NULL, 0);
|
|
{
|
|
String source = value_74;
|
|
String value_77 = source;
|
|
SourceRange value_78 = compiler_text_TrimRange(source);
|
|
value_75 = StringRange(value_77, (int64_t)value_78.begin, (int64_t)value_77.length);
|
|
}
|
|
String value = value_75;
|
|
if ((int64_t)(value).length > 0LL && (uint8_t)ZIRAN_INDEX(value.data, value.length, 0) == 123) {
|
|
StatementExpressionError* value_79 = &(result.error);
|
|
(*value_79) = StatementExpressionError_ArrayLiteral;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static String
|
|
compiler_expression_Trim(String source)
|
|
{
|
|
SourceRange range = compiler_text_TrimRange(source);
|
|
return StringRange(source, (int64_t)range.begin, (int64_t)range.end);
|
|
}
|
|
|
|
static String
|
|
compiler_expression_SkipSpace(String source)
|
|
{
|
|
String value_0 = source;
|
|
SourceRange value_1 = compiler_text_TrimRange(source);
|
|
return StringRange(value_0, (int64_t)value_1.begin, (int64_t)value_0.length);
|
|
}
|
|
|
|
static int64_t
|
|
compiler_expression_FindByte(String source, uint8_t wanted)
|
|
{
|
|
{
|
|
int64_t range_first_0 = 0LL;
|
|
int64_t range_last_0 = (int64_t)((uint64_t)((int64_t)(source).length) - UINT64_C(1));
|
|
int64_t at = range_first_0;
|
|
if (at <= range_last_0) for (;;) {
|
|
if ((uint8_t)source.data[at] == wanted) {
|
|
return at;
|
|
}
|
|
if (at == range_last_0) {
|
|
break;
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
}
|
|
return -1LL;
|
|
}
|
|
|
|
static int64_t
|
|
compiler_expression_FindPair(String source, String wanted)
|
|
{
|
|
{
|
|
int64_t range_first_0 = 0LL;
|
|
int64_t range_last_0 = (int64_t)((uint64_t)((int64_t)(source).length) - UINT64_C(2));
|
|
int64_t at = range_first_0;
|
|
if (at <= range_last_0) for (;;) {
|
|
if (StringEqual(StringRange(source, (int64_t)at, (int64_t)((int64_t)((uint64_t)at + UINT64_C(2)))), wanted)) {
|
|
return at;
|
|
}
|
|
if (at == range_last_0) {
|
|
break;
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
}
|
|
return -1LL;
|
|
}
|
|
|
|
static bool
|
|
compiler_expression_WordAt(String source, int64_t at, String word)
|
|
{
|
|
bool value_0 = at >= 0LL && (int64_t)(word).length <= (int64_t)((uint64_t)((int64_t)(source).length) - (uint64_t)at);
|
|
bool value_1 = value_0 && StringEqual(StringRange(source, (int64_t)at, (int64_t)((int64_t)((uint64_t)at + (uint64_t)((int64_t)(word).length)))), word);
|
|
bool value_2 = value_1;
|
|
return value_2 && ((int64_t)((uint64_t)at + (uint64_t)((int64_t)(word).length)) == (int64_t)(source).length || compiler_text_SpaceByte((uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)at + (uint64_t)((int64_t)(word).length))))));
|
|
}
|
|
|
|
static bool
|
|
compiler_expression_AssignmentOperator(String operator)
|
|
{
|
|
bool value_0 = StringEqual(operator, StringLiteral("=")) || StringEqual(operator, StringLiteral("+=")) || StringEqual(operator, StringLiteral("-="));
|
|
bool value_1 = value_0 || StringEqual(operator, StringLiteral("*=")) || StringEqual(operator, StringLiteral("/="));
|
|
bool value_2 = value_1 || StringEqual(operator, StringLiteral("%=")) || StringEqual(operator, StringLiteral("&="));
|
|
bool value_3 = value_2 || StringEqual(operator, StringLiteral("|=")) || StringEqual(operator, StringLiteral("^="));
|
|
return value_3 || StringEqual(operator, StringLiteral("<<=")) || StringEqual(operator, StringLiteral(">>="));
|
|
}
|