write/append code-dedupe iterate

This commit is contained in:
John Kerl 2016-07-26 22:48:51 -04:00
parent 4424d474eb
commit 005dcc3b90
4 changed files with 44 additions and 54 deletions

View file

@ -168,6 +168,8 @@ char* mlr_dsl_ast_node_describe_type(mlr_dsl_ast_node_type_t type) {
case MD_AST_NODE_TYPE_CONDITIONAL_BLOCK: return "conditional_block"; break;
case MD_AST_NODE_TYPE_FILTER: return "filter"; break;
case MD_AST_NODE_TYPE_UNSET: return "unset"; break;
case MD_AST_NODE_TYPE_FILE_WRITE: return "file_write"; break;
case MD_AST_NODE_TYPE_FILE_APPEND: return "file_append"; break;
case MD_AST_NODE_TYPE_TEE: return "tee"; break;
case MD_AST_NODE_TYPE_TEE_WRITE: return "tee_write"; break;
case MD_AST_NODE_TYPE_TEE_APPEND: return "tee_append"; break;
@ -187,15 +189,9 @@ char* mlr_dsl_ast_node_describe_type(mlr_dsl_ast_node_type_t type) {
case MD_AST_NODE_TYPE_EMIT_LASHED_WRITE: return "emit_lashed_write"; break;
case MD_AST_NODE_TYPE_EMIT_LASHED_APPEND: return "emit_lashed_append"; break;
case MD_AST_NODE_TYPE_DUMP: return "dump"; break;
case MD_AST_NODE_TYPE_DUMP_WRITE: return "dump_write"; break;
case MD_AST_NODE_TYPE_DUMP_APPEND: return "dump_append"; break;
case MD_AST_NODE_TYPE_EDUMP: return "edump"; break;
case MD_AST_NODE_TYPE_PRINT: return "print"; break;
case MD_AST_NODE_TYPE_PRINT_WRITE: return "print_write"; break;
case MD_AST_NODE_TYPE_PRINT_APPEND: return "print_append"; break;
case MD_AST_NODE_TYPE_PRINTN: return "printn"; break;
case MD_AST_NODE_TYPE_PRINTN_WRITE: return "printn_write"; break;
case MD_AST_NODE_TYPE_PRINTN_APPEND: return "printn_append"; break;
case MD_AST_NODE_TYPE_EPRINT: return "eprint"; break;
case MD_AST_NODE_TYPE_EPRINTN: return "eprintn"; break;
case MD_AST_NODE_TYPE_STDOUT: return "stdout"; break;

View file

@ -33,6 +33,8 @@ typedef enum _mlr_dsl_ast_node_type_t {
MD_AST_NODE_TYPE_CONDITIONAL_BLOCK,
MD_AST_NODE_TYPE_FILTER,
MD_AST_NODE_TYPE_UNSET,
MD_AST_NODE_TYPE_FILE_WRITE,
MD_AST_NODE_TYPE_FILE_APPEND,
MD_AST_NODE_TYPE_TEE,
MD_AST_NODE_TYPE_TEE_WRITE,
MD_AST_NODE_TYPE_TEE_APPEND,
@ -52,15 +54,9 @@ typedef enum _mlr_dsl_ast_node_type_t {
MD_AST_NODE_TYPE_EMIT_LASHED_WRITE,
MD_AST_NODE_TYPE_EMIT_LASHED_APPEND,
MD_AST_NODE_TYPE_DUMP,
MD_AST_NODE_TYPE_DUMP_WRITE,
MD_AST_NODE_TYPE_DUMP_APPEND,
MD_AST_NODE_TYPE_EDUMP,
MD_AST_NODE_TYPE_PRINT,
MD_AST_NODE_TYPE_PRINT_WRITE,
MD_AST_NODE_TYPE_PRINT_APPEND,
MD_AST_NODE_TYPE_PRINTN,
MD_AST_NODE_TYPE_PRINTN_WRITE,
MD_AST_NODE_TYPE_PRINTN_APPEND,
MD_AST_NODE_TYPE_EPRINT,
MD_AST_NODE_TYPE_EPRINTN,
MD_AST_NODE_TYPE_STDOUT,

View file

@ -965,41 +965,47 @@ md_edump(A) ::= MD_TOKEN_EDUMP(O). {
A = mlr_dsl_ast_node_alloc_unary(O->text, MD_AST_NODE_TYPE_DUMP,
mlr_dsl_ast_node_alloc_zary("stderr", MD_AST_NODE_TYPE_STDERR));
}
md_dump_write(A) ::= MD_TOKEN_DUMP(O) MD_TOKEN_GT md_rhs(B). {
A = mlr_dsl_ast_node_alloc_unary(O->text, MD_AST_NODE_TYPE_DUMP_WRITE, B);
md_dump_write(A) ::= MD_TOKEN_DUMP(O) MD_TOKEN_GT md_rhs(F). {
A = mlr_dsl_ast_node_alloc_unary(O->text, MD_AST_NODE_TYPE_DUMP,
mlr_dsl_ast_node_alloc_unary(O->text, MD_AST_NODE_TYPE_FILE_WRITE, F));
}
md_dump_append(A) ::= MD_TOKEN_DUMP(O) MD_TOKEN_BITWISE_RSH md_rhs(B). {
A = mlr_dsl_ast_node_alloc_unary(O->text, MD_AST_NODE_TYPE_DUMP_APPEND, B);
md_dump_append(A) ::= MD_TOKEN_DUMP(O) MD_TOKEN_BITWISE_RSH md_rhs(F). {
A = mlr_dsl_ast_node_alloc_unary(O->text, MD_AST_NODE_TYPE_DUMP,
mlr_dsl_ast_node_alloc_unary(O->text, MD_AST_NODE_TYPE_FILE_APPEND, F));
}
md_print(A) ::= MD_TOKEN_PRINT(O) md_rhs(B). {
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINT_WRITE, B,
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINT, B,
mlr_dsl_ast_node_alloc_zary("stdout", MD_AST_NODE_TYPE_STDOUT));
}
md_eprint(A) ::= MD_TOKEN_EPRINT(O) md_rhs(B). {
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINT_WRITE, B,
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINT, B,
mlr_dsl_ast_node_alloc_zary("stdout", MD_AST_NODE_TYPE_STDERR));
}
md_print_write(A) ::= MD_TOKEN_PRINT(O) MD_TOKEN_GT md_output_file(F) MD_TOKEN_COMMA md_rhs(C). {
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINT_WRITE, C, F);
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINT, C,
mlr_dsl_ast_node_alloc_unary(O->text, MD_AST_NODE_TYPE_FILE_WRITE, F));
}
md_print_append(A) ::= MD_TOKEN_PRINT(O) MD_TOKEN_BITWISE_RSH md_output_file(F) MD_TOKEN_COMMA md_rhs(C). {
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINT_APPEND, C, F);
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINT, C,
mlr_dsl_ast_node_alloc_unary(O->text, MD_AST_NODE_TYPE_FILE_APPEND, F));
}
md_printn(A) ::= MD_TOKEN_PRINTN(O) md_rhs(B). {
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINTN_WRITE, B,
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINTN, B,
mlr_dsl_ast_node_alloc_zary("stdout", MD_AST_NODE_TYPE_STDOUT));
}
md_eprintn(A) ::= MD_TOKEN_EPRINTN(O) md_rhs(B). {
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINTN_WRITE, B,
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINTN, B,
mlr_dsl_ast_node_alloc_zary("stderr", MD_AST_NODE_TYPE_STDERR));
}
md_printn_write(A) ::= MD_TOKEN_PRINTN(O) MD_TOKEN_GT md_output_file(F) MD_TOKEN_COMMA md_rhs(C). {
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINTN_WRITE, C, F);
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINTN, C,
mlr_dsl_ast_node_alloc_unary(O->text, MD_AST_NODE_TYPE_FILE_WRITE, F));
}
md_printn_append(A) ::= MD_TOKEN_PRINTN(O) MD_TOKEN_BITWISE_RSH md_output_file(F) MD_TOKEN_COMMA md_rhs(C). {
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINTN_APPEND, C, F);
A = mlr_dsl_ast_node_alloc_binary(O->text, MD_AST_NODE_TYPE_PRINTN, C,
mlr_dsl_ast_node_alloc_unary(O->text, MD_AST_NODE_TYPE_FILE_APPEND, F));
}
// ----------------------------------------------------------------

View file

@ -39,9 +39,8 @@ static mlr_dsl_cst_statement_t* alloc_emit(mlr_dsl_ast_node_t* p, int t
static mlr_dsl_cst_statement_t* alloc_emit_to_file(mlr_dsl_ast_node_t* p, int ti, int cf, int dfp, file_output_mode_t m);
static mlr_dsl_cst_statement_t* alloc_emit_lashed(mlr_dsl_ast_node_t* p, int ti, int cf, int dfp);
static mlr_dsl_cst_statement_t* alloc_emit_lashed_to_file(mlr_dsl_ast_node_t* p, int ti, int cf, int dfp, file_output_mode_t m);
static mlr_dsl_cst_statement_t* alloc_dump(mlr_dsl_ast_node_t* p, int ti, int cf, file_output_mode_t m);
static mlr_dsl_cst_statement_t* alloc_print(mlr_dsl_ast_node_t* p, int ti, int cf,
file_output_mode_t m, FILE* stdfp, char* print_terminator);
static mlr_dsl_cst_statement_t* alloc_dump(mlr_dsl_ast_node_t* p, int ti, int cf);
static mlr_dsl_cst_statement_t* alloc_print(mlr_dsl_ast_node_t* p, int ti, int cf, char* print_terminator);
static mlr_dsl_cst_statement_t* alloc_conditional_block(mlr_dsl_ast_node_t* p, int ti, int cf);
static mlr_dsl_cst_statement_t* alloc_if_head(mlr_dsl_ast_node_t* p, int ti, int cf);
@ -481,27 +480,15 @@ static mlr_dsl_cst_statement_t* alloc_cst_statement(mlr_dsl_ast_node_t* pnode, i
break;
case MD_AST_NODE_TYPE_DUMP:
return alloc_dump(pnode, type_inferencing, context_flags, MODE_WRITE);
break;
case MD_AST_NODE_TYPE_DUMP_WRITE:
return alloc_dump(pnode, type_inferencing, context_flags, MODE_WRITE);
break;
case MD_AST_NODE_TYPE_DUMP_APPEND:
return alloc_dump(pnode, type_inferencing, context_flags, MODE_APPEND);
return alloc_dump(pnode, type_inferencing, context_flags);
break;
case MD_AST_NODE_TYPE_PRINT_WRITE:
return alloc_print(pnode, type_inferencing, context_flags, MODE_WRITE, NULL, "\n");
break;
case MD_AST_NODE_TYPE_PRINT_APPEND:
return alloc_print(pnode, type_inferencing, context_flags, MODE_APPEND, NULL, "\n");
case MD_AST_NODE_TYPE_PRINT:
return alloc_print(pnode, type_inferencing, context_flags, "\n");
break;
case MD_AST_NODE_TYPE_PRINTN_WRITE:
return alloc_print(pnode, type_inferencing, context_flags, MODE_WRITE, NULL, "");
break;
case MD_AST_NODE_TYPE_PRINTN_APPEND:
return alloc_print(pnode, type_inferencing, context_flags, MODE_APPEND, NULL, "");
case MD_AST_NODE_TYPE_PRINTN:
return alloc_print(pnode, type_inferencing, context_flags, "");
break;
default:
@ -1413,21 +1400,24 @@ static mlr_dsl_cst_statement_t* alloc_filter(mlr_dsl_ast_node_t* pnode, int type
// ----------------------------------------------------------------
static mlr_dsl_cst_statement_t* alloc_dump(mlr_dsl_ast_node_t* pnode, int type_inferencing,
int context_flags, file_output_mode_t file_output_mode)
int context_flags)
{
mlr_dsl_cst_statement_t* pstatement = alloc_blank();
mlr_dsl_ast_node_t* pfilename_node = pnode->pchildren->phead->pvvalue;
if (pfilename_node->type == MD_AST_NODE_TYPE_STDOUT) {
mlr_dsl_ast_node_t* poutput_node = pnode->pchildren->phead->pvvalue;
if (poutput_node->type == MD_AST_NODE_TYPE_STDOUT) {
pstatement->pnode_handler = handle_dump;
pstatement->stdfp = stdout;
} else if (pfilename_node->type == MD_AST_NODE_TYPE_STDERR) {
} else if (poutput_node->type == MD_AST_NODE_TYPE_STDERR) {
pstatement->pnode_handler = handle_dump;
pstatement->stdfp = stderr;
} else {
// xxx replicate x all & rid of {VARIOUS}_WRITE/APPEND node types
mlr_dsl_ast_node_t* pfilename_node = poutput_node->pchildren->phead->pvvalue;
pstatement->poutput_filename_evaluator = rval_evaluator_alloc_from_ast(pfilename_node,
type_inferencing, context_flags);
pstatement->file_output_mode = file_output_mode;
pstatement->file_output_mode = poutput_node->type == MD_AST_NODE_TYPE_FILE_APPEND
? MODE_APPEND : MODE_WRITE;
pstatement->pmulti_out = multi_out_alloc();
pstatement->pnode_handler = handle_dump_to_file;
}
@ -1437,7 +1427,7 @@ static mlr_dsl_cst_statement_t* alloc_dump(mlr_dsl_ast_node_t* pnode, int type_i
// ----------------------------------------------------------------
static mlr_dsl_cst_statement_t* alloc_print(mlr_dsl_ast_node_t* pnode, int type_inferencing, int context_flags,
file_output_mode_t file_output_mode, /*xxx rid of */ FILE* stdfp, char* print_terminator)
char* print_terminator)
{
if ((pnode->pchildren == NULL) || (pnode->pchildren->length != 2)) {
fprintf(stderr, "%s: internal coding error detected in file %s at line %d.\n",
@ -1450,17 +1440,19 @@ static mlr_dsl_cst_statement_t* alloc_print(mlr_dsl_ast_node_t* pnode, int type_
pstatement->print_terminator = print_terminator;
// xxx replicate x all
mlr_dsl_ast_node_t* pfilename_node = pnode->pchildren->phead->pnext->pvvalue;
if (pfilename_node->type == MD_AST_NODE_TYPE_STDOUT) {
mlr_dsl_ast_node_t* poutput_node = pnode->pchildren->phead->pnext->pvvalue;
if (poutput_node->type == MD_AST_NODE_TYPE_STDOUT) {
pstatement->pnode_handler = handle_print;
pstatement->stdfp = stdout;
} else if (pfilename_node->type == MD_AST_NODE_TYPE_STDERR) {
} else if (poutput_node->type == MD_AST_NODE_TYPE_STDERR) {
pstatement->pnode_handler = handle_print;
pstatement->stdfp = stderr;
} else {
mlr_dsl_ast_node_t* pfilename_node = poutput_node->pchildren->phead->pvvalue;
pstatement->poutput_filename_evaluator = rval_evaluator_alloc_from_ast(pfilename_node,
type_inferencing, context_flags);
pstatement->file_output_mode = file_output_mode;
pstatement->file_output_mode = poutput_node->type == MD_AST_NODE_TYPE_FILE_APPEND
? MODE_APPEND : MODE_WRITE;
pstatement->pmulti_out = multi_out_alloc();
pstatement->pnode_handler = handle_print_to_file;
}