Add semantic passes
This commit is contained in:
43
cli/cl.cpp
Normal file
43
cli/cl.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include "elna/cli/cl.hpp"
|
||||
#include "elna/source/types.hpp"
|
||||
#include <iostream>
|
||||
|
||||
namespace elna
|
||||
{
|
||||
namespace cli
|
||||
{
|
||||
void print_error(const std::unique_ptr<source::error>& compile_error)
|
||||
{
|
||||
std::cerr << compile_error->path().string() << ":"
|
||||
<< compile_error->line() << ':' << compile_error->column()
|
||||
<< ": " << compile_error->what() << std::endl;
|
||||
}
|
||||
|
||||
constexpr std::size_t pointer_size = 4;
|
||||
|
||||
std::shared_ptr<source::symbol_table> add_builtin_symbols()
|
||||
{
|
||||
source::symbol_table result;
|
||||
std::vector<std::shared_ptr<const source::type>> intrinsic_arguments;
|
||||
|
||||
auto boolean_info = std::make_shared<source::type_info>(source::boolean_type);
|
||||
auto int_info = std::make_shared<source::type_info>(source::int_type);
|
||||
result.enter("Boolean", boolean_info);
|
||||
result.enter("Int", int_info);
|
||||
|
||||
intrinsic_arguments.push_back(int_info->type());
|
||||
auto writei = std::make_shared<source::intrinsic_info>(
|
||||
source::procedure_type{ intrinsic_arguments, pointer_size });
|
||||
result.enter("writei", writei);
|
||||
intrinsic_arguments.clear();
|
||||
|
||||
intrinsic_arguments.push_back(boolean_info->type());
|
||||
auto writeb = std::make_shared<source::intrinsic_info>(
|
||||
source::procedure_type{ intrinsic_arguments, pointer_size });
|
||||
result.enter("writeb", writeb);
|
||||
intrinsic_arguments.clear();
|
||||
|
||||
return std::make_shared<source::symbol_table>(std::move(result));
|
||||
}
|
||||
}
|
||||
}
|
15
cli/main.cpp
15
cli/main.cpp
@ -1,21 +1,22 @@
|
||||
#include <elna/source/driver.hpp>
|
||||
#include "elna/source/semantic.hpp"
|
||||
#include "elna/cli/cl.hpp"
|
||||
#include "parser.hpp"
|
||||
#include <sstream>
|
||||
|
||||
constexpr std::size_t pointer_size = 4;
|
||||
|
||||
int main()
|
||||
{
|
||||
elna::source::driver driver{ "-" };
|
||||
std::istringstream inp(R"(
|
||||
const world = 5, hello = 7;
|
||||
var x: int, y: boolean;
|
||||
var x: Int;
|
||||
|
||||
proc f();
|
||||
begin
|
||||
x := 8
|
||||
end;
|
||||
|
||||
begin
|
||||
while false do inc(5)
|
||||
end.
|
||||
)");
|
||||
|
||||
@ -33,6 +34,12 @@ int main()
|
||||
}
|
||||
return result;
|
||||
}
|
||||
auto symbol_table = elna::cli::add_builtin_symbols();
|
||||
elna::source::name_analysis_visitor name_analysis_visitor{ symbol_table, "-", pointer_size };
|
||||
elna::source::type_analysis_visitor type_analysis_visitor{ symbol_table, "-", pointer_size };
|
||||
|
||||
name_analysis_visitor.visit(driver.tree.get());
|
||||
|
||||
for (auto& definition : driver.tree->definitions())
|
||||
{
|
||||
if (auto const_definition = dynamic_cast<elna::source::constant_definition *>(definition.get()))
|
||||
|
Reference in New Issue
Block a user