From d6b4b6dc9985849cecb44a0e397f9bfbb8bc89b2 Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Sun, 6 May 2018 19:15:19 -0400 Subject: added xml() and xmlac(), which which collect xml to strings Going to eliminate the specialized print forms, instead opting for functions. Later on we can make these lazy. --- src/bytecode.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/bytecode.h | 3 +++ src/declare.cc | 8 ++++++++ src/print.c | 22 ++++++++++++++++++++++ 4 files changed, 88 insertions(+) (limited to 'src') diff --git a/src/bytecode.c b/src/bytecode.c index c59e1382..fbd3891e 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -153,6 +153,39 @@ static head_t *tree_to_str( program_t *prg, tree_t **sp, tree_t *tree, int trim, return ret; } +static head_t *tree_to_str_xml( program_t *prg, tree_t **sp, tree_t *tree, int trim, int attrs ) +{ + /* Collect the tree data. */ + StrCollect collect; + init_str_collect( &collect ); + + colm_print_tree_collect_xml( prg, sp, &collect, tree, trim ); + + /* Set up the input stream. */ + head_t *ret = string_alloc_full( prg, collect.data, collect.length ); + + str_collect_destroy( &collect ); + + return ret; +} + +static head_t *tree_to_str_xml_ac( program_t *prg, tree_t **sp, tree_t *tree, int trim, int attrs ) +{ + /* Collect the tree data. */ + StrCollect collect; + init_str_collect( &collect ); + + colm_print_tree_collect_xml_ac( prg, sp, &collect, tree, trim ); + + /* Set up the input stream. */ + head_t *ret = string_alloc_full( prg, collect.data, collect.length ); + + str_collect_destroy( &collect ); + + return ret; +} + + static word_t stream_append_tree( program_t *prg, tree_t **sp, stream_t *dest, tree_t *input ) { long length = 0; @@ -1541,6 +1574,28 @@ again: vm_push_tree( str ); break; } + case IN_TREE_TO_STR_XML: { + debug( prg, REALM_BYTECODE, "IN_TREE_TO_STR_XML_AC\n" ); + + tree_t *tree = vm_pop_tree(); + head_t *res = tree_to_str_xml( prg, sp, tree, false, 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_XML_AC: { + debug( prg, REALM_BYTECODE, "IN_TREE_TO_STR_XML_AC\n" ); + + tree_t *tree = vm_pop_tree(); + head_t *res = tree_to_str_xml_ac( prg, sp, tree, false, 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: { debug( prg, REALM_BYTECODE, "IN_TREE_TO_STR\n" ); diff --git a/src/bytecode.h b/src/bytecode.h index 30c1478c..d4bec751 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -235,6 +235,9 @@ typedef unsigned char uchar; #define IN_PRINT_STREAM 0x8a #define IN_PRINT_DUMP 0xf6 +#define IN_TREE_TO_STR_XML 0x6e +#define IN_TREE_TO_STR_XML_AC 0x6f + #define IN_HOST 0xea #define IN_CALL_WC 0x8c diff --git a/src/declare.cc b/src/declare.cc index 10c56902..e7357e6e 100644 --- a/src/declare.cc +++ b/src/declare.cc @@ -1024,6 +1024,14 @@ void Compiler::declareGlobalFields() method = initFunction( uniqueTypeInt, rootNamespace, globalObjectDef, "system", IN_SYSTEM, IN_SYSTEM, uniqueTypeStr, true ); + method = initFunction( uniqueTypeStr, rootNamespace, globalObjectDef, "xml", + IN_TREE_TO_STR_XML, IN_TREE_TO_STR_XML, uniqueTypeAny, true ); + method->useCallObj = false; + + method = initFunction( uniqueTypeStr, rootNamespace, globalObjectDef, "xmlac", + IN_TREE_TO_STR_XML_AC, IN_TREE_TO_STR_XML_AC, uniqueTypeAny, true ); + method->useCallObj = false; + addStdin(); addStdout(); addStderr(); diff --git a/src/print.c b/src/print.c index d30c3c6d..471bfbd1 100644 --- a/src/print.c +++ b/src/print.c @@ -745,3 +745,25 @@ void colm_postfix_tree_file( program_t *prg, tree_t **sp, struct stream_impl *im fflush( impl->file ); } +void colm_print_tree_collect_xml( program_t *prg, tree_t **sp, + StrCollect *collect, tree_t *tree, int trim ) +{ + struct colm_print_args print_args = { + collect, false, false, trim, &append_collect, + &xml_open, &xml_term, &xml_close + }; + + colm_print_tree_args( prg, sp, &print_args, tree ); +} + +void colm_print_tree_collect_xml_ac( 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, + &xml_open, &xml_term, &xml_close + }; + + colm_print_tree_args( prg, sp, &print_args, tree ); +} + -- cgit v1.2.1