Add support for funciton default param initialiser

This commit is contained in:
Doyle 2019-03-23 16:08:12 +11:00
parent 2f750ecc52
commit b61ad4a51a
3 changed files with 187 additions and 165 deletions

View File

@ -265,6 +265,10 @@ struct DqnInspect_Struct
int members_len;
};
// NOTE(doyle): For compiler testing
// #include "../Data/DqnInspect_TestData.h"
// #include "../Data/DqnInspect_TestDataGenerated.cpp"
#ifdef DQN_INSPECT_EXECUTABLE_IMPLEMENTATION
#define _CRT_SECURE_NO_WARNINGS
@ -1288,9 +1292,41 @@ void ParseCPPStruct(CPPTokeniser *tokeniser)
void SkipFunctionParam(CPPTokeniser *tokeniser)
{
CPPToken token = CPPTokeniser_PeekToken(tokeniser);
while (token.type != CPPTokenType::EndOfStream && token.type != CPPTokenType::Comma &&
token.type != CPPTokenType::CloseParen)
while (token.type != CPPTokenType::EndOfStream)
{
if (token.type == CPPTokenType::OpenParen)
{
for (int level = 1; level != 0;)
{
CPPTokeniser_NextToken(tokeniser);
token = CPPTokeniser_PeekToken(tokeniser);
if (token.type == CPPTokenType::OpenParen) level++;
if (token.type == CPPTokenType::CloseParen) level--;
}
CPPTokeniser_NextToken(tokeniser);
token = CPPTokeniser_PeekToken(tokeniser);
}
if (token.type == CPPTokenType::LeftBrace)
{
for (int level = 1; level != 0;)
{
CPPTokeniser_NextToken(tokeniser);
token = CPPTokeniser_PeekToken(tokeniser);
if (token.type == CPPTokenType::LeftBrace) level++;
if (token.type == CPPTokenType::RightBrace) level--;
}
CPPTokeniser_NextToken(tokeniser);
token = CPPTokeniser_PeekToken(tokeniser);
}
if (token.type == CPPTokenType::CloseParen)
break;
if (token.type == CPPTokenType::Comma)
break;
CPPTokeniser_NextToken(tokeniser);
token = CPPTokeniser_PeekToken(tokeniser);
}
@ -1312,9 +1348,8 @@ void ParseCPPInspectPrototype(CPPTokeniser *tokeniser)
MemArenaScopedRegion mem_region = MemArena_MakeScopedRegion(&global_main_arena);
{
LinkedList<FunctionDefaultParam> *link_iterator = nullptr;
token = CPPTokeniser_PeekToken(tokeniser);
if (!ExpectToken(token, CPPTokenType::OpenParen)) return;
token = CPPTokeniser_NextToken(tokeniser);
if (!CPPTokeniser_AcceptTokenIfType(tokeniser, CPPTokenType::OpenParen, &token))
return;
for (token = CPPTokeniser_NextToken(tokeniser);
token.type != CPPTokenType::CloseParen && token.type != CPPTokenType::EndOfStream;
@ -1337,20 +1372,18 @@ void ParseCPPInspectPrototype(CPPTokeniser *tokeniser)
continue;
}
token = CPPTokeniser_NextToken(tokeniser);
char *default_param_start = token.str;
token = CPPTokeniser_PeekToken(tokeniser);
char *default_value_start = token.str;
// NOTE(doyle): Include the quotes in the param value
CPPToken prev_token = CPPTokeniser_PrevToken(tokeniser);
if (prev_token.type == CPPTokenType::String)
default_param_start--;
if (token.type == CPPTokenType::String) // NOTE(doyle): Include the quotes in the param value
default_value_start--;
SkipFunctionParam(tokeniser);
CPPToken peek_token = CPPTokeniser_PeekToken(tokeniser);
if (peek_token.type != CPPTokenType::Comma && peek_token.type != CPPTokenType::CloseParen)
continue;
char *default_param_end = peek_token.str;
char *default_value_end = peek_token.str;
auto *link = MEM_ARENA_ALLOC_STRUCT(&global_main_arena, LinkedList<FunctionDefaultParam>);
*link = {};
@ -1359,7 +1392,7 @@ void ParseCPPInspectPrototype(CPPTokeniser *tokeniser)
link_iterator = link;
link->value.name = default_param_name;
link->value.value = StringLiteral(default_param_start, static_cast<int>(default_param_end - default_param_start));
link->value.value = StringLiteral(default_value_start, static_cast<int>(default_value_end - default_value_start));
}
}
@ -1483,11 +1516,16 @@ enum struct InspectMode
char *EnumOrStructOrFunctionLexer(CPPTokeniser *tokeniser, char *ptr, b32 lexing_function)
{
// NOTE(doyle): If we're lexing a function we're doing DQN_INSPECT_GENERATE_PROTOTYPE()
// Where we want to lex the macro and then the function following it, so 2 iterations
int iterations = lexing_function ? 2 : 1;
for (int i = 0; i < iterations; ++i)
{
int indent_level = 0;
bool started_lexing_brace_scope = false;
bool started_lexing_function = false;
int open_paren_level = 0;
int close_paren_level = 0;
int paren_level = 0;
for (; ptr;)
{
while (CharIsWhitespace(ptr[0])) ptr++;
@ -1502,8 +1540,8 @@ char *EnumOrStructOrFunctionLexer(CPPTokeniser *tokeniser, char *ptr, b32 lexing
case '}': { token->type = CPPTokenType::RightBrace; indent_level--; } break;
case '[': { token->type = CPPTokenType::LeftSqBracket; } break;
case ']': { token->type = CPPTokenType::RightSqBracket; } break;
case '(': { token->type = CPPTokenType::OpenParen; started_lexing_function = true; open_paren_level++; } break;
case ')': { token->type = CPPTokenType::CloseParen; close_paren_level++; } break;
case '(': { token->type = CPPTokenType::OpenParen; started_lexing_function = true; paren_level++; } break;
case ')': { token->type = CPPTokenType::CloseParen; paren_level--; } break;
case ',': { token->type = CPPTokenType::Comma; } break;
case ';': { token->type = CPPTokenType::SemiColon; } break;
case '=': { token->type = CPPTokenType::Equals; } break;
@ -1591,13 +1629,14 @@ char *EnumOrStructOrFunctionLexer(CPPTokeniser *tokeniser, char *ptr, b32 lexing
{
if (lexing_function)
{
if (started_lexing_function && open_paren_level == close_paren_level && open_paren_level == 2)
return ptr;
if (started_lexing_function && paren_level == 0)
break;
}
else
{
if (started_lexing_brace_scope && indent_level == 0)
return ptr;
break;
}
}
}
}
@ -1683,8 +1722,6 @@ int main(int argc, char *argv[])
file_include_contents_hash_define_len = extracted_file_name_len;
}
if (mode == InspectMode::Code)
{
fprintf(output_file,
"//\n"
"// %s\n"
@ -1704,24 +1741,6 @@ int main(int argc, char *argv[])
file_include_contents_hash_define_len,
file_include_contents_hash_define
);
}
else
{
fprintf(output_file,
"//\n"
"// %s\n"
"//\n"
"\n"
"#ifndef DQN_INSPECT_%.*s\n"
"#define DQN_INSPECT_%.*s\n"
"\n",
file_name,
file_include_contents_hash_define_len,
file_include_contents_hash_define,
file_include_contents_hash_define_len,
file_include_contents_hash_define
);
}
CPPTokeniser tokeniser = {};
tokeniser.spaces_per_indent = 4;
@ -1809,8 +1828,6 @@ int main(int argc, char *argv[])
break;
}
if (mode == InspectMode::Code)
{
fprintf(output_file,
"\n#undef ARRAY_COUNT\n"
"#undef CHAR_COUNT\n"
@ -1819,14 +1836,6 @@ int main(int argc, char *argv[])
file_include_contents_hash_define_len,
file_include_contents_hash_define);
}
else
{
fprintf(output_file,
"\n#endif // DQN_INSPECT_%.*s\n\n",
file_include_contents_hash_define_len,
file_include_contents_hash_define);
}
}
fclose(output_file);

View File

@ -4,12 +4,14 @@ DQN_INSPECT enum struct EnumWithMetadata
Count,
};
struct V3 { float test; };
struct V3 { V3(float a, float b) { (void)a; (void)b; } float test; };
struct V4 { float test; };
template <typename T, int Size>
struct Array { T data[Size]; };
enum struct OpenGLShader { Vertex, Count, };
DQN_INSPECT struct SampleStruct
{
// #if 0
@ -34,10 +36,13 @@ DQN_INSPECT struct SampleStruct
};
DQN_INSPECT_GENERATE_PROTOTYPE(b = {}, c = nullptr, e = false, f = 1, g = "Hello world")
void Function1(int a, float b, char const *c, bool e, int f, char *g) { }
void Function1(int a, float b, char const *c, bool e, int f, char *g)
{
(void)a; (void)b; (void)c; (void)e; (void)f; (void)g;
}
DQN_INSPECT_GENERATE_PROTOTYPE(foo = V3(10, 20), bar = {120, 150})
void *Function2(V3 foo, V3 bar) { (void)foo; (void)bar; return nullptr; }
DQN_INSPECT_GENERATE_PROTOTYPE()
void *Function2() { }
DQN_INSPECT_GENERATE_PROTOTYPE()
Array<int const *, 3> const *const Function3(Array<int, 32> const *foobar) { }
Array<int const *, 3> const *const Function3(Array<int, 32> const *foobar) { (void)foobar; return {}; }

View File

@ -7,6 +7,11 @@
#ifndef DQN_INSPECT_DQNINSPECT_TESTDATA_H
#define DQN_INSPECT_DQNINSPECT_TESTDATA_H
// NOTE: These macros are undefined at the end of the file so to not pollute namespace
#define ARRAY_COUNT(array) sizeof(array)/sizeof((array)[0])
#define CHAR_COUNT(str) (ARRAY_COUNT(str) - 1)
#define STR_AND_LEN(str) str, CHAR_COUNT(str)
char const *DqnInspect_EnumWithMetadata_Strings[] = {"Rect", "Count", };
char const *DqnInspect_EnumString(EnumWithMetadata val)
@ -225,8 +230,11 @@ DqnInspect_Struct const *DqnInspect_GetStruct(SampleStruct const *val)
}
void Function1(int a, float b = {}, char const *c = nullptr, bool e = false, int f = 1, char *g = "Hello world");
void *Function2();
void *Function2(V3 foo = V3(10, 20), V3 bar = {120, 150});
Array<int const *, 3> const *const Function3(Array<int, 32> const *foobar);
#undef ARRAY_COUNT
#undef CHAR_COUNT
#undef STR_AND_LEN
#endif // DQN_INSPECT_DQNINSPECT_TESTDATA_H