AST reprogramming #5

Open
TopchetoEU wants to merge 15 commits from TopchetoEU/ast-reporgramming into master
20 changed files with 218 additions and 175 deletions
Showing only changes of commit b8594aecd6 - Show all commits

View File

@ -1,14 +1,15 @@
#pragma once #pragma once
#include <string>
#include <set>
#include <map> #include <map>
#include <unordered_set>
#include <unordered_map>
#include <memory> #include <memory>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include "compiler/treeifier/tokenizer.hh" #include "compiler/treeifier/tokenizer.hh"
#include "utils/data.hh"
#include "lang/common.hh" #include "lang/common.hh"
#include "utils/data.hh"
using namespace std::string_literals; using namespace std::string_literals;
using namespace ppc; using namespace ppc;
@ -73,6 +74,28 @@ namespace ppc::comp::tree::ast {
data::map_t nmsp_to_map(const loc_namespace_name_t &nmsp); data::map_t nmsp_to_map(const loc_namespace_name_t &nmsp);
loc_namespace_name_t map_to_nmsp(const data::map_t &map); loc_namespace_name_t map_to_nmsp(const data::map_t &map);
} // namespace conv
class construct_t {
public:
virtual const std::string &name() const = 0;
};
class parser_t {
public:
virtual bool parse(ast_ctx_t &ctx, size_t &res_i, construct_t *&out) const = 0;
virtual bool simplify(ast_ctx_t &ctx, size_t &res_i, const construct_t *global, const construct_t *container, const construct_t *current) const = 0;
};
namespace constr {
class glob_con_t: public construct_t {
const std::string &name() const { return "$_glob"s; }
bool parse(ast_ctx_t &ctx, size_t &res_i, construct_t *&out) const;
};
}
namespace parsers {
} }
parser_func_t parse_glob, parse_nmsp, parse_identifier, parse_type, parse_exp, parse_stat_exp; parser_func_t parse_glob, parse_nmsp, parse_identifier, parse_type, parse_exp, parse_stat_exp;

View File

