forked from leuat/TRSE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trc.cpp
202 lines (182 loc) · 6.13 KB
/
trc.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "trc.h"
#include <QDebug>
#include <QTextDocument>
#include <stdio.h>
#include <QStandardPaths>
ClascExec::ClascExec(int argc, char *argv[]) {
app = new QApplication(argc, argv);
app->setOrganizationDomain("lemonspawn.com");
app->setApplicationName("TRSE");
// core = new QCoreApplication(argc,argv);
// this->core = core;
// first values are "trse" and "-cli", so start at 2
for (int i=2;i<argc;i++) {
auto s = QString(argv[i]);
if (s.startsWith("--")) {
QString opt = s.remove("--");
if (opt.toLower()=="define") {
if (i==argc-1) {
qInfo() << "Error : define requires a parameter '--define X86'";
m_hasError = true;
break;
}
i+=1;
auto arg = QString(argv[i]);
m_options[s] = QStringList()<<arg;
}
}
else
{
m_args.append(s);
QStringList l = s.split("=");
if (l.count()!=2) {
qInfo() << "Error : parameters must be of the format 'p1=someting'";
m_hasError = true;
break;
}
else
m_vals[l[0].toLower().trimmed()] = l[1].trimmed();
}
}
}
void ClascExec::RequireParam(QString param)
{
if (!m_vals.contains(param))
throw QString("Parameter '"+param+"' is required.");
}
void ClascExec::RequireFile(QString param)
{
RequireParam(param);
if (!QFile::exists(m_vals[param]))
throw QString("Could not find file: " + m_vals[param]);
}
int ClascExec::Perform()
{
if (m_hasError)
return 1;
m_settings = QSharedPointer<CIniFile>(new CIniFile());
m_project = QSharedPointer<CIniFile>(new CIniFile());
try {
RequireFile("input_file");
RequireParam("op");
if (!m_vals.contains("assemble"))
m_vals["assemble"] = "yes";
QString op = m_vals["op"].toLower();
if (m_vals.contains("output_file"))
m_outputFile = m_vals["output_file"];
if (op=="project") {
// RequireFile("settings");
RequireFile("project");
if (m_vals.contains("settings")) {
m_settings->Load(m_vals["settings"]);
}
else {
auto path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QString iniFileName = path +QDir::separator()+ "trse.ini";
if (QFile::exists(iniFileName))
m_settings->Load(iniFileName);
else {
Out("Could not load TRSE settings! Please make sure you have started TRSE as a windowed application at least once before using the CLI.");
return false;
}
}
// Set some settings params
// Turn off threaded compliation
m_settings->setFloat("compile_thread",0);
m_project->Load(m_vals["project"]);
m_failure = CompileFromProject(m_vals["input_file"],m_vals["assemble"]=="yes");
}
else
if (op=="orgasm") {
m_failure= Assemble(m_vals["input_file"]);
}
else {
m_failure = 1;
Out("Unknown operation: " +op);
}
}
catch (QString s) {
PrintUsage();
Out("Fatal error:");
Out(s);
return 1;
}
if (m_failure) {
QTextDocument doc;
doc.setHtml( m_builder->getOutput() );
Out(doc.toPlainText());
}
return m_failure;
}
int ClascExec::CompileFromProject(QString sourceFile, bool assemble)
{
QString system = m_project->getString("system");
Out("Compiling '"+sourceFile+"' for system: " +system);
Syntax::s.Init(AbstractSystem::SystemFromString(system),m_settings, m_project);
QString source = Util::loadTextFile(sourceFile);
// Out(QDir::currentPath());
m_builder = new SourceBuilder(m_settings,m_project, QDir::currentPath()+"/", sourceFile);
m_builder->m_options = m_options;
m_failure=0;
if (!m_builder->Build(source)) {
m_failure=1;
}
else {
if (assemble) {
m_builder->Assemble();
m_failure = !m_builder->m_assembleSuccess;
}
}
// if (m_outputFile!="")
// QFile::rename(m_builder->m_filename+".asm", m_outputFile);
if (m_failure) {
QTextDocument doc;
doc.setHtml( m_builder->getOutput() );
Out(doc.toPlainText());
}
return m_failure;
}
int ClascExec::Assemble(QString file)
{
Orgasm orgAsm;
QString filename = file.split(".")[0] + ".prg";
if (m_outputFile!="")
filename = m_outputFile;
/* for(QString k: symTab->m_constants.keys()) {
orgAsm.m_constants[k] = Util::numToHex(symTab->m_constants[k]->m_value->m_fVal);
}
*/
Out("Assembling file:'"+filename);
orgAsm.Assemble(file, filename);
Out(orgAsm.m_output);
if (orgAsm.m_output.contains("Complete"))
return 0;
return 1;
}
void ClascExec::PrintUsage()
{
Out("Welcome to the TRSE CLI (command-line interface) assembler/compiler!");
Out("");
Out("Usage: ");
Out("");
Out("trse -cli op=[ operation types ] input_file=[ source file ] output_file=[ optional output file ].... [ type specific operation parameters ]");
Out("Valid operation types are: project, orgasm");
Out("");
Out("Examples: ");
Out(" compile + assemble a project with a main source file: ");
Out(" trc op=project project=myDemo.trse input_file=main_demo.ras");
Out(" Use a custom settings file: ");
Out(" trc op=project settings=trse.ini project=myDemo.trse input_file=main_demo.ras");
Out(" To only compile and not perform any assembling, use the 'assemble=no' option ");
Out(" trc project=myDemo.trse input_file=main_demo.ras no_assembling");
Out("");
Out(" Use OrgAsm to assemble an .asm to .prg: ");
Out(" trc op=orgasm input_file=main_demo.asm");
Out("");;
}
void ClascExec::Out(QString m)
{
// qInfo() << m << endl;
// printf(m.toStdString().c_str());
std::cout << m.toStdString() << std::endl;
}