aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorRaymaekers Luca <luca@spacehb.net>2025-03-17 14:05:23 +0100
committerRaymaekers Luca <luca@spacehb.net>2025-03-17 14:05:23 +0100
commitb163092f847a6fa28623fb0d23dceb6437c2c1ca (patch)
treed796fcf4fd277603668f7757ac24714fb4deb3ef /source
parentb4f5cf20865ce0249dd244d7aae73acd150f4b69 (diff)
checkpoint
Diffstat (limited to 'source')
-rw-r--r--source/meta.c115
1 files changed, 59 insertions, 56 deletions
diff --git a/source/meta.c b/source/meta.c
index d36420b..39de3c8 100644
--- a/source/meta.c
+++ b/source/meta.c
@@ -2,8 +2,8 @@
C preprocessor that provides meta functionality.
To-Do's
- - [ ] Most parsing assertion should really be an error message, create nice syntax errors
- Idea: Byte encoding to show where the errors happens in the file and highlight that.
+ - [ ] Error messages instead of asserts
+ - Show byte offset to show where the errors happens in the file and highlight that.
- [ ] Expanding over multiple tables
- [ ] Syntactic sugar to specify array of single values ?
- [ ] Preserve indent when expansion is on a new line
@@ -146,19 +146,22 @@ int
main(int ArgC, char *Args[])
{
char *Filename = 0;
- arena ScratchArena = {0};
- ScratchArena.Size = Megabyte(2);
- ScratchArena.Memory = mmap(0, Megabyte(4), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
-
- Assert(ScratchArena.Memory);
+
+ char *Storage = mmap(0, Megabyte(4), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ Assert(Storage);
+ arena ScratchArena = {
+ .Memory = Storage,
+ .Pos = 0,
+ .Size = Megabyte(1)
+ };
arena TablesArena = {
- .Memory = ScratchArena.Memory + Megabyte(2),
+ .Memory = Storage + ScratchArena.Size,
.Pos = 0,
- .Size = Megabyte(2)
+ .Size = Megabyte(1)
};
-
table *Tables = (table*)TablesArena.Memory;
i32 TablesCount = 0;
+ char *Out = ScratchArena.Memory + TablesArena.Size;
if (ArgC > 1)
{
@@ -174,13 +177,13 @@ main(int ArgC, char *Args[])
// NOTE(luca): The memory is assumed to stay mapped until program exits, because we will use
// pointers into that memory.
s8 File = ReadEntireFileIntoMemory(Filename);
- char *Buffer = File.Memory;
+ char *In = File.Memory;
for (i64 At = 0;
At < File.Size;
At++)
{
- if (Buffer[At] == '@')
+ if (In[At] == '@')
{
At++;
@@ -189,7 +192,7 @@ main(int ArgC, char *Args[])
s8 ExpandKeyword = S8("expand");
s8 Keywords[] = { TableKeyword, TableGenEnumKeyword, ExpandKeyword };
- if (!strncmp(Buffer + At, S8_ARG(ExpandKeyword)))
+ if (!strncmp(In + At, S8_ARG(ExpandKeyword)))
{
i32 ExpressionAt = 0;
s8 ExpressionTableName = {0};
@@ -205,49 +208,49 @@ main(int ArgC, char *Args[])
At += ExpandKeyword.Size;
Assert(At < File.Size);
- Assert(Buffer[At] == '(');
+ Assert(In[At] == '(');
At++;
ExpressionTableNameAt = At;
- while (!IsWhitespace(Buffer[At]) && At < File.Size) At++;
+ while (!IsWhitespace(In[At]) && At < File.Size) At++;
Assert(At < File.Size);
- ExpressionTableName.Memory = Buffer + ExpressionTableNameAt;
+ ExpressionTableName.Memory = In + ExpressionTableNameAt;
ExpressionTableName.Size = At - ExpressionTableNameAt;
- while (IsWhitespace(Buffer[At]) && At < File.Size) At++;
+ while (IsWhitespace(In[At]) && At < File.Size) At++;
Assert(At < File.Size);
ExpressionTableArgumentAt = At;
- while (Buffer[At] != ')' && At < File.Size) At++;
+ while (In[At] != ')' && At < File.Size) At++;
Assert(At < File.Size);
- ExpressionTableArgument.Memory = Buffer + ExpressionTableArgumentAt;
+ ExpressionTableArgument.Memory = In + ExpressionTableArgumentAt;
ExpressionTableArgument.Size = At - ExpressionTableArgumentAt;
At++;
- while (IsWhitespace(Buffer[At]) && At < File.Size) At++;
+ while (IsWhitespace(In[At]) && At < File.Size) At++;
Assert(At < File.Size);
- Assert(Buffer[At] == '`');
+ Assert(In[At] == '`');
At++;
ExpressionAt = At;
// TODO: multiple expansions in one expression
- while (Buffer[At] != '`')
+ while (In[At] != '`')
{
- if (Buffer[At] == '$' && Buffer[At + 1] == '(')
+ if (In[At] == '$' && In[At + 1] == '(')
{
Expansion.Start = At;
At += 2;
ExpandArgumentAt = At;
- while (Buffer[At] != '.' && At < File.Size) At++;
+ while (In[At] != '.' && At < File.Size) At++;
Assert(At < File.Size);
- ExpandArgument.Memory = Buffer + ExpandArgumentAt;
+ ExpandArgument.Memory = In + ExpandArgumentAt;
ExpandArgument.Size = At - ExpandArgumentAt;
At++;
ExpandArgumentLabelAt = At;
- while (Buffer[At] != ')' && At < File.Size) At++;
+ while (In[At] != ')' && At < File.Size) At++;
Assert(At < File.Size);
- ExpandArgumentLabel.Memory = Buffer + ExpandArgumentLabelAt;
+ ExpandArgumentLabel.Memory = In + ExpandArgumentLabelAt;
ExpandArgumentLabel.Size = At - ExpandArgumentLabelAt;
Expansion.End = At;
@@ -285,7 +288,7 @@ main(int ArgC, char *Args[])
// TODO(now): the bug
- while (Buffer[At] != '`' && At < File.Size) At++;
+ while (In[At] != '`' && At < File.Size) At++;
Assert(File.Size);
for (i32 ElementAt = 0;
@@ -293,9 +296,9 @@ main(int ArgC, char *Args[])
ElementAt++)
{
s8 ExpansionText = CurrentTable->Elements[ElementAt * CurrentTable->LabelsCount + LabelIndex];
- write(STDOUT_FILENO, Buffer + ExpressionAt, Expansion.Start - ExpressionAt);
+ write(STDOUT_FILENO, In + ExpressionAt, Expansion.Start - ExpressionAt);
write(STDOUT_FILENO, S8_ARG(ExpansionText));
- write(STDOUT_FILENO, Buffer + Expansion.End + 1, At - (Expansion.End + 1));
+ write(STDOUT_FILENO, In + Expansion.End + 1, At - (Expansion.End + 1));
write(STDOUT_FILENO, S8_LIT("\n"));
}
@@ -308,13 +311,13 @@ main(int ArgC, char *Args[])
At++;
}
- else if (!strncmp(Buffer + At, TableGenEnumKeyword.Memory, TableGenEnumKeyword.Size))
+ else if (!strncmp(In + At, TableGenEnumKeyword.Memory, TableGenEnumKeyword.Size))
{
// TODO: not implemented yet
- while (Buffer[At] != '}' && At < File.Size) At++;
+ while (In[At] != '}' && At < File.Size) At++;
Assert(At < File.Size);
}
- else if (!strncmp(Buffer + At, TableKeyword.Memory, TableKeyword.Size))
+ else if (!strncmp(In + At, TableKeyword.Memory, TableKeyword.Size))
{
s8 TableName = {0};
i32 LabelsCount = 0;
@@ -324,23 +327,23 @@ main(int ArgC, char *Args[])
// Parse the labels
At += TableKeyword.Size;
- Assert(Buffer[At] == '(');
+ Assert(In[At] == '(');
i32 BeginParenAt = At;
At++;
i32 CurrentLabelAt = At;
s8* CurrentLabel = 0;
- while (Buffer[At] != ')')
+ while (In[At] != ')')
{
- if (Buffer[At] == ',')
+ if (In[At] == ',')
{
CurrentLabel = (s8*)ArenaPush(&ScratchArena, sizeof(*CurrentLabel));
- CurrentLabel->Memory = Buffer + CurrentLabelAt;
+ CurrentLabel->Memory = In + CurrentLabelAt;
CurrentLabel->Size = At - CurrentLabelAt;
LabelsCount++;
At++;
- while (IsWhitespace(Buffer[At]) && At < File.Size) At++;
+ while (IsWhitespace(In[At]) && At < File.Size) At++;
Assert(At < File.Size);
CurrentLabelAt = At;
}
@@ -358,24 +361,24 @@ main(int ArgC, char *Args[])
{
Labels = (s8*)(ScratchArena.Memory);
CurrentLabel = (s8*)ArenaPush(&ScratchArena, sizeof(*CurrentLabel));
- CurrentLabel->Memory = Buffer + CurrentLabelAt;
+ CurrentLabel->Memory = In + CurrentLabelAt;
CurrentLabel->Size = At - CurrentLabelAt;
LabelsCount++;
}
// Parse table name
At++;
- while (IsWhitespace(Buffer[At]) && At < File.Size) At++;
+ while (IsWhitespace(In[At]) && At < File.Size) At++;
Assert(At < File.Size);
i32 TableNameAt = At;
- while (!IsWhitespace(Buffer[At]) && At < File.Size) At++;
+ while (!IsWhitespace(In[At]) && At < File.Size) At++;
Assert(At < File.Size);
- TableName.Memory = Buffer + TableNameAt;
+ TableName.Memory = In + TableNameAt;
TableName.Size = At - TableNameAt;
- while (IsWhitespace(Buffer[At]) && At < File.Size) At++;
+ while (IsWhitespace(In[At]) && At < File.Size) At++;
Assert(At < File.Size);
- Assert(Buffer[At] == '{');
+ Assert(In[At] == '{');
At++;
if (LabelsCount == 0)
@@ -395,15 +398,15 @@ main(int ArgC, char *Args[])
while (!ShouldStop)
{
- while (IsWhitespace(Buffer[At]) && At < File.Size) At++;
+ while (IsWhitespace(In[At]) && At < File.Size) At++;
Assert(At < File.Size);
- if (Buffer[At] == '}')
+ if (In[At] == '}')
{
ShouldStop = true;
}
else
{
- Assert(Buffer[At] == '{');
+ Assert(In[At] == '{');
At++;
CurrentElement = (s8*)ArenaPush(&ScratchArena, sizeof(*CurrentElement) * LabelsCount);
@@ -412,12 +415,12 @@ main(int ArgC, char *Args[])
LabelAt < LabelsCount;
LabelAt++)
{
- while (IsWhitespace(Buffer[At]) && At < File.Size) At++;
+ while (IsWhitespace(In[At]) && At < File.Size) At++;
Assert(At < File.Size);
CurrentElementAt = At;
IsPair = true;
- switch (Buffer[At])
+ switch (In[At])
{
case '\'': PairChar = '\''; break;
case '"': PairChar = '"'; break;
@@ -432,25 +435,25 @@ main(int ArgC, char *Args[])
// same character to open and to close. We can also assume
// that a label within an element must be a minimum of 1
// character so skipping should be fine.
- while (Buffer[At] != PairChar && At < File.Size) At++;
+ while (In[At] != PairChar && At < File.Size) At++;
Assert(At < File.Size);
At++;
}
else
{
- while (!IsWhitespace(Buffer[At]) && At < File.Size) At++;
+ while (!IsWhitespace(In[At]) && At < File.Size) At++;
Assert(At < File.Size);
}
- CurrentElement[LabelAt].Memory = Buffer + CurrentElementAt;
+ CurrentElement[LabelAt].Memory = In + CurrentElementAt;
CurrentElement[LabelAt].Size = At - CurrentElementAt;
}
ElementsCount++;
// Find end of element '}'
- while (IsWhitespace(Buffer[At]) && At < File.Size) At++;
+ while (IsWhitespace(In[At]) && At < File.Size) At++;
Assert(At < File.Size);
- Assert(Buffer[At] == '}');
+ Assert(In[At] == '}');
At++;
}
}
@@ -467,13 +470,13 @@ main(int ArgC, char *Args[])
else
{
// ERROR: What if the code contains a non meta-"@_expand" tag ???
- write(STDOUT_FILENO, Buffer + At, 1);
+ write(STDOUT_FILENO, In + At, 1);
}
}
else
{
- write(STDOUT_FILENO, Buffer + At, 1);
+ write(STDOUT_FILENO, In + At, 1);
}
}