-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
124 lines (104 loc) · 2.49 KB
/
main.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// liaison - interpreter&compiler for the language with the same name
// (c) friol 2023-2024-2025
//
#include <string>
#include <iostream>
#include <fstream>
#include <streambuf>
#include "parser.h"
#include "interpreter.h"
#include "compiler.h"
#include "vm.h"
#define COMPILE_IT
//
//
//
const std::string appVersion = "0.8";
void usage()
{
std::cout << "liaison v" << appVersion << std::endl;
std::cout << "Usage: liaison <sourcefile.lia>" << std::endl;
}
int main(int argc,char** argv)
{
/*if (argc < 2)
{
usage();
return 1;
}*/
std::string sourceFileName;
if (argc == 2)
{
sourceFileName = argv[1];
//std::cout << sourceFileName << std::endl;
}
else
{
//sourceFileName = "d:\\prova\\aoc2023.17.lia";
//sourceFileName = "d:\\prova\\liaPrograms\\aoc03.2015.lia"; // performance test // res is 2360, elapsed: 4.8s in Debug
//sourceFileName = "d:\\prova\\liaison\\examples\\test.lia"; // test suite
//sourceFileName = "d:\\prova\\liaison\\examples\\aoc2024\\aoc01.2024.lia";
//sourceFileName = "d:\\prova\\liaPrograms\\test.lia";
sourceFileName = "d:\\prova\\liaPrograms\\vmtest.lia";
}
std::ifstream infile(sourceFileName);
if (!infile)
{
std::cout << "Error opening source file." << std::endl;
return 1;
}
std::stringstream buffer;
buffer << infile.rdbuf();
//std::cout << "Program read from file: " << std::endl << buffer.str() << std::endl;
//
liaParser theLiaParser;
liaInterpreter theLiaInterpreter;
if (theLiaParser.parseCode(buffer.str()) == 0)
{
if (theLiaInterpreter.validateAst(theLiaParser.theAst) != 0) return 1;
theLiaInterpreter.getFunctions(theLiaParser.theAst);
//theLiaInterpreter.dumpFunctions();
if (theLiaInterpreter.storeGlobalVariables(theLiaParser.theAst) != 0)
{
return 1;
}
std::vector<std::string> params;
if (argc > 2)
{
for (int p = 2;p < argc;p++)
{
std::string prm = argv[p];
params.push_back(prm);
}
}
#ifdef COMPILE_IT
//liaCompiler theCompiler;
//theCompiler.compile(theLiaParser.theAst, params, theLiaInterpreter.getLiaFunctions());
//theCompiler.exeCute();
liaVM theVM;
try
{
theVM.getGlobalVariables(theLiaInterpreter.getGlobalScope());
theVM.compile(theLiaParser.theAst, params, theLiaInterpreter.getLiaFunctions());
theVM.exeCuteProgram();
}
catch (vmException& ex)
{
ex = ex;
return 1;
}
#else
try
{
theLiaInterpreter.exeCute(theLiaParser.theAst, params);
}
catch (interpreterException& ex)
{
ex = ex;
return 1;
}
#endif
}
return 0;
}