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

📄 stack.c

📁 一个小型的c语言编译器,做的非常好,不过不是我做的.
💻 C
字号:
#include	"stack.h"
#include	"common.h"
#include	"Error.h"
#include	<stdio.h>
#include	<string.h>
#include	<assert.h>

#define		LABEL_STACK_SIZE	64
#define		BREAK_STACK_SIZE	64
#define		CONTINUE_STACK_SIZE	64
#define		SWITCH_STACK_SIZE	64
#define		INCLUDE_STACK_SIZE	8

#define		CHECK_LABEL_STACK_OVERFLOW	assert(sg_label_stack_sp < LABEL_STACK_SIZE);
#define		CHECK_LABEL_STACK_BOTTOM	assert(sg_label_stack_sp > -1);

#define		CHECK_BREAK_STACK_OVERFLOW	assert(sg_break_stack_sp < BREAK_STACK_SIZE);
#define		CHECK_BREAK_STACK_BOTTOM	assert(sg_break_stack_sp > -1);

#define		CHECK_CONTINUE_STACK_OVERFLOW	assert(sg_continue_stack_sp < CONTINUE_STACK_SIZE);
#define		CHECK_CONTINUE_STACK_BOTTOM		assert(sg_continue_stack_sp > -1);

#define		CHECK_SWITCH_STACK_OVERFLOW		assert(sg_switch_stack_sp < SWITCH_STACK_SIZE);
#define		CHECK_SWITCH_STACK_BOTTOM		assert(sg_switch_stack_sp > -1);

#define		CHECK_INCLUDE_STACK_OVERFLOW	assert(sg_include_stack_sp < INCLUDE_STACK_SIZE);
#define		CHECK_INCLUDE_STACK_BOTTOM		assert(sg_include_stack_sp > -1);

static	char	sg_label_stack[LABEL_STACK_SIZE][LABEL_LEN];
static	int		sg_label_stack_sp = -1;

static	char	sg_break_stack[BREAK_STACK_SIZE][LABEL_LEN];
static	int		sg_break_stack_sp = -1;

static	char	sg_continue_stack[CONTINUE_STACK_SIZE][LABEL_LEN];
static	int		sg_continue_stack_sp = -1;

static	symbol	*sg_switch_stack[SWITCH_STACK_SIZE];
static	int		sg_switch_stack_sp = -1;

static	FILE	*sg_include_stack[INCLUDE_STACK_SIZE];
static	int		sg_include_stack_sp = -1;
extern	FILE *yyin;

void	push_label(const char *lb)
{
	strcpy(sg_label_stack[++sg_label_stack_sp], lb);

	CHECK_LABEL_STACK_OVERFLOW
}

char	*pop_label()
{
	CHECK_LABEL_STACK_BOTTOM

	return	sg_label_stack[sg_label_stack_sp--];
}

void	push_break_label(const char *lb)
{
	strcpy(sg_break_stack[++sg_break_stack_sp], lb);

	CHECK_BREAK_STACK_OVERFLOW
}

// get current break stack top
char	*get_break_stack_top()
{
	if ( sg_break_stack_sp < 0 )
	{
		yyerror("illegal break");
		user_exit(1);
	}

	return	sg_break_stack[sg_break_stack_sp];
}

char	*pop_break_label()
{
	CHECK_BREAK_STACK_BOTTOM

	return	sg_break_stack[sg_break_stack_sp--];
}

void	push_continue_label(const char *lb)
{
	strcpy(sg_continue_stack[++sg_continue_stack_sp], lb);

	CHECK_CONTINUE_STACK_OVERFLOW
}

// get current continue stack top
char	*get_continue_stack_top()
{
	if ( sg_continue_stack_sp < 0 )
	{
		yyerror("illegal continue");
		user_exit(1);
	}

	return	sg_continue_stack[sg_continue_stack_sp];
}

char	*pop_continue_label()
{
	CHECK_CONTINUE_STACK_BOTTOM

	return	sg_continue_stack[sg_continue_stack_sp--];
}

void	push_switch(symbol *sw)
{
	sg_switch_stack[++sg_switch_stack_sp] = sw;

	CHECK_SWITCH_STACK_OVERFLOW
}

void	add_case_to_switch(symbol *cs)
{
	assert( cs );

	if ( sg_switch_stack_sp < 0 )
	{
		yyerror("illegal case");
		user_exit(1);
	}

	link_symbol_list(sg_switch_stack[sg_switch_stack_sp], cs);
}

symbol	*pop_switch()
{
	CHECK_SWITCH_STACK_BOTTOM

	return	sg_switch_stack[sg_switch_stack_sp--];
}

void	push_include(char *filename)
{
	FILE	*fp = fopen(filename, "r");

	if ( !fp )
	// open input file error
	{
		parse_error("can's open input file or include file :", filename);
		user_exit(1);
	}
	sg_include_stack[++sg_include_stack_sp] = fp;
	yyin = fp;

	CHECK_INCLUDE_STACK_OVERFLOW
}

int		pop_include()
{
	CHECK_INCLUDE_STACK_BOTTOM

	fclose(sg_include_stack[sg_include_stack_sp--]);
	if (sg_include_stack_sp >= 0)
	{
		yyin = sg_include_stack[sg_include_stack_sp];
		return	0;
	}

	yyin = stdin;
	return	1; // eof of yylex
}

void	InitStack()
{
	sg_label_stack_sp = -1;
	sg_break_stack_sp = -1;
	sg_continue_stack_sp = -1;
	sg_switch_stack_sp = -1;
	sg_include_stack_sp = -1;
}

void	DestoryStack()
{
	assert(sg_label_stack_sp == -1);
	assert(sg_break_stack_sp == -1);
	assert(sg_continue_stack_sp == -1);
	assert(sg_switch_stack_sp == -1);
	assert(sg_include_stack_sp == -1);
}



⌨️ 快捷键说明

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