diff --git a/bin/build_examples.sh b/bin/build_examples.sh index 92eee3c..414f54a 100755 --- a/bin/build_examples.sh +++ b/bin/build_examples.sh @@ -11,6 +11,10 @@ bin/bld_core.sh show_ctx examps="examples" +bin/bld_core.sh unit expr_intro $examps/expr/expr_intro.c + +exit + bin/bld_core.sh unit hello_world $examps/intro/hello_world.c bin/bld_core.sh unit parse_check $examps/intro/parse_check.c bin/bld_core.sh unit data_desk_like $examps/intro/data_desk_like_template.c diff --git a/bin/run_examples.sh b/bin/run_examples.sh index 331a8c3..8756617 100755 --- a/bin/run_examples.sh +++ b/bin/run_examples.sh @@ -8,6 +8,12 @@ root_path=$PWD build_path=$root_path/build examps=$root_path/examples +echo ~~~ Running Expression Intro ~~~ +$build_path/expr_intro.exe $examps/expr/expr_intro.mdesk +echo + +exit + echo ~~~ Running Type Metadata Generator Example ~~~ cd $examps/type_metadata/generated $build_path/type_metadata.exe $examps/type_metadata/types.mdesk diff --git a/examples/expr/expr_intro.c b/examples/expr/expr_intro.c index 70b33af..66966e7 100644 --- a/examples/expr/expr_intro.c +++ b/examples/expr/expr_intro.c @@ -14,10 +14,39 @@ static MD_Arena *arena = 0; //~ expression setup and helpers ////////////////////////////////////////////// +enum +{ + OpAdd, + OpMul, +}; + void print_expression(FILE *out, MD_Expr *expr) { - // TODO(allen): finish + MD_ExprOpr *op = expr->op; + if (op == 0) + { + MD_Node *node = expr->md_node; + if (node->raw_string.size != 0 && + MD_NodeIsNil(node->first_child)) + { + fprintf(out, "%.*s", MD_S8VArg(node->raw_string)); + } + else + { + MD_CodeLoc loc = MD_CodeLocFromNode(node); + MD_PrintMessage(stderr, loc, MD_MessageKind_Error, + MD_S8Lit("the expression system does not expect this kind of node")); + } + } + else + { + fprintf(out, "("); + print_expression(out, expr->left); + fprintf(out, " %.*s ", MD_S8VArg(op->string)); + print_expression(out, expr->right); + fprintf(out, ")"); + } } @@ -82,7 +111,7 @@ int main(int argc, char **argv) } // print the expression - fprintf(stdout, "%s = "); + fprintf(stdout, "%.*s = ", MD_S8VArg(node->string)); print_expression(stdout, parse.expr); fprintf(stdout, ";\n"); } diff --git a/tests/expression_tests.c b/tests/expression_tests.c index c4b7734..9de772c 100644 --- a/tests/expression_tests.c +++ b/tests/expression_tests.c @@ -160,9 +160,11 @@ static void parenthesize_exclude_outer(MD_Arena *arena, OperatorDescription *des MD_S8Match(op_s, MD_S8Lit("{}"), 0) || MD_S8Match(op_s, MD_S8Lit("[)"), 0) || MD_S8Match(op_s, MD_S8Lit("(]"), 0)) { - MD_S8ListPush(arena, l, MD_S8Substring(op_s, 0, 1)); - MD_S8ListPush(arena, l, MD_S8Lit("...")); - MD_S8ListPush(arena, l, MD_S8Substring(op_s, 1, 2)); + MD_u8 buf[5]; + buf[0] = op_s.str[0]; + buf[1] = buf[2] = buf[3] = '.'; + buf[4] = op_s.str[1]; + MD_S8ListPush(arena, l, MD_S8(buf, sizeof(buf))); } else {