⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.c

📁 在linux下实行的简单的c语言编译器
💻 C
字号:
#include "Utils.h"/*Print the token when a token is read.*/void printToken(TokenType token, char *tokenString){	switch(token)	{		case INT:		case CHAR:		case FLOAT:		case VOID:		case IF:		case ELSE:		case WHILE:		case RETURN:			fprintf(g_lst_file, "Reserved word: %s\n", tokenString);			break;		case PLUS: fprintf(g_lst_file, "+\n"); break;		case SUB: fprintf(g_lst_file, "-\n"); break;		case MUT: fprintf(g_lst_file, "*\n"); break;		case DIV: fprintf(g_lst_file, "/\n"); break;		case LT: fprintf(g_lst_file, "<\n"); break;		case LE: fprintf(g_lst_file, "<=\n"); break;		case GT: fprintf(g_lst_file, ">\n"); break;		case GE: fprintf(g_lst_file, ">=\n"); break;		case EQ: fprintf(g_lst_file, "==\n"); break;		case NEQ: fprintf(g_lst_file, "!=\n"); break;		case AND: fprintf(g_lst_file, "&&\n"); break;		case OR: fprintf(g_lst_file, "||\n"); break;		case NOT: fprintf(g_lst_file, "!\n"); break;		case ASSIGN: fprintf(g_lst_file, "=\n"); break;		case SEMI: fprintf(g_lst_file, ";\n"); break;		case COMMA: fprintf(g_lst_file, ",\n"); break;		case LP: fprintf(g_lst_file, "(\n"); break;		case RP: fprintf(g_lst_file, ")\n"); break;		case LSP: fprintf(g_lst_file, "[\n"); break;		case RSP: fprintf(g_lst_file, "]\n"); break;		case LFP: fprintf(g_lst_file, "{\n"); break;		case RFP: fprintf(g_lst_file, "}\n"); break;		case ENDFILE: fprintf(g_lst_file, "EOF\n"); break;		case NUM: fprintf(g_lst_file, "INTEGER:%s\n",tokenString); break;		case FNUM: fprintf(g_lst_file, "FLOAT:%s\n",tokenString); break;		case SCHAR: fprintf(g_lst_file, "CHAR:%s\n",tokenString); break;		case ID: fprintf(g_lst_file, "ID:%s\n",tokenString); break;		case ERROR: fprintf(g_lst_file, "ERROR:%s\n",tokenString); break;				default:			fprintf(g_lst_file, "Scan Finished!\n");			break;	}/*switch (token)*/}void printType(Type type){	switch (type)	{		case Void:			fprintf(g_lst_file, "Void");			break;		case Int:			fprintf(g_lst_file, "Int");			break;		case Float:			fprintf(g_lst_file, "Float");			break;		case Char:			fprintf(g_lst_file, "Char");			break;		case Bool:			fprintf(g_lst_file, "Bool");			break;		default:			fprintf(g_lst_file, "Unknow Type");			break;	}/*end switch (type)*/}TreeNode *newDecNode(DecKind kind){	TreeNode *t = (TreeNode*)malloc(sizeof(TreeNode));		int i;	if (t == NULL)		fprintf(g_lst_file, "Out of memory! line %d\n", g_lineno);	else	{		for (i = 0; i < MAXCHILDREN; i++)			t->child[i] = NULL;		t->sibling = NULL;		t->nodeKind = Dec;		t->lineno = g_lineno;		t->call_stmt = 0;		t->kind.dec = kind;	}	return t;	}TreeNode *newStmtNode(StmtKind kind){	TreeNode *t = (TreeNode*)malloc(sizeof(TreeNode));	int i;	if (t == NULL)		fprintf(g_lst_file, "Out of memory! line %d\n", g_lineno);	else	{		for (i = 0; i < MAXCHILDREN; i++)			t->child[i] = NULL;		t->sibling = NULL;		t->nodeKind = Stmt;		t->lineno = g_lineno;		t->call_stmt = 0;		t->kind.stmt = kind;	}	return t;}TreeNode *newExpNode(ExpKind kind){	TreeNode *t = (TreeNode*)malloc(sizeof(TreeNode));	int i;	if (t == NULL)		fprintf(g_lst_file, "Out of memory! line %d\n", g_lineno);	else	{		for (i = 0; i < MAXCHILDREN; i++)			t->child[i] = NULL;		t->sibling = NULL;		t->nodeKind = Exp;		t->lineno = g_lineno;		t->call_stmt = 0;		t->kind.exp = kind;	}	return t;}/*used by printTree to store current number of  spaces to indent*/static s_indentno = 0;/*increase or decrease indentation*/#define INDENT s_indentno+=2#define UNINDENT s_indentno-=2static void printSpaces(){	int i;	for (i = 0; i < s_indentno; i++)		fprintf(g_lst_file, " ");}void printTree(TreeNode *tree){	int i;	INDENT;		while (tree != NULL)	{		printSpaces();			switch (tree->nodeKind)		{		case Dec:			switch (tree->kind.dec)			{			case VarK:				fprintf(g_lst_file, "ID Decl Var_Type: ");				printType(tree->type);				fprintf(g_lst_file, "\n");				break;			case FunDecK:				fprintf(g_lst_file, "Fun Decl name: %s\t Return_Type", tree->attr.name);				printType(tree->type);				fprintf(g_lst_file, "\n");				break;			case FunDefK:				fprintf(g_lst_file, "Fun Def name: %s\t Return_Type: ", tree->attr.name);				printType(tree->type);				fprintf(g_lst_file, "\n");				break;			case ParamK:				fprintf(g_lst_file, "Param Decl Param_Type: ");				printType(tree->type);				fprintf(g_lst_file, "\n");				break;			case CompK:				fprintf(g_lst_file, "Comp Decl \n");				break;		}/*end switch (tree->kind.dec)*/		break;		case Stmt:			switch (tree->kind.stmt)			{			case IfK:				fprintf(g_lst_file, "If\n");				break;			case WhileK:				fprintf(g_lst_file, "While\n");				break;			case AssignK:				fprintf(g_lst_file, "Assign\n");				break;			case ReturnK:				fprintf(g_lst_file, "Return\n");				break;			case CallK:				fprintf(g_lst_file, "Call to: %s\n", tree->attr.name);				break;			case ContinueK:				fprintf(g_lst_file, "Continue\n");				break;			case BreakK:				fprintf(g_lst_file, "Break\n");				break;			}/*end switch (tree->kind.stmt)*/			break;		case Exp:			switch (tree->kind.exp)			{			case NumK:				fprintf(g_lst_file, "Num_Const: %d\n", tree->attr.val.i);				break;			case FNumK:				fprintf(g_lst_file, "Float_Const: %f\n", tree->attr.val.f);				break;			case CharK:				fprintf(g_lst_file, "Char_Const: %c\n", tree->attr.val.i);				break;			case IdK:				fprintf(g_lst_file, "Var: %s\n", tree->attr.name);				break;			case NotK:				fprintf(g_lst_file, "Not: \n");				break;			default:				fprintf(g_lst_file, "op: ");				printToken(tree->attr.op, "\0");			}/*end switch (tree->kind.exp)*/			break;		default:			fprintf(g_lst_file, "Unknown Trenode\n");			break;		}/*end switch (tree->nodeKind)*/					for (i = 0; i < MAXCHILDREN; i++)			printTree(tree->child[i]);		tree = tree->sibling;	}/*end while (tree != NULL)*/	UNINDENT;}void combineFiles(char *filename){	char asmBuf[65000];	char lineBuf[255];	char fileName[255];	char cmd[255];	fclose(g_data_file);	fclose(g_asm_file);	fclose(g_bss_file);	strcpy(fileName, filename);	strcat(fileName, ".b");	g_bss_file = fopen(fileName, "r");	strcpy(fileName, filename);	strcat(fileName, ".d");	g_data_file = fopen(fileName, "r");		strcpy(fileName, filename);	strcat(fileName, ".s");	g_asm_file = fopen(fileName, "r");	while (fgets(lineBuf, 80, g_data_file)!=NULL)		strcat(asmBuf, lineBuf);	while (fgets(lineBuf, 80, g_bss_file) !=NULL)		strcat(asmBuf, lineBuf);	while (fgets(lineBuf, 80, g_asm_file) != NULL)		strcat(asmBuf, lineBuf); 		fclose(g_asm_file);	g_asm_file = fopen(fileName, "w");	fwrite(asmBuf, 1, strlen(asmBuf), g_asm_file);	strcpy(fileName, filename);	strcat(fileName, ".b");	sprintf(cmd, "rm %s", fileName);		system(cmd);		strcpy(fileName, filename);	strcat(fileName, ".d");	sprintf(cmd, "rm %s", fileName);		system(cmd);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -