summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@colm.net>2018-05-06 19:15:19 -0400
committerAdrian Thurston <thurston@colm.net>2018-05-06 19:15:19 -0400
commitd6b4b6dc9985849cecb44a0e397f9bfbb8bc89b2 (patch)
treeeabd98909730343097cba412de88bdd64c964381 /src
parent776ddfc296be0edd2093b932e2b61c6407f29b94 (diff)
downloadcolm-d6b4b6dc9985849cecb44a0e397f9bfbb8bc89b2.tar.gz
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.
Diffstat (limited to 'src')
-rw-r--r--src/bytecode.c55
-rw-r--r--src/bytecode.h3
-rw-r--r--src/declare.cc8
-rw-r--r--src/print.c22
4 files changed, 88 insertions, 0 deletions
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 );
+}
+