summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2015-01-04 14:39:40 -0500
committerAdrian Thurston <thurston@complang.org>2015-01-04 14:39:40 -0500
commitb86d5b2fa8e4ab4773a0fdb252d5dd8356feec16 (patch)
tree2b80ce329e7c67d3434674ba7259d0289c8b61e9 /src
parent7f6c8260b492b5bbc4213e22b98c8aa7a3a437ab (diff)
downloadcolm-b86d5b2fa8e4ab4773a0fdb252d5dd8356feec16.tar.gz
more rough work on object-based lists
Diffstat (limited to 'src')
-rw-r--r--src/bytecode.c15
-rw-r--r--src/bytecode.h2
-rw-r--r--src/declare.cc19
-rw-r--r--src/loadinit.cc2
-rw-r--r--src/pdarun.h1
-rw-r--r--src/resolve.cc4
-rw-r--r--src/struct.c41
-rw-r--r--src/struct.h12
-rw-r--r--src/synthesis.cc8
-rw-r--r--src/tree.c26
-rw-r--r--src/tree.h9
11 files changed, 87 insertions, 52 deletions
diff --git a/src/bytecode.c b/src/bytecode.c
index b561b306..83f4eda0 100644
--- a/src/bytecode.c
+++ b/src/bytecode.c
@@ -3062,20 +3062,16 @@ again:
vm_push( prg->trueVal );
break;
}
- case IN_GET_LIST2EL_MEM_R: {
+ case IN_GET_LIST_EL_MEM_R: {
short field;
read_half( field );
- debug( prg, REALM_BYTECODE, "IN_GET_LIST2EL_MEM_R\n" );
+ debug( prg, REALM_BYTECODE, "IN_GET_LIST_EL_MEM_R\n" );
Tree *obj = vm_pop();
-
- Tree *val = colm_get_attr( obj, 1 );
-
- treeUpref( val );
+ Tree *val = colm_list_el_get( (ListEl*)obj, field );
vm_push( val );
break;
-
}
case IN_LIST_POP_TAIL_WC: {
debug( prg, REALM_BYTECODE, "IN_LIST_POP_TAIL_WC\n" );
@@ -3202,10 +3198,7 @@ again:
debug( prg, REALM_BYTECODE, "IN_GET_LIST_MEM_R\n" );
Tree *obj = vm_pop();
- treeDownref( prg, sp, obj );
-
- Tree *val = getListMem( (List*)obj, field );
- treeUpref( val );
+ Tree *val = colm_list_get( (List*)obj, field );
vm_push( val );
break;
}
diff --git a/src/bytecode.h b/src/bytecode.h
index 64091880..246c5d41 100644
--- a/src/bytecode.h
+++ b/src/bytecode.h
@@ -179,7 +179,7 @@ typedef unsigned char uchar;
#define IN_LIST2_PUSH_TAIL_WV 0xf2
#define IN_LIST2_PUSH_TAIL_WC 0xf3
#define IN_LIST2_PUSH_TAIL_BKT 0xf4
-#define IN_GET_LIST2EL_MEM_R 0xf5
+#define IN_GET_LIST_EL_MEM_R 0xf5
#define IN_GET_LIST2_MEM_R 0xf6
#define IN_GET_LIST_MEM_R 0x79
diff --git a/src/declare.cc b/src/declare.cc
index 4014c729..f6c9d87c 100644
--- a/src/declare.cc
+++ b/src/declare.cc
@@ -927,12 +927,17 @@ void Compiler::initListFunctions( GenericType *gen )
void Compiler::initListElField( GenericType *gen, const char *name, int offset )
{
+ TypeRef *typeRef = TypeRef::cons(
+ internal, TypeRef::ListEl, 0, gen->typeArg, 0 );
+
+ typeRef->resolveType( this );
+
/* Make the type ref and create the field. */
- TypeRef *typeRef = TypeRef::cons( internal, gen->utArg );
ObjectField *el = ObjectField::cons( internal,
ObjectField::InbuiltOffType, typeRef, name );
- el->inGetR = IN_GET_LIST2EL_MEM_R;
+ el->inGetR = IN_GET_LIST_EL_MEM_R;
+ el->inGetValR = IN_GET_LIST_EL_MEM_R;
// el->inGetWC = IN_GET_LIST2EL_MEM_WC;
// el->inGetWV = IN_GET_LIST2EL_MEM_WV;
// el->inSetWC = IN_SET_LIST2EL_MEM_WC;
@@ -948,12 +953,18 @@ void Compiler::initListElField( GenericType *gen, const char *name, int offset )
void Compiler::initListElFields( GenericType *gen )
{
initListElField( gen, "next", 0 );
+ initListElField( gen, "value", 2 );
}
void Compiler::initListField( GenericType *gen, const char *name, int offset )
{
+ /* Type reference for the list element. */
+ TypeRef *typeRef = TypeRef::cons(
+ internal, TypeRef::ListEl, 0, gen->typeArg, 0 );
+
+ typeRef->resolveType( this );
+
/* Make the type ref and create the field. */
- TypeRef *typeRef = TypeRef::cons( internal, gen->utArg );
ObjectField *el = ObjectField::cons( internal,
ObjectField::InbuiltOffType, typeRef, name );
@@ -963,6 +974,8 @@ void Compiler::initListField( GenericType *gen, const char *name, int offset )
el->inSetWC = IN_SET_LIST_MEM_WC;
el->inSetWV = IN_SET_LIST_MEM_WV;
+ el->inGetValR = IN_GET_LIST_MEM_R;
+
gen->objDef->rootScope->insertField( el->name, el );
/* Zero for head, One for tail. */
diff --git a/src/loadinit.cc b/src/loadinit.cc
index aba62146..07b78701 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, 0x3 );
+ colm_set_debug( program, 0 );
colm_run_program( program, 2, argv );
/* Extract the parse tree. */
diff --git a/src/pdarun.h b/src/pdarun.h
index 6312e249..14f1ffc9 100644
--- a/src/pdarun.h
+++ b/src/pdarun.h
@@ -8,6 +8,7 @@
#include <colm/input.h>
#include <colm/defs.h>
#include <colm/tree.h>
+#include <colm/struct.h>
#ifdef __cplusplus
extern "C" {
diff --git a/src/resolve.cc b/src/resolve.cc
index 59a12f0a..7dda501c 100644
--- a/src/resolve.cc
+++ b/src/resolve.cc
@@ -146,10 +146,10 @@ UniqueType *TypeRef::resolveTypeListEl( Compiler *pd )
pd->nextGenericId++, typeRef1 );
nspace->genericList.append( generic );
+ inMap->generic = generic;
+ /* Note this is recurse, all of above must complete. */
generic->declare( pd, nspace );
-
- inMap->generic = generic;
}
generic = inMap->generic;
diff --git a/src/struct.c b/src/struct.c
index 3386db14..1667b864 100644
--- a/src/struct.c
+++ b/src/struct.c
@@ -112,7 +112,7 @@ void colm_list_destroy( Program *prg, Tree **sp, struct colm_struct *s )
while ( el != 0 ) {
ListEl *next = el->next;
treeDownref( prg, sp, el->value );
- listElFree( prg, el );
+ //listElFree( prg, el );
el = next;
}
}
@@ -151,3 +151,42 @@ Map *colm_map_new( struct colm_program *prg )
map->id = STRUCT_INBUILT_ID;
return map;
}
+
+Tree *colm_list_get( List *list, Word field )
+{
+ Tree *result = 0;
+ switch ( field ) {
+ case 0:
+ result = list->head;
+ break;
+ case 1:
+ result = list->tail;
+ break;
+ default:
+ assert( 0 );
+ break;
+ }
+ return result;
+}
+
+Tree *colm_list_el_get( ListEl *listEl, Word field )
+{
+ Tree *result = 0;
+ switch ( field ) {
+ case 0:
+ result = listEl->prev;
+ break;
+ case 1:
+ result = listEl->next;
+ break;
+ case 2:
+ result = listEl->value;
+ treeUpref( result );
+ break;
+// default:
+// assert( false );
+// break;
+ }
+ return result;
+}
+
diff --git a/src/struct.h b/src/struct.h
index c6f6d0b1..284cbbbd 100644
--- a/src/struct.h
+++ b/src/struct.h
@@ -52,6 +52,14 @@ typedef struct colm_stream
StreamImpl *impl;
} Stream;
+typedef struct colm_list_el
+{
+ void *buffer[8];
+ Tree *value;
+ struct colm_list_el *next;
+ struct colm_list_el *prev;
+} ListEl;
+
/* Must overlay colm_inbuilt. */
typedef struct colm_list
{
@@ -116,7 +124,11 @@ struct colm_struct *colm_struct_inbuilt( struct colm_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 );
+
List *colm_list_new( struct colm_program *prg );
+Tree *colm_list_get( List *list, Word field );
+Tree *colm_list_el_get( ListEl *listEl, Word field );
+
Map *colm_map_new( struct colm_program *prg );
#if defined(__cplusplus)
diff --git a/src/synthesis.cc b/src/synthesis.cc
index c3098cc6..99762863 100644
--- a/src/synthesis.cc
+++ b/src/synthesis.cc
@@ -276,7 +276,7 @@ UniqueType *LangVarRef::loadField( Compiler *pd, CodeVect &code,
UniqueType *elUT = el->typeRef->uniqueType;
- if ( elUT->typeId == TYPE_STRUCT ) {
+ if ( elUT->typeId == TYPE_STRUCT || elUT->typeId == TYPE_GENERIC ) {
code.append( el->inGetValR );
}
else {
@@ -587,7 +587,7 @@ void LangVarRef::setField( Compiler *pd, CodeVect &code,
/* Ensure that the field is referenced. */
inObject->referenceField( pd, el );
- if ( exprUT->typeId == TYPE_STRUCT ) {
+ if ( exprUT->typeId == TYPE_STRUCT || exprUT->typeId == TYPE_GENERIC ) {
if ( pd->revertOn && revert )
code.append( el->inSetValWV );
else
@@ -1077,8 +1077,8 @@ UniqueType *LangTerm::evaluateNew( Compiler *pd, CodeVect &code ) const
/* What is being newstructed. */
UniqueType *replUT = typeRef->uniqueType;
- if ( replUT->typeId != TYPE_STRUCT )
- error(loc) << "can only new a struct" << endp;
+ if ( replUT->typeId != TYPE_STRUCT && replUT->typeId != TYPE_GENERIC )
+ error(loc) << "can only new a struct or generic" << endp;
code.append( IN_NEW_STRUCT );
code.appendHalf( replUT->structEl->id );
diff --git a/src/tree.c b/src/tree.c
index 4e532022..f985b7f9 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -22,8 +22,10 @@
#define BUFFER_INITIAL_SIZE 4096
-void listPrepend( List *list, ListEl *newEl ) { listAddBefore(list, list->head, newEl ); }
-void listAppend( List *list, ListEl *newEl ) { listAddAfter(list, list->tail, newEl ); }
+void listPrepend( List *list, ListEl *newEl )
+ { listAddBefore( list, list->head, newEl ); }
+void listAppend( List *list, ListEl *newEl )
+ { listAddAfter( list, list->tail, newEl ); }
ListEl *listDetach( List *list, ListEl *el );
ListEl *listDetachFirst(List *list ) { return listDetach(list, list->head); }
@@ -1570,7 +1572,8 @@ void listPushTail( Program *prg, List *list, Tree *val )
{
if ( val != 0 )
assert( val->refs >= 1 );
- ListEl *listEl = listElAllocate( prg );
+ ListEl *listEl = malloc(sizeof(ListEl));
+ memset( listEl, 0, sizeof(ListEl));
listEl->value = val;
listAppend( list, listEl );
}
@@ -1641,23 +1644,6 @@ Tree *listRemoveHead( Program *prg, List *list )
return tree;
}
-Tree *getListMem( List *list, Word field )
-{
- Tree *result = 0;
- switch ( field ) {
- case 0:
- result = list->head->value;
- break;
- case 1:
- result = list->tail->value;
- break;
- default:
- assert( false );
- break;
- }
- return result;
-}
-
Tree *getParserMem( Parser *parser, Word field )
{
Tree *result = 0;
diff --git a/src/tree.h b/src/tree.h
index 1204c011..f1299bcd 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -116,14 +116,6 @@ typedef struct _Str
Head *value;
} Str;
-typedef struct _ListEl
-{
- /* Must overlay kid. */
- Tree *value;
- struct _ListEl *next;
- struct _ListEl *prev;
-} ListEl;
-
/*
* Maps
*/
@@ -266,7 +258,6 @@ void listPushHead( struct colm_program *prg, List *list, Tree *val );
void list2PushTail( struct colm_program *prg, Tree **sp, List *list, Tree *val );
Tree *listRemoveEnd( struct colm_program *prg, List *list );
Tree *listRemoveHead( struct colm_program *prg, List *list );
-Tree *getListMem( List *list, Word field );
Tree *getListMemSplit( struct colm_program *prg, List *list, Word field );
Tree *getParserMem( Parser *parser, Word field );