Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Implementation of Programming Languages / Transpiler / src / LLVMVisitor / LLVMVisitor.cpp
blob: d3eea2d4e1e4c694fd10b8de3df17a66eb5588fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "LLVMVisitor.h"

LLVMVisitor::LLVMVisitor(const char* fileName) : outFile(std::ofstream(fileName)), reg() {
	if (!outFile.is_open())
		throw std::runtime_error("Couldn't open file for writing!");
}

std::string LLVMVisitor::Visit(const Integer& vint) {
	std::string valRegister = *(reg++);
	outFile << valRegister + " = " + std::to_string(vint.get_value()) + "\n";
	return valRegister;
}

std::string LLVMVisitor::Visit(const Float& vfloat) {
	std::string valRegister = *(reg++);
	outFile << valRegister + " = " + std::to_string(vfloat.get_value()) + "\n";
	return valRegister;
}

std::string LLVMVisitor::f_add(const BuiltInFunction& bfun) {
	std::string leftArg = bfun.get_args()[0]->Visit(*this);
	std::string rightArg = bfun.get_args()[1]->Visit(*this);
	std::string valRegister = *(reg++);
	outFile << valRegister << " = add i32 " << leftArg << ", " << rightArg << "\n";
	return valRegister;
}

std::string LLVMVisitor::Visit(const BuiltInFunction& bfun) {
	if (bfun.get_name() == "+")
		return f_add(bfun);
	return "";
}