Rez: command line options

This commit is contained in:
Wolfgang Thaller 2014-10-09 22:15:13 +02:00
parent 2b64cb707d
commit 127db48a26
9 changed files with 108 additions and 34 deletions

View File

@ -19,7 +19,7 @@ cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "--std=c++11") set(CMAKE_CXX_FLAGS "--std=c++11")
find_package(Boost COMPONENTS wave filesystem system thread regex) find_package(Boost COMPONENTS wave filesystem system thread regex program_options)
find_package(BISON REQUIRED) find_package(BISON REQUIRED)

View File

@ -2,8 +2,13 @@
#include <iostream> #include <iostream>
#include "ResourceDefinitions.h" #include "ResourceDefinitions.h"
ResourceCompiler::ResourceCompiler(TypeDefinitionPtr type, CompoundExprPtr body) ResourceCompiler::ResourceCompiler(
: typeDefinition(type), body(body), currentOffset(0), currentField(nullptr) TypeDefinitionPtr type, CompoundExprPtr body, bool verboseFlag)
: typeDefinition(type),
body(body),
currentOffset(0),
currentField(nullptr),
verboseFlag(verboseFlag)
{ {
} }
@ -15,6 +20,7 @@ std::string ResourceCompiler::resourceData()
void ResourceCompiler::write(int nBits, int value) void ResourceCompiler::write(int nBits, int value)
{ {
if(verboseFlag)
std::cout << "[" << nBits << " bits] = " << std::hex << value << std::dec << std::endl; std::cout << "[" << nBits << " bits] = " << std::hex << value << std::dec << std::endl;
unsigned mask = 1 << (nBits-1); unsigned mask = 1 << (nBits-1);
@ -41,7 +47,6 @@ ExprPtr ResourceCompiler::lookupIdentifier(std::string name, const Subscripts &s
{ {
if(ExprPtr val = currentField->lookupNamedValue(name)) if(ExprPtr val = currentField->lookupNamedValue(name))
{ {
std::cout << "current field: " << name << " found.";
return val; return val;
} }
} }
@ -50,7 +55,7 @@ ExprPtr ResourceCompiler::lookupIdentifier(std::string name, const Subscripts &s
if(p != labelValues.end()) if(p != labelValues.end())
return p->second; return p->second;
std::cout << "ID lookup failed: " << name << std::endl; std::cerr << "ID lookup failed: " << name << std::endl;
return nullptr; return nullptr;
} }
@ -62,15 +67,15 @@ void ResourceCompiler::defineLabel(const std::string &name)
void ResourceCompiler::compile() void ResourceCompiler::compile()
{ {
std::cout << "(first pass)\n"; if(verboseFlag) std::cout << "(first pass)\n";
currentOffset = 0; currentOffset = 0;
data.clear(); data.clear();
typeDefinition->compile(body, this, true); typeDefinition->compile(body, this, true);
std::cout << "(second pass)\n"; if(verboseFlag) std::cout << "(second pass)\n";
currentOffset = 0; currentOffset = 0;
data.clear(); data.clear();
typeDefinition->compile(body, this, false); typeDefinition->compile(body, this, false);
std::cout << "(done)\n"; if(verboseFlag) std::cout << "(done)\n";
} }

View File

@ -32,10 +32,11 @@ class ResourceCompiler
Subscripts currentSubscripts; Subscripts currentSubscripts;
std::vector<unsigned char> data; std::vector<unsigned char> data;
bool verboseFlag;
void beginArrayScope(std::string& arrayName, int index); void beginArrayScope(std::string& arrayName, int index);
public: public:
ResourceCompiler(TypeDefinitionPtr type, CompoundExprPtr body); ResourceCompiler(TypeDefinitionPtr type, CompoundExprPtr body, bool verboseFlag);
std::string resourceData(); std::string resourceData();

View File

@ -1,6 +1,7 @@
#include <fstream>
#include <iostream> #include <iostream>
#include "boost/program_options.hpp"
#include "boost/filesystem.hpp"
#include "boost/filesystem/fstream.hpp"
#include "RezParser.generated.hh" #include "RezParser.generated.hh"
#include "RezLexer.h" #include "RezLexer.h"
@ -9,24 +10,83 @@
#include "ResourceFiles.h" #include "ResourceFiles.h"
#include "BinaryIO.h" #include "BinaryIO.h"
int main() namespace po = boost::program_options;
{ namespace fs = boost::filesystem;
//RezLexer lexer("/home/wolfgang/Projects/Retro68/RIncludes/Types.r");
RezLexer lexer("/home/wolfgang/Projects/Retro68/Rez/Test.r");
RezWorld world;
RezParser parser(lexer, world);
parser.parse(); static po::options_description desc;
static void usage()
{
std::cerr << "Usage: " << "Rez [options] input-file\n";
std::cerr << desc << std::endl;
}
int main(int argc, const char *argv[])
{
desc.add_options()
("help,h", "show this help message")
("output,o", po::value<std::string>()->default_value("rez.output.rsrc"), "output file")
("type,t", po::value<std::string>()->default_value("rsrc"), "output file finder type code")
("creator,c", po::value<std::string>()->default_value("RSED"), "output file finder creator code")
("debug,d", "debug logging")
;
po::options_description hidden, alldesc;
hidden.add_options()
("input", po::value<std::vector<std::string>>(), "input file" )
;
alldesc.add(desc).add(hidden);
po::variables_map options;
try
{ {
std::ofstream dataOut("test.rsrc"); auto parsed = po::command_line_parser(argc, argv)
system("mkdir -p .rsrc"); .options(alldesc)
std::ofstream rsrcOut(".rsrc/test.rsrc"); .positional(po::positional_options_description().add("input", -1))
.style(po::command_line_style::default_style |
po::command_line_style::allow_long_disguise)
.run();
po::store(parsed, options);
}
catch(po::error& e)
{
std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
usage();
return 1;
}
po::notify(options);
if(options.count("help") || !options.count("input"))
{
usage();
return 0;
}
RezWorld world;
if(options.count("debug"))
world.verboseFlag = true;
for(std::string fn : options["input"].as<std::vector<std::string>>())
{
RezLexer lexer(fn);
RezParser parser(lexer, world);
parser.parse();
}
std::string outfile = options["output"].as<std::string>();
{
fs::path dataPath(outfile);
fs::create_directory(dataPath.parent_path() / ".rsrc");
fs::create_directory(dataPath.parent_path() / ".finf");
fs::ofstream dataOut(dataPath);
fs::ofstream rsrcOut(dataPath.parent_path() / ".rsrc" / dataPath.filename());
fs::ofstream finfOut(dataPath.parent_path() / ".finf" / dataPath.filename());
world.getResources().writeFork(rsrcOut); world.getResources().writeFork(rsrcOut);
system("mkdir -p .finf"); ostype(finfOut, options["type"].as<std::string>());
std::ofstream finfOut(".finf/test.rsrc"); ostype(finfOut, options["creator"].as<std::string>());
ostype(finfOut, "rsrc");
ostype(finfOut, "RSED");
for(int i = 8; i < 32; i++) for(int i = 8; i < 32; i++)
byte(finfOut, 0); byte(finfOut, 0);
} }

View File

@ -113,7 +113,6 @@ RezLexer::WaveToken RezLexer::nextWave()
else else
{ {
WaveToken tok = *pImpl->iter++; WaveToken tok = *pImpl->iter++;
std::cout << tok.get_value();
return tok; return tok;
} }
} }

View File

@ -167,9 +167,9 @@ type_definition : "type" type_spec
world.fieldLists.push(td); world.fieldLists.push(td);
} }
"{" field_definitions "}" "{" field_definitions "}"
{ world.fieldLists.pop(); std::cout << "TYPE " << $2 << std::endl; } { world.fieldLists.pop(); if(world.verboseFlag) std::cout << "TYPE " << $2 << std::endl; }
| "type" type_spec "as" type_spec | "type" type_spec "as" type_spec
{ std::cout << "TYPE " << $2 << std::endl; } { if(world.verboseFlag) std::cout << "TYPE " << $2 << std::endl; }
; ;
%type <ResType> res_type; %type <ResType> res_type;

