Add pushc and popc directives

This commit is contained in:
dbrotz
2019-08-30 19:36:23 -07:00
parent 461ef6cea5
commit d3db5f0d76
8 changed files with 149 additions and 61 deletions

View File

@@ -621,6 +621,8 @@ static void strsubUTF8(char *dest, const char *src, uint32_t pos, uint32_t len)
%token T_POP_CHARMAP
%token T_POP_NEWCHARMAP
%token T_POP_SETCHARMAP
%token T_POP_PUSHC
%token T_POP_POPC
%token T_POP_SHIFT
%token T_POP_ENDR
%token T_POP_FAIL
@@ -776,6 +778,8 @@ simple_pseudoop : include
| charmap
| newcharmap
| setcharmap
| pushc
| popc
| rept
| shift
| fail
@@ -1047,11 +1051,19 @@ newcharmap : T_POP_NEWCHARMAP T_ID
{
charmap_New($2, $4);
}
;
setcharmap : T_POP_SETCHARMAP T_ID
{
charmap_Set($2);
}
;
pushc : T_POP_PUSHC { charmap_Push(); }
;
popc : T_POP_POPC { charmap_Pop(); }
;
printt : T_POP_PRINTT string
{

View File

@@ -20,11 +20,18 @@
#define CHARMAP_HASH_SIZE (1 << 9)
struct CharmapStackEntry {
struct Charmap *charmap;
struct CharmapStackEntry *next;
};
static struct Charmap *tHashedCharmaps[CHARMAP_HASH_SIZE];
static struct Charmap *mainCharmap;
static struct Charmap *currentCharmap;
struct CharmapStackEntry *charmapStack;
static void warnSectionCharmap(void)
{
static bool warned = false;
@@ -119,6 +126,32 @@ void charmap_Set(const char *name)
currentCharmap = *ppCharmap;
}
void charmap_Push(void)
{
struct CharmapStackEntry *stackEntry;
stackEntry = malloc(sizeof(struct CharmapStackEntry));
if (stackEntry == NULL)
fatalerror("No memory for charmap stack");
stackEntry->charmap = currentCharmap;
stackEntry->next = charmapStack;
charmapStack = stackEntry;
}
void charmap_Pop(void)
{
if (charmapStack == NULL)
fatalerror("No entries in the charmap stack");
struct CharmapStackEntry *top = charmapStack;
currentCharmap = top->charmap;
charmapStack = top->next;
free(top);
}
void charmap_InitMain(void)
{
mainCharmap = charmap_New("main", NULL);

View File

@@ -481,6 +481,8 @@ const struct sLexInitString lexer_strings[] = {
{"charmap", T_POP_CHARMAP},
{"newcharmap", T_POP_NEWCHARMAP},
{"setcharmap", T_POP_SETCHARMAP},
{"pushc", T_POP_PUSHC},
{"popc", T_POP_POPC},
{"fail", T_POP_FAIL},
{"warn", T_POP_WARN},

View File

@@ -1146,7 +1146,8 @@ a different encoding for other purposes, for example. Initially, there is
one character map called
.Sy main
and it is automatically selected as the current character map from the
beginning.
beginning. There is also a character map stack that can be used to save and
restore which character map is currently active.
.Bl -column "NEWCHARMAP name, basename"
.It Sy Command Ta Sy Meaning
.It Ic NEWCHARMAP Ar name Ta Creates a new, empty character map called
@@ -1156,6 +1157,8 @@ beginning.
copied from character map
.Ic basename .
.It Ic SETCHARMAP Ar name Ta Switch to character map Ic name .
.It Ic PUSHC Ta Push the current character map onto the stack.
.It Ic POPC Ta Pop a character map off the stack and switch to it.
.El
.Pp
.Sy Note: