From 838283c70654d5eb31a6a488e2719888f784fb83 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Sat, 9 Oct 2021 20:14:07 -0700 Subject: [PATCH] [examples] start setting up expression parser intro --- examples/expr/expr_intro.c | 92 ++++++++++++++++++++++++++++++++++ examples/expr/expr_intro.mdesk | 14 ++++++ source/md.h | 6 ++- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 examples/expr/expr_intro.c create mode 100644 examples/expr/expr_intro.mdesk diff --git a/examples/expr/expr_intro.c b/examples/expr/expr_intro.c new file mode 100644 index 0000000..70b33af --- /dev/null +++ b/examples/expr/expr_intro.c @@ -0,0 +1,92 @@ +/* +** Example: expressions intro +** +** TODO +** +*/ + +//~ includes and globals ////////////////////////////////////////////////////// + +#include "md.h" +#include "md.c" + +static MD_Arena *arena = 0; + +//~ expression setup and helpers ////////////////////////////////////////////// + +void +print_expression(FILE *out, MD_Expr *expr) +{ + // TODO(allen): finish +} + + +//~ main ////////////////////////////////////////////////////////////////////// + +int main(int argc, char **argv) +{ + // setup the global arena + arena = MD_ArenaAlloc(); + + // parse all files passed to the command line + MD_Node *list = MD_MakeList(arena); + for (int i = 1; i < argc; i += 1) + { + + // parse the file + MD_String8 file_name = MD_S8CString(argv[i]); + MD_ParseResult parse_result = MD_ParseWholeFile(arena, file_name); + + // print metadesk errors + for (MD_Message *message = parse_result.errors.first; + message != 0; + message = message->next) + { + MD_CodeLoc code_loc = MD_CodeLocFromNode(message->node); + MD_PrintMessage(stdout, code_loc, message->kind, message->string); + } + + // save to parse results list + if (parse_result.errors.max_message_kind < MD_MessageKind_Error) + { + MD_PushNewReference(arena, list, parse_result.node); + } + } + + // setup the expression system + MD_ExprOprTable table = {0}; + { + MD_ExprOprList list = {0}; + + MD_ExprOprPush(arena, &list, MD_ExprOprKind_Binary, 1, MD_S8Lit("+"), OpAdd, 0); + MD_ExprOprPush(arena, &list, MD_ExprOprKind_Binary, 2, MD_S8Lit("*"), OpMul, 0); + + table = MD_ExprBakeOperatorTableFromList(arena, &list); + } + + // print the verbose parse results + for (MD_EachNode(root_it, list->first_child)) + { + MD_Node *root = MD_ResolveNodeFromReference(root_it); + for (MD_EachNode(node, root->first_child)) + { + MD_ExprParseResult parse = MD_ExprParse(arena, &table, node->first_child, MD_NilNode()); + + // print errors + for (MD_Message *message = parse.errors.first; + message != 0; + message = message->next) + { + MD_CodeLoc code_loc = MD_CodeLocFromNode(message->node); + MD_PrintMessage(stdout, code_loc, message->kind, message->string); + } + + // print the expression + fprintf(stdout, "%s = "); + print_expression(stdout, parse.expr); + fprintf(stdout, ";\n"); + } + } + + return 0; +} diff --git a/examples/expr/expr_intro.mdesk b/examples/expr/expr_intro.mdesk new file mode 100644 index 0000000..90cc1fc --- /dev/null +++ b/examples/expr/expr_intro.mdesk @@ -0,0 +1,14 @@ +/* +** Example: expressions intro +** +** TODO +*/ + +a: 1; +b: 2; +c: 3; + +w: 100; +x: a*w + b; +y: b*w + c; +z: c*w + a; diff --git a/source/md.h b/source/md.h index 970eb32..5c839a6 100644 --- a/source/md.h +++ b/source/md.h @@ -727,6 +727,7 @@ typedef struct MD_MessageList MD_MessageList; struct MD_MessageList { MD_MessageKind max_message_kind; + // TODO(allen): rename MD_u64 node_count; MD_Message *first; MD_Message *last; @@ -780,7 +781,8 @@ struct MD_ExprOprList typedef struct MD_ExprOprTable MD_ExprOprTable; struct MD_ExprOprTable { - MD_ExprOprList table[MD_ExprOprKind_COUNT]; // TODO(mal): Hash? + // TODO(mal): @upgrade_potential Hash? + MD_ExprOprList table[MD_ExprOprKind_COUNT]; MD_MessageList errors; }; @@ -791,6 +793,7 @@ struct MD_Expr struct MD_Expr *left; struct MD_Expr *right; MD_b32 is_op; + // TODO(allen): this should be MD_ExprOpr* MD_u32 op_id; void *op_ptr; MD_Node *md_node; @@ -818,6 +821,7 @@ struct MD_ExprParseCtx } accel; #undef MD_POSTFIX_SETLIKE_OP_COUNT + // TODO(allen): different message list type here MD_MessageList errors; };