diff options
author | Adrian Thurston <thurston@complang.org> | 2012-12-30 10:01:27 -0500 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2012-12-30 10:01:27 -0500 |
commit | 7db01e426f3d5231bdfb1ec46bbed32981e1e62f (patch) | |
tree | 3b17eda19de8e89cd06973f6a490612d2cb7e12d | |
parent | 6fb0f6702bd45d485c317ce63bf84022bb1589f5 (diff) | |
download | colm-7db01e426f3d5231bdfb1ec46bbed32981e1e62f.tar.gz |
implemented stdout and stderr
-rw-r--r-- | colm/bytecode.c | 35 | ||||
-rw-r--r-- | colm/keyops.h | 10 | ||||
-rw-r--r-- | colm/synthesis.cc | 2 | ||||
-rw-r--r-- | colm/tree.c | 17 | ||||
-rw-r--r-- | colm/tree.h | 2 |
5 files changed, 53 insertions, 13 deletions
diff --git a/colm/bytecode.c b/colm/bytecode.c index 34b3f622..cb4d50e0 100644 --- a/colm/bytecode.c +++ b/colm/bytecode.c @@ -946,7 +946,10 @@ again: Stream *stream = (Stream*)vm_pop(); while ( n-- > 0 ) { Tree *tree = vm_pop(); - printTreeFile( prg, sp, stream->file, tree, true ); + if ( stream->file != 0 ) + printTreeFile( prg, sp, stream->file, tree, true ); + else + printTreeFd( prg, sp, stream->fd, tree, true ); treeDownref( prg, sp, tree ); } treeDownref( prg, sp, (Tree*)stream ); @@ -3492,6 +3495,36 @@ again: vm_push( (Tree*)prg->stdinVal ); break; } + case IN_GET_STDOUT: { + debug( REALM_BYTECODE, "IN_GET_STDOUT\n" ); + + /* Pop the root object. */ + Tree *obj = vm_pop(); + treeDownref( prg, sp, obj ); + if ( prg->stdoutVal == 0 ) { + prg->stdoutVal = openStreamFd( prg, 1 ); + treeUpref( (Tree*)prg->stdoutVal ); + } + + treeUpref( (Tree*)prg->stdoutVal ); + vm_push( (Tree*)prg->stdoutVal ); + break; + } + case IN_GET_STDERR: { + debug( REALM_BYTECODE, "IN_GET_STDERR\n" ); + + /* Pop the root object. */ + Tree *obj = vm_pop(); + treeDownref( prg, sp, obj ); + if ( prg->stderrVal == 0 ) { + prg->stderrVal = openStreamFd( prg, 2 ); + treeUpref( (Tree*)prg->stderrVal ); + } + + treeUpref( (Tree*)prg->stderrVal ); + vm_push( (Tree*)prg->stderrVal ); + break; + } case IN_LOAD_ARGV: { Half field; read_half( field ); diff --git a/colm/keyops.h b/colm/keyops.h index de0e0c06..a79881e7 100644 --- a/colm/keyops.h +++ b/colm/keyops.h @@ -103,16 +103,6 @@ struct HostLang bool explicitUnsigned; }; - -/* Target language. */ -enum HostLangType -{ - CCode, - DCode, - JavaCode, - RubyCode -}; - extern HostLang *hostLang; extern HostLang hostLangC; diff --git a/colm/synthesis.cc b/colm/synthesis.cc index f24516c2..13d54dc6 100644 --- a/colm/synthesis.cc +++ b/colm/synthesis.cc @@ -3294,7 +3294,7 @@ void Compiler::addStdout() TypeRef *typeRef = TypeRef::cons( internal, uniqueTypeStr ); /* Create the field and insert it into the map. */ - ObjField *el = new ObjField( internal, typeRef, "stout" ); + ObjField *el = new ObjField( internal, typeRef, "stdout" ); el->beenReferenced = true; el->beenInitialized = true; el->isConst = true; diff --git a/colm/tree.c b/colm/tree.c index 769a7b97..93a529d8 100644 --- a/colm/tree.c +++ b/colm/tree.c @@ -27,6 +27,8 @@ #include <colm/map.h> #include <string.h> #include <stdlib.h> +#include <stdio.h> +#include <unistd.h> #include <assert.h> #define true 1 @@ -242,6 +244,7 @@ Stream *openStreamFd( Program *prg, long fd ) { Stream *res = (Stream*)mapElAllocate( prg ); res->id = LEL_ID_STREAM; + res->fd = fd; res->in = newSourceStreamFd( fd ); initSourceStream( res->in ); return res; @@ -2052,7 +2055,12 @@ void appendCollect( struct ColmPrintArgs *args, const char *data, int length ) void appendFile( struct ColmPrintArgs *args, const char *data, int length ) { - fwrite( data, length, 1, (FILE*)args->arg ); + fwrite( data, 1, length, (FILE*)args->arg ); +} + +void appendFd( struct ColmPrintArgs *args, const char *data, int length ) +{ + write( (long)args->arg, data, length ); } Tree *treeTrim( struct ColmProgram *prg, Tree **sp, Tree *tree ) @@ -2476,6 +2484,13 @@ void printTreeFile( Program *prg, Tree **sp, FILE *out, Tree *tree, int trim ) printTreeArgs( prg, sp, &printArgs, tree ); } +void printTreeFd( Program *prg, Tree **sp, int fd, Tree *tree, int trim ) +{ + struct ColmPrintArgs printArgs = { (void*)((long)fd), true, false, trim, &appendFd, + &printNull, &printTermTree, &printNull }; + printTreeArgs( prg, sp, &printArgs, tree ); +} + void printXmlStdout( Program *prg, Tree **sp, Tree *tree, int commAttr, int trim ) { struct ColmPrintArgs printArgs = { stdout, commAttr, commAttr, trim, &appendFile, diff --git a/colm/tree.h b/colm/tree.h index 30b06762..828bab36 100644 --- a/colm/tree.h +++ b/colm/tree.h @@ -190,6 +190,7 @@ typedef struct _Stream Kid *child; FILE *file; + int fd; SourceStream *in; } Stream; @@ -348,6 +349,7 @@ Tree *treeTrim( struct ColmProgram *prg, Tree **sp, 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 printTreeFd( struct ColmProgram *prg, Tree **sp, int fd, Tree *tree, int trim ); void printXmlStdout( struct ColmProgram *prg, Tree **sp, Tree *tree, int commAttr, int trim ); /* |