refactor(frontend): 添加对只编译前端的支持

This commit is contained in:
jing
2026-03-17 18:18:55 +08:00
parent 10ea8aad14
commit a091d9108a
6 changed files with 126 additions and 62 deletions

View File

@@ -3,17 +3,27 @@
add_subdirectory(utils)
add_subdirectory(ir)
add_subdirectory(frontend)
add_subdirectory(sem)
add_subdirectory(irgen)
add_subdirectory(mir)
if(NOT COMPILER_PARSE_ONLY)
add_subdirectory(sem)
add_subdirectory(irgen)
add_subdirectory(mir)
endif()
add_executable(compiler
main.cpp
)
target_link_libraries(compiler PRIVATE
frontend
sem
irgen
mir
utils
)
if(NOT COMPILER_PARSE_ONLY)
target_link_libraries(compiler PRIVATE
sem
irgen
mir
)
target_compile_definitions(compiler PRIVATE COMPILER_PARSE_ONLY=0)
else()
target_compile_definitions(compiler PRIVATE COMPILER_PARSE_ONLY=1)
endif()

View File

@@ -3,18 +3,66 @@
// 的最小返回表达式编译。
// 后续需要自行添加
grammar SysY;
/*===-------------------------------------------===*/
/* Lexer rules */
/*===-------------------------------------------===*/
INT: 'int';
RETURN: 'return';
ASSIGN: '=';
ADD: '+';
LPAREN: '(';
RPAREN: ')';
LBRACE: '{';
RBRACE: '}';
SEMICOLON: ';';
ID: [a-zA-Z_][a-zA-Z_0-9]*;
ILITERAL: [0-9]+;
WS: [ \t\r\n] -> skip;
LINECOMMENT: '//' ~[\r\n]* -> skip;
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
/*===-------------------------------------------===*/
/* Syntax rules */
/*===-------------------------------------------===*/
compUnit
: funcDef EOF
;
funcDef
: Int Ident L_PAREN R_PAREN block
decl
: btype varDef SEMICOLON
;
block
: L_BRACE blockItem* R_BRACE
btype
: INT
;
varDef
: lValue (ASSIGN initValue)?
;
initValue
: exp
;
funcDef
: funcType ID LPAREN RPAREN blockStmt
;
funcType
: INT
;
blockStmt
: LBRACE blockItem* RBRACE
;
blockItem
@@ -22,63 +70,29 @@ blockItem
| stmt
;
decl
: varDecl
;
stmt
: returnStmt
;
varDecl
: Int Ident (Assign exp)? Semi
;
returnStmt
: Return exp Semi
: RETURN exp SEMICOLON
;
exp
: addExp
: LPAREN exp RPAREN # parenExp
| var # varExp
| number # numberExp
| exp ADD exp # additiveExp
;
addExp
: primary (AddOp primary)*
var
: ID
;
primary
: Number
| Ident
| L_PAREN exp R_PAREN
lValue
: ID
;
Int : 'int';
Return : 'return';
AddOp : '+';
Assign : '=';
Semi : ';';
L_PAREN : '(';
R_PAREN : ')';
L_BRACE : '{';
R_BRACE : '}';
Ident
: [a-zA-Z_][a-zA-Z_0-9]*
;
Number
: [0-9]+
;
WS
: [ \t\r\n]+ -> skip
;
COMMENT
: '//' ~[\r\n]* -> skip
;
BLOCK_COMMENT
: '/*' .*? '*/' -> skip
number
: ILITERAL
;

View File

@@ -4,10 +4,12 @@
#include "frontend/AntlrDriver.h"
#include "frontend/SyntaxTreePrinter.h"
#if !COMPILER_PARSE_ONLY
#include "ir/IR.h"
#include "irgen/IRGen.h"
#include "mir/MIR.h"
#include "sem/Sema.h"
#endif
#include "utils/CLI.h"
#include "utils/Log.h"
@@ -26,6 +28,7 @@ int main(int argc, char** argv) {
need_blank_line = true;
}
#if !COMPILER_PARSE_ONLY
auto* comp_unit = dynamic_cast<SysYParser::CompUnitContext*>(antlr.tree);
if (!comp_unit) {
throw std::runtime_error(FormatError("main", "语法树根节点不是 compUnit"));
@@ -51,6 +54,12 @@ int main(int argc, char** argv) {
}
mir::PrintAsm(*machine_func, std::cout);
}
#else
if (opts.emit_ir || opts.emit_asm) {
throw std::runtime_error(
FormatError("main", "当前为 parse-only 构建IR/汇编输出已禁用"));
}
#endif
} catch (const std::exception& ex) {
PrintException(std::cerr, ex);
return 1;