ready to write type tracker

This commit is contained in:
2024-08-14 09:57:42 +00:00
parent 1ba056f0ac
commit 7995862a30
5 changed files with 182 additions and 14 deletions

View File

@ -1,12 +1,26 @@
#include <functional>
#include "astnode_visitor.h"
#include "scope.hpp"
#include "tools.h"
// Structural AST Nodes
void ASTSemanticCheckVisitor::ActuralVisit(FuncDef_ASTNode *node) { node->func_body->accept(this); }
void ASTSemanticCheckVisitor::ActuralVisit(FuncDef_ASTNode *node) {
is_in_func = true;
cur_func_schema = std::dynamic_pointer_cast<FunctionScope>(node->current_scope)->schema;
std::cerr << "enter function " << node->func_name << std::endl;
node->func_body->accept(this);
std::cerr << "leave function " << node->func_name << std::endl;
is_in_func = false;
}
void ASTSemanticCheckVisitor::ActuralVisit(ClassDef_ASTNode *node) {
for (auto var : node->member_variables) {
var->accept(this);
}
for (auto ch : node->sorted_children) {
ch->accept(this);
if (std::dynamic_pointer_cast<DefinitionStatement_ASTNode>(ch) == nullptr) {
ch->accept(this);
}
}
}
@ -22,9 +36,22 @@ void ASTSemanticCheckVisitor::ActuralVisit(EmptyStatement_ASTNode *node) {}
void ASTSemanticCheckVisitor::ActuralVisit(DefinitionStatement_ASTNode *node) {
auto cur_scope = node->current_scope;
for (const auto &var : node->vars) {
std::string base_type;
if (std::holds_alternative<IdentifierType>(node->var_type)) {
base_type = std::get<IdentifierType>(node->var_type);
} else {
base_type = std::get<ArrayType>(node->var_type).basetype;
}
if (!ClassExists(base_type)) {
throw SemanticError("Undefined class " + base_type, 1);
}
if (!cur_scope->add_variable(var.first, node->var_type)) {
throw SemanticError("Variable redefinition for " + var.first, 1);
}
if (var.second) {
var.second->accept(this);
// TODO type check
}
}
}
@ -32,6 +59,7 @@ void ASTSemanticCheckVisitor::ActuralVisit(ExprStatement_ASTNode *node) { node->
void ASTSemanticCheckVisitor::ActuralVisit(IfStatement_ASTNode *node) {
node->condition->accept(this);
// TODO type check
node->if_clause->accept(this);
if (node->has_else_clause) {
node->else_clause->accept(this);
@ -40,7 +68,10 @@ void ASTSemanticCheckVisitor::ActuralVisit(IfStatement_ASTNode *node) {
void ASTSemanticCheckVisitor::ActuralVisit(WhileStatement_ASTNode *node) {
node->condition->accept(this);
// TODO type check
loop_level++;
node->loop_body->accept(this);
loop_level--;
}
void ASTSemanticCheckVisitor::ActuralVisit(ForStatement_ASTNode *node) {
@ -49,14 +80,22 @@ void ASTSemanticCheckVisitor::ActuralVisit(ForStatement_ASTNode *node) {
}
if (node->condition) {
node->condition->accept(this);
// TODO type check
}
if (node->update) {
node->update->accept(this);
}
loop_level++;
node->loop_body->accept(this);
loop_level--;
}
void ASTSemanticCheckVisitor::ActuralVisit(JmpStatement_ASTNode *node) {}
void ASTSemanticCheckVisitor::ActuralVisit(JmpStatement_ASTNode *node) {
if (loop_level == 0 && node->jmp_type > 0) throw SemanticError("Jump statement outside loop", 1);
if (node->jmp_type == 0) {
// TODO : return type check
}
}
void ASTSemanticCheckVisitor::ActuralVisit(SuiteStatement_ASTNode *node) {
for (auto ch : node->statements) {
@ -67,112 +106,198 @@ void ASTSemanticCheckVisitor::ActuralVisit(SuiteStatement_ASTNode *node) {
// Expression AST Nodes
void ASTSemanticCheckVisitor::ActuralVisit(NewArrayExpr_ASTNode *node) {
// TODO: Implement this method
for (size_t i = 0; i < node->dim_size.size(); i++) {
if (node->dim_size[i]) {
node->dim_size[i]->accept(this);
// TODO type check
}
}
if (node->has_initial_value) {
node->initial_value->accept(this);
// TODO type check
}
}
void ASTSemanticCheckVisitor::ActuralVisit(NewConstructExpr_ASTNode *node) {
// TODO: Implement this method
}
void ASTSemanticCheckVisitor::ActuralVisit(NewConstructExpr_ASTNode *node) {}
void ASTSemanticCheckVisitor::ActuralVisit(NewExpr_ASTNode *node) {
// TODO: Implement this method
}
void ASTSemanticCheckVisitor::ActuralVisit(NewExpr_ASTNode *node) {}
void ASTSemanticCheckVisitor::ActuralVisit(AccessExpr_ASTNode *node) {
// TODO: Implement this method
node->base->accept(this);
// TODO: member check
if (node->is_function) {
// TODO arg number check
for (auto arg : node->arguments) {
arg->accept(this);
// TODO type check
}
}
}
void ASTSemanticCheckVisitor::ActuralVisit(IndexExpr_ASTNode *node) {
// TODO: Implement this method
node->base->accept(this);
// TODO: dimension check
for (auto idx : node->indices) {
idx->accept(this);
// TODO type check
}
}
void ASTSemanticCheckVisitor::ActuralVisit(SuffixExpr_ASTNode *node) {
// TODO: Implement this method
node->base->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(PrefixExpr_ASTNode *node) {
// TODO: Implement this method
node->base->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(OppositeExpr_ASTNode *node) {
// TODO: Implement this method
node->base->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(LNotExpr_ASTNode *node) {
// TODO: Implement this method
node->base->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(BNotExpr_ASTNode *node) {
// TODO: Implement this method
node->base->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(MDMExpr_ASTNode *node) {
// TODO: Implement this method
node->left->accept(this);
node->right->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(PMExpr_ASTNode *node) {
// TODO: Implement this method
node->left->accept(this);
node->right->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(RLExpr_ASTNode *node) {
// TODO: Implement this method
node->left->accept(this);
node->right->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(GGLLExpr_ASTNode *node) {
// TODO: Implement this method
node->left->accept(this);
node->right->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(NEExpr_ASTNode *node) {
// TODO: Implement this method
node->left->accept(this);
node->right->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(BAndExpr_ASTNode *node) {
// TODO: Implement this method
node->left->accept(this);
node->right->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(BXorExpr_ASTNode *node) {
// TODO: Implement this method
node->left->accept(this);
node->right->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(BOrExpr_ASTNode *node) {
// TODO: Implement this method
node->left->accept(this);
node->right->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(LAndExpr_ASTNode *node) {
// TODO: Implement this method
node->left->accept(this);
node->right->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(LOrExpr_ASTNode *node) {
// TODO: Implement this method
node->left->accept(this);
node->right->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(TernaryExpr_ASTNode *node) {
// TODO: Implement this method
node->condition->accept(this);
// TODO: type check
node->src1->accept(this);
node->src2->accept(this);
// TODO: type check
}
void ASTSemanticCheckVisitor::ActuralVisit(AssignExpr_ASTNode *node) {
// TODO: Implement this method
node->dest->accept(this);
node->src->accept(this);
// TODO: check type and assignability
}
void ASTSemanticCheckVisitor::ActuralVisit(ThisExpr_ASTNode *node) {
// TODO: Implement this method
}
void ASTSemanticCheckVisitor::ActuralVisit(ThisExpr_ASTNode *node) {}
void ASTSemanticCheckVisitor::ActuralVisit(ParenExpr_ASTNode *node) {
// TODO: Implement this method
node->expr->accept(this);
node->expr_type_info = node->expr->expr_type_info;
}
void ASTSemanticCheckVisitor::ActuralVisit(IDExpr_ASTNode *node) {
// TODO: Implement this method
// TODO: process type
node->expr_type_info = node->current_scope->fetch_varaible(node->id);
}
void ASTSemanticCheckVisitor::ActuralVisit(FunctionCallExpr_ASTNode *node) {
// TODO: Implement this method
// TODO: check function existence and arg number
for (auto arg : node->arguments) {
arg->accept(this);
// TODO: type check
}
}
void ASTSemanticCheckVisitor::ActuralVisit(FormattedStringExpr_ASTNode *node) {
// TODO: Implement this method
for (auto arg : node->exprs) {
arg->accept(this);
// TODO: type check
}
}
void ASTSemanticCheckVisitor::ActuralVisit(ConstantExpr_ASTNode *node) {
// TODO: Implement this method
// type process
if (std::holds_alternative<IdentifierType>(node->expr_type_info)) {
return;
} else {
;
}
}

View File

@ -35,6 +35,8 @@ std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNod
}
}
ASTSemanticCheckVisitor visitor;
visitor.global_scope = global_scope;
global_scope->classes["string"] = nullptr; // TODO: add string class
visitor.visit(src.get());
return src;
}

View File

@ -675,7 +675,8 @@ std::any Visitor::visitNew_array_expression(MXParser::New_array_expressionContex
new_array->has_initial_value = true;
new_array->initial_value = std::dynamic_pointer_cast<ConstantExpr_ASTNode>(
std::any_cast<std::shared_ptr<Expr_ASTNode>>(visit(context->constant())));
}
} else
new_array->has_initial_value = false;
nodetype_stk.pop_back();
return std::static_pointer_cast<Expr_ASTNode>(new_array);