mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
Make funcfiftlib compilation compatible with modern compilers (#618)
* wip: make funcfiftlib compilation compatible with modern compilers * wip: add methods needed for another compiler * fix: tdutils port config if emscripten * feat: func source and realpath callback * fix: invalid fift compilation exceptions --------- Co-authored-by: krigga <krigga7@gmail.com>
This commit is contained in:
parent
82e231d0a7
commit
4590ed381b
8 changed files with 142 additions and 31 deletions
|
@ -123,5 +123,7 @@ int main(int argc, char* const argv[]) {
|
|||
sources.push_back(std::string(argv[optind++]));
|
||||
}
|
||||
|
||||
funC::read_callback = funC::fs_read_callback;
|
||||
|
||||
return funC::func_proceed(sources, *outs, std::cerr);
|
||||
}
|
||||
|
|
|
@ -30,6 +30,8 @@
|
|||
#include "parser/lexer.h"
|
||||
#include <getopt.h>
|
||||
#include "git.h"
|
||||
#include <fstream>
|
||||
#include "td/utils/port/path.h"
|
||||
|
||||
namespace funC {
|
||||
|
||||
|
@ -39,6 +41,28 @@ bool interactive = false;
|
|||
GlobalPragma pragma_allow_post_modification{"allow-post-modification"};
|
||||
GlobalPragma pragma_compute_asm_ltr{"compute-asm-ltr"};
|
||||
std::string generated_from, boc_output_filename;
|
||||
ReadCallback::Callback read_callback;
|
||||
|
||||
td::Result<std::string> fs_read_callback(ReadCallback::Kind kind, const char* query) {
|
||||
switch (kind) {
|
||||
case ReadCallback::Kind::ReadFile: {
|
||||
std::ifstream ifs{query};
|
||||
if (ifs.fail()) {
|
||||
auto msg = std::string{"cannot open source file `"} + query + "`";
|
||||
return td::Status::Error(msg);
|
||||
}
|
||||
std::stringstream ss;
|
||||
ss << ifs.rdbuf();
|
||||
return ss.str();
|
||||
}
|
||||
case ReadCallback::Kind::Realpath: {
|
||||
return td::realpath(td::CSlice(query));
|
||||
}
|
||||
default: {
|
||||
return td::Status::Error("Unknown query kind");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
#include "parser/srcread.h"
|
||||
#include "parser/lexer.h"
|
||||
#include "parser/symtable.h"
|
||||
#include "td/utils/Status.h"
|
||||
|
||||
namespace funC {
|
||||
|
||||
|
@ -845,6 +846,35 @@ extern std::vector<SymDef*> glob_func, glob_vars;
|
|||
*
|
||||
*/
|
||||
|
||||
class ReadCallback {
|
||||
public:
|
||||
/// Noncopyable.
|
||||
ReadCallback(ReadCallback const&) = delete;
|
||||
ReadCallback& operator=(ReadCallback const&) = delete;
|
||||
|
||||
enum class Kind
|
||||
{
|
||||
ReadFile,
|
||||
Realpath
|
||||
};
|
||||
|
||||
static std::string kindString(Kind _kind)
|
||||
{
|
||||
switch (_kind)
|
||||
{
|
||||
case Kind::ReadFile:
|
||||
return "source";
|
||||
case Kind::Realpath:
|
||||
return "realpath";
|
||||
default:
|
||||
throw ""; // todo ?
|
||||
}
|
||||
}
|
||||
|
||||
/// File reading or generic query callback.
|
||||
using Callback = std::function<td::Result<std::string>(ReadCallback::Kind, const char*)>;
|
||||
};
|
||||
|
||||
// defined in parse-func.cpp
|
||||
bool parse_source(std::istream* is, const src::FileDescr* fdescr);
|
||||
bool parse_source_file(const char* filename, src::Lexem lex = {}, bool is_main = false);
|
||||
|
@ -1691,6 +1721,9 @@ void define_builtins();
|
|||
extern int verbosity, indent, opt_level;
|
||||
extern bool stack_layout_comments, op_rewrite_comments, program_envelope, asm_preamble, interactive;
|
||||
extern std::string generated_from, boc_output_filename;
|
||||
extern ReadCallback::Callback read_callback;
|
||||
|
||||
td::Result<std::string> fs_read_callback(ReadCallback::Kind kind, const char* query);
|
||||
|
||||
class GlobalPragma {
|
||||
public:
|
||||
|
|
|
@ -22,8 +22,6 @@
|
|||
#include "openssl/digest.hpp"
|
||||
#include "block/block.h"
|
||||
#include "block-parse.h"
|
||||
#include <fstream>
|
||||
#include "td/utils/port/path.h"
|
||||
|
||||
namespace sym {
|
||||
|
||||
|
@ -1757,7 +1755,7 @@ bool parse_source_file(const char* filename, src::Lexem lex, bool is_main) {
|
|||
}
|
||||
}
|
||||
|
||||
auto path_res = td::realpath(td::CSlice(filename));
|
||||
auto path_res = read_callback(ReadCallback::Kind::Realpath, filename);
|
||||
if (path_res.is_error()) {
|
||||
auto error = path_res.move_as_error();
|
||||
lex.error(error.message().c_str());
|
||||
|
@ -1784,17 +1782,19 @@ bool parse_source_file(const char* filename, src::Lexem lex, bool is_main) {
|
|||
source_files[real_filename] = cur_source;
|
||||
cur_source->is_main = is_main;
|
||||
source_fdescr.push_back(cur_source);
|
||||
std::ifstream ifs{filename};
|
||||
if (ifs.fail()) {
|
||||
auto msg = std::string{"cannot open source file `"} + filename + "`";
|
||||
auto file_res = read_callback(ReadCallback::Kind::ReadFile, filename);
|
||||
if (file_res.is_error()) {
|
||||
auto msg = file_res.move_as_error().message().str();
|
||||
if (lex.tp) {
|
||||
lex.error(msg);
|
||||
} else {
|
||||
throw src::Fatal{msg};
|
||||
}
|
||||
}
|
||||
auto file_str = file_res.move_as_ok();
|
||||
std::stringstream ss{file_str};
|
||||
inclusion_locations.push(lex.loc);
|
||||
bool res = parse_source(&ifs, cur_source);
|
||||
bool res = parse_source(&ss, cur_source);
|
||||
inclusion_locations.pop();
|
||||
return res;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue