Example #1
0
void Compile(const string &filePath)
{
	ifstream is(filePath);

	if (!is.is_open())
	{
		cout << "Cannot open file '" << filePath << "'\n";
		return;
	}

	Lexer lex;
	vector<Token> tokens;
	int lineNumber = 1;

	while (!is.eof())
	{
		string expr;

		getline(is, expr);
		if (expr.length() == 0)
		{
			continue;
		}
		
		vector<Token> line = lex.ParseLine(expr, lineNumber++);
		for (const Token &token : line)
		{
			tokens.push_back(token);
		}
	}

	string programName = filePath.substr(filePath.find_last_of('\\') + 1);
	programName = programName.substr(0, programName.find_last_of('.'));

	shared_ptr<IAst> ast = Parse(tokens);

	GeneratorVisitor generator(programName);
	ast->accept(generator);
	
	int status = generator.Compile();
	if (status != 0)
	{
		cout << "ILAsm returned error (code: " << status << ")\n";
	}
}
Example #2
0
void Debug()
{
	Lexer lex;
	int lineNumber = 1;
	vector<Token> tokens;

	while (true)
	{
		string expr;

		getline(cin, expr);
		if (expr.length() == 0)
		{
			break;
		}
		
		vector<Token> line = lex.ParseLine(expr, lineNumber++);
		for (const Token &token : line)
		{
			tokens.push_back(token);
		}
	}

	shared_ptr<IAst> ast = Parse(tokens);

	PrintVisitor pv;
	ast->accept(pv);

	GeneratorVisitor generator("debug_test");
	ast->accept(generator);
	
	int status = generator.Compile();
	if (status != 0)
	{
		cout << "ILAsm returned error (code: " << status << ")\n";
	}
}