[examples] write expressions intro example

This commit is contained in:
Allen Webster
2021-10-09 20:58:33 -07:00
parent 39e10cd246
commit ac9f415180
4 changed files with 46 additions and 5 deletions
+4
View File
@@ -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
+6
View File
@@ -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
+31 -2
View File
@@ -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");
}
+5 -3
View File
@@ -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
{