Skip to content

Commit

Permalink
Bump version and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pubby committed Aug 4, 2023
1 parent 9932c0d commit a8f59ad
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ SRCDIR:=src
OBJDIR:=obj
INCS:=-I$(SRCDIR)

VERSION := "0.8"
VERSION := "0.9"
GIT_COMMIT := "$(shell git describe --all --abbrev=8 --dirty --always)"

#override CXX:=clang++
Expand Down
151 changes: 150 additions & 1 deletion doc/doc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3522,7 +3522,7 @@ Syntax:
----
macro("macro_name", "args"...)
----
- `"macro_name"` is the string literal name of the macro file being invoked, without the `.macrofile` extension.
- `"macro_name"` is the string literal name of the macro file being invoked, without the `.macrofile` extension. If the string is empty, no macro is invoked.
- `"args"` are a comma-separated list of string literals to be substituted into the `.macrofab` file.

*Macro Files:*
Expand Down Expand Up @@ -3564,6 +3564,154 @@ vars
U something = 100
----

*Modifiers:*

- <<mod_flags, `+fork_scope`>>

=== `mapfab` [[kw_mapfab]]

The `mapfab` keyword parses a `.mapfab` file and invokes a series of <<kw_macro, macros>> based on the data.
It is only usable at top-level scope.

[NOTE]
http://pubby.games/mapfab.html[MapFab] is a level editor designed to be used with NESFab.

Syntax:
----
mapfab(target, "mapfab_file", "chr_macro", "palette_macro", "metatiles_macro", "level_macro")
----
- `target` specifies the output target to use for the level tiles.
- `"mapfab_file"` is the string literal path to the `.mapfab` file.
- `"chr_macro"` is the name of the macro to invoke for each CHR definition.
- `"palette_macro"` is the name of the macro to invoke for each palette definition.
- `"metatiles_macro"` is the name of the macro to invoke for each metatile set definition.
- `"level_macro"` is the name of the macro to invoke for each level definition.

If any of the macro names are the empty string (`""`), those macros are not invoked.

*CHR Macro:*

The following macro arguments are supplied for each CHR definition:
----
#:name:# // The name of the CHR definition
#:path:# // The path to the CHR definition
----

Additionally, the following private definitions are defined:

----
ct Int _index // The unique index of the CHR definition.
----

[NOTE]
It can make sense to ignore `path`, and instead use `name` to derive the desired path.

*Palette Macro:*

The following macro arguments are supplied for each palette definition:
----
#:name:# // The name of the palette definition, which is an integer from 0 to 255.
----

Additionally, the following private definitions are defined:

----
ct Int _index // The unique index of the palette definition.
ct U[25] _palette // The palette's data.
----

*Metatiles Macro:*

The following macro arguments are supplied for each metatile set definition:
----
#:name:# // The name of the metatile definition.
#:chr_name:# // The name of the CHR definition the metatile set uses for display.
#:palette_name:# // The name of the palette definition the metatile set uses for display.
----

Additionally, the following private definitions are defined:

----
ct Int _index // The unique index of the metatile set definition.
ct Int _num // The number of metatiles in the set.
ct U[_num] _nw // The north-west tiles of each metatile.
ct U[_num] _ne // The north-east tiles of each metatile.
ct U[_num] _sw // The south-west tiles of each metatile.
ct U[_num] _se // The south-east tiles of each metatile.
ct U[_num] _attributes // The 2-bit attribute data of each metatile.
ct U[_num] _collisions // The 6-bit collision data of each metatile.
ct U[_num] _combined // The two arrays above combined: attribute | (collision << 2)
----

[NOTE]
Typically, `chr_name` and `palette_name` should be ignored for metatile sets,
as level macros have this information too.

*Levels Macro:*

The following macro arguments are supplied for each level set definition:
----
#:name:# // The name of the level definition.
#:chr_name:# // The name of the CHR definition the metatile set uses for display.
#:palette_name:# // The name of the palette definition the metatile set uses for display.
#:metatiles_name:# // The name of the metatile set definition the metatile set uses for display.
#:macro_name:# // Contents of MapFab's macro field.
----

Additionally, the following private definitions are defined:

----
ct Int _index // The unique index of the level definition.
ct Int _width // The width of the level, in tiles.
ct Int _height // The height of the level, in tiles.
ct U[_width * _height] _row_major // The level's contents in a row-major order.
ct U[_width * _height] _column_major // The level's contents in a column-major order.
----

For each object class (`CLASS`), the following <<type_vec, VECs>> are defined:

----
ct Int{} _CLASS_x
ct Int{} _CLASS_y
----

For each field (`FIELD`) in `CLASS`, additional VECs are defined, with the string of each field wrapped inside a cast.
----
ct TYPE{} _CLASS_FIELD
----

For example, if the class `foo` had three objects in this level,
and each object had a field `U bar`, the following definitions would exist:

----
ct Int{} _foo_x = Int{}(203, -3, 3099)
ct Int{} _foo_y = Int{}(13, 991, -30)
ct U{} _foo_bar = U{}(U(0), U(5), U(2))
----

*Output Target Conversions*

The data of each level (`_row_major` and `_column_major`) are converted based on specified target:

[cols="1,3"]
|===
|Conversion Target |Description

| <<file_raw, `raw`>>
| No conversion

| <<file_pbz, `pbz`>>
| Compress using PBZ compression

| <<file_rlz, `rlz`>>
| Compress using RLZ compression (no terminator)

|===

*Modifiers:*

- <<mod_flags, `+fork_scope`>>

=== `audio` [[kw_audio]]

The `audio` keyword imports and converts audio data from an external file,
Expand Down Expand Up @@ -3735,6 +3883,7 @@ The following flags exist:
- `palette_3`: Converts 4-byte palettes into 3-byte palettes.
- `palette_25`: Converts 32-byte palettes into 25-byte palettes.
- `+sloppy`, `-sloppy`: Enables / disables faster compilation speed, at the cost of performance.
- `+fork_scope`: The invoked macro(s) will have access to private definitions inside the invoking file.

Example:
----
Expand Down
13 changes: 12 additions & 1 deletion src/compiler_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "format.hpp"
#include "options.hpp"
#include "assert.hpp"
#include "text.hpp"

namespace
{
Expand Down Expand Up @@ -90,7 +91,17 @@ std::string fmt_error(

assert(file->index() == pstring.file_i);

std::string str(fmt("%: %%:" CONSOLE_RESET " %\n", fmt_source_pos(*file, pstring), color, prefix, what));
std::string str;

if(auto const* invoke = file->invoke())
{
str += fmt(CONSOLE_BOLD "In the invocation of macro \"%\":\n" CONSOLE_RESET, escape(invoke->name));
unsigned i = 0;
for(std::string const& arg : invoke->args)
str += fmt(" With argument % = \"%\"\n", i++, escape(arg));
}

str += fmt("%: %%:" CONSOLE_RESET " %\n", fmt_source_pos(*file, pstring), color, prefix, what);

char const* line_begin = get_line_begin(file->source(), pstring);
char const* line_end = get_line_end(file->source(), pstring);
Expand Down
7 changes: 6 additions & 1 deletion src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
struct macro_result_t
{
fs::path path;
macro_invocation_t invoke;
std::string contents;
ident_map_t<global_ht> private_globals;
ident_map_t<group_ht> private_groups;
Expand Down Expand Up @@ -66,7 +67,10 @@ void invoke_macro(
{
std::lock_guard<std::mutex> lock(invoke_mutex);
if(invoke_set.insert(invoke).second)
new_macro_results.push_back({ pair->second.file, std::move(str), std::move(private_globals), std::move(private_groups) });
{
new_macro_results.push_back({ pair->second.dir / pair->second.file, std::move(invoke), std::move(str),
std::move(private_globals), std::move(private_groups) });
}
}
}

Expand Down Expand Up @@ -229,5 +233,6 @@ void file_contents_t::reset(unsigned file_i)
m_source = macro.contents.data();
m_private_globals = &macro.private_globals;
m_private_groups = &macro.private_groups;
m_invoke = &macro.invoke;
}
}
2 changes: 2 additions & 0 deletions src/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ struct file_contents_t
std::size_t size() const { return m_size; }
ident_map_t<global_ht> const* private_globals() const { return m_private_globals; }
ident_map_t<group_ht> const* private_groups() const { return m_private_groups; }
macro_invocation_t const* invoke() const { return m_invoke; }

void clear() { m_alloc.reset(); m_size = 0; m_source = nullptr; }
void reset(unsigned file_i);
Expand All @@ -92,6 +93,7 @@ struct file_contents_t
std::unique_ptr<char[]> m_alloc;
ident_map_t<global_ht> const* m_private_globals = nullptr;
ident_map_t<group_ht> const* m_private_groups = nullptr;
macro_invocation_t const* m_invoke = nullptr;
};

#endif
41 changes: 33 additions & 8 deletions src/mapfab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include "convert_compress.hpp"

