summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2012-05-22 15:12:20 +0000
committerAdrian Thurston <thurston@complang.org>2012-05-22 15:12:20 +0000
commit82ebe66edf90112f0370dde9460aa86dad3eb813 (patch)
tree2a3abe6dd94a5e4f796d15f2ef68dafb7bccbae9
parent4f6c820a6374239e11c441ce061880958ff6d679 (diff)
downloadcolm-82ebe66edf90112f0370dde9460aa86dad3eb813.tar.gz
added trim control flag to print code, auto-trimming all colm print calls
The print implemenation now takes a trim flag. The colm print function now sets this flag by default. This is a change to the colm language back to 0.5 semantics. The $ conversion uses this flag too (also 0.5 semantics), in the previous commit it issued a tree trim operation. The % operation gives a string conversion without triming.
-rw-r--r--colm/bytecode.c29
-rw-r--r--colm/bytecode.h1
-rw-r--r--colm/codegen.cc2
-rw-r--r--colm/colm.h1
-rw-r--r--colm/compile.cc9
-rw-r--r--colm/tree.c20
-rw-r--r--colm/tree.h10
-rw-r--r--test/accum1.lm2
-rw-r--r--test/accum2.lm2
-rw-r--r--test/context1.lm2
-rw-r--r--test/func.lm2
-rw-r--r--test/ignore1.lm3
-rw-r--r--test/ignore2.lm2
-rw-r--r--test/order2.lm4
-rw-r--r--test/reparse.lm2
-rw-r--r--test/til.lm4
-rw-r--r--test/translate1.lm2
17 files changed, 54 insertions, 43 deletions
diff --git a/colm/bytecode.c b/colm/bytecode.c
index b5606635..57d31c78 100644
--- a/colm/bytecode.c
+++ b/colm/bytecode.c
@@ -164,13 +164,13 @@ void parserSetContext( Program *prg, Tree **sp, Parser *parser, Tree *val )
parser->pdaRun->context = splitTree( prg, val );
}
-Head *treeToStr( Program *prg, Tree **sp, Tree *tree )
+Head *treeToStr( Program *prg, Tree **sp, Tree *tree, int trim )
{
/* Collect the tree data. */
StrCollect collect;
initStrCollect( &collect );
- printTreeCollect( prg, sp, &collect, tree );
+ printTreeCollect( prg, sp, &collect, tree, trim );
/* Set up the input stream. */
Head *ret = stringAllocFull( prg, collect.data, collect.length );
@@ -189,7 +189,7 @@ Word streamAppend( Program *prg, Tree **sp, Tree *input, InputStream *inputStrea
/* Collect the tree data. */
StrCollect collect;
initStrCollect( &collect );
- printTreeCollect( prg, sp, &collect, input );
+ printTreeCollect( prg, sp, &collect, input, true );
/* Load it into the input. */
appendData( inputStream, collect.data, collect.length );
@@ -349,7 +349,7 @@ long streamPush( Program *prg, Tree **sp, FsmRun *fsmRun, InputStream *in, Tree
/* Collect the tree data. */
StrCollect collect;
initStrCollect( &collect );
- printTreeCollect( prg, sp, &collect, tree );
+ printTreeCollect( prg, sp, &collect, tree, true );
streamPushText( fsmRun, in, collect.data, collect.length );
long length = collect.length;
@@ -893,7 +893,7 @@ again:
while ( n-- > 0 ) {
Tree *tree = vm_pop();
- printTreeFile( prg, sp, stdout, tree );
+ printTreeFile( prg, sp, stdout, tree, true );
treeDownref( prg, sp, tree );
}
break;
@@ -906,7 +906,7 @@ again:
while ( n-- > 0 ) {
Tree *tree = vm_pop();
- printXmlStdout( prg, sp, tree, true );
+ printXmlStdout( prg, sp, tree, true, true );
treeDownref( prg, sp, tree );
}
break;
@@ -918,7 +918,7 @@ again:
while ( n-- > 0 ) {
Tree *tree = vm_pop();
- printXmlStdout( prg, sp, tree, false );
+ printXmlStdout( prg, sp, tree, false, true );
treeDownref( prg, sp, tree );
}
break;
@@ -931,7 +931,7 @@ again:
Stream *stream = (Stream*)vm_pop();
while ( n-- > 0 ) {
Tree *tree = vm_pop();
- printTreeFile( prg, sp, stream->file, tree );
+ printTreeFile( prg, sp, stream->file, tree, true );
treeDownref( prg, sp, tree );
}
treeDownref( prg, sp, (Tree*)stream );
@@ -1543,7 +1543,18 @@ again:
debug( REALM_BYTECODE, "IN_TREE_TO_STR\n" );
Tree *tree = vm_pop();
- Head *res = treeToStr( prg, sp, tree );
+ Head *res = treeToStr( prg, sp, tree, true );
+ Tree *str = constructString( prg, res );
+ treeUpref( str );
+ vm_push( str );
+ treeDownref( prg, sp, tree );
+ break;
+ }
+ case IN_TREE_TO_STR_NOTRIM: {
+ debug( REALM_BYTECODE, "IN_TREE_TO_STR_NOTRIM\n" );
+
+ Tree *tree = vm_pop();
+ Head *res = treeToStr( prg, sp, tree, false );
Tree *str = constructString( prg, res );
treeUpref( str );
vm_push( str );
diff --git a/colm/bytecode.h b/colm/bytecode.h
index 1c8545d1..bb639e94 100644
--- a/colm/bytecode.h
+++ b/colm/bytecode.h
@@ -214,6 +214,7 @@ typedef unsigned char uchar;
#define IN_INT_TO_STR 0x8b
#define IN_TREE_TO_STR 0x8c
+#define IN_TREE_TO_STR_NOTRIM 0xfd
#define IN_CREATE_TOKEN 0x8d
#define IN_MAKE_TOKEN 0x8e
diff --git a/colm/codegen.cc b/colm/codegen.cc
index 3abd99c3..400e4b9a 100644
--- a/colm/codegen.cc
+++ b/colm/codegen.cc
@@ -77,7 +77,7 @@ void ParseData::generateExports()
"inline std::string printTreeStr( ColmProgram *prg, ColmTree *tree )\n"
"{\n"
" std::string str;\n"
- " ColmPrintArgs printArgs = { &str, 1, 0, &appendString, \n"
+ " ColmPrintArgs printArgs = { &str, 1, 0, 1, &appendString, \n"
" &printNull, &printTermTree, &printNull };\n"
" printTreeArgs( prg, vm_root(prg), &printArgs, tree );\n"
" return str;\n"
diff --git a/colm/colm.h b/colm/colm.h
index 335259b4..4f169254 100644
--- a/colm/colm.h
+++ b/colm/colm.h
@@ -20,6 +20,7 @@ struct ColmPrintArgs
void *arg;
int comm;
int attr;
+ int trim;
void (*out)( struct ColmPrintArgs *args, const char *data, int length );
void (*openTree)( struct ColmProgram *prg, struct ColmTree **sp,
struct ColmPrintArgs *args, struct ColmKid *parent, struct ColmKid *kid );
diff --git a/colm/compile.cc b/colm/compile.cc
index f948d33f..14084a5a 100644
--- a/colm/compile.cc
+++ b/colm/compile.cc
@@ -1720,22 +1720,19 @@ UniqueType *LangExpr::evaluate( ParseData *pd, CodeVect &code ) const
return pd->uniqueTypeBool;
}
case '$': {
- UniqueType *rt = right->evaluate( pd, code );
- if ( rt->typeId == TYPE_TREE && rt != pd->uniqueTypeInt && rt != pd->uniqueTypeStr && rt != pd->uniqueTypeBool )
- code.append( IN_TREE_TRIM );
+ right->evaluate( pd, code );
code.append( IN_TREE_TO_STR );
return pd->uniqueTypeStr;
}
case '%': {
right->evaluate( pd, code );
- code.append( IN_TREE_TO_STR );
+ code.append( IN_TREE_TO_STR_NOTRIM );
return pd->uniqueTypeStr;
}
case '^': {
UniqueType *rt = right->evaluate( pd, code );
- if ( rt->typeId == TYPE_TREE && rt != pd->uniqueTypeInt && rt != pd->uniqueTypeStr && rt != pd->uniqueTypeBool )
- code.append( IN_TREE_TRIM );
+ code.append( IN_TREE_TRIM );
return rt;
}
case OP_Deref: {
diff --git a/colm/tree.c b/colm/tree.c
index 722a474c..66e5e025 100644
--- a/colm/tree.c
+++ b/colm/tree.c
@@ -2096,12 +2096,15 @@ enum VisitType
NonTerm,
};
+#define TF_TERM_SEEN 0x1
+
void printKid( Program *prg, Tree **sp, struct ColmPrintArgs *printArgs, Kid *kid )
{
enum ReturnType rt;
Kid *parent = 0;
Kid *leadingIgnore = 0;
enum VisitType visitType;
+ int flags = 0;
/* Iterate the kids passed in. We are expecting a next, which will allow us
* to print the trailing ignore list. */
@@ -2188,7 +2191,7 @@ rec_call:
/* Print the leading ignore list. Also implement the suppress right
* in the process. */
- if ( printArgs->comm ) {
+ if ( printArgs->comm && (!printArgs->trim || (flags & TF_TERM_SEEN && kid->tree->id > 0)) ) {
ignore = leadingIgnore;
while ( ignore != 0 ) {
if ( ignore->tree->flags & AF_SUPPRESS_RIGHT )
@@ -2232,6 +2235,9 @@ rec_call:
printArgs->openTree( prg, sp, printArgs, parent, kid );
}
+ if ( visitType == Term )
+ flags |= TF_TERM_SEEN;
+
if ( visitType == Term || visitType == IgnoreData ) {
/* Print contents. */
if ( kid->tree->id < prg->rtd->firstNonTermId ) {
@@ -2455,23 +2461,23 @@ void closeTreeXml( Program *prg, Tree **sp, struct ColmPrintArgs *args, Kid *par
args->out( args, ">", 1 );
}
-void printTreeCollect( Program *prg, Tree **sp, StrCollect *collect, Tree *tree )
+void printTreeCollect( Program *prg, Tree **sp, StrCollect *collect, Tree *tree, int trim )
{
- struct ColmPrintArgs printArgs = { collect, 1, 0, &appendCollect,
+ struct ColmPrintArgs printArgs = { collect, true, false, trim, &appendCollect,
&printNull, &printTermTree, &printNull };
printTreeArgs( prg, sp, &printArgs, tree );
}
-void printTreeFile( Program *prg, Tree **sp, FILE *out, Tree *tree )
+void printTreeFile( Program *prg, Tree **sp, FILE *out, Tree *tree, int trim )
{
- struct ColmPrintArgs printArgs = { out, 1, 0, &appendFile,
+ struct ColmPrintArgs printArgs = { out, true, false, trim, &appendFile,
&printNull, &printTermTree, &printNull };
printTreeArgs( prg, sp, &printArgs, tree );
}
-void printXmlStdout( Program *prg, Tree **sp, Tree *tree, int commAttr )
+void printXmlStdout( Program *prg, Tree **sp, Tree *tree, int commAttr, int trim )
{
- struct ColmPrintArgs printArgs = { stdout, commAttr, commAttr, &appendFile,
+ struct ColmPrintArgs printArgs = { stdout, commAttr, commAttr, trim, &appendFile,
&openTreeXml, &printTermXml, &closeTreeXml };
printTreeArgs( prg, sp, &printArgs, tree );
}
diff --git a/colm/tree.h b/colm/tree.h
index e897b07f..4425cfc5 100644
--- a/colm/tree.h
+++ b/colm/tree.h
@@ -328,9 +328,6 @@ Tree *treeRevIterPrevChild( struct ColmProgram *prg, Tree ***psp, RevTreeIter *i
Tree *treeIterNextRepeat( struct ColmProgram *prg, Tree ***psp, TreeIter *iter );
Tree *treeIterPrevRepeat( struct ColmProgram *prg, Tree ***psp, TreeIter *iter );
-void printXmlStdout( struct ColmProgram *prg, Tree **sp, Tree *tree, int commAttr );
-
-
/* An automatically grown buffer for collecting tokens. Always reuses space;
* never down resizes. */
typedef struct _StrCollect
@@ -346,10 +343,9 @@ void strCollectAppend( StrCollect *collect, const char *data, long len );
void strCollectClear( StrCollect *collect );
Tree *treeTrim( struct ColmProgram *prg, Tree **sp, Tree *tree );
-void printTree( struct ColmProgram *prg, Tree **sp, StrCollect *collect, Tree *tree );
-void printTermTree( struct ColmProgram *prg, Tree **sp, struct ColmPrintArgs *printArgs, Kid *kid );
-void printTreeCollect( struct ColmProgram *prg, Tree **sp, StrCollect *collect, Tree *tree );
-void printTreeFile( struct ColmProgram *prg, Tree **sp, FILE *out, Tree *tree );
+void printTreeCollect( struct ColmProgram *prg, Tree **sp, StrCollect *collect, Tree *tree, int trim );
+void printTreeFile( struct ColmProgram *prg, Tree **sp, FILE *out, Tree *tree, int trim );
+void printXmlStdout( struct ColmProgram *prg, Tree **sp, Tree *tree, int commAttr, int trim );
#if defined(__cplusplus)
}
diff --git a/test/accum1.lm b/test/accum1.lm
index 716bb20a..186125a3 100644
--- a/test/accum1.lm
+++ b/test/accum1.lm
@@ -23,4 +23,4 @@ for Id: id in Input {
S: start = Output()
-print( S )
+print( S '\n' )
diff --git a/test/accum2.lm b/test/accum2.lm
index b17b8557..e1eac4d3 100644
--- a/test/accum2.lm
+++ b/test/accum2.lm
@@ -30,4 +30,4 @@ cons SP: parser<ctx::start> []
SP.ctx = cons ctx []
send SP stdin
Input: ctx::start = SP()
-print( Input )
+print( Input '\n' )
diff --git a/test/context1.lm b/test/context1.lm
index 6f585031..6f703956 100644
--- a/test/context1.lm
+++ b/test/context1.lm
@@ -30,4 +30,4 @@ context ctx
CTX: ctx = cons ctx[]
Input: ctx::start = parse ctx::start( CTX stdin )
-print( Input )
+print( Input '\n' )
diff --git a/test/func.lm b/test/func.lm
index 52f6f3c5..61665140 100644
--- a/test/func.lm
+++ b/test/func.lm
@@ -27,7 +27,7 @@ int main()
InputFile: stream = open( 'func.in' "r" )
P: program = parse program( InputFile )
func( P )
- print( P )
+ print( P '\n' )
}
main()
diff --git a/test/ignore1.lm b/test/ignore1.lm
index ebf7413f..2922116d 100644
--- a/test/ignore1.lm
+++ b/test/ignore1.lm
@@ -45,9 +45,8 @@ def item_list
parse Attrs: attr_list( stdin )
-print( Attrs '\n' )
+print( %Attrs '\n' )
-## Reconstruct the left hand side with the
construct IL: item_list
["<wrapper .[Attrs]. ></wrapper>"]
diff --git a/test/ignore2.lm b/test/ignore2.lm
index 716bb20a..186125a3 100644
--- a/test/ignore2.lm
+++ b/test/ignore2.lm
@@ -23,4 +23,4 @@ for Id: id in Input {
S: start = Output()
-print( S )
+print( S '\n' )
diff --git a/test/order2.lm b/test/order2.lm
index 33458524..8f45a17f 100644
--- a/test/order2.lm
+++ b/test/order2.lm
@@ -91,6 +91,6 @@ match P [C: c '%%' protocol*]
Output << [%C]
Output <<
"#include <assert.h>
- "
+Output << '\n\n'
-print( Output.finish() )
+print( Output.finish() '\n\n' )
diff --git a/test/reparse.lm b/test/reparse.lm
index c6b6a95b..9b70b555 100644
--- a/test/reparse.lm
+++ b/test/reparse.lm
@@ -18,5 +18,5 @@ S: start = cons start[ Input ]
Again: start = parse start( Input )
-print( Again )
+print( %Again )
diff --git a/test/til.lm b/test/til.lm
index 3dfe8e5b..dba96c1d 100644
--- a/test/til.lm
+++ b/test/til.lm
@@ -160,9 +160,9 @@ for S: statement* in P
" [^First]
" [^Rest]
"while [^Expr];
- ^Following]
+ Following]
}
}
}
-print(P '\n')
+print( %P )
diff --git a/test/translate1.lm b/test/translate1.lm
index 472ef86a..b5c6ca9e 100644
--- a/test/translate1.lm
+++ b/test/translate1.lm
@@ -20,5 +20,5 @@ def start
[item*]
Input: start = parse start( stdin )
-print( Input )
+print( %Input )