@ -10,7 +10,8 @@ group_t &ast_ctx_t::group(const std::string &name) {
else return groups[name]; else return groups[name];
} }
ast_ctx_t::ast_ctx_t(msg_stack_t &messages, std::vector<token_t> &tokens): messages(messages), tokens(tokens) { ast_ctx_t::ast_ctx_t(msg_stack_t &messages, std::vector<token_t> &tokens):
messages(messages), tokens(tokens) {
group("$_exp_val") group("$_exp_val")
.add("$_var", parse_exp_var) .add("$_var", parse_exp_var)
.add("$_int", parse_exp_int_lit) .add("$_int", parse_exp_int_lit)

View File

@ -1,5 +1,5 @@
#include <sstream>
#include "compiler/treeifier/ast.hh" #include "compiler/treeifier/ast.hh"
#include <sstream>
namespace ppc::comp::tree::ast::conv { namespace ppc::comp::tree::ast::conv {
data::map_t identifier_to_map(const located_t<std::string> &loc) { data::map_t identifier_to_map(const located_t<std::string> &loc) {

View File

@ -1,6 +1,6 @@
#include "compiler/treeifier/ast/helper.hh" #include "compiler/treeifier/ast/helper.hh"
#include <map>
#include <algorithm> #include <algorithm>
#include <map>
enum precedence_t { enum precedence_t {
NONE, NONE,
@ -170,10 +170,7 @@ bool pop_call(size_t n, location_t loc, std::vector<located_t<op_data_t>> &op_st
bool pop_until(const op_data_t &data, tree_helper_t &h, std::vector<located_t<op_data_t>> &op_stack, array_t &res) { bool pop_until(const op_data_t &data, tree_helper_t &h, std::vector<located_t<op_data_t>> &op_stack, array_t &res) {
while (!op_stack.empty()) { while (!op_stack.empty()) {
auto &back_data = op_stack.back(); auto &back_data = op_stack.back();
if (data.assoc ? if (data.assoc ? back_data.precedence >= data.precedence : back_data.precedence > data.precedence) break;
back_data.precedence >= data.precedence :
back_data.precedence > data.precedence
) break;
if (!pop(op_stack, res)) return h.err("Expected an expression on the right side of this operator."); if (!pop(op_stack, res)) return h.err("Expected an expression on the right side of this operator.");
} }
@ -188,7 +185,9 @@ bool ast::parse_exp_int_lit(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
if (h.curr().is_int_literal()) { if (h.curr().is_int_literal()) {
auto &arr = out["content"].array({}); auto &arr = out["content"].array({});
for (auto b : h.curr().literal()) arr.push_back((float)b); for (auto b : h.curr().literal()) {
arr.push_back((float)b);
}
out["location"] = conv::loc_to_map(h.loc()); out["location"] = conv::loc_to_map(h.loc());
return h.submit(true); return h.submit(true);
} }
@ -200,7 +199,9 @@ bool ast::parse_exp_str_lit(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
if (h.curr().is_str_literal()) { if (h.curr().is_str_literal()) {
auto &arr = out["content"].array({}); auto &arr = out["content"].array({});
for (auto b : h.curr().literal()) arr.push_back((float)b); for (auto b : h.curr().literal()) {
arr.push_back((float)b);
}
out["location"] = conv::loc_to_map(h.loc()); out["location"] = conv::loc_to_map(h.loc());
return h.submit(true); return h.submit(true);
} }
@ -301,9 +302,8 @@ bool ast::parse_exp(ast_ctx_t &ctx, size_t &res_i, map_t &out) {
}; };
h.force_parse(parse_identifier, "Expected an identifier.", member_access["name"].map({})); h.force_parse(parse_identifier, "Expected an identifier.", member_access["name"].map({}));
member_access["location"] = conv::loc_to_map( member_access["location"] = conv::loc_to_map(
conv::map_to_loc(member_access["name"].map()["location"].string()).intersect( conv::map_to_loc(member_access["name"].map()["location"].string())
conv::map_to_loc(res.back().map()["location"].string()) .intersect(conv::map_to_loc(res.back().map()["location"].string()))
)
); );
res.pop_back(); res.pop_back();
res.push_back(member_access); res.push_back(member_access);

View File

@ -1,6 +1,5 @@
#include "compiler/treeifier/ast.hh" #include "compiler/treeifier/ast.hh"
#include "compiler/treeifier/ast/helper.hh" #include "compiler/treeifier/ast/helper.hh"
// #include "./type.cc"
using namespace ppc::comp::tree::ast; using namespace ppc::comp::tree::ast;

View File

@ -1,6 +1,6 @@
#include <sstream>
#include "compiler/treeifier/lexer.hh" #include "compiler/treeifier/lexer.hh"
#include "utils/message.hh" #include "utils/message.hh"
#include <sstream>
using namespace ppc; using namespace ppc;
using namespace ppc::messages; using namespace ppc::messages;
@ -214,7 +214,8 @@ std::vector<token_t> token_t::parse_file(ppc::messages::msg_stack_t &msg_stack,
std::vector<char> contents; std::vector<char> contents;
int c; int c;
while ((c = f.get()) != EOF) contents.push_back(c); while ((c = f.get()) != EOF)
contents.push_back(c);
return parse_many(msg_stack, filename, std::string { contents.begin(), contents.end() }); return parse_many(msg_stack, filename, std::string { contents.begin(), contents.end() });
} }

View File

@ -1,5 +1,5 @@
#include <string>
#include "compiler/treeifier/tokenizer.hh" #include "compiler/treeifier/tokenizer.hh"
#include <string>
using namespace ppc::comp::tree; using namespace ppc::comp::tree;
using namespace ppc::comp; using namespace ppc::comp;

View File

@ -1,7 +1,7 @@
#include <string>
#include <algorithm>
#include "compiler/treeifier/tokenizer.hh"
#include "compiler/treeifier/lexer.hh" #include "compiler/treeifier/lexer.hh"
#include "compiler/treeifier/tokenizer.hh"
#include <algorithm>
#include <string>
using namespace ppc; using namespace ppc;
using namespace messages; using namespace messages;

View File

@ -1,6 +1,5 @@
#include <sstream>
#include "lang/common.hh" #include "lang/common.hh"
#include <sstream>
namespace ppc::lang { namespace ppc::lang {
std::string loc_namespace_name_t::to_string() const { std::string loc_namespace_name_t::to_string() const {
@ -57,4 +56,3 @@ namespace ppc::lang {
return res; return res;
} }
} }

View File

@ -32,9 +32,15 @@ definition_t::definition_t(const definition_t &val) {
definition_t::~definition_t() { definition_t::~definition_t() {
switch (this->kind) { switch (this->kind) {
case FUNCTION: delete this->val.func; break; case FUNCTION:
case FIELD: delete this->val.field; break; delete this->val.func;
case STRUCT: delete this->val.str; break; break;
case FIELD:
delete this->val.field;
break;
case STRUCT:
delete this->val.str;
break;
} }
} }
@ -53,7 +59,6 @@ field_t &definition_t::get_field() {
} }
statement_t statement_t::call(const namespace_name_t &func) { statement_t statement_t::call(const namespace_name_t &func) {
return { CALL, { .call = new auto(func) } }; return { CALL, { .call = new auto(func) } };
} }

View File

@ -1,6 +1,9 @@
#include <vector>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <vector>
#include <string>
using namespace std::string_literals;
struct project_t { struct project_t {
std::string output; std::string output;
@ -52,7 +55,7 @@ project_t read_project(std::istream &f) {
std::vector<std::string> deps {}; std::vector<std::string> deps {};
if (name.length() == 0) { if (name.length() == 0) {
throw (std::string)"The name of a project may not be empty."; throw "The name of a project may not be empty."s;
} }
if (end_ch == -1) { if (end_ch == -1) {
@ -63,14 +66,14 @@ project_t read_project(std::istream &f) {
} }
if (name.find(',') != std::string::npos || name.find(' ') != std::string::npos) { if (name.find(',') != std::string::npos || name.find(' ') != std::string::npos) {
throw (std::string)"The name of a project may not contain spaces or commas."; throw "The name of a project may not contain spaces or commas."s;
} }
while (true) { while (true) {
std::string dep = read_str(f, " \v\t\r\n", ",\n", end_ch); std::string dep = read_str(f, " \v\t\r\n", ",\n", end_ch);
if (dep.find(' ') != std::string::npos) { if (dep.find(' ') != std::string::npos) {
throw (std::string)"The name of a dependency may not contain spaces."; throw "The name of a dependency may not contain spaces."s;
} }
if (dep.length()) { if (dep.length()) {
@ -97,7 +100,7 @@ int main(int argc, const char* argv[]) {
argv++; argv++;
if (argc != 3) { if (argc != 3) {
throw (std::string)"Incorrect usage. Syntax: [src-dir] [project-name] [output|deps]."; throw "Incorrect usage. Syntax: [src-dir] [project-name] [output|deps]."s;
} }
std::string proj_path = (std::string) argv[0] + "/" + argv[1] + ".proj"; std::string proj_path = (std::string) argv[0] + "/" + argv[1] + ".proj";
@ -106,7 +109,7 @@ int main(int argc, const char* argv[]) {
std::ifstream f { proj_path, std::ios_base::in }; std::ifstream f { proj_path, std::ios_base::in };
if (!f.is_open()) { if (!f.is_open()) {
throw (std::string)"The project '" + argv[1] +"' doesn't exist."; throw "The project '"s + argv[1] + "' doesn't exist."s;
} }
project_t project = read_project(f); project_t project = read_project(f);
@ -121,7 +124,7 @@ int main(int argc, const char* argv[]) {
} }
} }
else { else {
throw (std::string)"Invalid command given. Available commands: output, deps."; throw "Invalid command given. Available commands: output, deps."s;
} }
std::cout << std::endl; std::cout << std::endl;

View File

@ -8,25 +8,25 @@
#define DISABLE_NEWLINE_AUTO_RETURN 0x8 #define DISABLE_NEWLINE_AUTO_RETURN 0x8
#endif #endif
#include <windows.h>
#include <conio.h> #include <conio.h>
#include <windows.h>
#undef ERROR #undef ERROR
#undef INFO #undef INFO
#endif #endif
#include <sstream> #include "./opions.hh"
#include <fstream> #include "compiler/treeifier/ast.hh"
#include <iostream>
#include <cstdio>
#include "utils/threading.hh"
#include "utils/strings.hh"
#include "utils/json.hh"
#include "compiler/treeifier/lexer.hh" #include "compiler/treeifier/lexer.hh"
#include "compiler/treeifier/tokenizer.hh" #include "compiler/treeifier/tokenizer.hh"
#include "compiler/treeifier/ast.hh" #include "utils/json.hh"
#include "./opions.hh" #include "utils/strings.hh"
#include "utils/threading.hh"
#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
using std::cout; using std::cout;
using std::size_t; using std::size_t;
@ -35,8 +35,7 @@ using namespace ppc::comp::tree;
using namespace ppc::comp::tree::ast; using namespace ppc::comp::tree::ast;
void add_flags(options::parser_t &parser) { void add_flags(options::parser_t &parser) {
parser.add_flag({ parser.add_flag({ .name = "version",
.name = "version",
.shorthands = "v", .shorthands = "v",
.description = "Displays version and license agreement of this binary", .description = "Displays version and license agreement of this binary",
.execute = [](options::parser_t &parser, const std::string &option, ppc::messages::msg_stack_t &global_stack) { .execute = [](options::parser_t &parser, const std::string &option, ppc::messages::msg_stack_t &global_stack) {
@ -50,10 +49,8 @@ void add_flags(options::parser_t &parser) {
<< "\n" << "\n"
<< " License: MIT Copyright (C) TopchetoEU\n"; << " License: MIT Copyright (C) TopchetoEU\n";
exit(0); exit(0);
} } });
}); parser.add_flag({ .name = "help",
parser.add_flag({
.name = "help",
.shorthands = "h", .shorthands = "h",
.description = "Displays a list of all flags and their meaning", .description = "Displays a list of all flags and their meaning",
.execute = [](options::parser_t &parser, const std::string &option, ppc::messages::msg_stack_t &global_stack) { .execute = [](options::parser_t &parser, const std::string &option, ppc::messages::msg_stack_t &global_stack) {
@ -89,25 +86,28 @@ void add_flags(options::parser_t &parser) {
const size_t padding = 24; const size_t padding = 24;
const size_t msg_width = 80 - padding; const size_t msg_width = 80 - padding;
for (size_t i = 0; i < padding - n; i++) cout << ' '; for (size_t i = 0; i < padding - n; i++)
cout << ' ';
int len = flag.description.length(); int len = flag.description.length();
for (size_t i = 0; i < len / msg_width; i++) { for (size_t i = 0; i < len / msg_width; i++) {
for (size_t j = 0; j < msg_width; j++) cout << flag.description[i * msg_width + j]; for (size_t j = 0; j < msg_width; j++)
cout << flag.description[i * msg_width + j];
cout << std::endl; cout << std::endl;
for (size_t j = 0; j < padding; j++) cout << ' '; for (size_t j = 0; j < padding; j++)
cout << ' ';
} }
int remainder = len % msg_width; int remainder = len % msg_width;
for (int i = 0; i < remainder; i++) cout << flag.description[len - remainder + i]; for (int i = 0; i < remainder; i++)
cout << flag.description[len - remainder + i];
} }
printf("\n"); printf("\n");
} }
} } });
});
parser.add_flag({ parser.add_flag({
.name = "silent", .name = "silent",
.shorthands = "qs", .shorthands = "qs",
@ -118,14 +118,12 @@ void add_flags(options::parser_t &parser) {
.description = "Sets a lower limit of messages that will print. Accepted values: 'all', 'debug', 'suggestion', 'info', 'warning', 'error', 'none'", .description = "Sets a lower limit of messages that will print. Accepted values: 'all', 'debug', 'suggestion', 'info', 'warning', 'error', 'none'",
.match_type = options::MATCH_PREFIX, .match_type = options::MATCH_PREFIX,
}); });
parser.add_flag({ parser.add_flag({ .name = "print-what",
.name = "print-what",
.description = "Prints a 'what?' type of message (you'll see)", .description = "Prints a 'what?' type of message (you'll see)",
.match_type = options::MATCH_PREFIX, .match_type = options::MATCH_PREFIX,
.execute = [](options::parser_t &parser, const std::string &option, ppc::messages::msg_stack_t &global_stack) { .execute = [](options::parser_t &parser, const std::string &option, ppc::messages::msg_stack_t &global_stack) {
global_stack.push(messages::message_t((messages::message_t::level_t)69, "IDK LOL.")); global_stack.push(messages::message_t((messages::message_t::level_t)69, "IDK LOL."));
} } });
});
} }
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {

View File

@ -1,8 +1,8 @@
#pragma once #pragma once
#include <vector>
#include "utils/message.hh"
#include "utils/data.hh" #include "utils/data.hh"
#include "utils/message.hh"
#include <vector>
namespace ppc::options { namespace ppc::options {
enum flag_match_type_t { enum flag_match_type_t {

View File

@ -1,5 +1,5 @@
#include <set>
#include "./opions.hh" #include "./opions.hh"
#include <set>
using namespace ppc; using namespace ppc;

View File

@ -1,4 +1,7 @@
#include "utils/data.hh" #include "utils/data.hh"
#include <string>
using namespace std::string_literals;
namespace ppc::data { namespace ppc::data {
bool value_t::is_null() const { bool value_t::is_null() const {
@ -44,45 +47,45 @@ namespace ppc::data {
array_t &value_t::array() { array_t &value_t::array() {
if (is_array()) return *val.arr; if (is_array()) return *val.arr;
else throw (std::string)"The value isn't an array."; else throw "The value isn't an array."s;
} }
map_t &value_t::map() { map_t &value_t::map() {
if (is_map()) return *val.map; if (is_map()) return *val.map;
else throw (std::string)"The value isn't a map."; else throw "The value isn't a map."s;
} }
number_t &value_t::number() { number_t &value_t::number() {
if (is_number()) return val.num; if (is_number()) return val.num;
else throw (std::string)"The value isn't a number."; else throw "The value isn't a number."s;
} }
string_t &value_t::string() { string_t &value_t::string() {
if (is_string()) return *val.str; if (is_string()) return *val.str;
else throw (std::string)"The value isn't a string."; else throw "The value isn't a string."s;
} }
bool_t &value_t::boolean() { bool_t &value_t::boolean() {
if (is_bool()) return val.bl; if (is_bool()) return val.bl;
else throw (std::string)"The value isn't a bool."; else throw "The value isn't a bool."s;
} }
const array_t &value_t::array() const { const array_t &value_t::array() const {
if (is_array()) return *val.arr; if (is_array()) return *val.arr;
else throw (std::string)"The value isn't an array."; else throw "The value isn't an array."s;
} }
const map_t &value_t::map() const { const map_t &value_t::map() const {
if (is_map()) return *val.map; if (is_map()) return *val.map;
else throw (std::string)"The value isn't a map."; else throw "The value isn't a map."s;
} }
number_t value_t::number() const { number_t value_t::number() const {
if (is_number()) return val.num; if (is_number()) return val.num;
else throw (std::string)"The value isn't a number."; else throw "The value isn't a number."s;
} }
const string_t &value_t::string() const { const string_t &value_t::string() const {
if (is_string()) return *val.str; if (is_string()) return *val.str;
else throw (std::string)"The value isn't a string."; else throw "The value isn't a string."s;
} }
bool_t value_t::boolean() const { bool_t value_t::boolean() const {
if (is_bool()) return val.bl; if (is_bool()) return val.bl;
else throw (std::string)"The value isn't a bool."; else throw "The value isn't a bool."s;
} }
value_t::value_t() { value_t::value_t() {

View File

@ -67,7 +67,6 @@ bool location_t::operator==(const location_t &other) const {
} }
std::string empty = ""; std::string empty = "";
location_t::location_t(): location_t::location_t():
@ -84,7 +83,8 @@ location_t::location_t(const std::string &filename, std::size_t line, std::size_
location_t(filename, line, start, code_start, -1) { } location_t(filename, line, start, code_start, -1) { }
location_t::location_t(std::size_t line, std::size_t start, std::size_t code_start, std::size_t length): location_t::location_t(std::size_t line, std::size_t start, std::size_t code_start, std::size_t length):
location_t(empty, line, start, code_start, length) { } location_t(empty, line, start, code_start, length) { }
location_t::location_t(const std::string &filename, std::size_t line, std::size_t start, std::size_t code_start, std::size_t length): filename(filename) { location_t::location_t(const std::string &filename, std::size_t line, std::size_t start, std::size_t code_start, std::size_t length):
filename(filename) {
this->length = length; this->length = length;
this->code_start = code_start; this->code_start = code_start;
this->line = line; this->line = line;

View File

@ -1,6 +1,6 @@
#include "utils/message.hh"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "utils/message.hh"
using namespace ppc; using namespace ppc;
@ -10,12 +10,24 @@ namespace ppc::messages {
std::string level_readable; std::string level_readable;
switch (level) { switch (level) {
case message_t::DEBUG: level_readable = "debug"; break; case message_t::DEBUG:
case message_t::SUGGESTION: level_readable = "suggestion"; break; level_readable = "debug";
case message_t::INFO: level_readable = "info"; break; break;
case message_t::WARNING: level_readable = "warning"; break; case message_t::SUGGESTION:
case message_t::ERROR: level_readable = "error"; break; level_readable = "suggestion";
default: level_readable = "what?"; break; break;
case message_t::INFO:
level_readable = "info";
break;
case message_t::WARNING:
level_readable = "warning";
break;
case message_t::ERROR:
level_readable = "error";
break;
default:
level_readable = "what?";
break;
} }
std::stringstream res {}; std::stringstream res {};

View File

@ -1,5 +1,5 @@
#include <sstream>
#include "utils/strings.hh" #include "utils/strings.hh"
#include <sstream>
using namespace ppc; using namespace ppc;