AST building #2

Merged
TopchetoEU merged 74 commits from TopchetoEU/ast-building into master 2022-10-28 11:58:03 +00:00
7 changed files with 35 additions and 27 deletions
Showing only changes of commit df0e17d450 - Show all commits

View File

@ -33,8 +33,8 @@ namespace ppc {
bool operator ==(version_t other) const;
inline bool operator !=(version_t other) const { return !(*this == other); }
version_t(uint16_t major, uint16_t minor, uint32_t revision) : major { major }, minor { minor }, revision { revision } { }
version_t(uint16_t major, uint16_t minor) : version_t { major, minor, -1u } { }
version_t(uint16_t major) : version_t { major, -1u, -1u } { }
version_t(uint16_t major, uint16_t minor, uint32_t revision) : major(major), minor(minor), revision(revision) { }
version_t(uint16_t major, uint16_t minor) : version_t(major, minor, -1u) { }
version_t(uint16_t major) : version_t(major, -1u, -1u) { }
};
}

View File

@ -70,7 +70,7 @@ namespace ppc::data {
public:
value_t &operator [](std::string name) {
if (values.find(name) == values.end()) {
values.emplace(name, value_t { });
values.emplace(name, value_t());
}
return values[name];

View File

@ -17,6 +17,12 @@ namespace ppc::messages {
location_t location;
std::string content;
message_t(level_t level, std::string content, location_t loc = location_t::NONE) :
level(level),
content(content),
location(loc) { }
message_t() : message_t(DEBUG, "") { }
std::string to_string() const;
bool is_severe() const;
};

View File

@ -231,13 +231,13 @@ static process_res_t hex_process(char curr) {
static process_res_t bin_process(char curr) {
if (curr == '0' || curr == '1') return lexer_none();
else if (is_digit(curr))
throw messages::message_t { messages::message_t::ERROR, NO_LOCATION, "A binary literal may only contain zeroes and ones." };
throw messages::message_t(messages::message_t::ERROR, "A binary literal may only contain zeroes and ones.", location_t::NONE);
else return lexer_end();
}
static process_res_t oct_process(char curr) {
if (is_oct(curr)) return lexer_none();
else if (is_digit(curr))
throw messages::message_t { messages::message_t::ERROR, NO_LOCATION, "An octal literal may only contain octal digits." };
throw messages::message_t(messages::message_t::ERROR, "An octal literal may only contain octal digits.", location_t::NONE);
else return lexer_end();
}
@ -378,13 +378,13 @@ std::vector<token_t> token_t::parse_many(ppc::messages::msg_stack_t &msg_stack,
}
}
catch (const messages::message_t &msg) {
throw messages::message_t { msg.level, { filename, line, start, i - length, length }, msg.content };
throw messages::message_t(msg.level, msg.content, location_t(filename, line, start, i - length, length));
}
}
location_t loc = { filename, line, start, i - length, length };
if (curr.type) {
tokens.push_back(token_t {
tokens.push_back({
curr.type, std::string { curr_token.begin(), curr_token.end() },
{ filename, line, start, i - length, length }
});

View File

@ -12,7 +12,7 @@ static std::vector<char> parse_string(msg_stack_t &msg_stack, bool is_char, lex:
bool escaping = false;
std::vector<char> res { };
std::vector<char> res;
location_t curr_char_loc = token.location;
curr_char_loc.length = 1;
curr_char_loc.start++;
@ -34,7 +34,7 @@ static std::vector<char> parse_string(msg_stack_t &msg_stack, bool is_char, lex:
// TODO: Add support for oct, hex and utf8 literals
else if (c == literal_char || c == '\\') new_c = c;
else {
throw message_t { message_t::ERROR, curr_char_loc, "Unescapable character." };
throw message_t(message_t::ERROR, "Unescapable character.", curr_char_loc);
}
res.push_back(new_c);
escaping = false;
@ -49,8 +49,8 @@ static std::vector<char> parse_string(msg_stack_t &msg_stack, bool is_char, lex:
if (c == '\n') break;
}
if (is_char) throw message_t { message_t::ERROR, token.location, "Unterminated char literal." };
else throw message_t { message_t::ERROR, token.location, "Unterminated string literal." };
if (is_char) throw message_t(message_t::ERROR, "Unterminated char literal.", token.location);
else throw message_t(message_t::ERROR, "Unterminated string literal.", token.location);
}
static tok::token_t parse_int(msg_stack_t &msg_stack, lex::token_t token) {
enum radix_t {
@ -98,7 +98,7 @@ static tok::token_t parse_int(msg_stack_t &msg_stack, lex::token_t token) {
case OCTAL:
digit = c - '0';
if (digit < 0 || digit > 7) {
throw message_t { message_t::ERROR, token.location, "Octal literals may contain numbers between 0 and 7." };
throw message_t(message_t::ERROR, "Octal literals may contain numbers between 0 and 7.", token.location);
}
res <<= 3;
res |= digit;
@ -112,14 +112,14 @@ static tok::token_t parse_int(msg_stack_t &msg_stack, lex::token_t token) {
if (c >= 'a' && c <= 'f') digit = c - 'a' + 9;
else if (c >= 'A' && c <= 'F') digit = c - 'A' + 9;
else if (c >= '0' && c <= '9') digit = c - '0';
else throw message_t { message_t::ERROR, token.location, "Invalid character '"s + c + "' in hex literal." };
else throw message_t(message_t::ERROR, "Invalid character '"s + c + "' in hex literal.", token.location);
res <<= 4;
res |= digit;
break;
}
}
return tok::token_t { res, token.location };
return tok::token_t(res, token.location);
}
static tok::token_t parse_float(msg_stack_t &msg_stack, lex::token_t token) {
double whole = 0, fract = 0;
@ -143,20 +143,20 @@ static tok::token_t parse_float(msg_stack_t &msg_stack, lex::token_t token) {
}
}
return tok::token_t { whole + fract, token.location };
return tok::token_t(whole + fract, token.location);
}
tok::token_t tok::token_t::parse(messages::msg_stack_t &msg_stack, lex::token_t in) {
switch (in.type) {
case lex::token_t::IDENTIFIER:
return tok::token_t { in.data, in.location };
return tok::token_t(in.data, in.location);
case lex::token_t::OPERATOR:
try {
auto op = tok::operator_find(in.data);
return token_t { op, in.location };
return token_t(op, in.location);
}
catch (std::string &err) {
throw message_t { message_t::ERROR, in.location, "Operator not recognised."s };
throw message_t(message_t::ERROR, "Operator not recognised."s, in.location);
}
case lex::token_t::BIN_LITERAL:
case lex::token_t::OCT_LITERAL:
@ -169,11 +169,11 @@ tok::token_t tok::token_t::parse(messages::msg_stack_t &msg_stack, lex::token_t
return { parse_string(msg_stack, false, in) };
case lex::token_t::CHAR_LITERAL: {
auto str = parse_string(msg_stack, true, in);
if (str.size() != 1) throw message_t { message_t::ERROR, in.location, "Char literal must consist of just one character." };
if (str.size() != 1) throw message_t(message_t::ERROR, "Char literal must consist of just one character.", in.location);
return str.front();
}
default:
throw message_t { message_t::ERROR, in.location, "Token type not recognised." };
throw message_t(message_t::ERROR, "Token type not recognised.", in.location);
}
}
std::vector<tok::token_t> tok::token_t::parse_many(messages::msg_stack_t &msg_stack, std::vector<lex::token_t> tokens) {

View File

@ -81,15 +81,15 @@ ppc::data::value_t::value_t() {
}
ppc::data::value_t::value_t(const ppc::data::array_t &val) {
this->type = type_t::Arr;
this->val.arr = new array_t { val };
this->val.arr = new array_t(val);
}
ppc::data::value_t::value_t(const ppc::data::map_t &val) {
this->type = type_t::Map;
this->val.map = new map_t { val };
this->val.map = new map_t(val);
}
ppc::data::value_t::value_t(const ppc::data::string_t &val) {
this->type = type_t::Str;
this->val.str = new string_t { val };
this->val.str = new string_t(val);
}
ppc::data::value_t::value_t(ppc::data::bool_t val) {
this->type = type_t::Bool;
@ -103,13 +103,13 @@ ppc::data::value_t::value_t(const ppc::data::value_t &other) {
type = other.type;
switch (other.type) {
case type_t::Map:
val.map = new map_t { *other.val.map };
val.map = new map_t(*other.val.map);
break;
case type_t::Arr:
val.arr = new array_t { *other.val.arr };
val.arr = new array_t(*other.val.arr);
break;
case type_t::Str:
val.str = new string_t { *other.val.str };
val.str = new string_t(*other.val.str);
break;
default:
val = other.val;

View File

@ -115,3 +115,5 @@ location_t::location_t(std::string filename, std::size_t line, std::size_t start
this->code_start = code_start;
this->filename = filename;
}
const location_t location_t::NONE = { };