Skip to content

Commit

Permalink
修复了调用函数后在中间遇到返回语句无法结束的bug,通过了所有测试用例!撒花!
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinaNuke committed Oct 23, 2021
1 parent 0538a84 commit 6b6356d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions ASTInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ using namespace clang;

#include "Environment.h"

class ReturnException : public std::exception {};

class InterpreterVisitor : public EvaluatedExprVisitor<InterpreterVisitor> {
public:
explicit InterpreterVisitor(const ASTContext &context, Environment *env)
Expand Down Expand Up @@ -71,7 +73,10 @@ class InterpreterVisitor : public EvaluatedExprVisitor<InterpreterVisitor> {
mEnv->enterfunc(call);

// 遍历执行函数体
VisitStmt(call->getDirectCallee()->getBody());
try {
VisitStmt(call->getDirectCallee()->getBody());
} catch (ReturnException e) {
}

// 弹出栈帧并进行返回值绑定
mEnv->exitfunc(call);
Expand All @@ -87,6 +92,7 @@ class InterpreterVisitor : public EvaluatedExprVisitor<InterpreterVisitor> {

// clang/AST/Stmt.h: class ReturnStmt
mEnv->retstmt(ret->getRetValue());
throw ReturnException();
}

virtual void VisitDeclStmt(DeclStmt *declstmt) {
Expand Down Expand Up @@ -195,7 +201,11 @@ class InterpreterConsumer : public ASTConsumer {
mEnv.init(decl);

FunctionDecl *entry = mEnv.getEntry();
mVisitor.VisitStmt(entry->getBody());

try {
mVisitor.VisitStmt(entry->getBody());
} catch (ReturnException e) {
}
}

private:
Expand Down

0 comments on commit 6b6356d

Please sign in to comment.