[examples] hello_world.c first pass; parse check touch ups; API touch ups

This commit is contained in:
Allen Webster
2021-09-16 20:06:20 -07:00
parent e11ddda2fb
commit 883c384e38
5 changed files with 211 additions and 156 deletions
+37
View File
@@ -0,0 +1,37 @@
/*
** Example: hello world
**
**
**
*/
//~ Includes and globals //////////////////////////////////////////////////////
// @notes Metadesk is a source-include library. So we include the
// file "md.c" into the usage code directly. The library
#include "md.h"
#include "md.c"
// @notes For simple single-threaded memory management in a run-once-and-exit
// utility, a single global arena is our prefered setup.
static MD_Arena *arena = 0;
//~ main //////////////////////////////////////////////////////////////////////
int
main(int argc, char **argv){
// setup the global arena
// @notes Metadesk arenas do linear reserve-and-commit allocation. This
// code makes an arena with a 1 terabyte reserve which works so long as
// we're only doing one or a few arenas.
arena = MD_ArenaAlloc(1ull << 40);
// parse a string
MD_String8 name = MD_S8Lit("<name>");
MD_String8 hello_world = MD_S8Lit("hello world");
MD_ParseResult parse = MD_ParseWholeString(arena, hello_world_name, hello_world);
// print the results
MD_PrintDebugDumpFromNode(stdout, parse.node, MD_GenerateFlags_All);
}
+6 -11
View File
@@ -9,33 +9,28 @@
//~ Includes and globals //////////////////////////////////////////////////////
// @notes Print helpers will make it easier to print the errors. This brings in
// C's stdio.h. It is possible to use error messages without print helpers,
// but a dependency on stdio.h is fine here so we will use it.
#define MD_ENABLE_PRINT_HELPERS 1
#include "md.h"
#include "md.c"
// @notes For simple single-threaded memory management in a run-once-and-exit
// utility, a single global arena is our prefered setup.
// utility, a single global arena is our recommended approach.
static MD_Arena *arena = 0;
//~ main //////////////////////////////////////////////////////////////////////
int main(int argument_count, char **arguments)
int main(int argc, char **argv)
{
// setup the global arena
// @notes Metadesk arenas do linear reserve-and-commit allocation. This
// code makes an arena with a 1 terabyte reserve which works quite nicely,
// so long as we aren't doing more than a few of them.
// code makes an arena with a 1 terabyte reserve which works so long as
// we're only doing one or a few arenas.
arena = MD_ArenaAlloc(1ull << 40);
// parse all files passed to the command line
MD_Node *list = MD_MakeList(arena);
for (int i = 1; i < argument_count; i += 1)
for (int i = 1; i < argc; i += 1)
{
// parse the file
@@ -43,7 +38,7 @@ int main(int argument_count, char **arguments)
// and then does the whole parse. In a simple utility program like
// this metadesk's default implementations for the overrides make
// this work.
MD_String8 file_name = MD_S8CString(arguments[i]);
MD_String8 file_name = MD_S8CString(argv[i]);
MD_ParseResult parse_result = MD_ParseWholeFile(arena, file_name);
// print metadesk errors
+153 -133
View File
File diff suppressed because it is too large Load Diff
+9 -6
View File
@@ -77,8 +77,8 @@
# define MD_DEFAULT_SCRATCH 1
#endif
#if !defined(MD_ENABLE_PRINT_HELPERS)
# define MD_ENABLE_PRINT_HELPERS 0
#if !defined(MD_DISABLE_PRINT_HELPERS)
# define MD_DISABLE_PRINT_HELPERS 0
#endif
@@ -316,9 +316,8 @@
//~ Linkage Wrappers
#if !defined(MD_FUNCTION)
# define MD_FUNCTION
# define MD_FUNCTION static
#endif
#define MD_FUNCTION_IMPL MD_FUNCTION
#if !defined(MD_GLOBAL)
# define MD_GLOBAL static
@@ -1048,7 +1047,7 @@ MD_FUNCTION void MD_MessageListPush(MD_MessageList *list, MD_Message *
MD_FUNCTION void MD_MessageListConcat(MD_MessageList *list, MD_MessageList *to_push);
MD_FUNCTION MD_ParseResult MD_ParseResultZero(void);
MD_FUNCTION MD_ParseResult MD_ParseOneNode(MD_Arena *arena, MD_String8 string, MD_u64 offset);
MD_FUNCTION MD_ParseResult MD_ParseWholeString(MD_Arena *arena, MD_String8 filename, MD_String8 contents);
MD_FUNCTION MD_ParseResult MD_ParseWholeString(MD_Arena *arena, MD_String8 name, MD_String8 contents);
MD_FUNCTION MD_ParseResult MD_ParseWholeFile(MD_Arena *arena, MD_String8 filename);
@@ -1106,7 +1105,7 @@ MD_FUNCTION MD_String8 MD_StringFromMessageKind(MD_MessageKind kind);
MD_FUNCTION MD_String8 MD_FormatMessage(MD_Arena *arena, MD_CodeLoc loc, MD_MessageKind kind,
MD_String8 string);
#if MD_ENABLE_PRINT_HELPERS
#if !MD_DISABLE_PRINT_HELPERS
#include <stdio.h>
MD_FUNCTION void MD_PrintMessage(FILE *file, MD_CodeLoc loc, MD_MessageKind kind,
MD_String8 string);
@@ -1140,6 +1139,10 @@ MD_FUNCTION void MD_ReconstructionFromNode(MD_Arena *arena, MD_String8List *out,
int indent, MD_String8 indent_string,
MD_GenerateFlags flags);
#if !MD_DISABLE_PRINT_HELPERS
MD_FUNCTION void MD_PrintDebugDumpFromNode(FILE *file, MD_Node *node, MD_GenerateFlags flags);
#endif
//~ Command Line Argument Helper
MD_FUNCTION MD_String8List MD_StringListFromArgCV(MD_Arena *arena, int argument_count,
+6 -6
View File
@@ -785,8 +785,8 @@ int main(void)
MD_String8 expected = MD_S8Lit("@foo\n@bar\n@baz\na:\n{\n b,\n c,\n d,\n e,\n f,\n}");
MD_ParseResult parse = MD_ParseOneNode(arena, code, 0);
MD_String8List actual_strings = {0};
MD_DebugStringListFromNode(arena, &actual_strings, parse.node,
0, MD_S8Lit(" "), MD_GenerateFlags_Tree);
MD_DebugDumpFromNode(arena, &actual_strings, parse.node,
0, MD_S8Lit(" "), MD_GenerateFlags_Tree);
MD_String8 actual = MD_S8ListJoin(arena, actual_strings, 0);
TestResult(MD_S8Match(expected, actual, 0));
}
@@ -795,8 +795,8 @@ int main(void)
MD_String8 expected = MD_S8Lit("@foo(1,\n 2,\n 3)\na:\n{\n x,\n y,\n}");
MD_ParseResult parse = MD_ParseOneNode(arena, code, 0);
MD_String8List actual_strings = {0};
MD_DebugStringListFromNode(arena, &actual_strings, parse.node,
0, MD_S8Lit(" "), MD_GenerateFlags_Tree);
MD_DebugDumpFromNode(arena, &actual_strings, parse.node,
0, MD_S8Lit(" "), MD_GenerateFlags_Tree);
MD_String8 actual = MD_S8ListJoin(arena, actual_strings, 0);
TestResult(MD_S8Match(expected, actual, 0));
}
@@ -805,8 +805,8 @@ int main(void)
MD_String8 expected = MD_S8Lit("/*\n foo\n*/\na");
MD_ParseResult parse = MD_ParseOneNode(arena, code, 0);
MD_String8List actual_strings = {0};
MD_DebugStringListFromNode(arena, &actual_strings, parse.node, 0, MD_S8Lit(" "),
MD_GenerateFlags_Tree|MD_GenerateFlag_Comments);
MD_DebugDumpFromNode(arena, &actual_strings, parse.node, 0, MD_S8Lit(" "),
MD_GenerateFlags_Tree|MD_GenerateFlag_Comments);
MD_String8 actual = MD_S8ListJoin(arena, actual_strings, 0);
TestResult(MD_S8Match(expected, actual, 0));
}