forked from leuat/TRSE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrc.cpp
More file actions
147 lines (129 loc) · 3.88 KB
/
trc.cpp
File metadata and controls
147 lines (129 loc) · 3.88 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
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
#include "trc.h"
#include <QDebug>
#include <QTextDocument>
#include <stdio.h>
int main(int argc, char *argv[])
{
// QCoreApplication app(argc, argv);
ClascExec ras(argc, argv);
return ras.Perform();
// return app.exec();
}
ClascExec::ClascExec(int argc, char *argv[]) {
Out("Welcome to trc, the TRSE command-line assembler/compiler!");
Out("");
for (int i=1;i<argc;i++) {
m_args.append(QString(argv[i]));
QStringList l = QString(argv[i]).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;
try {
RequireFile("input_file");
RequireParam("op");
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");
m_settings.Load(m_vals["settings"]);
m_project.Load(m_vals["project"]);
m_failure = CompileFromProject(m_vals["input_file"]);
}
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;
}
return m_failure;
}
int ClascExec::CompileFromProject(QString sourceFile)
{
QString system = m_project.getString("system");
Out("Compiling '"+sourceFile+"' for system: " +system);
Out("");
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);
int failure=0;
if (!m_builder->Build(source)) {
failure=1;
}
if (m_outputFile!="")
QFile::rename(m_builder->m_filename+".asm", m_outputFile);
QTextDocument doc;
doc.setHtml( m_builder->getOutput() );
Out(doc.toPlainText());
return 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("Usage: ");
Out("");
Out("trc 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 a project with a main source file: ");
Out(" trc op=project settings=~/.TRSE/fluff64.ini project=myDemo.trse input_file=main_demo.ras");
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;
}