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

📄 tools.c

📁 北京航空航天大学计算机系要求的编译器大作业。大概6000行代码。
💻 C
字号:
/***************************************
By BHU 35060122 Peng Hui;
2008.02
description:
A small compiler to translate a C program to 80X80 Assembly Code
***************************************/

#include"Global.h"

char *toString(int n)
{
	static char str[5];
	int i = 0;
	char c;
	int x = n;
	if(n == 0)
	{
		return "0";
	}
	else
	{
		while(x > 0)
		{
			c = x%10 + '0' - 0;
			x =  x/10;
			str[i++] = c;
		}

		x = i-1;
		for(i = 0; i <= x/2; i++)  // 转置字符串
		{
			c = str[i];
			str[i] = str[x-i];
			str[x-i] = c;
		}
		str[x+1] = '\0';
		return str;
	}
}

int parseInt(char *str)
{
	int i = 0;
	int start;
	int l;   // 表示字符串的长度
	int value = 0;
	int sw = 1;

	l = strlen(str);

	if(str[0] == '-')
	{
		start = 1;
		sw = -1;
	}
	else if(str[0] == '+')
	{
		start = 1;
	}
	else
		start = 0;

	for(i = start; i < l; i++)
	{
		if(str[i] < '0' || str[i] > '9')
		{
			return 0;
		}
		else
		{
			value *= 10;
			value += str[i] - '0';
		}
	}

	return (sw*value);
}

char *NextTemp()
{
	static char cons[10] = "t_t"; 

	int i = 1;
	char str[5];
	strcpy(str,toString(serial++));
	for(i = 3;;i++)
	{
		cons[i] = str[i-3];
		if(str[i-1] == '\0')
		{
			break;
		}
	}
	
	return cons;
}

void Enter(Kind kind,Type type,char *name,int value,int level)
{
	Table *temp;

	temp = (Table *)malloc(sizeof(Table));
	temp->kind = kind;
	temp->lev = level;
	temp->value = value;
	temp->type = type;
	strcpy(temp->name,name);
	temp->next = NULL;
	if(head1 == NULL)
	{
	//	current = temp;
	//	head1 = temp;
		head1 = (Table *)malloc(sizeof(Table));
		head1->next = temp;
		current = temp;
	}
	else
	{
		current->next = temp;
		current = temp;
	}
	if(kind == FUNC)
	{
		head2 = current;
	}
}

void genMCode(char *op,char *add1,char *add2,char *add3)
{
	MCode *t;
	t = (MCode *)malloc(sizeof(MCode));
	t->next = NULL;
	strcpy(t->op,op);
	strcpy(t->ad1,add1);
	strcpy(t->ad2,add2);
	strcpy(t->ad3,add3);
	strcpy(t->label,"");

	if(head == NULL)
	{
		head = t;
		tc = t;
	}
	else
	{
		tc->next = t;
		tc = tc->next;
	}
}
char *NextLabel()
{
	static char cons[5] = "L"; 
	int i = 1;
	char str[5];
	strcpy(str,toString(serial1++));
	for(i = 1;;i++)
	{
		cons[i] = str[i-1];
		if(str[i-1] == '\0')
			break;
	}
	return cons;
}


char *NextStr()
{
	static char cons[10] = "_str_"; 
	int i = 1;
	char str[5];
	strcpy(str,toString(serial2++));
	for(i = 1;;i++)
	{
		cons[i] = str[i-1];
		if(str[i-1] == '\0')
			break;
	}
	return cons;
}


void Destroy(AddTable* head)
{
	if(head == NULL)
		return;
	else
	{
		Destroy(head->next);
		free(head);
	}
}

⌨️ 快捷键说明

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