void convert_mapfab(mapfab_convert_type_t ct, std::uint8_t const* const begin, std::size_t size,
pstring_t at, fs::path mapfab_path, mapfab_macros_t const& macros)
pstring_t at, fs::path mapfab_path, mapfab_macros_t const& macros,
ident_map_t<global_ht>* base_private_globals,
ident_map_t<group_ht>* base_private_groups)
{
using namespace std::literals;

Expand Down Expand Up @@ -68,6 +70,12 @@ void convert_mapfab(mapfab_convert_type_t ct, std::uint8_t const* const begin, s
for(unsigned i = 0; i < num_chr; ++i)
{
ident_map_t<global_ht> private_globals;
if(base_private_globals)
private_globals = *base_private_globals;
ident_map_t<group_ht> private_groups;
if(base_private_groups)
private_groups = *base_private_groups;

define_ct_int(private_globals.lookup(at, "_index"sv), at, TYPE_INT, i);

macro_invocation_t m = { macros.chr };
Expand All @@ -78,7 +86,7 @@ void convert_mapfab(mapfab_convert_type_t ct, std::uint8_t const* const begin, s
if(path.is_relative())
path = base_path / path;
m.args.push_back(path.string());
invoke_macro(std::move(m), std::move(private_globals), {});
invoke_macro(std::move(m), std::move(private_globals), std::move(private_groups));
dprint(log, "MAPFAB_CHR_MACRO", i);
}

Expand All @@ -90,15 +98,20 @@ void convert_mapfab(mapfab_convert_type_t ct, std::uint8_t const* const begin, s
for(unsigned i = 0; i < num_palettes; ++i)
{
// Defines:

ident_map_t<global_ht> private_globals;
if(base_private_globals)
private_globals = *base_private_globals;
ident_map_t<group_ht> private_groups;
if(base_private_groups)
private_groups = *base_private_groups;

global_t& g = private_globals.lookup(at, "_palette"sv);
define_ct(g, at, palette_data.data() + 25*i, 25);
define_ct_int(private_globals.lookup(at, "_index"sv), at, TYPE_INT, i);

macro_invocation_t m = { macros.palette };
m.args.push_back(std::to_string(i));
invoke_macro(std::move(m), std::move(private_globals), {});
invoke_macro(std::move(m), std::move(private_globals), std::move(private_groups));
dprint(log, "MAPFAB_PALETTE_MACRO", i);
}

Expand Down Expand Up @@ -140,6 +153,12 @@ void convert_mapfab(mapfab_convert_type_t ct, std::uint8_t const* const begin, s
mt_combined[i] = (mt_attributes[i] & 0b11) | (mt_collisions[i] << 2);

ident_map_t<global_ht> private_globals;
if(base_private_globals)
private_globals = *base_private_globals;
ident_map_t<group_ht> private_groups;
if(base_private_groups)
private_groups = *base_private_groups;

define_ct_int(private_globals.lookup(at, "_index"sv), at, TYPE_INT, i);
define_ct_int(private_globals.lookup(at, "_num"sv), at, TYPE_INT, num);
define_ct(private_globals.lookup(at, "_nw"sv), at, mt_nw.data(), num);
Expand All @@ -154,7 +173,7 @@ void convert_mapfab(mapfab_convert_type_t ct, std::uint8_t const* const begin, s
m.args.push_back(name);
m.args.push_back(chr_name);
m.args.push_back(std::to_string(palette));
invoke_macro(std::move(m), std::move(private_globals), {});
invoke_macro(std::move(m), std::move(private_globals), std::move(private_groups));
}

struct field_t
Expand Down Expand Up @@ -227,6 +246,12 @@ void convert_mapfab(mapfab_convert_type_t ct, std::uint8_t const* const begin, s
}

ident_map_t<global_ht> private_globals;
if(base_private_globals)
private_globals = *base_private_globals;
ident_map_t<group_ht> private_groups;
if(base_private_groups)
private_groups = *base_private_groups;

define_ct_int(private_globals.lookup(at, "_index"sv), at, TYPE_INT, i);
define_ct_int(private_globals.lookup(at, "_width"sv), at, TYPE_INT, w);
define_ct_int(private_globals.lookup(at, "_height"sv), at, TYPE_INT, h);
Expand Down Expand Up @@ -288,19 +313,19 @@ void convert_mapfab(mapfab_convert_type_t ct, std::uint8_t const* const begin, s
{
if(k != j)
append += ", ";
append += objects[i][k];
append += fmt("%(%)", field.type, objects[i][k]);
}
append += ")\n";
}
}

macro_invocation_t m = { macros.level.empty() ? macro_name : macros.level };
macro_invocation_t m = { macros.level };
m.args.push_back(name);
m.args.push_back(chr_name);
m.args.push_back(std::to_string(palette));
m.args.push_back(metatiles_name);
m.args.push_back(macro_name);
invoke_macro(std::move(m), std::move(private_globals), {}, append);
invoke_macro(std::move(m), std::move(private_globals), std::move(private_groups), std::move(append));
}
}

9 changes: 8 additions & 1 deletion src/mapfab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ enum mapfab_convert_type_t
MAPFAB_PBZ,
};

template<typename Handle>
class ident_map_t;
class global_ht;
class group_ht;

void convert_mapfab(mapfab_convert_type_t ct, std::uint8_t const* const begin, std::size_t size,
pstring_t at, fs::path mapfab_path, mapfab_macros_t const& macros);
pstring_t at, fs::path mapfab_path, mapfab_macros_t const& macros,
ident_map_t<global_ht>* private_globals,
ident_map_t<group_ht>* private_groups);

#endif
2 changes: 1 addition & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ auto parser_t<P>::parse_file(token_type_t tt, Fn const& fn)
fs::path preferred_dir = file.path();
preferred_dir.remove_filename();

fn(file_pstring, script, preferred_dir, std::move(mods), std::move(args));
fn(file_pstring, script, std::filesystem::absolute(preferred_dir), std::move(mods), std::move(args));
}

template<typename P>
Expand Down
Loading

0 comments on commit a8f59ad

Please sign in to comment.