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

📄 tokenizer.cpp

📁 C-MINUS编译器
💻 CPP
字号:
/**:	Tokenizer.cpp	implementation file
&
*		author:	lonelyforest;
*		data:	2006.03.16
*/

#include "Tokenizer.h"
#include <stdio.h>

/**:	construction & destruction	
*	author:	lonelyforest;
*	data:	2006.03.16
*/
//-----------------------------------------------------------------------------
Tokenizer::Tokenizer(const std::string &filename) : source_name(filename),
			is_good_(true), TraceSource(true), lineno_(0),
			bufsize(0), linepos(0)
{
	err_count = 0;
	warn_count= 0;
	// trace compile
	insert_list("\tSimple C- Compiler\n");
	//outputMsg(-1, "\tSimple C- Compiler");

    	sprintf(msg_temp, "Compiling  \"%s\" ...\n", filename.c_str());
	insert_list(msg_temp);
	outputMsg(-1, msg_temp);

	read_file(filename.c_str());
}

//	define construction to pure virtual ...
Tokenizer::~Tokenizer()
{
}

/**:	read_file & store_file
*	author:	lonelyforest;
*	data:	2006.03.16
*/
//-----------------------------------------------------------------------------
void Tokenizer::read_file(const char *filename)
{
	ifstream infile(filename);

	if ( !infile )
	{		
		is_good_ = false;
		outputMsg(-1,"err_count add....");
		err_count = 1;
		sprintf(msg_temp, "Open source file [%s] fail ....", filename);

		outputMsg(-1, msg_temp);
		outputMsg(-1, "Compile must be stop, and check the file wether exists!");
	}
	else
	{
		store_file(infile);
	}
}
//-----------------------------------------------------------------------------
void Tokenizer::store_file(ifstream &is)
{
	string linetext;
	while(getline(is, linetext ))
	{
		linetext += "\n";
		lines_of_source.push_back(linetext);
	}
}


/**:	getNextchar & unGetNextChar	
*	author:	lonelyforest;
*	data:	2006.03.16
*/
//-----------------------------------------------------------------------------
char Tokenizer::getNextChar()
{	
	if ( !(linepos < bufsize ))
	{
		++lineno_;	// now, lineno_ start with 1 !!

		if ( lineno_ > lines_of_source.size())
		{
			return EOF;
		}

		linepos = 0;
		bufsize = lines_of_source[lineno_ -1].length();

		// trace source file
		sprintf(msg_temp, "%4d: %s", lineno_, lines_of_source[lineno_-1].c_str());
		insert_list(msg_temp);
	}


	return lines_of_source[lineno_-1][linepos++];
}
//-----------------------------------------------------------------------------
void Tokenizer::unGetNextChar()
{
	linepos--;
}


/**:	insert_list;
*	insert messages to list_msg_
*	author:	lonelyforest;
*	data:	2006.03.16
*/
//-----------------------------------------------------------------------------
void Tokenizer::insert_list(const std::string& msg)
{
	if (TraceSource) insert_list(msg.c_str());
}

void Tokenizer::insert_list(const char *msg)
{
	if (TraceSource)	list_msg_.push_back(msg);
}
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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