diff options
author | Adrian Thurston <thurston@complang.org> | 2015-01-02 13:54:40 -0500 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2015-01-02 13:54:40 -0500 |
commit | 3a89385a6495b9012342af527615f3a6b2be5f94 (patch) | |
tree | 0e13ad873f9be4fe0f971ba067d85421f5a5a5cf /src | |
parent | c8bf7e7cba2b61c0b87d0192ae5a5d3868b6d857 (diff) | |
download | colm-3a89385a6495b9012342af527615f3a6b2be5f94.tar.gz |
start using stream and parser structs
Diffstat (limited to 'src')
-rw-r--r-- | src/bytecode.c | 43 | ||||
-rw-r--r-- | src/input.c | 22 | ||||
-rw-r--r-- | src/loadinit.cc | 2 | ||||
-rw-r--r-- | src/program.c | 2 | ||||
-rw-r--r-- | src/struct.c | 69 | ||||
-rw-r--r-- | src/struct.h | 6 | ||||
-rw-r--r-- | src/synthesis.cc | 3 | ||||
-rw-r--r-- | src/tree.c | 6 |
8 files changed, 77 insertions, 76 deletions
diff --git a/src/bytecode.c b/src/bytecode.c index 4fa5d96b..861abad8 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -2069,7 +2069,7 @@ again: StreamImpl *si = streamToImpl( (Stream*)sptr ); streamAppend( prg, sp, input, si ); - vm_push( (Tree*)sptr ); + //vm_push( (Tree*)sptr ); treeDownref( prg, sp, input ); break; } @@ -2082,8 +2082,8 @@ again: StreamImpl *si = streamToImpl( (Stream*)sptr ); Word len = streamAppend( prg, sp, input, si ); - treeUpref( (Tree*)sptr ); - vm_push( (Tree*)sptr ); +// treeUpref( (Tree*)sptr ); +// vm_push( (Tree*)sptr ); rcodeUnitStart( exec ); rcodeCode( exec, IN_INPUT_APPEND_BKT ); @@ -2121,8 +2121,8 @@ again: StreamImpl *si = streamToImpl( (Stream*)sptr ); streamAppendStream( prg, sp, si, input ); - vm_push( (Tree*)sptr ); - treeDownref( prg, sp, input ); + //vm_push( (Tree*)sptr ); + //treeDownref( prg, sp, input ); break; } case IN_INPUT_APPEND_STREAM_WV: { @@ -2134,8 +2134,8 @@ again: StreamImpl *si = streamToImpl( (Stream*)sptr ); Word len = streamAppendStream( prg, sp, si, input ); - treeUpref( (Tree*)sptr ); - vm_push( (Tree*)sptr ); + //treeUpref( (Tree*)sptr ); + //vm_push( (Tree*)sptr ); rcodeUnitStart( exec ); rcodeCode( exec, IN_INPUT_APPEND_STREAM_BKT ); @@ -2204,8 +2204,8 @@ again: case IN_PARSE_SAVE_STEPS: { debug( prg, REALM_BYTECODE, "IN_PARSE_SAVE_STEPS\n" ); - Struct *parser = (Struct*)vm_pop(); - PdaRun *pdaRun = colm_struct_get_field_type( parser, PdaRun*, 6 ); + Parser *parser = (Parser*)vm_pop(); + PdaRun *pdaRun = parser->pdaRun; long steps = pdaRun->steps; vm_push( (SW)exec->parser ); @@ -2297,10 +2297,8 @@ again: debug( prg, REALM_BYTECODE, "IN_PARSE_FRAG_WC %hd\n", stopId ); - - PdaRun *pdaRun = colm_struct_get_field_type( exec->parser, PdaRun *, 6 ); - Stream *input = colm_struct_get_field_type( exec->parser, Stream *, 7 ); - exec->pcr = parseFrag( prg, sp, pdaRun, input, stopId, exec->pcr ); + exec->pcr = parseFrag( prg, sp, exec->parser->pdaRun, + exec->parser->input, stopId, exec->pcr ); /* If done, jump to the terminating instruction, otherwise fall * through to call some code, then jump back here. */ @@ -2332,10 +2330,8 @@ again: debug( prg, REALM_BYTECODE, "IN_PARSE_FRAG_WV %hd\n", stopId ); - PdaRun *pdaRun = colm_struct_get_field_type( exec->parser, PdaRun *, 6 ); - Stream *input = colm_struct_get_field_type( exec->parser, Stream *, 7 ); - - exec->pcr = parseFrag( prg, sp, pdaRun, input, stopId, exec->pcr ); + exec->pcr = parseFrag( prg, sp, exec->parser->pdaRun, + exec->parser->input, stopId, exec->pcr ); /* If done, jump to the terminating instruction, otherwise fall * through to call some code, then jump back here. */ @@ -2405,13 +2401,12 @@ again: debug( prg, REALM_BYTECODE, "IN_PARSE_FINISH_WC %hd\n", stopId ); - PdaRun *pdaRun = colm_struct_get_field_type( exec->parser, PdaRun *, 6 ); - Stream *input = colm_struct_get_field_type( exec->parser, Stream *, 7 ); Tree *result = 0; - exec->pcr = parseFinish( &result, prg, sp, pdaRun, input, false, exec->pcr ); + exec->pcr = parseFinish( &result, prg, sp, + exec->parser->pdaRun, exec->parser->input, false, exec->pcr ); - colm_struct_set_field_type( exec->parser, Tree*, 8, result ); + exec->parser->result = result; /* If done, jump to the terminating instruction, otherwise fall * through to call some code, then jump back here. */ @@ -2659,8 +2654,7 @@ again: debug( prg, REALM_BYTECODE, "IN_GET_INPUT\n" ); Parser *parser = (Parser*)vm_pop(); - Stream *stream = colm_struct_get_field_type( - (struct colm_struct *)parser, Stream*, 7 ); + Stream *stream = parser->input; vm_push( (Tree*)stream ); //treeDownref( prg, sp, (Tree*)parser ); @@ -2671,8 +2665,7 @@ again: Parser *parser = (Parser*)vm_pop(); Stream *stream = (Stream*)vm_pop(); - colm_struct_set_field_type( (struct colm_struct *)parser, - Stream*, 7, stream ); + parser->input = stream; break; } case IN_CONSTRUCT_TERM: { diff --git a/src/input.c b/src/input.c index e4dfee4c..5d138636 100644 --- a/src/input.c +++ b/src/input.c @@ -1013,18 +1013,18 @@ Stream *openStreamFile( Program *prg, char *name, FILE *file ) { StreamImpl *impl = newSourceStreamFile( name, file ); - struct colm_struct *s = colm_struct_inbuilt( prg, 16, 0 ); - colm_struct_set_field( s, 15, (Tree*)impl ); - return (Stream*) s; + struct colm_stream *s = colm_stream_new2( prg ); + s->impl = impl; + return s; } Stream *openStreamFd( Program *prg, char *name, long fd ) { StreamImpl *impl = newSourceStreamFd( name, fd ); - struct colm_struct *s = colm_struct_inbuilt( prg, 16, 0 ); - colm_struct_set_field( s, 15, (Tree*)impl ); - return (Stream*) s; + struct colm_stream *s = colm_stream_new2( prg ); + s->impl = impl; + return s; } Stream *openFile( Program *prg, Tree *name, Tree *mode ) @@ -1060,17 +1060,17 @@ Stream *colm_stream_new( Program *prg ) { StreamImpl *impl = newSourceStreamGeneric( "<internal>" ); - struct colm_struct *s = colm_struct_inbuilt( prg, 16, 0 ); - colm_struct_set_field( s, 15, (Tree*)impl ); - return (Stream*) s; + struct colm_stream *stream = colm_stream_new2( prg ); + stream->impl = impl; + return stream; } StreamImpl *colm_stream_impl( struct colm_struct *s ) { - return (StreamImpl*) colm_struct_get_field( s, 15 ); + return ((Stream*)s)->impl; } StreamImpl *streamToImpl( Stream *ptr ) { - return (StreamImpl*) colm_struct_get_field( ((struct colm_struct*)ptr), 15 ); + return ptr->impl; } diff --git a/src/loadinit.cc b/src/loadinit.cc index 07b78701..aba62146 100644 --- a/src/loadinit.cc +++ b/src/loadinit.cc @@ -347,7 +347,7 @@ void LoadInit::go( long activeRealm ) argv[2] = 0; colm_program *program = colm_new_program( &colm_object ); - colm_set_debug( program, 0 ); + colm_set_debug( program, 0x3 ); colm_run_program( program, 2, argv ); /* Extract the parse tree. */ diff --git a/src/program.c b/src/program.c index f5c83a51..4824fa55 100644 --- a/src/program.c +++ b/src/program.c @@ -161,8 +161,6 @@ Program *colm_new_program( RuntimeData *rtd ) assert( sizeof(Pointer) <= sizeof(Tree) ); assert( sizeof(Map) <= sizeof(MapEl) ); assert( sizeof(List) <= sizeof(MapEl) ); - assert( sizeof(Stream) <= sizeof(MapEl) ); - assert( sizeof(Parser) <= sizeof(MapEl) ); prg->rtd = rtd; prg->ctxDepParsing = 1; diff --git a/src/struct.c b/src/struct.c index f17ce779..2682f9dd 100644 --- a/src/struct.c +++ b/src/struct.c @@ -3,18 +3,17 @@ #include <stdlib.h> #include <string.h> +#include <assert.h> + +#define STRUCT_INBUILT_ID -1 struct colm_tree *colm_get_global( Program *prg, long pos ) { return colm_struct_get_field( prg->global, pos ); } -static struct colm_struct *colm_struct_new_size( Program *prg, int size ) +static void colm_struct_add( Program *prg, struct colm_struct *item ) { - size_t memsize = sizeof(struct colm_struct) + ( sizeof(Tree*) * size ); - struct colm_struct *item = (struct colm_struct*) malloc( memsize ); - memset( item, 0, memsize ); - if ( prg->heap.head == 0 ) { prg->heap.head = prg->heap.tail = item; item->prev = item->next = 0; @@ -25,7 +24,15 @@ static struct colm_struct *colm_struct_new_size( Program *prg, int size ) prg->heap.tail->next = item; prg->heap.tail = item; } - +} + +static struct colm_struct *colm_struct_new_size( Program *prg, int size ) +{ + size_t memsize = sizeof(struct colm_struct) + ( sizeof(Tree*) * size ); + struct colm_struct *item = (struct colm_struct*) malloc( memsize ); + memset( item, 0, memsize ); + + colm_struct_add( prg, item ); return item; } @@ -36,21 +43,10 @@ struct colm_struct *colm_struct_new( Program *prg, int id ) return s; } -struct colm_struct *colm_struct_inbuilt( Program *prg, int size, - colm_destructor_t destructor ) -{ - struct colm_struct *s = colm_struct_new_size( prg, size + 1 ); - s->id = -1; - colm_struct_set_field_type( s, colm_destructor_t, 0, destructor ); - return s; -} - void colm_struct_delete( Program *prg, Tree **sp, struct colm_struct *el ) { - if ( el->id == -1 ) { - colm_destructor_t destructor = colm_struct_get_field_type( - el, colm_destructor_t, 0 ); - + if ( el->id == STRUCT_INBUILT_ID ) { + colm_destructor_t destructor = ((struct colm_inbuilt*)el)->destructor; if ( destructor != 0 ) (*destructor)( prg, sp, el ); } @@ -66,16 +62,16 @@ void colm_struct_delete( Program *prg, Tree **sp, struct colm_struct *el ) free( el ); } -void colm_parser_destroy( Program *prg, Tree **sp, struct colm_struct *parser ) +void colm_parser_destroy( Program *prg, Tree **sp, struct colm_struct *s ) { + struct colm_parser *parser = (struct colm_parser*) s; + /* Free the PDA run. */ - PdaRun *pdaRun = colm_struct_get_field_type( parser, PdaRun *, 6 ); - clearPdaRun( prg, sp, pdaRun ); - free( pdaRun ); + clearPdaRun( prg, sp, parser->pdaRun ); + free( parser->pdaRun ); - /* Free the result. */ - Tree *result = colm_struct_get_field_type( parser, Tree *, 8 ); - treeDownref( prg, sp, result ); +// /* Free the result. */ +// treeDownref( prg, sp, parser->result ); } Parser *colm_parser_new( Program *prg, GenericInfo *gi ) @@ -86,9 +82,24 @@ Parser *colm_parser_new( Program *prg, GenericInfo *gi ) colm_pda_init( prg, pdaRun, prg->rtd->pdaTables, gi->parserId, 0, 0, 0 ); - struct colm_struct *s = colm_struct_inbuilt( prg, 16, colm_parser_destroy ); - colm_struct_set_field_type( s, PdaRun*, 6, pdaRun ); + size_t memsize = sizeof(struct colm_parser); + struct colm_parser *parser = (struct colm_parser*) malloc( memsize ); + memset( parser, 0, memsize ); + colm_struct_add( prg, (struct colm_struct*) parser ); - return (Parser*) s; + parser->id = STRUCT_INBUILT_ID; + parser->destructor = colm_parser_destroy; + parser->pdaRun = pdaRun; + + return parser; } +Stream *colm_stream_new2( Program *prg ) +{ + size_t memsize = sizeof(struct colm_stream); + struct colm_stream *stream = (struct colm_stream*) malloc( memsize ); + memset( stream, 0, memsize ); + colm_struct_add( prg, (struct colm_struct *)stream ); + stream->id = STRUCT_INBUILT_ID; + return stream; +} diff --git a/src/struct.h b/src/struct.h index b23dbce6..1034a007 100644 --- a/src/struct.h +++ b/src/struct.h @@ -31,6 +31,8 @@ typedef struct colm_parser struct colm_struct *prev, *next; colm_destructor_t destructor; + void *buffer[10]; + struct _PdaRun *pdaRun; struct colm_stream *input; Tree *result; @@ -44,10 +46,11 @@ typedef struct colm_stream struct colm_struct *prev, *next; colm_destructor_t destructor; + void *buffer[8]; + StreamImpl *impl; } Stream; - struct colm_struct *colm_struct_new( struct colm_program *prg, int id ); void colm_struct_delete( struct colm_program *prg, struct colm_tree **sp, struct colm_struct *el ); @@ -71,6 +74,7 @@ struct colm_struct *colm_struct_inbuilt( Program *prg, int size, Parser *colm_parser_new( struct colm_program *prg, GenericInfo *gi ); Stream *colm_stream_new( struct colm_program *prg ); +Stream *colm_stream_new2( struct colm_program *prg ); #if defined(__cplusplus) } diff --git a/src/synthesis.cc b/src/synthesis.cc index 111bd286..0abfa7c1 100644 --- a/src/synthesis.cc +++ b/src/synthesis.cc @@ -1371,7 +1371,6 @@ UniqueType *LangTerm::evaluateParse( Compiler *pd, CodeVect &code, else code.append( IN_INPUT_APPEND_WC ); } - code.append( IN_POP ); code.append( IN_DUP_TOP ); @@ -1559,8 +1558,6 @@ void LangTerm::evaluateSendParser( Compiler *pd, CodeVect &code, bool strings ) else code.append( IN_INPUT_APPEND_WC ); - code.append( IN_POP ); - code.append( IN_DUP_TOP ); parseFrag( pd, code, 0 ); @@ -1716,13 +1716,11 @@ Tree *getParserMem( Parser *parser, Word field ) Tree *result = 0; switch ( field ) { case 0: { - result = colm_struct_get_field_type( - (struct colm_struct*)parser, Tree *, 8 ); + result = parser->result; break; } case 1: { - PdaRun *pdaRun = colm_struct_get_field_type( - (struct colm_struct*)parser, PdaRun *, 6 ); + PdaRun *pdaRun = parser->pdaRun; result = pdaRun->parseErrorText; break; } |