AST reprogramming #5
@ -1,14 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "compiler/treeifier/tokenizer.hh"
|
||||
#include "utils/data.hh"
|
||||
#include "lang/common.hh"
|
||||
#include "utils/data.hh"
|
||||
|
||||
using namespace std::string_literals;
|
||||
using namespace ppc;
|
||||
@ -73,6 +74,28 @@ namespace ppc::comp::tree::ast {
|
||||
|
||||
data::map_t nmsp_to_map(const loc_namespace_name_t &nmsp);
|
||||
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;
|
||||
|
@ -10,7 +10,8 @@ group_t &ast_ctx_t::group(const std::string &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")
|
||||
.add("$_var", parse_exp_var)
|
||||
.add("$_int", parse_exp_int_lit)
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <sstream>
|
||||
#include "compiler/treeifier/ast.hh"
|
||||
#include <sstream>
|
||||
|
||||
namespace ppc::comp::tree::ast::conv {
|
||||
data::map_t identifier_to_map(const located_t<std::string> &loc) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "compiler/treeifier/ast/helper.hh"
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
|
||||
enum precedence_t {
|
||||
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) {
|
||||
while (!op_stack.empty()) {
|
||||
auto &back_data = op_stack.back();
|
||||
if (data.assoc ?
|
||||
back_data.precedence >= data.precedence :
|
||||
back_data.precedence > data.precedence
|
||||
) break;
|
||||
if (data.assoc ? 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.");
|
||||
}
|
||||
@ -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()) {
|
||||
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());
|
||||
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()) {
|
||||
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());
|
||||
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({}));
|
||||
member_access["location"] = conv::loc_to_map(
|
||||
conv::map_to_loc(member_access["name"].map()["location"].string()).intersect(
|
||||
conv::map_to_loc(res.back().map()["location"].string())
|
||||
)
|
||||
conv::map_to_loc(member_access["name"].map()["location"].string())
|
||||
.intersect(conv::map_to_loc(res.back().map()["location"].string()))
|
||||
);
|
||||
res.pop_back();
|
||||
res.push_back(member_access);
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "compiler/treeifier/ast.hh"
|
||||
#include "compiler/treeifier/ast/helper.hh"
|
||||
// #include "./type.cc"
|
||||
|
||||
using namespace ppc::comp::tree::ast;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include <sstream>
|
||||
#include "compiler/treeifier/lexer.hh"
|
||||
#include "utils/message.hh"
|
||||
#include <sstream>
|
||||
|
||||
using namespace ppc;
|
||||
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;
|
||||
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() });
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <string>
|
||||
#include "compiler/treeifier/tokenizer.hh"
|
||||
#include <string>
|
||||
|
||||
using namespace ppc::comp::tree;
|
||||
using namespace ppc::comp;
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include "compiler/treeifier/tokenizer.hh"
|
||||
#include "compiler/treeifier/lexer.hh"
|
||||
#include "compiler/treeifier/tokenizer.hh"
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
using namespace ppc;
|
||||
using namespace messages;
|
||||
|
@ -1,6 +1,5 @@
|
||||
|
||||
#include <sstream>
|
||||
#include "lang/common.hh"
|
||||
#include <sstream>
|
||||
|
||||
namespace ppc::lang {
|
||||
std::string loc_namespace_name_t::to_string() const {
|
||||
@ -57,4 +56,3 @@ namespace ppc::lang {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,9 +32,15 @@ definition_t::definition_t(const definition_t &val) {
|
||||
|
||||
definition_t::~definition_t() {
|
||||
switch (this->kind) {
|
||||
case FUNCTION: delete this->val.func; break;
|
||||
case FIELD: delete this->val.field; break;
|
||||
case STRUCT: delete this->val.str; break;
|
||||
case FUNCTION:
|
||||
delete this->val.func;
|
||||
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) {
|
||||
return { CALL, { .call = new auto(func) } };
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
struct project_t {
|
||||
std::string output;
|
||||
@ -52,7 +55,7 @@ project_t read_project(std::istream &f) {
|
||||
std::vector<std::string> deps {};
|
||||
|
||||
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) {
|
||||
@ -63,14 +66,14 @@ project_t read_project(std::istream &f) {
|
||||
}
|
||||
|
||||
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) {
|
||||
std::string dep = read_str(f, " \v\t\r\n", ",\n", end_ch);
|
||||
|
||||
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()) {
|
||||
@ -97,7 +100,7 @@ int main(int argc, const char* argv[]) {
|
||||
argv++;
|
||||
|
||||
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";
|
||||
@ -106,7 +109,7 @@ int main(int argc, const char* argv[]) {
|
||||
std::ifstream f { proj_path, std::ios_base::in };
|
||||
|
||||
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);
|
||||
@ -121,7 +124,7 @@ int main(int argc, const char* argv[]) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw (std::string)"Invalid command given. Available commands: output, deps.";
|
||||
throw "Invalid command given. Available commands: output, deps."s;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
|
@ -8,25 +8,25 @@
|
||||
#define DISABLE_NEWLINE_AUTO_RETURN 0x8
|
||||
#endif
|
||||
|
||||
#include <windows.h>
|
||||
#include <conio.h>
|
||||
#include <windows.h>
|
||||
|
||||
#undef ERROR
|
||||
#undef INFO
|
||||
|
||||
#endif
|
||||
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include "utils/threading.hh"
|
||||
#include "utils/strings.hh"
|
||||
#include "utils/json.hh"
|
||||
#include "./opions.hh"
|
||||
#include "compiler/treeifier/ast.hh"
|
||||
#include "compiler/treeifier/lexer.hh"
|
||||
#include "compiler/treeifier/tokenizer.hh"
|
||||
#include "compiler/treeifier/ast.hh"
|
||||
#include "./opions.hh"
|
||||
#include "utils/json.hh"
|
||||
#include "utils/strings.hh"
|
||||
#include "utils/threading.hh"
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
using std::cout;
|
||||
using std::size_t;
|
||||
@ -35,8 +35,7 @@ using namespace ppc::comp::tree;
|
||||
using namespace ppc::comp::tree::ast;
|
||||
|
||||
void add_flags(options::parser_t &parser) {
|
||||
parser.add_flag({
|
||||
.name = "version",
|
||||
parser.add_flag({ .name = "version",
|
||||
.shorthands = "v",
|
||||
.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) {
|
||||
@ -50,10 +49,8 @@ void add_flags(options::parser_t &parser) {
|
||||
<< "\n"
|
||||
<< " License: MIT Copyright (C) TopchetoEU\n";
|
||||
exit(0);
|
||||
}
|
||||
});
|
||||
parser.add_flag({
|
||||
.name = "help",
|
||||
} });
|
||||
parser.add_flag({ .name = "help",
|
||||
.shorthands = "h",
|
||||
.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) {
|
||||
@ -89,25 +86,28 @@ void add_flags(options::parser_t &parser) {
|
||||
const size_t padding = 24;
|
||||
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();
|
||||
|
||||
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;
|
||||
for (size_t j = 0; j < padding; j++) cout << ' ';
|
||||
for (size_t j = 0; j < padding; j++)
|
||||
cout << ' ';
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
});
|
||||
} });
|
||||
parser.add_flag({
|
||||
.name = "silent",
|
||||
.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'",
|
||||
.match_type = options::MATCH_PREFIX,
|
||||
});
|
||||
parser.add_flag({
|
||||
.name = "print-what",
|
||||
parser.add_flag({ .name = "print-what",
|
||||
.description = "Prints a 'what?' type of message (you'll see)",
|
||||
.match_type = options::MATCH_PREFIX,
|
||||
.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."));
|
||||
}
|
||||
});
|
||||
} });
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
|
@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "utils/message.hh"
|
||||
#include "utils/data.hh"
|
||||
#include "utils/message.hh"
|
||||
#include <vector>
|
||||
|
||||
namespace ppc::options {
|
||||
enum flag_match_type_t {
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <set>
|
||||
#include "./opions.hh"
|
||||
#include <set>
|
||||
|
||||
using namespace ppc;
|
||||
|
||||
|
@ -1,4 +1,7 @@
|
||||
#include "utils/data.hh"
|
||||
#include <string>
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
namespace ppc::data {
|
||||
bool value_t::is_null() const {
|
||||
@ -44,45 +47,45 @@ namespace ppc::data {
|
||||
|
||||
array_t &value_t::array() {
|
||||
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() {
|
||||
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() {
|
||||
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() {
|
||||
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() {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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() {
|
||||
|
@ -67,7 +67,6 @@ bool location_t::operator==(const location_t &other) const {
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string empty = "";
|
||||
|
||||
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::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::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->code_start = code_start;
|
||||
this->line = line;
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "utils/message.hh"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "utils/message.hh"
|
||||
|
||||
using namespace ppc;
|
||||
|
||||
@ -10,12 +10,24 @@ namespace ppc::messages {
|
||||
std::string level_readable;
|
||||
|
||||
switch (level) {
|
||||
case message_t::DEBUG: level_readable = "debug"; break;
|
||||
case message_t::SUGGESTION: level_readable = "suggestion"; 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;
|
||||
case message_t::DEBUG:
|
||||
level_readable = "debug";
|
||||
break;
|
||||
case message_t::SUGGESTION:
|
||||
level_readable = "suggestion";
|
||||
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 {};
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <sstream>
|
||||
#include "utils/strings.hh"
|
||||
#include <sstream>
|
||||
|
||||
using namespace ppc;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user