-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add most the pass * finish typecheck * code change * update readme * update readme
- Loading branch information
1 parent
14cc99a
commit 4c83ea2
Showing
34 changed files
with
462 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include "ast/ast.h" | ||
#include "ast/passes/pass.h" | ||
#include "ast/symbol/symbol_table.h" | ||
|
||
class Builtin : public Pass { | ||
private: | ||
shared_ptr<SymbolTable> symbol_table; | ||
bool is_builtin(std::string name); | ||
|
||
public: | ||
explicit Builtin(shared_ptr<SymbolTable> symbol_table); | ||
void visit_call(shared_ptr<ast::Call>) override; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include "ast/ast.h" | ||
#include "ast/passes/pass.h" | ||
#include "ast/symbol/symbol.h" | ||
#include "ast/symbol/symbol_table.h" | ||
|
||
class Reference : public Pass { | ||
private: | ||
shared_ptr<SymbolTable> symbol_table; | ||
bool is_builtin(std::string name); | ||
|
||
public: | ||
explicit Reference(shared_ptr<SymbolTable> symbol_table); | ||
void visit_variable(shared_ptr<ast::Variable>) override; | ||
void visit_call(shared_ptr<ast::Call>) override; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include <stack> | ||
#include "ast/ast.h" | ||
#include "ast/passes/pass.h" | ||
|
||
class TypeCheck : public Pass { | ||
private: | ||
std::stack<shared_ptr<ast::Function>> func_stack; | ||
void check_numeric(Type type, size_t line); | ||
void check_bool(Type type, size_t line); | ||
|
||
public: | ||
explicit TypeCheck(); | ||
void visit_declaration(shared_ptr<ast::Declaration>) override; | ||
void visit_function(shared_ptr<ast::Function>) override; | ||
void visit_call(shared_ptr<ast::Call>) override; | ||
void visit_return(shared_ptr<ast::Return>) override; | ||
void visit_binary_operator(shared_ptr<ast::BinaryOperator>) override; | ||
void visit_unary_operator(shared_ptr<ast::UnaryOperator>) override; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
#include <ostream> | ||
#include <sstream> | ||
|
||
class CompileTimeException : public std::exception { | ||
protected: | ||
std::string msg; | ||
|
||
public: | ||
const char* what() const noexcept override { return msg.c_str(); } | ||
}; | ||
|
||
#define DEF_COMPILE_TIME_EXCEPTION(NAME) \ | ||
class NAME : public CompileTimeException { \ | ||
public: \ | ||
NAME(unsigned line, const std::string& description) { \ | ||
std::stringstream buf; \ | ||
buf << #NAME << " on Line " << line << ": " << description \ | ||
<< std::endl; \ | ||
msg = buf.str(); \ | ||
} \ | ||
} | ||
|
||
DEF_COMPILE_TIME_EXCEPTION(MainError); | ||
|
||
DEF_COMPILE_TIME_EXCEPTION(SymbolError); | ||
|
||
DEF_COMPILE_TIME_EXCEPTION(SyntaxError); | ||
|
||
DEF_COMPILE_TIME_EXCEPTION(TypeError); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "ast/passes/builtin.h" | ||
#include "ast/symbol/function_symbol.h" | ||
|
||
Builtin::Builtin(shared_ptr<SymbolTable> symbol_table) : Pass("Builtin") { | ||
this->symbol_table = symbol_table; | ||
} | ||
|
||
void Builtin::visit_call(shared_ptr<ast::Call> node) { | ||
std::string name = node->get_name(); | ||
if (!is_builtin(name)) { | ||
return; | ||
} | ||
|
||
name += "_" + node->arguments[0]->get_type()->get_name(); | ||
std::optional<SymbolPtr> var = symbol_table->resolve(name); | ||
if (!var.has_value()) { | ||
throw std::runtime_error("found undefined function: " + name); | ||
} | ||
|
||
shared_ptr<FunctionSymbol> vs = | ||
dynamic_pointer_cast<FunctionSymbol>(var.value()); | ||
if (vs == nullptr) { | ||
throw std::runtime_error("found non function in symbol table"); | ||
} | ||
|
||
node->set_function(vs->function); | ||
} | ||
|
||
bool Builtin::is_builtin(std::string name) { | ||
if (name == "print" || name == "println") { | ||
return true; | ||
} | ||
|
||
return false; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.