2024-10-31 06:59:23 +00:00
|
|
|
/*
|
|
|
|
This file is part of TON Blockchain Library.
|
|
|
|
|
|
|
|
TON Blockchain Library is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
TON Blockchain Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "src-file.h"
|
2024-10-31 07:02:01 +00:00
|
|
|
#include "compiler-state.h"
|
2024-10-31 06:59:23 +00:00
|
|
|
#include <iostream>
|
2024-10-31 07:04:58 +00:00
|
|
|
#include <sstream>
|
2025-02-11 18:52:01 +00:00
|
|
|
#include <iomanip>
|
2024-10-31 06:59:23 +00:00
|
|
|
|
|
|
|
namespace tolk {
|
|
|
|
|
|
|
|
static_assert(sizeof(SrcLocation) == 8);
|
|
|
|
|
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner.
This is a common approach for functional languages, where
types are inferred from usage through unification.
As a result, type declarations are not necessary:
() f(a,b) { return a+b; } // a and b now int, since `+` (int, int)
While this approach works for now, problems arise with the introduction
of new types like bool, where `!x` must handle both int and bool.
It will also become incompatible with int32 and other strict integers.
This will clash with structure methods, struggle with proper generics,
and become entirely impractical for union types.
This PR completely rewrites the type system targeting the future.
1) type of any expression is inferred and never changed
2) this is available because dependent expressions already inferred
3) forall completely removed, generic functions introduced
(they work like template functions actually, instantiated while inferring)
4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)`
5) `as` keyword, for example `t.tupleAt(0) as int`
6) methods binding is done along with type inferring, not before
("before", as worked previously, was always a wrong approach)
2024-12-30 15:31:27 +00:00
|
|
|
const SrcFile* AllRegisteredSrcFiles::find_file(int file_id) const {
|
|
|
|
for (const SrcFile* file : all_src_files) {
|
2024-10-31 06:59:23 +00:00
|
|
|
if (file->file_id == file_id) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner.
This is a common approach for functional languages, where
types are inferred from usage through unification.
As a result, type declarations are not necessary:
() f(a,b) { return a+b; } // a and b now int, since `+` (int, int)
While this approach works for now, problems arise with the introduction
of new types like bool, where `!x` must handle both int and bool.
It will also become incompatible with int32 and other strict integers.
This will clash with structure methods, struggle with proper generics,
and become entirely impractical for union types.
This PR completely rewrites the type system targeting the future.
1) type of any expression is inferred and never changed
2) this is available because dependent expressions already inferred
3) forall completely removed, generic functions introduced
(they work like template functions actually, instantiated while inferring)
4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)`
5) `as` keyword, for example `t.tupleAt(0) as int`
6) methods binding is done along with type inferring, not before
("before", as worked previously, was always a wrong approach)
2024-12-30 15:31:27 +00:00
|
|
|
const SrcFile* AllRegisteredSrcFiles::find_file(const std::string& abs_filename) const {
|
|
|
|
for (const SrcFile* file : all_src_files) {
|
2024-10-31 06:59:23 +00:00
|
|
|
if (file->abs_filename == abs_filename) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner.
This is a common approach for functional languages, where
types are inferred from usage through unification.
As a result, type declarations are not necessary:
() f(a,b) { return a+b; } // a and b now int, since `+` (int, int)
While this approach works for now, problems arise with the introduction
of new types like bool, where `!x` must handle both int and bool.
It will also become incompatible with int32 and other strict integers.
This will clash with structure methods, struggle with proper generics,
and become entirely impractical for union types.
This PR completely rewrites the type system targeting the future.
1) type of any expression is inferred and never changed
2) this is available because dependent expressions already inferred
3) forall completely removed, generic functions introduced
(they work like template functions actually, instantiated while inferring)
4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)`
5) `as` keyword, for example `t.tupleAt(0) as int`
6) methods binding is done along with type inferring, not before
("before", as worked previously, was always a wrong approach)
2024-12-30 15:31:27 +00:00
|
|
|
const SrcFile* AllRegisteredSrcFiles::locate_and_register_source_file(const std::string& rel_filename, SrcLocation included_from) {
|
2024-10-31 07:04:58 +00:00
|
|
|
td::Result<std::string> path = G.settings.read_callback(CompilerSettings::FsReadCallbackKind::Realpath, rel_filename.c_str());
|
|
|
|
if (path.is_error()) {
|
|
|
|
if (included_from.is_defined()) {
|
|
|
|
throw ParseError(included_from, "Failed to import: " + path.move_as_error().message().str());
|
|
|
|
}
|
|
|
|
throw Fatal("Failed to locate " + rel_filename + ": " + path.move_as_error().message().str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string abs_filename = path.move_as_ok();
|
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner.
This is a common approach for functional languages, where
types are inferred from usage through unification.
As a result, type declarations are not necessary:
() f(a,b) { return a+b; } // a and b now int, since `+` (int, int)
While this approach works for now, problems arise with the introduction
of new types like bool, where `!x` must handle both int and bool.
It will also become incompatible with int32 and other strict integers.
This will clash with structure methods, struggle with proper generics,
and become entirely impractical for union types.
This PR completely rewrites the type system targeting the future.
1) type of any expression is inferred and never changed
2) this is available because dependent expressions already inferred
3) forall completely removed, generic functions introduced
(they work like template functions actually, instantiated while inferring)
4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)`
5) `as` keyword, for example `t.tupleAt(0) as int`
6) methods binding is done along with type inferring, not before
("before", as worked previously, was always a wrong approach)
2024-12-30 15:31:27 +00:00
|
|
|
if (const SrcFile* file = find_file(abs_filename)) {
|
2024-10-31 07:04:58 +00:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
td::Result<std::string> text = G.settings.read_callback(CompilerSettings::FsReadCallbackKind::ReadFile, abs_filename.c_str());
|
|
|
|
if (text.is_error()) {
|
|
|
|
if (included_from.is_defined()) {
|
|
|
|
throw ParseError(included_from, "Failed to import: " + text.move_as_error().message().str());
|
|
|
|
}
|
|
|
|
throw Fatal("Failed to read " + rel_filename + ": " + text.move_as_error().message().str());
|
|
|
|
}
|
|
|
|
|
|
|
|
SrcFile* created = new SrcFile(++last_registered_file_id, rel_filename, std::move(abs_filename), text.move_as_ok());
|
|
|
|
if (G.is_verbosity(1)) {
|
|
|
|
std::cerr << "register file_id " << created->file_id << " " << created->abs_filename << std::endl;
|
|
|
|
}
|
2024-10-31 06:59:23 +00:00
|
|
|
all_src_files.push_back(created);
|
|
|
|
return created;
|
|
|
|
}
|
|
|
|
|
2024-10-31 07:04:58 +00:00
|
|
|
SrcFile* AllRegisteredSrcFiles::get_next_unparsed_file() {
|
|
|
|
if (last_parsed_file_id >= last_registered_file_id) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner.
This is a common approach for functional languages, where
types are inferred from usage through unification.
As a result, type declarations are not necessary:
() f(a,b) { return a+b; } // a and b now int, since `+` (int, int)
While this approach works for now, problems arise with the introduction
of new types like bool, where `!x` must handle both int and bool.
It will also become incompatible with int32 and other strict integers.
This will clash with structure methods, struggle with proper generics,
and become entirely impractical for union types.
This PR completely rewrites the type system targeting the future.
1) type of any expression is inferred and never changed
2) this is available because dependent expressions already inferred
3) forall completely removed, generic functions introduced
(they work like template functions actually, instantiated while inferring)
4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)`
5) `as` keyword, for example `t.tupleAt(0) as int`
6) methods binding is done along with type inferring, not before
("before", as worked previously, was always a wrong approach)
2024-12-30 15:31:27 +00:00
|
|
|
return const_cast<SrcFile*>(all_src_files[++last_parsed_file_id]);
|
2024-10-31 07:04:58 +00:00
|
|
|
}
|
2024-10-31 06:59:23 +00:00
|
|
|
|
2024-10-31 07:16:19 +00:00
|
|
|
bool SrcFile::is_stdlib_file() const {
|
|
|
|
std::string_view rel(rel_filename);
|
|
|
|
return rel.size() > 10 && rel.substr(0, 8) == "@stdlib/"; // common.tolk, tvm-dicts.tolk, etc
|
|
|
|
}
|
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
bool SrcFile::is_offset_valid(int offset) const {
|
|
|
|
return offset >= 0 && offset < static_cast<int>(text.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
SrcFile::SrcPosition SrcFile::convert_offset(int offset) const {
|
|
|
|
if (!is_offset_valid(offset)) {
|
|
|
|
return SrcPosition{offset, -1, -1, "invalid offset"};
|
|
|
|
}
|
|
|
|
|
|
|
|
int line_idx = 0;
|
|
|
|
int char_idx = 0;
|
|
|
|
int line_offset = 0;
|
|
|
|
for (int i = 0; i < offset; ++i) {
|
|
|
|
char c = text[i];
|
|
|
|
if (c == '\n') {
|
|
|
|
line_idx++;
|
|
|
|
char_idx = 0;
|
|
|
|
line_offset = i + 1;
|
|
|
|
} else {
|
|
|
|
char_idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t line_len = text.size() - line_offset;
|
|
|
|
for (int i = line_offset; i < static_cast<int>(text.size()); ++i) {
|
|
|
|
if (text[i] == '\n') {
|
|
|
|
line_len = i - line_offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view line_str(text.data() + line_offset, line_len);
|
|
|
|
return SrcPosition{offset, line_idx + 1, char_idx + 1, line_str};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const SrcFile* src_file) {
|
|
|
|
return os << (src_file ? src_file->rel_filename : "unknown-location");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const Fatal& fatal) {
|
|
|
|
return os << fatal.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
const SrcFile* SrcLocation::get_src_file() const {
|
2024-10-31 07:02:01 +00:00
|
|
|
return G.all_src_files.find_file(file_id);
|
2024-10-31 06:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrcLocation::show(std::ostream& os) const {
|
|
|
|
const SrcFile* src_file = get_src_file();
|
|
|
|
os << src_file;
|
|
|
|
if (src_file && src_file->is_offset_valid(char_offset)) {
|
|
|
|
SrcFile::SrcPosition pos = src_file->convert_offset(char_offset);
|
|
|
|
os << ':' << pos.line_no << ':' << pos.char_no;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrcLocation::show_context(std::ostream& os) const {
|
|
|
|
const SrcFile* src_file = get_src_file();
|
|
|
|
if (!src_file || !src_file->is_offset_valid(char_offset)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SrcFile::SrcPosition pos = src_file->convert_offset(char_offset);
|
2025-02-11 18:52:01 +00:00
|
|
|
os << std::right << std::setw(4) << pos.line_no << " | ";
|
|
|
|
os << pos.line_str << "\n";
|
2024-10-31 06:59:23 +00:00
|
|
|
|
2025-02-11 18:52:01 +00:00
|
|
|
os << " " << " | ";
|
2024-10-31 06:59:23 +00:00
|
|
|
for (int i = 1; i < pos.char_no; ++i) {
|
|
|
|
os << ' ';
|
|
|
|
}
|
|
|
|
os << '^' << "\n";
|
|
|
|
}
|
|
|
|
|
2024-10-31 07:04:58 +00:00
|
|
|
std::string SrcLocation::to_string() const {
|
|
|
|
std::ostringstream os;
|
|
|
|
show(os);
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
std::ostream& operator<<(std::ostream& os, SrcLocation loc) {
|
|
|
|
loc.show(os);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrcLocation::show_general_error(std::ostream& os, const std::string& message, const std::string& err_type) const {
|
|
|
|
show(os);
|
|
|
|
if (!err_type.empty()) {
|
|
|
|
os << ": " << err_type;
|
|
|
|
}
|
|
|
|
os << ": " << message << std::endl;
|
|
|
|
show_context(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrcLocation::show_note(const std::string& err_msg) const {
|
|
|
|
show_general_error(std::cerr, err_msg, "note");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrcLocation::show_warning(const std::string& err_msg) const {
|
|
|
|
show_general_error(std::cerr, err_msg, "warning");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrcLocation::show_error(const std::string& err_msg) const {
|
|
|
|
show_general_error(std::cerr, err_msg, "error");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const ParseError& error) {
|
|
|
|
error.show(os);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParseError::show(std::ostream& os) const {
|
2025-02-11 18:52:01 +00:00
|
|
|
os << loc << ": error: " << message << std::endl;
|
|
|
|
if (current_function) {
|
|
|
|
os << " // in function `" << current_function->as_human_readable() << "`" << std::endl;
|
|
|
|
}
|
|
|
|
loc.show_context(os);
|
2024-10-31 06:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace tolk
|