change in literals, literal types

This commit is contained in:
ProgramSnail 2023-08-12 15:55:33 +03:00
parent 17ff590048
commit 43dfa75b74
10 changed files with 168 additions and 39 deletions

@ -1 +1 @@
Subproject commit 8e0cea277edec400aefe29ed7aca9037348b9806 Subproject commit 17a67d236113bf22fa516c08d2872f35b06dae9c

View file

@ -59,8 +59,10 @@
<RegExpr String="(\@|\:|(\?\?)|(\!\!)|(\=\>)|(\!\!\=\>))(?![a\+\\\-\*/%\^\!\?\|&amp;,&lt;>=\.])" attribute="Control Flow" context="#stay"/> <RegExpr String="(\@|\:|(\?\?)|(\!\!)|(\=\>)|(\!\!\=\>))(?![a\+\\\-\*/%\^\!\?\|&amp;,&lt;>=\.])" attribute="Control Flow" context="#stay"/>
<RegExpr String="\b[0-9]+\.[0-9]+\b" attribute="Float" context="#stay"/> <RegExpr String="\b[0-9]+\.[0-9]+f?\b" attribute="Float" context="#stay"/>
<RegExpr String="''([^\\\/]|(\\.))''" attribute="Character" context="#stay"/> <RegExpr String="''([^\\\/]|(\\.))''u?" attribute="Character" context="#stay"/>
<RegExpr String="\b[0-9]+[il]?\b" attribute="Decimal" context="#stay"/>
<RegExpr String="&quot;([^\\&quot;]|(\\.))*&quot;u?" attribute="String" context="#stay" />
<RegExpr String="((?&lt;![\+\\\-\*/%\^\!\?\|&amp;,&lt;>=\.])((\.+)|([\+\\\-\*/%\^\!\?\|&amp;,&lt;>=]+\.?\.?\.?)))|(\|)" attribute="Operator" context="#stay"/> <RegExpr String="((?&lt;![\+\\\-\*/%\^\!\?\|&amp;,&lt;>=\.])((\.+)|([\+\\\-\*/%\^\!\?\|&amp;,&lt;>=]+\.?\.?\.?)))|(\|)" attribute="Operator" context="#stay"/>
@ -72,9 +74,6 @@
<RegExpr String="([a-z_][a-z0-9_]*\.)*#[A-Z][a-zA-Z0-9]*(?![a-zA-Z0-9])" attribute="Typeclass" context="#stay"/> <RegExpr String="([a-z_][a-z0-9_]*\.)*#[A-Z][a-zA-Z0-9]*(?![a-zA-Z0-9])" attribute="Typeclass" context="#stay"/>
<RegExpr String="\b[0-9]+\b" attribute="Decimal" context="#stay"/>
<RegExpr String="&quot;([^\\&quot;]|(\\.))*&quot;" attribute="String" context="#stay" />
<DetectChar char="(" attribute="Symbol" context="#stay" beginRegion="OpenBracket"/> <DetectChar char="(" attribute="Symbol" context="#stay" beginRegion="OpenBracket"/>
<DetectChar char=")" attribute="Symbol" context="#stay" endRegion="OpenBracket"/> <DetectChar char=")" attribute="Symbol" context="#stay" endRegion="OpenBracket"/>

View file

@ -12,14 +12,25 @@ nodes::Node build_node(parser::ParseTree::Node parser_node);
// --- literals // --- literals
nodes::Literal build_float_number_literal(parser::ParseTree::Node parser_node); nodes::Literal build_float_literal(parser::ParseTree::Node parser_node);
nodes::Literal build_number_literal(parser::ParseTree::Node parser_node); nodes::Literal build_double_literal(parser::ParseTree::Node parser_node);
nodes::Literal build_int_literal(parser::ParseTree::Node parser_node);
nodes::Literal build_long_literal(parser::ParseTree::Node parser_node);
nodes::Literal build_index_literal(parser::ParseTree::Node parser_node);
nodes::Literal build_string_literal(parser::ParseTree::Node parser_node); nodes::Literal build_string_literal(parser::ParseTree::Node parser_node);
nodes::Literal
build_unicode_string_literal(parser::ParseTree::Node parser_node);
nodes::Literal build_char_literal(parser::ParseTree::Node parser_node); nodes::Literal build_char_literal(parser::ParseTree::Node parser_node);
nodes::Literal build_unicode_literal(parser::ParseTree::Node parser_node);
nodes::Literal build_bool_literal(parser::ParseTree::Node parser_node); nodes::Literal build_bool_literal(parser::ParseTree::Node parser_node);
nodes::Literal build_unit_literal(parser::ParseTree::Node parser_node); nodes::Literal build_unit_literal(parser::ParseTree::Node parser_node);

View file

@ -79,6 +79,14 @@ protected:
struct unit {}; struct unit {};
struct null {}; struct null {};
struct unicode_string {
std::string str;
};
struct unicode {
std::string ch;
};
class Literal : public Node { class Literal : public Node {
public: public:
template <typename T> template <typename T>
@ -103,7 +111,9 @@ public:
auto get_any() const { return &value_; } auto get_any() const { return &value_; }
private: private:
std::variant<double, long long, std::string, char, bool, unit, null> value_; std::variant<float, double, int32_t, int64_t, size_t, std::string,
unicode_string, char, unicode, bool, unit, null>
value_;
}; };
class Identifier : public Node { class Identifier : public Node {

View file

@ -83,10 +83,15 @@ enum class Type {
OPERATOR_TAIL2, OPERATOR_TAIL2,
OPERATOR_TAIL3, OPERATOR_TAIL3,
FLOAT_NUMBER_LITERAL, FLOAT_LITERAL,
NUMBER_LITERAL, DOUBLE_LITERAL,
INT_LITERAL,
LONG_LITERAL,
INDEX_LITERAL,
STRING_LITERAL, STRING_LITERAL,
UNICODE_STRING_LITERAL,
CHAR_LITERAL, CHAR_LITERAL,
UNICODE_LITERAL,
BOOL_LITERAL, BOOL_LITERAL,
UNIT_LITERAL, UNIT_LITERAL,
NULL_LITERAL, NULL_LITERAL,
@ -173,10 +178,15 @@ const static std::string OPERATOR_TAIL1 = "operator_tail1";
const static std::string OPERATOR_TAIL2 = "operator_tail2"; const static std::string OPERATOR_TAIL2 = "operator_tail2";
const static std::string OPERATOR_TAIL3 = "operator_tail3"; const static std::string OPERATOR_TAIL3 = "operator_tail3";
const static std::string FLOAT_NUMBER_LITERAL = "float_number_literal"; const static std::string FLOAT_LITERAL = "float_literal";
const static std::string NUMBER_LITERAL = "number_literal"; const static std::string DOUBLE_LITERAL = "double_literal";
const static std::string INT_LITERAL = "int_literal";
const static std::string LONG_LITERAL = "long_literal";
const static std::string INDEX_LITERAL = "index_literal";
const static std::string STRING_LITERAL = "string_literal"; const static std::string STRING_LITERAL = "string_literal";
const static std::string UNICODE_STRING_LITERAL = "unicode_string_literal";
const static std::string CHAR_LITERAL = "char_literal"; const static std::string CHAR_LITERAL = "char_literal";
const static std::string UNICODE_LITERAL = "unicode_literal";
const static std::string BOOL_LITERAL = "bool_literal"; const static std::string BOOL_LITERAL = "bool_literal";
const static std::string UNIT_LITERAL = "unit_literal"; const static std::string UNIT_LITERAL = "unit_literal";
const static std::string NULL_LITERAL = "null_literal"; const static std::string NULL_LITERAL = "null_literal";
@ -270,14 +280,24 @@ inline Type string_to_type(const std::string &str) {
return Type::OPERATOR_TAIL2; return Type::OPERATOR_TAIL2;
} else if (str == OPERATOR_TAIL3) { } else if (str == OPERATOR_TAIL3) {
return Type::OPERATOR_TAIL3; return Type::OPERATOR_TAIL3;
} else if (str == FLOAT_NUMBER_LITERAL) { } else if (str == FLOAT_LITERAL) {
return Type::FLOAT_NUMBER_LITERAL; return Type::FLOAT_LITERAL;
} else if (str == NUMBER_LITERAL) { } else if (str == DOUBLE_LITERAL) {
return Type::NUMBER_LITERAL; return Type::DOUBLE_LITERAL;
} else if (str == INT_LITERAL) {
return Type::INT_LITERAL;
} else if (str == LONG_LITERAL) {
return Type::LONG_LITERAL;
} else if (str == INDEX_LITERAL) {
return Type::INDEX_LITERAL;
} else if (str == STRING_LITERAL) { } else if (str == STRING_LITERAL) {
return Type::STRING_LITERAL; return Type::STRING_LITERAL;
} else if (str == UNICODE_STRING_LITERAL) {
return Type::UNICODE_STRING_LITERAL;
} else if (str == CHAR_LITERAL) { } else if (str == CHAR_LITERAL) {
return Type::CHAR_LITERAL; return Type::CHAR_LITERAL;
} else if (str == UNICODE_LITERAL) {
return Type::UNICODE_LITERAL;
} else if (str == BOOL_LITERAL) { } else if (str == BOOL_LITERAL) {
return Type::BOOL_LITERAL; return Type::BOOL_LITERAL;
} else if (str == UNIT_LITERAL) { } else if (str == UNIT_LITERAL) {

View file

@ -88,14 +88,35 @@ nodes::Node build_node(parser::ParseTree::Node parser_node) {
// --- literals // --- literals
nodes::Literal build_float_number_literal(parser::ParseTree::Node parser_node) { nodes::Literal build_float_literal(parser::ParseTree::Node parser_node) {
std::string literal = parser_node.get_value();
return nodes::Literal(
build_node(parser_node),
std::stof(literal.substr(0, literal.size() - 1))); // remove 'f' suffix
}
nodes::Literal build_double_literal(parser::ParseTree::Node parser_node) {
return nodes::Literal(build_node(parser_node), return nodes::Literal(build_node(parser_node),
std::stod(parser_node.get_value())); std::stod(parser_node.get_value()));
} }
nodes::Literal build_number_literal(parser::ParseTree::Node parser_node) { nodes::Literal build_int_literal(parser::ParseTree::Node parser_node) {
std::string literal = parser_node.get_value();
return nodes::Literal(build_node(parser_node), return nodes::Literal(build_node(parser_node),
std::stoll(parser_node.get_value())); (int32_t)std::stoll(literal.substr(
0, literal.size() - 1))); // remove 'i' suffix
}
nodes::Literal build_long_literal(parser::ParseTree::Node parser_node) {
std::string literal = parser_node.get_value();
return nodes::Literal(build_node(parser_node),
(int64_t)std::stoll(literal.substr(
0, literal.size() - 1))); // remove 'l' suffix
}
nodes::Literal build_index_literal(parser::ParseTree::Node parser_node) {
return nodes::Literal(build_node(parser_node),
(size_t)std::stoull(parser_node.get_value()));
} }
nodes::Literal build_string_literal(parser::ParseTree::Node parser_node) { nodes::Literal build_string_literal(parser::ParseTree::Node parser_node) {
@ -131,6 +152,16 @@ nodes::Literal build_string_literal(parser::ParseTree::Node parser_node) {
return nodes::Literal(build_node(parser_node), literal); return nodes::Literal(build_node(parser_node), literal);
} }
// TODO: decode escape characters, etc.
nodes::Literal
build_unicode_string_literal(parser::ParseTree::Node parser_node) {
std::string literal = parser_node.get_value();
// remove " from both sides + 'u' ("string"u)
return nodes::Literal(build_node(parser_node),
literal.substr(1, literal.size() - 3));
}
nodes::Literal build_char_literal(parser::ParseTree::Node parser_node) { nodes::Literal build_char_literal(parser::ParseTree::Node parser_node) {
std::string literal = parser_node.get_value(); std::string literal = parser_node.get_value();
@ -154,6 +185,15 @@ nodes::Literal build_char_literal(parser::ParseTree::Node parser_node) {
return nodes::Literal(build_node(parser_node), ch); return nodes::Literal(build_node(parser_node), ch);
} }
// TODO: decode escape characters, etc.
nodes::Literal build_unicode_literal(parser::ParseTree::Node parser_node) {
std::string literal = parser_node.get_value();
// remove '' from both sides + 'u' (''x''u)
return nodes::Literal(build_node(parser_node),
nodes::unicode{literal.substr(2, literal.size() - 5)});
}
nodes::Literal build_bool_literal(parser::ParseTree::Node parser_node) { nodes::Literal build_bool_literal(parser::ParseTree::Node parser_node) {
std::string literal = parser_node.get_value(); std::string literal = parser_node.get_value();

View file

@ -116,19 +116,35 @@ build_expression(parser::ParseTree::Node parser_node,
build_lambda(parser_node, expression_storage, type_storage), build_lambda(parser_node, expression_storage, type_storage),
is_scoped)); is_scoped));
// --- literals // --- literals
case tokens::Type::FLOAT_NUMBER_LITERAL: case tokens::Type::FLOAT_LITERAL:
return expression_storage.add_expression(
nodes::Expression(build_node(parser_node),
build_float_number_literal(parser_node), is_scoped));
case tokens::Type::NUMBER_LITERAL:
return expression_storage.add_expression(nodes::Expression( return expression_storage.add_expression(nodes::Expression(
build_node(parser_node), build_number_literal(parser_node), is_scoped)); build_node(parser_node), build_float_literal(parser_node), is_scoped));
case tokens::Type::DOUBLE_LITERAL:
return expression_storage.add_expression(nodes::Expression(
build_node(parser_node), build_double_literal(parser_node), is_scoped));
case tokens::Type::INT_LITERAL:
return expression_storage.add_expression(nodes::Expression(
build_node(parser_node), build_int_literal(parser_node), is_scoped));
case tokens::Type::LONG_LITERAL:
return expression_storage.add_expression(nodes::Expression(
build_node(parser_node), build_long_literal(parser_node), is_scoped));
case tokens::Type::INDEX_LITERAL:
return expression_storage.add_expression(nodes::Expression(
build_node(parser_node), build_index_literal(parser_node), is_scoped));
case tokens::Type::STRING_LITERAL: case tokens::Type::STRING_LITERAL:
return expression_storage.add_expression(nodes::Expression( return expression_storage.add_expression(nodes::Expression(
build_node(parser_node), build_string_literal(parser_node), is_scoped)); build_node(parser_node), build_string_literal(parser_node), is_scoped));
case tokens::Type::UNICODE_STRING_LITERAL:
return expression_storage.add_expression(nodes::Expression(
build_node(parser_node), build_unicode_string_literal(parser_node),
is_scoped));
case tokens::Type::CHAR_LITERAL: case tokens::Type::CHAR_LITERAL:
return expression_storage.add_expression(nodes::Expression( return expression_storage.add_expression(nodes::Expression(
build_node(parser_node), build_char_literal(parser_node), is_scoped)); build_node(parser_node), build_char_literal(parser_node), is_scoped));
case tokens::Type::UNICODE_LITERAL:
return expression_storage.add_expression(
nodes::Expression(build_node(parser_node),
build_unicode_literal(parser_node), is_scoped));
case tokens::Type::BOOL_LITERAL: case tokens::Type::BOOL_LITERAL:
return expression_storage.add_expression(nodes::Expression( return expression_storage.add_expression(nodes::Expression(
build_node(parser_node), build_bool_literal(parser_node), is_scoped)); build_node(parser_node), build_bool_literal(parser_node), is_scoped));
@ -412,7 +428,7 @@ nodes::Access build_tuple_access(parser::ParseTree::Node parser_node,
type_storage), type_storage),
expression_storage.add_expression(nodes::Expression( expression_storage.add_expression(nodes::Expression(
build_node(parser_node.nth_named_child(1)), build_node(parser_node.nth_named_child(1)),
build_number_literal(parser_node.nth_named_child(1)), false))); build_index_literal(parser_node.nth_named_child(1)), false)));
} }
// 'break' | 'continue' // 'break' | 'continue'

View file

@ -337,7 +337,7 @@ nodes::TypeCheckResult type_check_tuple_access(const nodes::Access &expression,
size_t index = *expression.get_index() size_t index = *expression.get_index()
->get<nodes::Literal>() ->get<nodes::Literal>()
.value() .value()
->get<long long>() // TODO: replace with size_t ->get<size_t>() // Index type
.value(); .value();
if (value_result.get().get_type()->to_builtin() != if (value_result.get().get_type()->to_builtin() !=

View file

@ -87,36 +87,54 @@ void print_modifier(const nodes::Modifier &modifier, Printer &printer,
void print_literal(const nodes::Literal &literal, Printer &printer) { void print_literal(const nodes::Literal &literal, Printer &printer) {
switch (literal.get_any()->index()) { switch (literal.get_any()->index()) {
case 0: // double case 0: // float
// print in parseable form ?? printer.print(std::to_string(*literal.get<float>().value()));
return;
case 1: // double
printer.print(std::to_string(*literal.get<double>().value())); printer.print(std::to_string(*literal.get<double>().value()));
return; return;
case 1: // long long case 2: // int32_t
printer.print(std::to_string(*literal.get<long long>().value())); printer.print(std::to_string(*literal.get<int32_t>().value()));
return; return;
case 2: // std::string case 3: // int64_t
printer.print(std::to_string(*literal.get<int64_t>().value()));
return;
case 4: // size_t
printer.print(std::to_string(*literal.get<size_t>().value()));
return;
case 5: // std::string
printer.print("\""); printer.print("\"");
printer.print_converted( printer.print_converted(
*literal.get<std::string>() *literal.get<std::string>()
.value()); // special symbols are converted inside .value()); // special symbols are converted inside
printer.print("\""); printer.print("\"");
return; return;
case 3: // char case 6: // unicode_string
printer.print("\"");
printer.print_converted(literal.get<nodes::unicode_string>()
.value()
->str); // special symbols are converted inside
printer.print("\"u");
return;
case 7: // char
printer.print("\'\'"); printer.print("\'\'");
printer.print_converted(std::string(1, *literal.get<char>().value())); printer.print_converted(std::string(1, *literal.get<char>().value()));
printer.print("\'\'"); printer.print("\'\'");
return; return;
case 4: // bool case 8: // unicode
printer.print("\'\'");
printer.print_converted(literal.get<nodes::unicode>().value()->ch);
printer.print("\'\'u");
return;
case 9: // bool
printer.print(literal.get<bool>().value() ? "true" : "false"); printer.print(literal.get<bool>().value() ? "true" : "false");
return; return;
case 5: // unit case 10: // unit
printer.print("()"); printer.print("()");
return; return;
case 6: // null case 11: // null
printer.print("null"); printer.print("null");
return; return;
default:
break;
} }
error_handling::handle_general_error("Unreachable"); error_handling::handle_general_error("Unreachable");

View file

@ -228,3 +228,18 @@ tuple_argument_test 'x : (A & B & C) = do_something;
// ((A1 & A2) | B | C) same to Variant[Tuple[A1 A2] B C] // ((A1 & A2) | B | C) same to Variant[Tuple[A1 A2] B C]
variant_argument_test 'x : ((A1 & A2) | B | C) = do_something; variant_argument_test 'x : ((A1 & A2) | B | C) = do_something;
literals_test = {
%float_number_literal := 1.0f;
%double_number_literal := 1.0;
%int_literal := 1i;
%long_literal := 1l;
%index_literal := 1;
%string_literal := "";
%unicode_string_literal := ""u;
%char_literal := ''a'';
%unicode_literal := ''↪''u;
%bool_literal := true;
%unit_literal := ();
%null_literal := null;
}