Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Implementation of Programming Languages / Transpiler / src / LLVMVisitor / LLVMVisitor.cpp
blob: 9ca6f8fd820d223afb40c2834cdfb1f6f88378ee (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
33
34
35
36
37
38
#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, unsigned i) {
	std::string leftArg = bfun.get_args()[i]->Visit(*this);
	std::string rightArg;
	if (bfun.get_args().size() - 2 == i) {
		rightArg = bfun.get_args()[i+1]->Visit(*this);
	}
	else {
		rightArg = f_add(bfun, i+1);
	}
	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 "";
}