Rez: write actual resource files

This commit is contained in:
Wolfgang Thaller 2014-10-08 02:52:34 +02:00
parent a9c399ff2a
commit 70b5b36d47
10 changed files with 85 additions and 32 deletions

View File

@ -56,6 +56,6 @@ add_executable(Rez
ResourceCompiler.cc
ResourceCompiler.h
)
target_link_libraries(Rez ${Boost_LIBRARIES})
target_link_libraries(Rez ResourceFiles ${Boost_LIBRARIES})
install(TARGETS Rez RUNTIME DESTINATION bin)

View File

@ -8,10 +8,31 @@ ResourceCompiler::ResourceCompiler(TypeDefinitionPtr type, CompoundExprPtr body)
}
std::string ResourceCompiler::resourceData()
{
return std::string(data.begin(), data.end());
}
void ResourceCompiler::write(int nBits, int value)
{
std::cout << "[" << nBits << " bits] = " << std::hex << value << std::dec << std::endl;
currentOffset += nBits;
unsigned mask = 1 << (nBits-1);
for(int i = 0; i < nBits; i++)
{
bool bit = (value & mask) != 0;
if(currentOffset % 8 == 0)
data.push_back(bit ? 0x80 : 0);
else if(bit)
data.back() |= (0x80 >> (currentOffset % 8));
++currentOffset;
mask >>= 1;
}
//currentOffset += nBits;
}
ExprPtr ResourceCompiler::lookupIdentifier(std::string name, const Subscripts &sub)
@ -19,13 +40,18 @@ ExprPtr ResourceCompiler::lookupIdentifier(std::string name, const Subscripts &s
if(currentField)
{
if(ExprPtr val = currentField->lookupNamedValue(name))
{
std::cout << "current field: " << name << " found.";
return val;
}
}
auto p = labelValues.find(std::make_pair(name, sub));
if(p != labelValues.end())
return p->second;
std::cout << "ID lookup failed: " << name << std::endl;
return nullptr;
}
@ -38,9 +64,11 @@ void ResourceCompiler::compile()
{
std::cout << "(first pass)\n";
currentOffset = 0;
data.clear();
typeDefinition->compile(body, this, true);
std::cout << "(second pass)\n";
currentOffset = 0;
data.clear();
typeDefinition->compile(body, this, false);
std::cout << "(done)\n";

View File

@ -31,10 +31,14 @@ class ResourceCompiler
Field* currentField;
Subscripts currentSubscripts;
std::vector<unsigned char> data;
void beginArrayScope(std::string& arrayName, int index);
public:
ResourceCompiler(TypeDefinitionPtr type, CompoundExprPtr body);
std::string resourceData();
void reserve(int nBits) { write(nBits, 0); }
void write(int nBits, int value);
int tell() { return currentOffset; }

View File

@ -4,16 +4,7 @@
#include "ResourceCompiler.h"
std::ostream &operator<<(std::ostream &out, ResType t)
{
char c1 = static_cast<char>((int)t >> 24);
char c2 = static_cast<char>((int)t >> 16);
char c3 = static_cast<char>((int)t >> 8);
char c4 = static_cast<char>((int)t);
out << "'" << c1 << c2 << c3 << c4 << "'";
return out;
}
std::ostream &operator<<(std::ostream &out, TypeSpec ts)
{

View File

@ -6,18 +6,8 @@
#include <map>
#include "Expression.h"
#include "ResType.h"
class ResType
{
int x;
public:
ResType() : x(0) {}
ResType(int x) : x(x) {}
operator int() const { return x; }
bool operator<(ResType y) const { return x < y.x; }
};
std::ostream& operator<<(std::ostream& out, ResType t);
class TypeSpec
{

View File

@ -6,6 +6,9 @@
#include "RezLexer.h"
#include "RezWorld.h"
#include "ResourceFiles.h"
#include "BinaryIO.h"
int main()
{
//RezLexer lexer("/home/wolfgang/Projects/Retro68/RIncludes/Types.r");
@ -14,5 +17,18 @@ int main()
RezParser parser(lexer, world);
parser.parse();
{
std::ofstream dataOut("test.rsrc");
system("mkdir -p .rsrc");
std::ofstream rsrcOut(".rsrc/test.rsrc");
world.getResources().writeFork(rsrcOut);
system("mkdir -p .finf");
std::ofstream finfOut(".finf/test.rsrc");
ostype(finfOut, "rsrc");
ostype(finfOut, "RSED");
for(int i = 8; i < 32; i++)
byte(finfOut, 0);
}
return 0;
}

View File

@ -88,11 +88,10 @@ RezLexer::RezLexer(std::string filename)
filename));
pImpl->ctx.add_include_path("/home/wolfgang/Projects/Retro68/RIncludes");
// ctx.add_macro_definition(...);
pImpl->ctx.add_macro_definition("DeRez", "0");
pImpl->ctx.add_macro_definition("Rez", "1");
pImpl->ctx.add_macro_definition("true", "1");
pImpl->ctx.add_macro_definition("false", "0");
pImpl->ctx.add_macro_definition("DeRez=0");
pImpl->ctx.add_macro_definition("Rez=1");
pImpl->ctx.add_macro_definition("true=1");
pImpl->ctx.add_macro_definition("false=0");
pImpl->iter = pImpl->ctx.begin();
}
@ -109,7 +108,14 @@ bool RezLexer::atEnd()
RezLexer::WaveToken RezLexer::nextWave()
{
return pImpl->iter == pImpl->ctx.end() ? WaveToken() : (*pImpl->iter++);
if(pImpl->iter == pImpl->ctx.end())
return WaveToken();
else
{
WaveToken tok = *pImpl->iter++;
std::cout << tok.get_value();
return tok;
}
}
RezLexer::WaveToken RezLexer::peekWave()

