forked from NUDT-compiler/nudt-compiler-cpp
Compare commits
No commits in common. 'lab2' and 'master' have entirely different histories.
@ -1 +0,0 @@
|
|||||||
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
||||||
cd "$ROOT"
|
|
||||||
|
|
||||||
# 生成ANTLR解析器代码
|
|
||||||
echo "生成ANTLR解析器代码..."
|
|
||||||
java -jar "$ROOT/third_party/antlr-4.13.2-complete.jar" -Dlanguage=Cpp -visitor -o "$ROOT/build/generated/antlr4" "$ROOT/src/antlr4/SysY.g4"
|
|
||||||
|
|
||||||
|
|
||||||
# 构建项目
|
|
||||||
echo "构建项目..."
|
|
||||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
|
|
||||||
cmake --build build --parallel
|
|
||||||
|
|
||||||
echo "恭喜通关"
|
|
||||||
@ -1,50 +1,39 @@
|
|||||||
#include "irgen/IRGen.h"
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
#include <any>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "RuleContext.h"
|
|
||||||
#include "SysYParser.h"
|
#include "SysYParser.h"
|
||||||
#include "ir/IR.h"
|
#include "ir/IR.h"
|
||||||
#include "utils/Log.h"
|
#include "utils/Log.h"
|
||||||
|
|
||||||
|
// 语句生成当前只实现了最小子集。
|
||||||
|
// 目前支持:
|
||||||
|
// - return <exp>;
|
||||||
|
//
|
||||||
|
// 还未支持:
|
||||||
|
// - 赋值语句
|
||||||
|
// - if / while 等控制流
|
||||||
|
// - 空语句、块语句嵌套分发之外的更多语句形态
|
||||||
|
|
||||||
std::any IRGenImpl::visitStmt(SysYParser::StmtContext* ctx) {
|
std::any IRGenImpl::visitStmt(SysYParser::StmtContext* ctx) {
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
throw std::runtime_error(FormatError("irgen", "缺少语句"));
|
throw std::runtime_error(FormatError("irgen", "缺少语句"));
|
||||||
}
|
}
|
||||||
|
if (ctx->returnStmt()) {
|
||||||
if (auto* r = dynamic_cast<SysYParser::ReturnStmtContext*>(ctx->returnStmt())) {
|
return ctx->returnStmt()->accept(this);
|
||||||
return visitReturnStmt(r);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 兼容不同 ANTLR 版本的遍历兜底
|
|
||||||
for (auto* ch : ctx->children) {
|
|
||||||
if (auto* rs = dynamic_cast<SysYParser::ReturnStmtContext*>(
|
|
||||||
dynamic_cast<antlr4::RuleContext*>(ch))) {
|
|
||||||
return visitReturnStmt(rs);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
throw std::runtime_error(FormatError("irgen", "暂不支持的语句类型"));
|
||||||
throw std::runtime_error(FormatError("irgen", "暂不支持的语句"));
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::any IRGenImpl::visitReturnStmt(SysYParser::ReturnStmtContext* ctx) {
|
std::any IRGenImpl::visitReturnStmt(SysYParser::ReturnStmtContext* ctx) {
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
throw std::runtime_error(FormatError("irgen", "缺少 return"));
|
throw std::runtime_error(FormatError("irgen", "缺少 return 语句"));
|
||||||
}
|
}
|
||||||
if (current_func_void_) {
|
if (!ctx->exp()) {
|
||||||
if (ctx->exp()) {
|
throw std::runtime_error(FormatError("irgen", "return 缺少表达式"));
|
||||||
throw std::runtime_error(FormatError("irgen", "void return 带值"));
|
|
||||||
}
|
|
||||||
// 修复点:原版的 CreateRetVoid() 名字错了,正确的 API 是 CreateRet()
|
|
||||||
builder_.CreateRet(nullptr);
|
|
||||||
} else {
|
|
||||||
if (!ctx->exp()) {
|
|
||||||
throw std::runtime_error(FormatError("irgen", "return 缺表达式"));
|
|
||||||
}
|
|
||||||
ir::Value* v = EvalExpr(*ctx->exp());
|
|
||||||
builder_.CreateRet(v);
|
|
||||||
}
|
}
|
||||||
return std::any(BlockFlow::Terminated);
|
ir::Value* v = EvalExpr(*ctx->exp());
|
||||||
|
builder_.CreateRet(v);
|
||||||
|
return BlockFlow::Terminated;
|
||||||
}
|
}
|
||||||
Loading…
Reference in new issue