Lumenarium/meta/foldhaus_meta.cpp

1220 lines
33 KiB
C++
Raw Normal View History

#include <windows.h>
#include <stdio.h>
#include <gs_language.h>
#include <gs_bucket.h>
#include "..\src\gs_platform.h"
#include <gs_memory_arena.h>
#if 1
2019-10-30 14:28:02 +00:00
#include "..\src\gs_string.h"
#else
#include <gs_string.h>
#endif
#include <gs_string_builder.h>
2019-10-30 14:28:02 +00:00
#include "gs_meta_error.h"
#include "gs_meta_lexer.h"
#include "foldhaus_meta_type_table.h"
2019-10-30 14:28:02 +00:00
error_list GlobalErrorList = {};
PLATFORM_ALLOC(StdAlloc)
{
u8* Result = (u8*)malloc(Size);
2019-10-30 14:28:02 +00:00
return Result;
}
struct source_code_file
{
string Path;
s32 FileSize;
string Contents;
s32 FirstTokenIndex;
s32 LastTokenIndex;
2019-10-30 14:28:02 +00:00
};
struct token_iter
2019-10-30 14:28:02 +00:00
{
gs_bucket<token>* Tokens;
token* TokenAt;
s32 TokenAtIndex;
s32 FirstToken;
s32 LastToken;
#define TOKEN_ITER_SNAPSHOTS_MAX 64
u32 SnapshotsUsed;
u32 Snapshots[TOKEN_ITER_SNAPSHOTS_MAX];
2019-10-30 14:28:02 +00:00
};
internal token*
NextToken (token_iter* Iter)
{
if (Iter->TokenAtIndex < Iter->LastToken)
{
Iter->TokenAtIndex++;
Iter->TokenAt = Iter->Tokens->GetElementAtIndex(Iter->TokenAtIndex);
}
return Iter->TokenAt;
}
internal b32
TokenAtEquals(token_iter* Iter, char* String)
{
b32 Result = false;
if (StringEqualsCharArray(Iter->TokenAt->Text, String))
{
Result = true;
NextToken(Iter);
}
return Result;
}
internal void
PushSnapshot (token_iter* Iter)
{
Iter->Snapshots[Iter->SnapshotsUsed++] = Iter->TokenAtIndex;
}
internal void
PopSnapshot (token_iter* Iter)
{
if (Iter->SnapshotsUsed > 0)
{
Iter->SnapshotsUsed -= 1;
}
}
2019-10-30 14:28:02 +00:00
internal void
ApplySnapshot (token_iter* Iter)
2019-10-30 14:28:02 +00:00
{
u32 SnapshotIndex = Iter->SnapshotsUsed;
u32 SnapshotPoint = Iter->Snapshots[SnapshotIndex];
Iter->TokenAtIndex = SnapshotPoint;
Iter->TokenAt = Iter->Tokens->GetElementAtIndex(SnapshotPoint);
2019-10-30 14:28:02 +00:00
}
internal void
ApplySnapshotIfNotParsedAndPop(b32 ParseSuccess, token_iter* Iter)
2019-10-30 14:28:02 +00:00
{
PopSnapshot(Iter);
if (!ParseSuccess)
2019-10-30 14:28:02 +00:00
{
ApplySnapshot(Iter);
2019-10-30 14:28:02 +00:00
}
}
2019-10-30 14:28:02 +00:00
internal s32
GetFileSize (char* FileName)
{
s32 Result = 0;
FILE* ReadFile = fopen(FileName, "r");
if (ReadFile)
{
fseek(ReadFile, 0, SEEK_END);
size_t FileSize = ftell(ReadFile);
fseek(ReadFile, 0, SEEK_SET);
Result = (s32)FileSize;
fclose(ReadFile);
}
return Result;
}
internal s32
ReadEntireFileAndNullTerminate (source_code_file* File)
2019-10-30 14:28:02 +00:00
{
s32 LengthRead = 0;
FILE* ReadFile = fopen(File->Path.Memory, "r");
2019-10-30 14:28:02 +00:00
if (ReadFile)
{
fseek(ReadFile, 0, SEEK_END);
size_t FileSize = ftell(ReadFile);
fseek(ReadFile, 0, SEEK_SET);
Assert(File->Contents.Memory == 0);
File->Contents.Max = (s32)FileSize + 1;
File->Contents.Memory = (char*)malloc(File->Contents.Max);
size_t ReadSize = fread(File->Contents.Memory, 1, FileSize, ReadFile);
File->Contents.Memory[FileSize] = 0;
File->Contents.Length = (s32)ReadSize;
LengthRead = (s32)ReadSize + 1;
2019-10-30 14:28:02 +00:00
fclose(ReadFile);
}
else
{
LogError(&GlobalErrorList, "Could Not Read File: %.*s", StringExpand(File->Path));
2019-10-30 14:28:02 +00:00
}
return LengthRead;
}
internal b32
FileAlreadyInSource(string Path, gs_bucket<source_code_file> SourceFiles)
2019-10-30 14:28:02 +00:00
{
b32 Result = false;
2019-10-30 14:28:02 +00:00
for (u32 i = 0; i < SourceFiles.Used; i++)
2019-10-30 14:28:02 +00:00
{
source_code_file* File = SourceFiles.GetElementAtIndex(i);
if (StringsEqual(File->Path, Path))
2019-10-30 14:28:02 +00:00
{
Result = true;
printf("-- File already in source: %.*s\n", StringExpand(Path));
2019-10-30 14:28:02 +00:00
break;
}
}
return Result;
}
internal void
EatToNextLine (tokenizer* Tokenizer)
2019-10-30 14:28:02 +00:00
{
while (AtValidPosition(*Tokenizer) && !IsNewline(*Tokenizer->At))
2019-10-30 14:28:02 +00:00
{
EatChar(Tokenizer);
2019-10-30 14:28:02 +00:00
}
}
internal s32
GetTypedefSize (token* Token)
{
s32 Size = 0;
token* LookingAt = Token->Next;
if (StringsEqual(LookingAt->Text, MakeStringLiteral("unsigned")) ||
StringsEqual(LookingAt->Text, MakeStringLiteral("signed")))
{
LookingAt = LookingAt->Next;
}
s32 Level = 1;
if (StringsEqual(LookingAt->Text, MakeStringLiteral("short")))
{
Level = -1;
LookingAt = LookingAt->Next;
}
while (StringsEqual(LookingAt->Text, MakeStringLiteral("long")))
{
Level= 2.f;
LookingAt = LookingAt->Next;
}
if (StringsEqual(LookingAt->Text, MakeStringLiteral("char")))
{
Size = 1;
}
else if (StringsEqual(LookingAt->Text, MakeStringLiteral("int")))
{
switch (Level)
{
case -1: { Size = 2; } break;
case 1: { Size = 4; } break;
case 2: { Size = 8; } break;
InvalidDefaultCase;
}
}
else if (StringsEqual(LookingAt->Text, MakeStringLiteral("float")))
{
Size = 4;
}
else if (StringsEqual(LookingAt->Text, MakeStringLiteral("double")))
{
Size = 8;
}
else if (StringsEqual(LookingAt->Text, MakeStringLiteral("bool")))
{
Size = 1;
}
else if (StringsEqual(LookingAt->Text, MakeStringLiteral("void")))
{
LogError(&GlobalErrorList, "void type Not Handled");
}
return Size;
}
internal string
GetTypedefIdentifier (token* Token)
{
string Identifier = {};
token* PreviousToken = Token;
token* LookingAt = Token->Next;
while (LookingAt->Type != Token_Semicolon)
{
PreviousToken = LookingAt;
LookingAt = LookingAt->Next;
}
if (PreviousToken->Type == Token_Identifier)
{
Identifier = PreviousToken->Text;
}
return Identifier;
}
internal s64
GetWallClock ()
{
LARGE_INTEGER Time;
if (!QueryPerformanceCounter(&Time))
{
s32 Error = GetLastError();
InvalidCodePath;
}
return (s64)Time.QuadPart;
}
internal s64
GetPerformanceFrequency ()
{
LARGE_INTEGER Frequency;
if (!QueryPerformanceFrequency(&Frequency))
{
s32 Error = GetLastError();
InvalidCodePath;
}
return (s64)Frequency.QuadPart;
}
internal r32
GetSecondsElapsed(s64 StartCycles, s64 EndCycles)
{
s64 Frequency = GetPerformanceFrequency();
r32 SecondsElapsed = (r32)(EndCycles - StartCycles) / (r32)(Frequency);
return SecondsElapsed;
}
2019-10-30 14:28:02 +00:00
internal void
AddFileToSource(string RelativePath, gs_bucket<source_code_file>* SourceFiles)
2019-10-30 14:28:02 +00:00
{
source_code_file File = {0};
File.FirstTokenIndex = -1;
File.LastTokenIndex = -1;
2019-10-30 14:28:02 +00:00
u32 PathLength = RelativePath.Length + 1;
File.Path = MakeString((char*)malloc(sizeof(char) * PathLength), 0, PathLength);
CopyStringTo(RelativePath, &File.Path);
NullTerminate(&File.Path);
File.FileSize = ReadEntireFileAndNullTerminate(&File);
if (File.FileSize > 0)
{
SourceFiles->PushElementOnBucket(File);
}
else
2019-10-30 14:28:02 +00:00
{
printf("Error: Could not load file %.*s\n", StringExpand(RelativePath));
}
}
internal void
TokenizeFile (source_code_file* File, gs_bucket<token>* Tokens)
{
tokenizer Tokenizer = {};
Tokenizer.At = File->Contents.Memory;
Tokenizer.Memory = File->Contents.Memory;
Tokenizer.MemoryLength = File->Contents.Max;
token* LastToken = 0;
while(AtValidPosition(Tokenizer))
{
token NewToken = GetNextToken(&Tokenizer);
u32 TokenIndex = Tokens->PushElementOnBucket(NewToken);
if (File->FirstTokenIndex < 0)
2019-10-30 14:28:02 +00:00
{
File->FirstTokenIndex = (s32)TokenIndex;
}
}
File->LastTokenIndex = Tokens->Used - 1;
}
internal b32
ParseMetaTag(token_iter* Iter, gs_bucket<token*>* TagList)
{
b32 Result = false;
PushSnapshot(Iter);
if (TokenAtEquals(Iter, "GSMetaTag") &&
TokenAtEquals(Iter, "("))
{
if (Iter->TokenAt->Type == Token_Identifier)
{
TagList->PushElementOnBucket(Iter->TokenAt);
NextToken(Iter);
if (TokenAtEquals(Iter, ")") &&
TokenAtEquals(Iter, ";"))
2019-10-30 14:28:02 +00:00
{
Result = true;
2019-10-30 14:28:02 +00:00
}
}
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
return Result;
2019-10-30 14:28:02 +00:00
}
internal b32
ShortInt (token_iter* Iter, s32* TypeIndexOut, type_table TypeTable)
2019-10-30 14:28:02 +00:00
{
b32 Result = false;
PushSnapshot(Iter);
2019-10-30 14:28:02 +00:00
if (TokenAtEquals(Iter, "unsigned") ||
TokenAtEquals(Iter, "signed"))
{
}
if (TokenAtEquals(Iter, "short"))
{
Result = true;
if (TokenAtEquals(Iter, "int"))
{
Result = true;
}
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
if (Result)
{
*TypeIndexOut = GetIndexOfType(MakeStringLiteral("short int"), TypeTable);
}
return Result;
}
internal b32
Int (token_iter* Iter, s32* TypeIndexOut, type_table TypeTable)
{
b32 Result = false;
PushSnapshot(Iter);
2019-10-30 14:28:02 +00:00
if (TokenAtEquals(Iter, "unsigned") ||
TokenAtEquals(Iter, "signed"))
{
}
2019-10-30 14:28:02 +00:00
if (TokenAtEquals(Iter, "int"))
{
Result = true;
}
2019-10-30 14:28:02 +00:00
ApplySnapshotIfNotParsedAndPop(Result, Iter);
if (Result)
{
*TypeIndexOut = GetIndexOfType(MakeStringLiteral("int"), TypeTable);
}
return Result;
}
internal b32
LongInt (token_iter* Iter, s32* TypeIndexOut, type_table TypeTable)
{
b32 Result = false;
PushSnapshot(Iter);
2019-10-30 14:28:02 +00:00
if (TokenAtEquals(Iter, "unsigned") ||
TokenAtEquals(Iter, "signed"))
2019-10-30 14:28:02 +00:00
{
Result = true;
2019-10-30 14:28:02 +00:00
}
if (TokenAtEquals(Iter, "long"))
2019-10-30 14:28:02 +00:00
{
Result = true;
if (TokenAtEquals(Iter, "int"))
{
Result = true;
}
2019-10-30 14:28:02 +00:00
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
if (Result)
{
*TypeIndexOut = GetIndexOfType(MakeStringLiteral("long int"), TypeTable);
}
return Result;
2019-10-30 14:28:02 +00:00
}
internal b32
LongLongInt (token_iter* Iter, s32* TypeIndexOut, type_table TypeTable)
{
b32 Result = false;
PushSnapshot(Iter);
if (TokenAtEquals(Iter, "unsigned") ||
TokenAtEquals(Iter, "signed"))
{
2019-10-30 14:28:02 +00:00
}
if (TokenAtEquals(Iter, "long"))
{
if (TokenAtEquals(Iter, "long"))
2019-10-30 14:28:02 +00:00
{
Result = true;
if (TokenAtEquals(Iter, "int"))
{
Result = true;
}
2019-10-30 14:28:02 +00:00
}
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
if (Result)
{
*TypeIndexOut = GetIndexOfType(MakeStringLiteral("long long int"), TypeTable);
}
return Result;
}
internal b32
ParseChar(token_iter* Iter, s32* TypeIndexOut, type_table TypeTable)
{
b32 Result = false;
PushSnapshot(Iter);
if (TokenAtEquals(Iter, "unsigned") ||
TokenAtEquals(Iter, "signed"))
{
2019-10-30 14:28:02 +00:00
}
if (TokenAtEquals(Iter, "char"))
{
Result = true;
}
2019-10-30 14:28:02 +00:00
ApplySnapshotIfNotParsedAndPop(Result, Iter);
if (Result)
{
*TypeIndexOut = GetIndexOfType(MakeStringLiteral("char"), TypeTable);
}
return Result;
}
internal b32
ParseBool(token_iter* Iter, s32* TypeIndexOut, type_table TypeTable)
{
b32 Result = false;
PushSnapshot(Iter);
if (TokenAtEquals(Iter, "bool"))
2019-10-30 14:28:02 +00:00
{
Result = true;
*TypeIndexOut = GetIndexOfType(MakeStringLiteral("bool"), TypeTable);
2019-10-30 14:28:02 +00:00
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
return Result;
}
internal b32
ParseFloat(token_iter* Iter, s32* TypeIndexOut, type_table TypeTable)
{
b32 Result = false;
PushSnapshot(Iter);
if (TokenAtEquals(Iter, "float"))
{
Result = true;
*TypeIndexOut= GetIndexOfType(MakeStringLiteral("float"), TypeTable);
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
return Result;
}
internal b32
ParseDouble(token_iter* Iter, s32* TypeIndexOut, type_table TypeTable)
{
b32 Result = false;
PushSnapshot(Iter);
if (TokenAtEquals(Iter, "double"))
{
Result = true;
*TypeIndexOut = GetIndexOfType(MakeStringLiteral("double"), TypeTable);
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
return Result;
}
// :UndeclaredType
// NOTE(Peter): If TypeIndexOut is -1, you need to call NextToken after this
// function to advance past the type identifier.
internal b32
ParseType(token_iter* Iter, type_table* TypeTable, s32* TypeIndexOut)
{
b32 Result = false;
*TypeIndexOut = -1;
PushSnapshot(Iter);
// TODO(Peter): Store signedness, and what makes up a type
if (ParseChar(Iter, TypeIndexOut, *TypeTable))
{
Result = true;
}
else if (StringsEqual(Iter->TokenAt->Text, MakeStringLiteral("wchar_t")))
{
NextToken(Iter);
Result = true;
*TypeIndexOut = GetIndexOfType(MakeStringLiteral("wchar_t"), *TypeTable);
}
else if (ParseBool(Iter, TypeIndexOut, *TypeTable))
{
NextToken(Iter);
Result = true;
}
else if (ShortInt(Iter, TypeIndexOut, *TypeTable))
{
Result = true;
}
else if (Int(Iter, TypeIndexOut, *TypeTable))
{
Result = true;
}
else if (LongInt(Iter, TypeIndexOut, *TypeTable))
{
Result = true;
}
else if (LongLongInt(Iter, TypeIndexOut, *TypeTable))
{
Result = true;
}
else if (ParseFloat(Iter, TypeIndexOut, *TypeTable))
{
Result = true;
}
else if (ParseDouble(Iter, TypeIndexOut, *TypeTable))
{
Result = true;
}
else if (StringsEqual(Iter->TokenAt->Text, MakeStringLiteral("void")))
{
NextToken(Iter);
Result = true;
*TypeIndexOut = GetIndexOfType(MakeStringLiteral("void"), *TypeTable);
}
else
{
*TypeIndexOut = GetIndexOfType(Iter->TokenAt->Text, *TypeTable);
if (*TypeIndexOut >= 0)
{
Result = true;
NextToken(Iter);
}
else if(Iter->TokenAt->Type == Token_Identifier)
{
Result = true;
// NOTE(Peter): In this case, we believe we are at a type identifier,
// however, it hasn't been declared yet. This is due to the fact that we
// tokenize files, then parse them, then import the files they include, and
// then begin tokenizing, parsing, etc for those files.
// In the case that we get an as-of-yet undeclared type, we leave it
// up to the calling site to determine what to do with that information
// :UndeclaredType
*TypeIndexOut = -1;
}
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
return Result;
}
internal b32
ParsePointer (token_iter* Iter)
{
b32 Result = false;
if (TokenAtEquals(Iter, "*"))
{
Result = true;
}
return Result;
}
internal b32
ParseStructMember(token_iter* Iter, gs_bucket<token*>* TagList, struct_member_decl* MemberDecl, type_table* TypeTable)
{
b32 Result = false;
PushSnapshot(Iter);
s32 TypeIndex = -1;
if (ParseType(Iter, TypeTable, &TypeIndex))
{
// :UndeclaredType
if (TypeIndex < 0)
{
TypeIndex = PushUndeclaredType(Iter->TokenAt->Text, TypeTable);
NextToken(Iter);
}
MemberDecl->Pointer = ParsePointer(Iter);
if (Iter->TokenAt->Type == Token_Identifier)
{
MemberDecl->TypeIndex = TypeIndex;
MemberDecl->Identifier = Iter->TokenAt->Text;
CopyMetaTagsAndClear(TagList, &MemberDecl->MetaTags);
NextToken(Iter);
// Array Notationg ie r32 x[2];
// NOTE(Peter): True initially because if there is no array notation, we
// are still ok to proceed
b32 ArrayParseSuccess = true;
if (TokenAtEquals(Iter, "["))
{
// NOTE(Peter): Once we get to this point, we have to complete the entire
// array notation before we have successfully parsed, hence setting
// ArrayParseSucces to false here.
ArrayParseSuccess = false;
if (Iter->TokenAt->Type == Token_Number)
{
parse_result ArrayCount = ParseUnsignedInt(StringExpand(Iter->TokenAt->Text));
MemberDecl->ArrayCount = ArrayCount.UnsignedIntValue;
NextToken(Iter);
if (TokenAtEquals(Iter, "]"))
{
ArrayParseSuccess = true;
}
}
}
// TODO(Peter): Handle comma separated members
// ie. r32 x, y, z;
if (TokenAtEquals(Iter, ";") && ArrayParseSuccess)
{
Result = true;
}
}
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
return Result;
}
internal b32
StructOrUnion(token_iter* Iter, type_definition_type* Type)
{
b32 Result = false;
if (TokenAtEquals(Iter, "struct"))
{
Result = true;
*Type = TypeDef_Struct;
}
else if (TokenAtEquals(Iter, "union"))
{
Result = true;
*Type = TypeDef_Union;
}
return Result;
}
internal b32
ParseStruct(token_iter* Iter, s32* StructTypeIndexOut, gs_bucket<token*>* TagList, type_table* TypeTable)
{
b32 Result = false;
*StructTypeIndexOut = -1;
PushSnapshot(Iter);
type_definition_type DeclType;
if (StructOrUnion(Iter, &DeclType))
{
string StructIdentifier = {};
if (Iter->TokenAt->Type == Token_Identifier)
{
StructIdentifier = Iter->TokenAt->Text;
NextToken(Iter);
}
if (TokenAtEquals(Iter, "{"))
{
type_definition StructDecl = {};
StructDecl.Identifier = StructIdentifier;
StructDecl.Type = DeclType;
CopyMetaTagsAndClear(TagList, &StructDecl.MetaTags);
while (!TokenAtEquals(Iter, "}"))
{
s32 MemberStructTypeIndex = {};
struct_member_decl MemberDecl = {};
if (ParseMetaTag(Iter, TagList))
{
}
else if (ParseStructMember(Iter, TagList, &MemberDecl, TypeTable))
{
StructDecl.Struct.MemberDecls.PushElementOnBucket(MemberDecl);
}
else if (ParseStruct(Iter, &MemberStructTypeIndex, TagList, TypeTable))
{
// NOTE(Peter): Pretty sure, since we just parsed the struct, that
// MemberStructTypeIndex should never be -1 (unknown type).
// Putting this Assert here for now, but remove if there's a valid
// reason that you might not be able to find a struct just parsed at
// this point.
Assert(MemberStructTypeIndex >= 0);
MemberDecl.TypeIndex = MemberStructTypeIndex;
StructDecl.Struct.MemberDecls.PushElementOnBucket(MemberDecl);
}
else
{
// TODO(Peter): Handle unions
NextToken(Iter);
}
}
if (TokenAtEquals(Iter, ";"))
{
Result = true;
*StructTypeIndexOut = TypeTable->Types.PushElementOnBucket(StructDecl);
}
}
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
return Result;
}
internal b32
ParseFunctionDeclaration (token_iter* Iter, token* Identifier, gs_bucket<token*>* TagList, type_table* TypeTable)
{
b32 Result = false;
PushSnapshot(Iter);
s32 TypeIndex = -1;
if (ParseType(Iter, TypeTable, &TypeIndex))
{
if (TypeIndex < 0) { NextToken(Iter); }
b32 IsPointer = ParsePointer(Iter);
if (Iter->TokenAt->Type == Token_Identifier)
{
*Identifier = *Iter->TokenAt;
NextToken(Iter);
if (TokenAtEquals(Iter, "("))
{
while(!TokenAtEquals(Iter, ")"))
{
// TODO(Peter): parse function params
NextToken(Iter);
}
if (TokenAtEquals(Iter, ";"))
{
Result = true;
}
}
}
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
if (!Result)
{
*Identifier = {0};
}
return Result;
}
internal b32
ParseTypedef(token_iter* Iter, gs_bucket<token*>* TagList, type_table* TypeTable)
{
b32 Result = false;
PushSnapshot(Iter);
if (TokenAtEquals(Iter, "typedef"))
{
token TypeToken = {0};
s32 TypeIndex = -1;
if (TokenAtEquals(Iter, "struct") &&
ParseStruct(Iter, &TypeIndex, TagList, TypeTable))
{
Result = true;
}
else if (ParseFunctionDeclaration(Iter, &TypeToken, TagList, TypeTable))
{
Result = true;
printf("New Function Type: %.*s\n", StringExpand(TypeToken.Text));
}
else if (ParseType(Iter, TypeTable, &TypeIndex))
{
if (TypeIndex < 0)
{
TypeIndex = PushUndeclaredType(Iter->TokenAt->Text, TypeTable);
NextToken(Iter);
}
b32 IsPointer = ParsePointer(Iter);
type_definition* BasisType = TypeTable->Types.GetElementAtIndex(TypeIndex);
type_definition NewType = {};
NewType.Size = BasisType->Size;
CopyMetaTagsAndClear(TagList, &NewType.MetaTags);
NewType.Type = BasisType->Type;
if (NewType.Type == TypeDef_Struct ||
NewType.Type == TypeDef_Union)
{
NewType.Struct = BasisType->Struct;
}
NewType.Pointer = BasisType->Pointer || IsPointer;
if (Iter->TokenAt->Type == Token_Identifier)
{
NewType.Identifier = Iter->TokenAt->Text;
NextToken(Iter);
printf("New Type: %.*s\n", StringExpand(NewType.Identifier));
Result = true;
s32 ExistingUndeclaredTypeIndex = GetIndexOfType(NewType.Identifier, *TypeTable);
if (ExistingUndeclaredTypeIndex < 0)
{
TypeTable->Types.PushElementOnBucket(NewType);
}
else
{
type_definition* ExistingTypeDef = TypeTable->Types.GetElementAtIndex(ExistingUndeclaredTypeIndex);
*ExistingTypeDef = NewType;
}
}
}
else
{
printf("unhandled typedef ");
while (!TokenAtEquals(Iter, ";"))
{
printf("%.*s ", StringExpand(Iter->TokenAt->Text));
NextToken(Iter);
}
printf("\n");
}
}
ApplySnapshotIfNotParsedAndPop(Result, Iter);
return Result;
}
internal void
PrintIndent (u32 Indent)
{
for (u32 i = 0; i < Indent; i++)
{
printf(" ");
}
}
internal void PrintStructDecl (type_definition* StructDecl, type_table TypeTable, u32 Indent);
internal void
PrintStructMember (struct_member_decl Member, type_table TypeTable, u32 Indent = 0)
{
type_definition* MemberTypeDef = TypeTable.Types.GetElementAtIndex(Member.TypeIndex);
if ((MemberTypeDef->Type == TypeDef_Struct || MemberTypeDef->Type == TypeDef_Union)
&& MemberTypeDef->Identifier.Length == 0)
{
PrintStructDecl(MemberTypeDef, TypeTable, Indent);
}
else
{
PrintIndent(Indent);
if (Member.TypeIndex == -1)
{
printf("???? ");
}
printf("%.*s ", StringExpand(MemberTypeDef->Identifier));
}
if (Member.Pointer)
{
printf("* ");
}
printf("%.*s", StringExpand(Member.Identifier));
if (Member.ArrayCount > 0)
{
printf("[%d]", Member.ArrayCount);
}
printf(";");
}
internal void
PrintStructDecl (type_definition* StructDecl, type_table TypeTable, u32 Indent = 0)
{
Assert(StructDecl->Type == TypeDef_Struct ||
StructDecl->Type == TypeDef_Union);
PrintIndent(Indent);
if (StructDecl->Type == TypeDef_Struct)
{
printf("struct ");
}
else if (StructDecl->Type == TypeDef_Union)
{
printf("union ");
}
else { InvalidCodePath; }
if (StructDecl->Identifier.Length > 0)
{
printf("%.*s ", StringExpand(StructDecl->Identifier));
}
printf("{\n");
for (u32 MemberIndex = 0; MemberIndex < StructDecl->Struct.MemberDecls.Used; MemberIndex++)
{
struct_member_decl* Member = StructDecl->Struct.MemberDecls.GetElementAtIndex(MemberIndex);
PrintStructMember(*Member, TypeTable, Indent + 1);
printf("\n");
}
PrintIndent(Indent);
printf("} ( size = %d ) ", StructDecl->Size);
2019-10-30 14:28:02 +00:00
}
// Step 1: Get All Tokens, for every file
// Step 2: Identify all preprocessor directives
// Step 3: Apply Preprocessor Directives && Generate Code
// Step 4: Write out new files
// Step 5: Compile
int main(int ArgCount, char** ArgV)
{
s64 TotalStart = GetWallClock();
2019-10-30 14:28:02 +00:00
if (ArgCount <= 1)
{
printf("Please supply at least one source directory to analyze.\n");
return 0;
}
memory_arena SourceFileArena = {};
gs_bucket<source_code_file> SourceFiles;
gs_bucket<token> Tokens;
2019-10-30 14:28:02 +00:00
string_builder NodeTypeBlock = {};
Write(MakeStringLiteral("enum node_type\n{\n"), &NodeTypeBlock);
2019-10-30 14:28:02 +00:00
string_builder NodeMembersBlock = {};
2019-10-30 14:28:02 +00:00
string_builder NodeSpecificationsBlock = {};
Write(MakeStringLiteral("node_specification NodeSpecifications[] = {\n"), &NodeSpecificationsBlock);
2019-10-30 14:28:02 +00:00
string_builder CallNodeProcBlock = {};
Write(MakeStringLiteral("internal void CallNodeProc(u32 SpecificationIndex, u8* Data, led* LEDs, s32 LEDsCount, r32 DeltaTime)\n{\n"), &CallNodeProcBlock);
Write(MakeStringLiteral("node_specification Spec = NodeSpecifications[SpecificationIndex];\n"), &CallNodeProcBlock);
Write(MakeStringLiteral("switch (Spec.Type)\n{\n"), &CallNodeProcBlock);
2019-11-12 04:34:56 +00:00
string CurrentWorkingDirectory = MakeString((char*)malloc(1024), 0, 1024);
2019-10-30 14:28:02 +00:00
if (ArgCount > 1)
2019-10-30 14:28:02 +00:00
{
string RootFile = MakeString(ArgV[1]);
AddFileToSource(RootFile, &SourceFiles);
s32 LastSlash = ReverseSearchForCharInSet(RootFile, "\\/");
Assert(LastSlash > 0);
2019-10-30 14:28:02 +00:00
string RootPath = Substring(RootFile, 0, LastSlash + 1);
CopyStringTo(RootPath, &CurrentWorkingDirectory);
2019-10-30 14:28:02 +00:00
}
// NOTE(Peter): this is a temporary list of GSMetaTags. It gets copied and cleared
// after use
gs_bucket<token*> TagList;
type_table TypeTable = {0};
PopulateTableWithDefaultCPPTypes(&TypeTable);
for (u32 i = 0; i < TypeTable.Types.Used; i++)
2019-10-30 14:28:02 +00:00
{
type_definition* TypeDefinition = TypeTable.Types.GetElementAtIndex(i);
printf("%.*s\n", StringExpand(TypeDefinition->Identifier));
2019-10-30 14:28:02 +00:00
}
s32 NodeProcCount = 0;
for (u32 SourceFileIdx = 0; SourceFileIdx < SourceFiles.Used; SourceFileIdx++)
2019-10-30 14:28:02 +00:00
{
source_code_file* File = SourceFiles.GetElementAtIndex(SourceFileIdx);
TokenizeFile(File, &Tokens);
2019-10-30 14:28:02 +00:00
token_iter Iter = {};
Iter.Tokens = &Tokens;
Iter.FirstToken = File->FirstTokenIndex;
Iter.LastToken = File->LastTokenIndex;
Iter.TokenAtIndex = Iter.FirstToken;
Iter.TokenAt = Tokens.GetElementAtIndex(Iter.TokenAtIndex);
2019-10-30 14:28:02 +00:00
while (Iter.TokenAtIndex < Iter.LastToken)
{
b32 ParseSuccess = false;
2019-10-30 14:28:02 +00:00
s32 TypeIndex = -1;
if (TokenAtEquals(&Iter, "#include"))
2019-10-30 14:28:02 +00:00
{
token* IncludeFile = Iter.TokenAt;
2019-10-30 14:28:02 +00:00
// NOTE(Peter): For now we aren't going in and preprocessing the header files
// we include from the system
// Token_Operator is used to check if the include is of the form '#include <header.h>'
// and skip it.
// TODO(Peter): This is only a rough approximation of ignoring system headers
// TODO(Peter): We should actually see what parsing system headers would entail
if (IncludeFile->Type != Token_Operator)
{
string TempFilePath = IncludeFile->Text;
// NOTE(Peter): if the path is NOT absolute ie "C:\etc
if (!(IsAlpha(TempFilePath.Memory[0]) &&
TempFilePath.Memory[1] == ':' &&
TempFilePath.Memory[2] == '\\'))
{
TempFilePath = CurrentWorkingDirectory;
ConcatString(IncludeFile->Text, &TempFilePath);
NullTerminate(&TempFilePath);
}
ParseSuccess = true;
if (!FileAlreadyInSource(TempFilePath, SourceFiles))
{
AddFileToSource(TempFilePath, &SourceFiles);
}
}
2019-10-30 14:28:02 +00:00
}
else if(ParseMetaTag(&Iter, &TagList))
2019-10-30 14:28:02 +00:00
{
ParseSuccess = true;
2019-10-30 14:28:02 +00:00
}
else if (ParseStruct(&Iter, &TypeIndex, &TagList, &TypeTable))
{
ParseSuccess = true;
2019-10-30 14:28:02 +00:00
}
else if (ParseTypedef(&Iter, &TagList, &TypeTable))
2019-10-30 14:28:02 +00:00
{
ParseSuccess = true;
}
if (!ParseSuccess)
{
NextToken(&Iter);
2019-10-30 14:28:02 +00:00
}
}
}
// Type Table Fixup
for (u32 i = 0; i < TypeTable.Types.Used; i++)
2019-10-30 14:28:02 +00:00
{
type_definition* TypeDef = TypeTable.Types.GetElementAtIndex(i);
if (TypeDef->Type == TypeDef_Struct)
{
FixUpStructSize(TypeDef, TypeTable);
}
else if (TypeDef->Type == TypeDef_Union)
{
FixUpUnionSize(TypeDef, TypeTable);
}
2019-10-30 14:28:02 +00:00
}
for (u32 i = 0; i < TypeTable.Types.Used; i++)
2019-10-30 14:28:02 +00:00
{
type_definition* TypeDef = TypeTable.Types.GetElementAtIndex(i);
if ((TypeDef->Type == TypeDef_Struct || TypeDef->Type == TypeDef_Union) && TypeDef->Identifier.Length > 0)
{
PrintStructDecl(TypeDef, TypeTable);
printf("\n\n");
}
2019-10-30 14:28:02 +00:00
}
s64 Cycles_Preprocess = GetWallClock();
2019-10-30 14:28:02 +00:00
MakeStringBuffer(Buffer, 256);
// Close Types Block - overwrite the last comma and '\' newline character with newlines.
Write(MakeStringLiteral("NodeType_Count,\n};\n\n"), &NodeTypeBlock);
2019-10-30 14:28:02 +00:00
// Close Specifications Block
Write(MakeStringLiteral("};\n"), &NodeSpecificationsBlock);
2019-10-30 14:28:02 +00:00
PrintF(&Buffer, "s32 NodeSpecificationsCount = %d;\n\n", NodeProcCount);
Write(Buffer, &NodeSpecificationsBlock);
2019-10-30 14:28:02 +00:00
// Close Call Node Proc Block
Write(MakeStringLiteral("}\n}\n"), &CallNodeProcBlock);
2019-10-30 14:28:02 +00:00
FILE* NodeGeneratedCPP = fopen("C:\\projects\\foldhaus\\src\\generated\\foldhaus_nodes_generated.cpp", "w");
if (NodeGeneratedCPP)
{
WriteStringBuilderToFile(NodeTypeBlock, NodeGeneratedCPP);
WriteStringBuilderToFile(NodeMembersBlock, NodeGeneratedCPP);
WriteStringBuilderToFile(NodeSpecificationsBlock, NodeGeneratedCPP);
WriteStringBuilderToFile(CallNodeProcBlock, NodeGeneratedCPP);
2019-10-30 14:28:02 +00:00
fclose(NodeGeneratedCPP);
}
PrintErrorList(GlobalErrorList);
s64 TotalEnd = GetWallClock();
r32 TotalTime = GetSecondsElapsed(TotalStart, TotalEnd);
printf("Metaprogram Preproc Time: %.*f sec\n", 6, TotalTime);
#if 0
for (u32 i = 0; i < Structs.Used; i++)
{
seen_node_struct* Struct = Structs.GetElementAtIndex(i);
#ifdef PRINT_ALL_INFO
printf("\n");
for (u32 j = 0; j < Struct->MetaTags.Used; j++)
{
token* MetaTag = Struct->MetaTags.GetElementAtIndex(j);
printf("GSMetaTag(%.*s)\n", StringExpand(MetaTag->Text));
}
#endif
printf("struct %.*s\n", StringExpand(Struct->Name));
#ifdef PRINT_ALL_INFO
for (u32 j = 0; j < Struct->MemberDecls.Used; j++)
{
struct_member_decl* Member = Struct->MemberDecls.GetElementAtIndex(j);
for (u32 k = 0; k < Member->MetaTags.Used; k++)
{
token* MetaTag = Member->MetaTags.GetElementAtIndex(k);
printf(" GSMetaTag(%.*s)\n", StringExpand(MetaTag->Text));
}
printf(" %.*s %.*s\n", StringExpand(Member->Type), StringExpand(Member->Identifier));
}
#endif
}
#endif
__debugbreak();
2019-10-30 14:28:02 +00:00
return 0;
}