Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Implementation of Programming Languages / Transpiler / src / main.cpp
blob: bc651e600d35026b03c031f7fcc0916a1b5e7fcf (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
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <deque>
#include "LLVMVisitor/LLVMVisitor.h"

void freeNodes(Value* v) {
	if (dynamic_cast<Funcall*>(v) != nullptr) {
		for (Value* val : static_cast<Funcall*>(v)->get_args())
			freeNodes(val);
	}
	else if (dynamic_cast<List*>(v) != nullptr) {
		for (Value* val : static_cast<List*>(v)->get_value())
			freeNodes(val);
	}
	delete v;
}

int main() {
	LLVMVisitor visitor("testout.ll");
	std::deque<Value*> nodes = {
		new Funcall("defvar", {
				new Name("myvar"),
				 new Integer(19) 
				}),
		new Funcall("+", {
				new Float(1),
				new Funcall("-", { new Float(5), new Float(10) }),
				new Float(2),
				new Float(123) }),
		new Funcall("defun", {
				new Name("notfun"),
				new List({new Name("a"), new Name("b")}),
				new Funcall("+", {
						new Integer(7),
						new Name("a"),
						new Name("b")
						}),
				new Funcall("if", {
						new Funcall(">=", { new Integer(5), new Integer(6) }),
						new Funcall("+", { new Integer(1), new Integer(2) }),
						new Funcall("-", { new Integer(3), new Integer(4) })
						})})
	};
	for (Value* n : nodes) {
		n->Visit(visitor);
	}
	visitor.insertMain();

	for (Value* n : nodes)
		freeNodes(n);
}