From 44c3e27061e018fd77c1ce2b547076a530b372c3 Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Wed, 14 Aug 2024 15:19:04 +0000 Subject: [PATCH] fix const-array --- include/tools.h | 4 ++++ src/ast/semanticvisitor.cpp | 3 ++- src/semantic/visitor.cpp | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/tools.h b/include/tools.h index 3ef8704..cbc7b9e 100644 --- a/include/tools.h +++ b/include/tools.h @@ -68,6 +68,9 @@ struct ArrayType { size_t level; }; inline bool operator==(const ArrayType &l, const ArrayType &r) { + if (!r.has_base_type) { + return l.level >= r.level; + } return l.has_base_type == r.has_base_type && l.basetype == r.basetype && l.level == r.level; } using ExprTypeInfo = std::variant; @@ -90,6 +93,7 @@ inline bool operator==(const ExprTypeInfo &l, const ExprTypeInfo &r) { return std::holds_alternative(r) && std::get(l) == std::get(r); } if (std::holds_alternative(l)) { + bool x = std::holds_alternative(r); return std::holds_alternative(r) && std::get(l) == std::get(r); } throw std::runtime_error("something strange happened"); diff --git a/src/ast/semanticvisitor.cpp b/src/ast/semanticvisitor.cpp index d52f88f..23f17f9 100644 --- a/src/ast/semanticvisitor.cpp +++ b/src/ast/semanticvisitor.cpp @@ -53,6 +53,7 @@ void ASTSemanticCheckVisitor::ActuralVisit(Program_ASTNode *node) { void ASTSemanticCheckVisitor::ActuralVisit(EmptyStatement_ASTNode *node) {} void ASTSemanticCheckVisitor::ActuralVisit(DefinitionStatement_ASTNode *node) { + std::cerr << "visit definition statement" << std::endl; auto cur_scope = node->current_scope; for (const auto &var : node->vars) { std::string base_type; @@ -535,6 +536,6 @@ void ASTSemanticCheckVisitor::ActuralVisit(ConstantExpr_ASTNode *node) { } }; search(node, 0); - node->expr_type_info = ArrayType{true, base_type, found_level}; + node->expr_type_info = ArrayType{found_base_type, base_type, found_level}; } } diff --git a/src/semantic/visitor.cpp b/src/semantic/visitor.cpp index f99ab5e..9688acd 100644 --- a/src/semantic/visitor.cpp +++ b/src/semantic/visitor.cpp @@ -1025,6 +1025,7 @@ std::any Visitor::visitConstant(MXParser::ConstantContext *context) { } constant_expr->value = std::move(tmp); constant_expr->level = max_sub_level + 1; + constant_expr->expr_type_info = ArrayType(); } else { throw std::runtime_error("unknown constant type"); }