View File

@ -346,10 +346,7 @@ function_argument_list1 : expression
resource : "resource" res_type "(" expression resource_attributes ")" "{" resource_body "}"
{
int id = $expression->evaluateInt(nullptr);
std::cout << "RESOURCE " << $2 << "(" << id << ")" << std::endl;
TypeDefinitionPtr type = world.getTypeDefinition($res_type, id);
ResourceCompiler compiler(type, $resource_body);
compiler.compile();
world.addResource($res_type, id, $resource_body);
}
;

View File

@ -1,4 +1,8 @@
#include "RezWorld.h"
#include "ResourceCompiler.h"
#include "ResourceFiles.h"
#include <iostream>
RezWorld::RezWorld()
{
@ -20,3 +24,13 @@ TypeDefinitionPtr RezWorld::getTypeDefinition(ResType type, int id)
return nullptr;
}
void RezWorld::addResource(ResType type, int id, CompoundExprPtr body)
{
std::cout << "RESOURCE " << type << "(" << id << ")" << std::endl;
TypeDefinitionPtr def = getTypeDefinition(type, id);
ResourceCompiler compiler(def, body);
compiler.compile();
resources.addResource(Resource(type, id, compiler.resourceData()));
}

View File

@ -5,6 +5,7 @@
#include <stack>
#include "ResourceDefinitions.h"
#include "Expression.h"
#include "ResourceFiles.h"
class RezWorld
{
@ -14,11 +15,17 @@ class RezWorld
std::stack<FieldListPtr> fieldLists;
std::stack<IdentifierExprPtr> functionCalls;
std::stack<SwitchFieldPtr> switches;
Resources resources;
public:
RezWorld();
void addTypeDefinition(TypeSpec spec, TypeDefinitionPtr type);
TypeDefinitionPtr getTypeDefinition(ResType type, int id);
void addResource(ResType type, int id, CompoundExprPtr body);
Resources& getResources() { return resources; }
};