Add errors ection to Part 4 of compiler posts
This commit is contained in:
30
04/main.cpp
30
04/main.cpp
@@ -1,7 +1,8 @@
|
||||
#include "ast.hpp"
|
||||
#include "parser.hpp"
|
||||
#include "type.hpp"
|
||||
#include <iostream>
|
||||
#include "parser.hpp"
|
||||
#include "error.hpp"
|
||||
#include "type.hpp"
|
||||
|
||||
void yy::parser::error(const std::string& msg) {
|
||||
std::cout << "An error occured: " << msg << std::endl;
|
||||
@@ -9,10 +10,9 @@ void yy::parser::error(const std::string& msg) {
|
||||
|
||||
extern std::vector<definition_ptr> program;
|
||||
|
||||
void typecheck_program(const std::vector<definition_ptr>& prog) {
|
||||
type_mgr mgr;
|
||||
type_env env;
|
||||
|
||||
void typecheck_program(
|
||||
const std::vector<definition_ptr>& prog,
|
||||
type_mgr& mgr, type_env& env) {
|
||||
type_ptr int_type = type_ptr(new type_base("Int"));
|
||||
type_ptr binop_type = type_ptr(new type_arr(
|
||||
int_type,
|
||||
@@ -40,6 +40,9 @@ void typecheck_program(const std::vector<definition_ptr>& prog) {
|
||||
|
||||
int main() {
|
||||
yy::parser parser;
|
||||
type_mgr mgr;
|
||||
type_env env;
|
||||
|
||||
parser.parse();
|
||||
for(auto& definition : program) {
|
||||
definition_defn* def = dynamic_cast<definition_defn*>(definition.get());
|
||||
@@ -51,6 +54,17 @@ int main() {
|
||||
|
||||
def->body->print(1, std::cout);
|
||||
}
|
||||
typecheck_program(program);
|
||||
std::cout << program.size() << std::endl;
|
||||
try {
|
||||
typecheck_program(program, mgr, env);
|
||||
} catch(unification_error& err) {
|
||||
std::cout << "failed to unify types: " << std::endl;
|
||||
std::cout << " (1) \033[34m";
|
||||
err.left->print(mgr, std::cout);
|
||||
std::cout << "\033[0m" << std::endl;
|
||||
std::cout << " (2) \033[32m";
|
||||
err.right->print(mgr, std::cout);
|
||||
std::cout << "\033[0m" << std::endl;
|
||||
} catch(type_error& err) {
|
||||
std::cout << "failed to type check program: " << err.description << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user