View File

@ -5,6 +5,7 @@
#include <iostream> #include <iostream>
RezWorld::RezWorld() RezWorld::RezWorld()
: verboseFlag(false)
{ {
} }
@ -27,9 +28,10 @@ TypeDefinitionPtr RezWorld::getTypeDefinition(ResType type, int id)
void RezWorld::addResource(ResType type, int id, CompoundExprPtr body) void RezWorld::addResource(ResType type, int id, CompoundExprPtr body)
{ {
if(verboseFlag)
std::cout << "RESOURCE " << type << "(" << id << ")" << std::endl; std::cout << "RESOURCE " << type << "(" << id << ")" << std::endl;
TypeDefinitionPtr def = getTypeDefinition(type, id); TypeDefinitionPtr def = getTypeDefinition(type, id);
ResourceCompiler compiler(def, body); ResourceCompiler compiler(def, body, verboseFlag);
compiler.compile(); compiler.compile();
resources.addResource(Resource(type, id, compiler.resourceData())); resources.addResource(Resource(type, id, compiler.resourceData()));

View File

@ -26,6 +26,8 @@ public:
void addResource(ResType type, int id, CompoundExprPtr body); void addResource(ResType type, int id, CompoundExprPtr body);
Resources& getResources() { return resources; } Resources& getResources() { return resources; }
bool verboseFlag;
}; };

View File

@ -3,12 +3,12 @@
*/ */
//#include "/home/wolfgang/Projects/Retro68/CExamples/Sample.r" #include "/home/wolfgang/Projects/Retro68/CExamples/Sample.r"
type 'TEST' { type 'TEST' {
integer zero, one, two, answer = 42, missed; integer zero, one, two, answer = 42, missed;
longint; longint;
//integer = (after - before) / 32; integer = (after - before) / 8;
integer = $$CountOf(foo); integer = $$CountOf(foo);
before: before:
array foo { array foo {
@ -16,6 +16,7 @@ type 'TEST' {
integer; integer;
integer; integer;
}; };
string;
after: after:
; ;
}; };
@ -23,6 +24,10 @@ type 'TEST' {
resource 'TEST' (128) { resource 'TEST' (128) {
answer, answer,
0x1234, 0x1234,
{ 1, 2; 3, 4; } { 1, 2; 3, 4; },
"Hello, "
"world: "
$"Abcd 1234";
}; };