Skip to content

Commit

Permalink
[cpp] implement semal::output::combine
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed May 12, 2024
1 parent 3aaea2a commit 904361b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cpp/src/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum class error_code
lex,
parse,
type,
semal,
_count
};

Expand All @@ -20,7 +21,8 @@ constexpr std::array<const char*, (int)error_code::_count> error_names
"badargs",
"lex",
"parse",
"type"
"type",
"semantic analysis"
};

#endif // PSYC_ERROR_HPP
23 changes: 23 additions & 0 deletions cpp/src/semal.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
#include "semal.hpp"
#include "diag.hpp"

namespace semal
{
const srcloc& context::location() const
{
return tree->get(this->path).meta;
}

void output::combine(const output& rhs)
{
// add functions, global variables, structs etc... to *this
for(const auto& [name, fn] : rhs.functions)
{
diag::assert_that(!this->functions.contains(name), error_code::semal, "at: {}: duplicate definition of function \"{}\" (previously defined at: {})", this->functions.at(name).ctx.location().to_string(), fn.name, fn.ctx.location().to_string());
this->functions[name] = fn;
}

for(const auto& [name, gvar] : rhs.global_variables)
{
diag::assert_that(!this->global_variables.contains(name), error_code::semal, "at: {}: duplicate definition of global variable \"{}\" (previously defined at: {})", this->functions.at(name).ctx.location().to_string(), gvar.name, gvar.ctx.location().to_string());
this->global_variables[name] = gvar;
}

for(const auto& [name, structdata] : rhs.struct_decls)
{
diag::assert_that(!this->struct_decls.contains(name), error_code::semal, "at: {}: duplicate definition of struct \"{}\" (previously defined at: {})", this->functions.at(name).ctx.location().to_string(), structdata.ty.name, structdata.ctx.location().to_string());
this->struct_decls[name] = structdata;
}
}

output analyse_predecl(const ast& tree)
Expand Down
12 changes: 9 additions & 3 deletions cpp/src/semal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@

namespace semal
{
struct context
{
const ast* tree;
ast::path_t path;
const srcloc& location() const;
};
struct local_variable_t
{
type ty;
std::string name;
ast::path_t context;
context ctx;
// codegen may want to track extra data. however, semal doesnt care about any of this.
mutable void* userdata = nullptr;
};
Expand All @@ -21,7 +27,7 @@ namespace semal
type return_ty;
std::string name;
std::vector<local_variable_t> params = {};
ast::path_t context;
context ctx;
bool is_method = false;
// codegen may want to track extra data. however, semal doesnt care about any of this.
mutable void* userdata = nullptr;
Expand All @@ -30,7 +36,7 @@ namespace semal
struct struct_t
{
struct_type ty;
ast::path_t context;
context ctx;
std::unordered_map<std::string, function_t> methods = {};
// codegen may want to track extra data. however, semal doesnt care about any of this.
mutable void* userdata = nullptr;
Expand Down

0 comments on commit 904361b

Please sign in to comment.