-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_test.cpp
More file actions
68 lines (61 loc) · 2.55 KB
/
compiler_test.cpp
File metadata and controls
68 lines (61 loc) · 2.55 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// =======================================================================================
// Made by: Rohit Yadav
// NIT Jalandhar
//
// File: compiler_test.cpp
// Purpose:
// - This file runs the compiled output (`prog.cpp`) using your custom-built VM.
// - It simulates how a user or interpreter would execute code written in Brolang.
//
// How it works:
// - Compiles Brolang code into bytecode (instructions).
// - Loads these instructions into the virtual machine (RohitVM).
// - Executes and displays the result.
//
// Usage Instructions:
// g++ broc.cpp lexer.cpp parser.cpp ast.cpp codegen.cpp emitter.cpp -o broc
// ./broc test.bro -o prog.cpp
// g++ compiler_test.cpp RohitVM.cpp RohitUtils.cpp -o run_bro
// ./run_bro
// =======================================================================================
#include "RohitVM.hpp"
#include <iostream>
#include <sstream>
// ---------------------------------------------------------------------------------------
// NOTE:
// - The file `prog.cpp` is generated by your compiler (broc).
// - It contains: std::vector<Instruction> prog;
// - This is the bytecode that will be executed.
// ---------------------------------------------------------------------------------------
#include "prog.cpp"
// =======================================================================================
// Function: runProgram
// Description:
// - Loads the given instruction list into the VM
// - Executes the VM and returns formatted output as a string
// Parameters:
// - prog: vector of compiled instructions (bytecode)
// - title: a label to print during execution for clarity
// Returns:
// - A formatted string representing the VM output for the given program
// =======================================================================================
std::string runProgram(const std::vector<Instruction>& prog, const std::string& title) {
VM vm;
std::ostringstream out;
out << "===============================\n";
out << "Running Compiled Program: " << title << "\n";
out << "===============================\n";
vm.loadProgram(prog);
vm.execute();
return out.str();
}
// =======================================================================================
// Function: main
// Purpose:
// - Entry point for running compiled Brolang programs on the custom VM.
// - Automatically loads `prog.cpp` and runs it.
// =======================================================================================
int main() {
std::cout << runProgram(prog, "test.bro") << "\n";
return 0;
}