From efa5ebb3b85d3849e27667ec584563aa3eee1ebd Mon Sep 17 00:00:00 2001 From: John Kerl Date: Tue, 10 May 2016 21:29:31 -0400 Subject: [PATCH] iterate on for/while/if language features: checkpoint --- c/dsls/mlr_dsl_parse.y | 62 ++++++++++++++++++++++-------------------- c/output/out-dev | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 30 deletions(-) diff --git a/c/dsls/mlr_dsl_parse.y b/c/dsls/mlr_dsl_parse.y index 9b88d00f3..1693d81ad 100644 --- a/c/dsls/mlr_dsl_parse.y +++ b/c/dsls/mlr_dsl_parse.y @@ -76,6 +76,8 @@ new_md_statement ::= new_md_while_block. //new_md_statement ::= new_md_emitp. //new_md_statement ::= new_md_emit. //new_md_statement ::= new_md_dump. +new_md_statement ::= MD_TOKEN_BREAK. +new_md_statement ::= MD_TOKEN_CONTINUE. //// ================================================================ new_md_begin_block ::= MD_TOKEN_BEGIN MD_TOKEN_LBRACE new_md_statements MD_TOKEN_RBRACE. @@ -88,16 +90,16 @@ new_md_bare_boolean(A) ::= md_rhs(B). { } // ---------------------------------------------------------------- -//new_md_cond_block(A) ::= md_rhs(B) MD_TOKEN_LBRACE new_md_statements(C) MD_TOKEN_RBRACE. { -// //mlr_dsl_ast_node_print(C); -// //mlr_dsl_ast_node_print(B); -// mlr_dsl_ast_node_print(A); -//} - -new_md_cond_block(A) ::= md_rhs(B) MD_TOKEN_LBRACE new_md_cond_block_statements(C) MD_TOKEN_RBRACE . { - A = mlr_dsl_ast_node_prepend_arg(C, B); +new_md_cond_block(A) ::= md_rhs(B) MD_TOKEN_LBRACE new_md_statements(C) MD_TOKEN_RBRACE. { + //mlr_dsl_ast_node_print(C); + //mlr_dsl_ast_node_print(B); + mlr_dsl_ast_node_print(A); } +//new_md_cond_block(A) ::= md_rhs(B) MD_TOKEN_LBRACE new_md_cond_block_statements(C) MD_TOKEN_RBRACE . { +// A = mlr_dsl_ast_node_prepend_arg(C, B); +//} + // Given "$x>0 {$a=1;$b=2;$c=3}": since this is a bottom-up parser, we get first the "$a=1", // then "$a=1;$b=2", then "$a=1;$b=2;$c=3", then finally "$x>0 {$a=1;$b=2;$c=3}". So: // @@ -108,27 +110,27 @@ new_md_cond_block(A) ::= md_rhs(B) MD_TOKEN_LBRACE new_md_cond_block_statements( // // We handle statements of the form 'true{ ; ; }' by parsing the empty spaces around the semicolons as NOP nodes. // But, the NOP nodes are immediately stripped here and are not included in the AST we return. -new_md_cond_block_statements(A) ::= new_md_cond_block_statement(B). { - if (B->type == MD_AST_NODE_TYPE_NOP) { - A = mlr_dsl_ast_node_alloc_zary("cond", MD_AST_NODE_TYPE_CONDITIONAL_BLOCK); - } else { - A = mlr_dsl_ast_node_alloc_unary("cond", MD_AST_NODE_TYPE_CONDITIONAL_BLOCK, B); - } -} -new_md_cond_block_statements(A) ::= new_md_cond_block_statements(B) MD_TOKEN_SEMICOLON new_md_cond_block_statement(C). { - if (C->type == MD_AST_NODE_TYPE_NOP) { - A = B; - } else { - A = mlr_dsl_ast_node_append_arg(B, C); - } } +//new_md_cond_block_statements(A) ::= new_md_cond_block_statement(B). { +// if (B->type == MD_AST_NODE_TYPE_NOP) { +// A = mlr_dsl_ast_node_alloc_zary("cond", MD_AST_NODE_TYPE_CONDITIONAL_BLOCK); +// } else { +// A = mlr_dsl_ast_node_alloc_unary("cond", MD_AST_NODE_TYPE_CONDITIONAL_BLOCK, B); +// } +//} +//new_md_cond_block_statements(A) ::= new_md_cond_block_statements(B) MD_TOKEN_SEMICOLON new_md_cond_block_statement(C). { +// if (C->type == MD_AST_NODE_TYPE_NOP) { +// A = B; +// } else { +// A = mlr_dsl_ast_node_append_arg(B, C); +// } } -// This allows for trailing semicolon, as well as empty string (or whitespace) between semicolons: -new_md_cond_block_statement(A) ::= . { - A = mlr_dsl_ast_node_alloc_zary("nop", MD_AST_NODE_TYPE_NOP); -} -new_md_cond_block_statement ::= MD_TOKEN_BREAK. -new_md_cond_block_statement ::= MD_TOKEN_CONTINUE. -//new_md_cond_block_statement ::= new_md_statement. +//// This allows for trailing semicolon, as well as empty string (or whitespace) between semicolons: +//new_md_cond_block_statement(A) ::= . { +// A = mlr_dsl_ast_node_alloc_zary("nop", MD_AST_NODE_TYPE_NOP); +//} +//new_md_cond_block_statement ::= MD_TOKEN_BREAK. +//new_md_cond_block_statement ::= MD_TOKEN_CONTINUE. +////new_md_cond_block_statement ::= new_md_statement. // ---------------------------------------------------------------- new_md_while_block(A) ::= MD_TOKEN_WHILE MD_TOKEN_LPAREN md_rhs(B) MD_TOKEN_RPAREN @@ -161,8 +163,8 @@ new_md_for_loop_full_srec(A) ::= MD_TOKEN_FOR MD_TOKEN_LPAREN new_md_for_body_statements ::= new_md_for_body_statement. new_md_for_body_statements ::= new_md_for_body_statement MD_TOKEN_SEMICOLON new_md_for_body_statements. new_md_for_body_statement ::= new_md_statement. -new_md_for_body_statement ::= MD_TOKEN_BREAK. -new_md_for_body_statement ::= MD_TOKEN_CONTINUE. +//new_md_for_body_statement ::= MD_TOKEN_BREAK. +//new_md_for_body_statement ::= MD_TOKEN_CONTINUE. ////new_md_for_body_statement ::= . //// ---------------------------------------------------------------- diff --git a/c/output/out-dev b/c/output/out-dev index 22a74f50d..3aef9e8c8 100644 --- a/c/output/out-dev +++ b/c/output/out-dev @@ -25,3 +25,50 @@ AST MAIN STATEMENTS (0): AST END STATEMENTS (0): mlr --from /dev/null put -v #true{$m=2} += (srec_assignment): + m (field_name). + 2 (strnum_literal). +AST BEGIN STATEMENTS (0): +AST MAIN STATEMENTS (0): +AST END STATEMENTS (0): + +mlr --from /dev/null put -v #for(k,v in $*) { } +AST BEGIN STATEMENTS (0): +AST MAIN STATEMENTS (0): +AST END STATEMENTS (0): + +mlr --from /dev/null put -v #for(k,v in $*) { $m=3} += (srec_assignment): + m (field_name). + 3 (strnum_literal). +AST BEGIN STATEMENTS (0): +AST MAIN STATEMENTS (0): +AST END STATEMENTS (0): + +mlr --from /dev/null put -v #for(k,v in $*) { true{$m=3} } += (srec_assignment): + m (field_name). + 3 (strnum_literal). +AST BEGIN STATEMENTS (0): +AST MAIN STATEMENTS (0): +AST END STATEMENTS (0): + +mlr --from /dev/null put -v #true{ for(k,v in $*) { true{$m=3} } } += (srec_assignment): + m (field_name). + 3 (strnum_literal). +AST BEGIN STATEMENTS (0): +AST MAIN STATEMENTS (0): +AST END STATEMENTS (0): + +mlr --from /dev/null put -v #true{ for(k,v in $*) { $m=1;break;continue;;$m=2 } } += (srec_assignment): + m (field_name). + 1 (strnum_literal). += (srec_assignment): + m (field_name). + 2 (strnum_literal). +AST BEGIN STATEMENTS (0): +AST MAIN STATEMENTS (0): +AST END STATEMENTS (0): +