New Assembler.

This commit is contained in:
Gav Wood 2014-05-26 19:41:46 +02:00
parent 34c3d06883
commit 36370900bb

View File

@ -36,7 +36,7 @@ void help()
<< "Options:" << endl << "Options:" << endl
<< " -b,--binary Parse, compile and assemble; output byte code in binary." << endl << " -b,--binary Parse, compile and assemble; output byte code in binary." << endl
<< " -x,--hex Parse, compile and assemble; output byte code in hex." << endl << " -x,--hex Parse, compile and assemble; output byte code in hex." << endl
// << " -a,--assembly Only parse and compile; show assembly." << endl << " -a,--assembly Only parse and compile; show assembly." << endl
<< " -t,--parse-tree Only parse; show parse tree." << endl << " -t,--parse-tree Only parse; show parse tree." << endl
<< " -h,--help Show this help message and exit." << endl << " -h,--help Show this help message and exit." << endl
<< " -V,--version Show the version and exit." << endl; << " -V,--version Show the version and exit." << endl;
@ -51,7 +51,7 @@ void version()
exit(0); exit(0);
} }
enum Mode { Binary, Hex, ParseTree }; enum Mode { Binary, Hex, Assembly, ParseTree };
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
@ -67,6 +67,8 @@ int main(int argc, char** argv)
mode = Binary; mode = Binary;
else if (arg == "-x" || arg == "--hex") else if (arg == "-x" || arg == "--hex")
mode = Hex; mode = Hex;
else if (arg == "-a" || arg == "--assembly")
mode = Assembly;
else if (arg == "-t" || arg == "--parse-tree") else if (arg == "-t" || arg == "--parse-tree")
mode = ParseTree; mode = ParseTree;
else if (arg == "-V" || arg == "--version") else if (arg == "-V" || arg == "--version")
@ -88,23 +90,23 @@ int main(int argc, char** argv)
else else
src = asString(contents(infile)); src = asString(contents(infile));
vector<string> errors;
if (src.empty()) if (src.empty())
cerr << "Empty file." << endl; cerr << "Empty file." << endl;
else if (mode == Binary || mode == Hex) else if (mode == Binary || mode == Hex)
{ {
vector<string> errors;
auto bs = compileLLL(src, &errors); auto bs = compileLLL(src, &errors);
if (mode == Hex) if (mode == Hex)
cout << toHex(bs) << endl; cout << toHex(bs) << endl;
else if (mode == Binary) else if (mode == Binary)
cout.write((char const*)bs.data(), bs.size()); cout.write((char const*)bs.data(), bs.size());
for (auto const& i: errors)
cerr << i << endl;
} }
else if (mode == ParseTree) else if (mode == ParseTree)
{
cout << parseLLL(src) << endl; cout << parseLLL(src) << endl;
} else if (mode == Assembly)
cout << compileLLLToAsm(src, &errors) << endl;
for (auto const& i: errors)
cerr << i << endl;
return 0; return 0;
} }