diff options
author | Adrian Thurston <thurston@complang.org> | 2015-12-15 18:03:12 -0500 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2015-12-15 18:03:12 -0500 |
commit | 5a80d5bf17dd9b176c034bae4d708710f6305419 (patch) | |
tree | 17856dc287703179e7f91b8a112c31eae5d5abbb | |
parent | 9bfd3f5510bdf3da7c4084a046cbba5ba7d673fe (diff) | |
download | colm-5a80d5bf17dd9b176c034bae4d708710f6305419.tar.gz |
added the $$ operator, which converts tree-to-str with attrs
-rw-r--r-- | src/bytecode.c | 22 | ||||
-rw-r--r-- | src/bytecode.h | 1 | ||||
-rw-r--r-- | src/colm.lm | 1 | ||||
-rw-r--r-- | src/loadcolm.cc | 5 | ||||
-rw-r--r-- | src/synthesis.cc | 9 | ||||
-rw-r--r-- | src/tree.c | 20 | ||||
-rw-r--r-- | src/tree.h | 3 |
7 files changed, 53 insertions, 8 deletions
diff --git a/src/bytecode.c b/src/bytecode.c index 6bb4d7b9..a9c33efb 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -140,13 +140,16 @@ void colm_parser_set_context( program_t *prg, tree_t **sp, parser_t *parser, str parser->pda_run->context = val; } -static head_t *tree_to_str( program_t *prg, tree_t **sp, tree_t *tree, int trim ) +static head_t *tree_to_str( program_t *prg, tree_t **sp, tree_t *tree, int trim, int attrs ) { /* Collect the tree data. */ StrCollect collect; init_str_collect( &collect ); - print_tree_collect( prg, sp, &collect, tree, trim ); + if ( attrs ) + print_tree_collect_a( prg, sp, &collect, tree, trim ); + else + print_tree_collect( prg, sp, &collect, tree, trim ); /* Set up the input stream. */ head_t *ret = string_alloc_full( prg, collect.data, collect.length ); @@ -1490,7 +1493,7 @@ again: debug( prg, REALM_BYTECODE, "IN_TREE_TO_STR\n" ); tree_t *tree = vm_pop_tree(); - head_t *res = tree_to_str( prg, sp, tree, false ); + head_t *res = tree_to_str( prg, sp, tree, false, false ); tree_t *str = construct_string( prg, res ); colm_tree_upref( str ); vm_push_tree( str ); @@ -1501,7 +1504,18 @@ again: debug( prg, REALM_BYTECODE, "IN_TREE_TO_STR_TRIM\n" ); tree_t *tree = vm_pop_tree(); - head_t *res = tree_to_str( prg, sp, tree, true ); + head_t *res = tree_to_str( prg, sp, tree, true, false ); + tree_t *str = construct_string( prg, res ); + colm_tree_upref( str ); + vm_push_tree( str ); + colm_tree_downref( prg, sp, tree ); + break; + } + case IN_TREE_TO_STR_TRIM_A: { + debug( prg, REALM_BYTECODE, "IN_TREE_TO_STR_TRIM_A\n" ); + + tree_t *tree = vm_pop_tree(); + head_t *res = tree_to_str( prg, sp, tree, true, true ); tree_t *str = construct_string( prg, res ); colm_tree_upref( str ); vm_push_tree( str ); diff --git a/src/bytecode.h b/src/bytecode.h index 793b3381..a5079a83 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -247,6 +247,7 @@ typedef unsigned long colm_value_t; #define IN_INT_TO_STR 0x97 #define IN_TREE_TO_STR 0x98 #define IN_TREE_TO_STR_TRIM 0x99 +#define IN_TREE_TO_STR_TRIM_A 0x18 #define IN_CREATE_TOKEN 0x9a #define IN_MAKE_TOKEN 0x9b diff --git a/src/colm.lm b/src/colm.lm index e50898a9..b87bad4f 100644 --- a/src/colm.lm +++ b/src/colm.lm @@ -607,6 +607,7 @@ def code_multiplicitive def code_unary [BANG code_factor] :Bang | [DOLLAR code_factor] :Dollar +| [DOLLAR DOLLAR code_factor] :DollarDollar | [CARET code_factor] :Caret | [PERCENT code_factor] :Percent | [code_factor] :Base diff --git a/src/loadcolm.cc b/src/loadcolm.cc index d9212976..e7bdbf2b 100644 --- a/src/loadcolm.cc +++ b/src/loadcolm.cc @@ -1977,6 +1977,11 @@ struct LoadColm expr = LangExpr::cons( unary.DOLLAR().loc(), '$', factor ); break; } + case code_unary::DollarDollar: { + LangExpr *factor = walkCodeFactor( unary.code_factor() ); + expr = LangExpr::cons( unary.DOLLAR().loc(), 'S', factor ); + break; + } case code_unary::Caret: { LangExpr *factor = walkCodeFactor( unary.code_factor() ); expr = LangExpr::cons( unary.CARET().loc(), '^', factor ); diff --git a/src/synthesis.cc b/src/synthesis.cc index 896c5496..2531de4b 100644 --- a/src/synthesis.cc +++ b/src/synthesis.cc @@ -2135,6 +2135,15 @@ UniqueType *LangExpr::evaluate( Compiler *pd, CodeVect &code ) const return pd->uniqueTypeStr; } + case 'S': { + UniqueType *ut = right->evaluate( pd, code ); + + if ( ut->typeId == TYPE_INT || ut->typeId == TYPE_BOOL ) + code.append( IN_INT_TO_STR ); + + code.append( IN_TREE_TO_STR_TRIM_A ); + return pd->uniqueTypeStr; + } case '%': { UniqueType *ut = right->evaluate( pd, code ); if ( ut->typeId == TYPE_INT || ut->typeId == TYPE_BOOL ) @@ -2041,9 +2041,10 @@ void colm_print_term_tree( program_t *prg, tree_t **sp, if ( kid->tree->id == LEL_ID_PTR ) { char buf[INT_SZ]; - print_args->out( print_args, "#", 1 ); - sprintf( buf, "%p", (void*) ((pointer_t*)kid->tree)->value ); + print_args->out( print_args, "#<", 2 ); + sprintf( buf, "%lx", ((pointer_t*)kid->tree)->value ); print_args->out( print_args, buf, strlen(buf) ); + print_args->out( print_args, ">", 1 ); } else if ( kid->tree->id == LEL_ID_STR ) { print_str( print_args, ((str_t*)kid->tree)->value ); @@ -2114,8 +2115,8 @@ void print_term_xml( program_t *prg, tree_t **sp, /*child = */ tree_child( prg, kid->tree ); if ( kid->tree->id == LEL_ID_PTR ) { - char ptr[32]; - sprintf( ptr, "%p\n", (void*)((pointer_t*)kid->tree)->value ); + char ptr[INT_SZ]; + sprintf( ptr, "%lx", ((pointer_t*)kid->tree)->value ); print_args->out( print_args, ptr, strlen(ptr) ); } else if ( kid->tree->id == LEL_ID_STR ) { @@ -2168,6 +2169,17 @@ void print_tree_collect( program_t *prg, tree_t **sp, colm_print_tree_args( prg, sp, &print_args, tree ); } +void print_tree_collect_a( program_t *prg, tree_t **sp, + StrCollect *collect, tree_t *tree, int trim ) +{ + struct colm_print_args print_args = { + collect, true, true, trim, &append_collect, + &colm_print_null, &colm_print_term_tree, &colm_print_null + }; + + colm_print_tree_args( prg, sp, &print_args, tree ); +} + void print_tree_file( program_t *prg, tree_t **sp, struct stream_impl *impl, tree_t *tree, int trim ) { @@ -308,6 +308,9 @@ tree_t *tree_trim( struct colm_program *prg, tree_t **sp, tree_t *tree ); void print_tree_collect( struct colm_program *prg, tree_t **sp, StrCollect *collect, tree_t *tree, int trim ); +void print_tree_collect_a( struct colm_program *prg, tree_t **sp, + StrCollect *collect, tree_t *tree, int trim ); + void print_tree_file( struct colm_program *prg, tree_t **sp, struct stream_impl *impl, tree_t *tree, int trim ); void print_xml_stdout( struct colm_program *prg, tree_t **sp, |