📄 tinyc.c
字号:
/* 张皖龙 @ SEU */
#include "Globals.h"
#include "Util.h"
#include "Scan.h"
#include "Parse.h"
#include "Symtab.h"
#include "Analyze.h"
#include "CodeGen.h"
#define NO_ANALYZE FALSE
/* 分配全局变量 */
int lineno = 0;
FILE * source;
FILE * listing;
FILE * inter_code;
/* 设置跟踪标记 */
int TraceScan = TRUE;
int TraceParse = TRUE;
int TraceAnalyze = TRUE;
int Error = FALSE;
int main(int argc, char * argv[])
{
TreeNode * syntaxTree;
char cfile[20], codefile[20]; /* 文件名 */
if(argc != 2)
{
fprintf(stderr,"使用命令: %s <filename>\n", argv[0]);
exit(1);
}
strcpy(codefile, argv[1]);
if (strchr(codefile, '.') != NULL)
{
int len = strcspn(codefile, ".");
strncpy(cfile, codefile, len);
cfile[len] = '\0';
}
else
{
strcpy(cfile, codefile);
strcat(codefile, ".c");
}
source = fopen(codefile,"r");
if (source==NULL)
{
printf("文件 %s 没找到!\n", codefile);
exit(1);
}
strcpy(codefile, cfile);
strcat(codefile, ".lst");
listing = fopen(codefile,"w");
if (listing == NULL)
{
printf("打不开 %s 文件\n", codefile);
exit(1);
}
/* 编译开始 */
syntaxTree = parse();
if (TraceParse)
{
fprintf(listing,"\n语法树:\n");
printTree(syntaxTree);
}
#if !NO_ANALYZE //如果还没分析
if (!Error)
{
fprintf(listing,"\n构建符号表...\n");
buildSymtab(syntaxTree);
fprintf(listing,"\n检查类型...\n");
typeCheck(syntaxTree);
fprintf(listing,"\n类型检查完毕!\n");
}
#if !NO_CODE //如果还没生成汇编码
if (!Error)
{
strcpy(codefile, cfile);
strcat(codefile, ".asm");
inter_code = fopen(codefile,"w");
if (inter_code == NULL)
{
printf("无法打开 %s 文件\n", codefile);
exit(1);
}
pTable = GlobalTable;
StackSegmentBegin();
StackSegmentEnd();
CodeSegmentBegin();
write();
CodeGen_TreeNode(syntaxTree);
CodeSegmentEnd();
fclose(inter_code);
}
#endif
#endif
fclose(source);
fclose(listing);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -