This adds the ability to list keywords, operators etc. implementing #350.

This commit is contained in:
Christoffer Lerno
2022-01-19 13:11:12 +01:00
parent 827499ca80
commit 3450016978
5 changed files with 104 additions and 2 deletions

View File

@@ -104,7 +104,15 @@ static void usage(void)
OUTPUT("");
OUTPUT(" -z <argument> - Send the <argument> as a parameter to the linker.");
OUTPUT("");
OUTPUT(" -mavx - Enable AVX on x64 targets.");
OUTPUT(" -mavx512 - Enable AVX512 on x64 targets.");
OUTPUT(" -mno-avx - Disable AVX on x64 targets.");
OUTPUT("");
OUTPUT(" --debug-stats - Print debug statistics.");
OUTPUT(" --list-keywords - List all keywords.");
OUTPUT(" --list-operators - List all operators.");
OUTPUT(" --list-attributes - List all attributes.");
OUTPUT(" --list-builtins - List all builtins.");
#ifndef NDEBUG
OUTPUT(" --debug-log - Print debug logging to stdout.");
#endif
@@ -285,10 +293,18 @@ static void print_version(void)
OUTPUT("LLVM default target: %s", llvm_target);
}
static void parse_option(BuildOptions *options)
{
switch (current_arg[1])
{
case '?':
if (match_shortopt("?"))
{
usage();
exit_compiler(COMPILER_SUCCESS_EXIT);
}
break;
case 'V':
if (match_shortopt("V"))
{
@@ -467,6 +483,30 @@ static void parse_option(BuildOptions *options)
debug_stats = true;
return;
}
if (match_longopt("list-keywords"))
{
options->print_keywords = true;
options->command = COMMAND_PRINT_SYNTAX;
return;
}
if (match_longopt("list-attributes"))
{
options->print_attributes = true;
options->command = COMMAND_PRINT_SYNTAX;
return;
}
if (match_longopt("list-builtins"))
{
options->print_builtins = true;
options->command = COMMAND_PRINT_SYNTAX;
return;
}
if (match_longopt("list-operators"))
{
options->print_operators = true;
options->command = COMMAND_PRINT_SYNTAX;
return;
}
if (match_longopt("threads"))
{
if (at_end() || next_is_opt()) error_exit("error: --threads needs a valid integer 1 or higher.");
@@ -547,7 +587,8 @@ static void parse_option(BuildOptions *options)
}
if (match_longopt("help"))
{
break;
usage();
exit_compiler(COMPILER_SUCCESS_EXIT);
}
break;
default:
@@ -621,6 +662,10 @@ BuildOptions parse_arguments(int argc, const char *argv[])
}
if (build_options.command == COMMAND_MISSING)
{
if (build_options.print_operators || build_options.print_builtins || build_options.print_keywords || build_options.print_attributes)
{
return build_options;
}
FAIL_WITH_ERR("No command found.");
}
return build_options;

View File

@@ -35,6 +35,7 @@ typedef enum
COMMAND_DOCS,
COMMAND_BENCH,
COMMAND_UNIT_TEST,
COMMAND_PRINT_SYNTAX,
} CompilerCommand;
typedef enum
@@ -207,6 +208,10 @@ typedef struct BuildOptions_
bool no_avx;
bool avx;
bool avx512;
bool print_keywords;
bool print_attributes;
bool print_builtins;
bool print_operators;
} BuildOptions;

View File

@@ -403,6 +403,55 @@ static void setup_bool_define(const char *id, bool value)
}
}
void print_syntax(BuildOptions *options)
{
symtab_init(64 * 1024);
if (options->print_keywords)
{
int index = 1;
for (int i = 1; i < TOKEN_LAST; i++)
{
const char *name = token_type_to_string((TokenType)i);
if (name[0] == '$' || (name[0] >= 'a' && name[0] <= 'z'))
{
if (name[1] == '$' || name[1] == '\0') continue;
printf("%2d %s\n", index++, name);
}
}
}
if (options->print_operators)
{
int index = 1;
for (int i = 1; i < TOKEN_LAST; i++)
{
if (i == TOKEN_DOCS_START || i == TOKEN_DOCS_END) continue;
const char *name = token_type_to_string((TokenType)i);
char first_char = name[0];
if (first_char == '$' || first_char == '@'
|| (first_char >= 'a' && first_char <= 'z')
|| (first_char >= 'A' && first_char <= 'Z'))
{
continue;
}
printf("%2d %s\n", index++, name);
}
}
if (options->print_attributes)
{
for (int i = 0; i < NUMBER_OF_ATTRIBUTES; i++)
{
printf("%2d @%s\n", i + 1, attribute_list[i]);
}
}
if (options->print_builtins)
{
for (int i = 0; i < NUMBER_OF_BUILTINS; i++)
{
printf("%2d $$%s\n", i + 1, builtin_list[i]);
}
}
}
void compile()
{
active_target.sources = target_expand_source_names(active_target.source_dirs, ".c3", ".c3t", true);

View File

@@ -14,4 +14,4 @@ void init_build_target(BuildTarget *build_target, BuildOptions *build_options);
void init_default_build_target(BuildTarget *target, BuildOptions *options);
void symtab_init(uint32_t max_size);
void symtab_destroy();
void print_syntax(BuildOptions *options);

View File

@@ -43,6 +43,9 @@ int main_real(int argc, const char *argv[])
switch (build_options.command)
{
case COMMAND_PRINT_SYNTAX:
print_syntax(&build_options);
break;
case COMMAND_INIT:
create_project(&build_options);
break;