forked from NUDT-compiler/nudt-compiler-cpp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.8 KiB
70 lines
1.8 KiB
#ifndef SEM_FUNC_H
|
|
#define SEM_FUNC_H
|
|
|
|
#include <vector>
|
|
|
|
#include "SysYParser.h"
|
|
|
|
namespace sem {
|
|
|
|
// 常量值类型
|
|
struct ConstValue {
|
|
bool is_int; // 是否为整型
|
|
long long int_val; // 整数值
|
|
double float_val; // 浮点值
|
|
};
|
|
|
|
// 编译时求值常量表达式
|
|
ConstValue EvaluateConstExp(SysYParser::ConstExpContext& ctx);
|
|
|
|
// 求值表达式
|
|
ConstValue EvaluateExp(SysYParser::AddExpContext& ctx);
|
|
|
|
// 求值乘法表达式
|
|
ConstValue EvaluateMulExp(SysYParser::MulExpContext& ctx);
|
|
|
|
// 求值一元表达式
|
|
ConstValue EvaluateUnaryExp(SysYParser::UnaryExpContext& ctx);
|
|
|
|
// 求值基本表达式
|
|
ConstValue EvaluatePrimaryExp(SysYParser::PrimaryExpContext& ctx);
|
|
|
|
// 辅助函数:检查值是否为零
|
|
bool IsZero(const ConstValue& val);
|
|
|
|
// 辅助函数:加法
|
|
ConstValue AddValues(const ConstValue& lhs, const ConstValue& rhs);
|
|
|
|
// 辅助函数:减法
|
|
ConstValue SubValues(const ConstValue& lhs, const ConstValue& rhs);
|
|
|
|
// 辅助函数:乘法
|
|
ConstValue MulValues(const ConstValue& lhs, const ConstValue& rhs);
|
|
|
|
// 辅助函数:除法
|
|
ConstValue DivValues(const ConstValue& lhs, const ConstValue& rhs);
|
|
|
|
// 辅助函数:取模
|
|
ConstValue ModValues(const ConstValue& lhs, const ConstValue& rhs);
|
|
|
|
// 辅助函数:取负
|
|
ConstValue NegValue(const ConstValue& val);
|
|
|
|
// 辅助函数:逻辑非
|
|
ConstValue NotValue(const ConstValue& val);
|
|
|
|
// 检查常量初始化器
|
|
size_t CheckConstInitVal(SysYParser::ConstInitValContext& ctx,
|
|
const std::vector<size_t>& dimensions,
|
|
bool is_int,
|
|
size_t total_elements);
|
|
|
|
// 检查变量初始化器
|
|
size_t CheckInitVal(SysYParser::InitValContext& ctx,
|
|
const std::vector<size_t>& dimensions,
|
|
bool is_int,
|
|
size_t total_elements);
|
|
|
|
} // namespace sem
|
|
|
|
#endif // SEM_FUNC_H
|