summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2015-01-04 16:29:36 -0500
committerAdrian Thurston <thurston@complang.org>2015-01-04 16:29:36 -0500
commit4070e94fcedeffc25128c2f613ee6aec687eae72 (patch)
treead3b5335d5f9cc493c0f24cd50f22170f3768a76
parent07547abf01f117076f36587f462a68dae0959b11 (diff)
downloadcolm-4070e94fcedeffc25128c2f613ee6aec687eae72.tar.gz
push towards embedded list elements
-rw-r--r--src/colm.lm8
-rw-r--r--src/consinit.cc32
-rw-r--r--src/declare.cc4
-rw-r--r--src/loadcolm.cc66
-rw-r--r--src/loadinit.cc30
-rw-r--r--src/parser.cc57
-rw-r--r--src/parser.h3
-rw-r--r--src/pdabuild.cc3
-rw-r--r--src/resolve.cc17
-rw-r--r--src/struct.c2
-rw-r--r--src/struct.h8
-rw-r--r--src/synthesis.cc10
-rw-r--r--src/tree.c2
13 files changed, 207 insertions, 35 deletions
diff --git a/src/colm.lm b/src/colm.lm
index 268dd145..ba02621a 100644
--- a/src/colm.lm
+++ b/src/colm.lm
@@ -245,6 +245,12 @@ def pre_eof_def
def alias_def
[ALIAS id type_ref]
+def list_el_def
+ [LIST_EL id]
+
+def map_el_def
+ [MAP_EL id]
+
def struct_item
[context_var_def] :ContextVar commit
| [literal_def] :Literal commit
@@ -260,6 +266,8 @@ def struct_item
| [export_def] :Export commit
| [pre_eof_def] :PreEof commit
| [precedence_def] :Precedence commit
+| [list_el_def] :ListEl commit
+| [map_el_def] :MapEl commit
def export_def
[EXPORT var_def opt_def_init]
diff --git a/src/consinit.cc b/src/consinit.cc
index 5fa1a763..5b67b0e0 100644
--- a/src/consinit.cc
+++ b/src/consinit.cc
@@ -727,10 +727,6 @@ void ConsInit::startProd()
void ConsInit::parseInput( StmtList *stmtList )
{
- /* Parse the "start" def. */
- NamespaceQual *nspaceQual = NamespaceQual::cons( curNspace() );
- TypeRef *typeRef = TypeRef::cons( internal, nspaceQual, String("start"), RepeatNone );
-
/* Pop argv, this yields the file name . */
CallArgVect *popArgs = new CallArgVect;
QualItemVect *popQual = new QualItemVect;
@@ -740,18 +736,35 @@ void ConsInit::parseInput( StmtList *stmtList )
0, curLocalFrame->rootScope, popQual, String("pop") );
LangExpr *pop = LangExpr::cons( LangTerm::cons( InputLoc(), popRef, popArgs ) );
+ NamespaceQual *nspaceQual = NamespaceQual::cons( curNspace() );
+ TypeRef *typeRef = TypeRef::cons( internal, nspaceQual, "argv_el", RepeatNone );
+ ObjectField *objField = ObjectField::cons( internal,
+ ObjectField::UserLocalType, typeRef, "A" );
+
+ LangStmt *stmt = varDef( objField, pop, LangStmt::AssignType );
+ stmtList->append( stmt );
+
/* Construct a literal string 'r', for second arg to open. */
- ConsItem *modeConsItem = ConsItem::cons( internal, ConsItem::InputText, String("r") );
+ ConsItem *modeConsItem = ConsItem::cons( internal,
+ ConsItem::InputText, String("r") );
ConsItemList *modeCons = new ConsItemList;
modeCons->append( modeConsItem );
LangExpr *modeExpr = LangExpr::cons( LangTerm::cons( internal, modeCons ) );
+
+ /* Reference A->value */
+ QualItemVect *qual = new QualItemVect;
+ qual->append( QualItem( QualItem::Arrow, internal, "A" ) );
+ LangVarRef *varRef = LangVarRef::cons( internal, 0,
+ curLocalFrame->rootScope, qual, String("value") );
+ LangExpr *Avalue = LangExpr::cons( LangTerm::cons( internal,
+ LangTerm::VarRefType, varRef ) );
/* Call open. */
QualItemVect *openQual = new QualItemVect;
LangVarRef *openRef = LangVarRef::cons( internal,
0, curLocalFrame->rootScope, openQual, String("open") );
CallArgVect *openArgs = new CallArgVect;
- openArgs->append( new CallArg(pop) );
+ openArgs->append( new CallArg(Avalue) );
openArgs->append( new CallArg(modeExpr) );
LangExpr *open = LangExpr::cons( LangTerm::cons( InputLoc(), openRef, openArgs ) );
@@ -760,9 +773,14 @@ void ConsInit::parseInput( StmtList *stmtList )
ConsItemList *list = ConsItemList::cons( consItem );
/* Will capture the parser to "P" */
- ObjectField *objField = ObjectField::cons( internal,
+ objField = ObjectField::cons( internal,
ObjectField::UserLocalType, 0, String("P") );
+ /* Parse the "start" def. */
+ nspaceQual = NamespaceQual::cons( curNspace() );
+ typeRef = TypeRef::cons( internal, nspaceQual,
+ String("start"), RepeatNone );
+
/* Parse the above list. */
LangExpr *parseExpr = parseCmd( internal, false, false, objField, typeRef, 0, list );
LangStmt *parseStmt = LangStmt::cons( internal, LangStmt::ExprType, parseExpr );
diff --git a/src/declare.cc b/src/declare.cc
index f6c9d87c..940c85fb 100644
--- a/src/declare.cc
+++ b/src/declare.cc
@@ -927,6 +927,7 @@ void Compiler::initListFunctions( GenericType *gen )
void Compiler::initListElField( GenericType *gen, const char *name, int offset )
{
+#if 0
TypeRef *typeRef = TypeRef::cons(
internal, TypeRef::ListEl, 0, gen->typeArg, 0 );
@@ -947,6 +948,7 @@ void Compiler::initListElField( GenericType *gen, const char *name, int offset )
/* Zero for head, One for tail. */
el->offset = offset;
+#endif
}
@@ -958,6 +960,7 @@ void Compiler::initListElFields( GenericType *gen )
void Compiler::initListField( GenericType *gen, const char *name, int offset )
{
+#if 0
/* Type reference for the list element. */
TypeRef *typeRef = TypeRef::cons(
internal, TypeRef::ListEl, 0, gen->typeArg, 0 );
@@ -980,6 +983,7 @@ void Compiler::initListField( GenericType *gen, const char *name, int offset )
/* Zero for head, One for tail. */
el->offset = offset;
+#endif
}
void Compiler::initListFields( GenericType *gen )
diff --git a/src/loadcolm.cc b/src/loadcolm.cc
index 1546bdf9..807b4458 100644
--- a/src/loadcolm.cc
+++ b/src/loadcolm.cc
@@ -721,10 +721,48 @@ struct LoadColm
void walkPrecedenceDef( precedence_def precedenceDef )
{
PredType predType = walkPredType( precedenceDef.pred_type() );
- PredDeclList *predDeclList = walkPredTokenList( precedenceDef.pred_token_list() );
+ PredDeclList *predDeclList = walkPredTokenList(
+ precedenceDef.pred_token_list() );
precedenceStmt( predType, predDeclList );
}
+ void walkListElDef( list_el_def Def )
+ {
+ /*
+ * The unique type. This is a def with a single empty form.
+ */
+ String name = Def.id().data();
+ ObjectDef *objectDef = ObjectDef::cons( ObjectDef::UserType,
+ name, pd->nextObjectId++ );
+
+ LelDefList *defList = new LelDefList;
+
+ Production *prod = BaseParser::production( InputLoc(),
+ new ProdElList, String(), false, 0, 0 );
+ prodAppend( defList, prod );
+
+ NtDef *ntDef = NtDef::cons( name, curNspace(), curStruct(), false );
+ BaseParser::cflDef( ntDef, objectDef, defList );
+
+ /*
+ * List element with the same name as containing context.
+ */
+ NamespaceQual *nspaceQual = NamespaceQual::cons( curNspace() );
+ String id = curStruct()->objectDef->name;
+ RepeatType repeatType = RepeatNone;
+ TypeRef *objTr = TypeRef::cons( InputLoc(), nspaceQual, id, repeatType );
+ TypeRef *elTr = TypeRef::cons( InputLoc(), TypeRef::ListEl, 0, objTr, 0 );
+
+ ObjectField *of = ObjectField::cons( InputLoc(),
+ ObjectField::UserFieldType, elTr, name );
+ contextVarDef( InputLoc(), of );
+ }
+
+ void walkMapElDef( map_el_def Def )
+ {
+ }
+
+
StmtList *walkInclude( include Include )
{
String lit = "";
@@ -824,22 +862,22 @@ struct LoadColm
tr = TypeRef::cons( typeRef.loc(), TypeRef::List, 0, type, 0 );
break;
}
- case type_ref::ListEl: {
- TypeRef *type = walkTypeRef( typeRef._type_ref() );
- tr = TypeRef::cons( typeRef.loc(), TypeRef::ListEl, 0, type, 0 );
- break;
- }
+// case type_ref::ListEl: {
+// TypeRef *type = walkTypeRef( typeRef._type_ref() );
+// tr = TypeRef::cons( typeRef.loc(), TypeRef::ListEl, 0, type, 0 );
+// break;
+// }
case type_ref::Map: {
TypeRef *key = walkTypeRef( typeRef.MapKeyType() );
TypeRef *value = walkTypeRef( typeRef.MapValueType() );
tr = TypeRef::cons( typeRef.loc(), TypeRef::Map, 0, key, value );
break;
}
- case type_ref::MapEl: {
- TypeRef *type = walkTypeRef( typeRef._type_ref() );
- tr = TypeRef::cons( typeRef.loc(), TypeRef::MapEl, 0, type, 0 );
- break;
- }
+// case type_ref::MapEl: {
+// TypeRef *type = walkTypeRef( typeRef._type_ref() );
+// tr = TypeRef::cons( typeRef.loc(), TypeRef::MapEl, 0, type, 0 );
+// break;
+// }
case type_ref::Parser: {
TypeRef *type = walkTypeRef( typeRef._type_ref() );
tr = TypeRef::cons( typeRef.loc(), TypeRef::Parser, 0, type, 0 );
@@ -2189,6 +2227,12 @@ struct LoadColm
case struct_item::Precedence:
walkPrecedenceDef( structItem.precedence_def() );
break;
+ case struct_item::ListEl:
+ walkListElDef( structItem.list_el_def() );
+ break;
+ case struct_item::MapEl:
+ walkMapElDef( structItem.map_el_def() );
+ break;
}
}
diff --git a/src/loadinit.cc b/src/loadinit.cc
index 37a40d9c..a5d0c20e 100644
--- a/src/loadinit.cc
+++ b/src/loadinit.cc
@@ -262,11 +262,6 @@ void LoadInit::walkDefinition( item &define )
void LoadInit::consParseStmt( StmtList *stmtList )
{
- /* Parse the "start" def. */
- NamespaceQual *nspaceQual = NamespaceQual::cons( curNspace() );
- TypeRef *typeRef = TypeRef::cons( internal, nspaceQual,
- String("start"), RepeatNone );
-
/* Pop argv, this yields the file name . */
CallArgVect *popArgs = new CallArgVect;
QualItemVect *popQual = new QualItemVect;
@@ -276,19 +271,35 @@ void LoadInit::consParseStmt( StmtList *stmtList )
curLocalFrame->rootScope, popQual, String("pop") );
LangExpr *pop = LangExpr::cons( LangTerm::cons( InputLoc(), popRef, popArgs ) );
+ NamespaceQual *nspaceQual = NamespaceQual::cons( curNspace() );
+ TypeRef *typeRef = TypeRef::cons( internal, nspaceQual, "argv_el", RepeatNone );
+ ObjectField *objField = ObjectField::cons( internal,
+ ObjectField::UserLocalType, typeRef, "A" );
+
+ LangStmt *stmt = varDef( objField, pop, LangStmt::AssignType );
+ stmtList->append( stmt );
+
/* Construct a literal string 'r', for second arg to open. */
ConsItem *modeConsItem = ConsItem::cons( internal,
ConsItem::InputText, String("r") );
ConsItemList *modeCons = new ConsItemList;
modeCons->append( modeConsItem );
LangExpr *modeExpr = LangExpr::cons( LangTerm::cons( internal, modeCons ) );
+
+ /* Reference A->value */
+ QualItemVect *qual = new QualItemVect;
+ qual->append( QualItem( QualItem::Arrow, internal, "A" ) );
+ LangVarRef *varRef = LangVarRef::cons( internal, 0,
+ curLocalFrame->rootScope, qual, String("value") );
+ LangExpr *Avalue = LangExpr::cons( LangTerm::cons( internal,
+ LangTerm::VarRefType, varRef ) );
/* Call open. */
QualItemVect *openQual = new QualItemVect;
LangVarRef *openRef = LangVarRef::cons( internal,
0, curLocalFrame->rootScope, openQual, String("open") );
CallArgVect *openArgs = new CallArgVect;
- openArgs->append( new CallArg(pop) );
+ openArgs->append( new CallArg(Avalue) );
openArgs->append( new CallArg(modeExpr) );
LangExpr *open = LangExpr::cons( LangTerm::cons( InputLoc(), openRef, openArgs ) );
@@ -297,9 +308,14 @@ void LoadInit::consParseStmt( StmtList *stmtList )
ConsItemList *list = ConsItemList::cons( consItem );
/* Will capture the parser to "P" */
- ObjectField *objField = ObjectField::cons( internal,
+ objField = ObjectField::cons( internal,
ObjectField::UserLocalType, 0, String("P") );
+ /* Ref the start def. */
+ nspaceQual = NamespaceQual::cons( curNspace() );
+ typeRef = TypeRef::cons( internal, nspaceQual,
+ String("start"), RepeatNone );
+
/* Parse the above list. */
LangExpr *parseExpr = parseCmd( internal, false, false, objField, typeRef, 0, list );
LangStmt *parseStmt = LangStmt::cons( internal, LangStmt::ExprType, parseExpr );
diff --git a/src/parser.cc b/src/parser.cc
index 25595f9d..9119a341 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -15,6 +15,58 @@
using std::endl;
+void BaseParser::listElDef( String name )
+{
+ /*
+ * The unique type. This is a def with a single empty form.
+ */
+ ObjectDef *objectDef = ObjectDef::cons( ObjectDef::UserType,
+ name, pd->nextObjectId++ );
+
+ LelDefList *defList = new LelDefList;
+
+ Production *prod = BaseParser::production( InputLoc(),
+ new ProdElList, String(), false, 0, 0 );
+ prodAppend( defList, prod );
+
+ NtDef *ntDef = NtDef::cons( name, curNspace(), curStruct(), false );
+ BaseParser::cflDef( ntDef, objectDef, defList );
+
+ /*
+ * List element with the same name as containing context.
+ */
+ NamespaceQual *nspaceQual = NamespaceQual::cons( curNspace() );
+ String id = curStruct()->objectDef->name;
+ RepeatType repeatType = RepeatNone;
+ TypeRef *objTr = TypeRef::cons( InputLoc(), nspaceQual, id, repeatType );
+ TypeRef *elTr = TypeRef::cons( InputLoc(), TypeRef::ListEl, 0, objTr, 0 );
+
+ ObjectField *of = ObjectField::cons( InputLoc(),
+ ObjectField::UserFieldType, elTr, name );
+ contextVarDef( InputLoc(), of );
+}
+
+void BaseParser::argvDecl()
+{
+ String structName = "argv_el";
+ structHead( internal, structName, ObjectDef::StructType );
+
+ /* First the argv value. */
+ String name = "value";
+ String type = "str";
+ NamespaceQual *nspaceQual = NamespaceQual::cons( curNspace() );
+ TypeRef *typeRef = TypeRef::cons( internal, nspaceQual, type, RepeatNone );
+ ObjectField *objField = ObjectField::cons( internal,
+ ObjectField::StructFieldType, typeRef, name );
+ contextVarDef( objField->loc, objField );
+
+ /* Now the list element. */
+ listElDef( "el" );
+
+ structStack.pop();
+ namespaceStack.pop();
+}
+
void BaseParser::init()
{
/* Set up the root namespace. */
@@ -55,6 +107,8 @@ void BaseParser::init()
pd->declareBaseLangEls();
pd->initUniqueTypes();
+ argvDecl();
+
/* Internal variables. */
addArgvList();
}
@@ -315,7 +369,8 @@ void BaseParser::literalDef( const InputLoc &loc, const String &data,
void BaseParser::addArgvList()
{
- TypeRef *typeRef = TypeRef::cons( internal, pd->uniqueTypeStr );
+ NamespaceQual *nspaceQual = NamespaceQual::cons( curNspace() );
+ TypeRef *typeRef = TypeRef::cons( internal, nspaceQual, "argv_el", RepeatNone );
pd->argvTypeRef = TypeRef::cons( internal, TypeRef::List, 0, typeRef, 0 );
}
diff --git a/src/parser.h b/src/parser.h
index 70436edd..f4730cb7 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -41,6 +41,9 @@ struct BaseParser
/* Lexical feedback. */
+ void listElDef( String name );
+
+ void argvDecl();
void init();
void addRegularDef( const InputLoc &loc, Namespace *nspace,
const String &name, LexJoin *join );
diff --git a/src/pdabuild.cc b/src/pdabuild.cc
index 34593905..8db8edf5 100644
--- a/src/pdabuild.cc
+++ b/src/pdabuild.cc
@@ -1564,7 +1564,8 @@ void Compiler::makeRuntimeData()
runtimeData->genericInfo[gen->id].keyType = gen->keyUT != 0 ?
gen->keyUT->typeId : 0;
runtimeData->genericInfo[gen->id].keyOffset = 0;
- runtimeData->genericInfo[gen->id].parserId = gen->utArg->langEl->parserId;
+ runtimeData->genericInfo[gen->id].parserId =
+ gen->typeId == GEN_PARSER ? gen->utArg->langEl->parserId : -1;
runtimeData->genericInfo[gen->id].elOffset = gen->elOffset;
}
}
diff --git a/src/resolve.cc b/src/resolve.cc
index 7dda501c..2751134c 100644
--- a/src/resolve.cc
+++ b/src/resolve.cc
@@ -110,6 +110,23 @@ UniqueType *TypeRef::resolveTypeList( Compiler *pd )
UniqueType *utValue = typeRef1->resolveType( pd );
+ /* Find the offset of the list element. */
+ int off = 0;
+ bool found = false;
+ FieldList *fieldList = utValue->structEl->context->objectDef->fieldList;
+ for ( FieldList::Iter f = *fieldList; f.lte(); f++, off++ ) {
+ UniqueType *fUT = f->value->typeRef->resolveType( pd );
+ if ( fUT->typeId == TYPE_GENERIC && fUT->generic != 0 &&
+ fUT->generic->typeId == GEN_LIST_EL )
+ {
+ found = true;
+ break;
+ }
+ }
+
+ if ( !found )
+ error( loc ) << "could not find list element in type ref" << endp;
+
UniqueGeneric searchKey( UniqueGeneric::List, utValue );
UniqueGeneric *inMap = pd->uniqueGenericMap.find( &searchKey );
if ( inMap == 0 ) {
diff --git a/src/struct.c b/src/struct.c
index 8d03583f..6a71194d 100644
--- a/src/struct.c
+++ b/src/struct.c
@@ -196,7 +196,7 @@ struct colm_list_el *colm_list_el_new( struct colm_program *prg )
struct colm_list_el *el = (struct colm_list_el*) malloc( memsize );
memset( el, 0, memsize );
colm_struct_add( prg, (struct colm_struct *)el );
- el->id = STRUCT_INBUILT_ID;
+ el->id = -22; //STRUCT_INBUILT_ID;
return el;
}
diff --git a/src/struct.h b/src/struct.h
index b556684b..16161eed 100644
--- a/src/struct.h
+++ b/src/struct.h
@@ -57,10 +57,10 @@ typedef struct colm_list_el
{
short id;
struct colm_struct *prev, *next;
- colm_destructor_t destructor;
-
- void *buffer[8];
-
+// colm_destructor_t destructor;
+//
+// void *buffer[8];
+//
Tree *value;
struct colm_list_el *list_next;
struct colm_list_el *list_prev;
diff --git a/src/synthesis.cc b/src/synthesis.cc
index 99762863..d8b54551 100644
--- a/src/synthesis.cc
+++ b/src/synthesis.cc
@@ -1080,8 +1080,14 @@ UniqueType *LangTerm::evaluateNew( Compiler *pd, CodeVect &code ) const
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 );
+ if ( replUT->typeId == TYPE_GENERIC ) {
+ code.append( IN_CONS_GENERIC );
+ code.appendHalf( replUT->generic->id );
+ }
+ else {
+ code.append( IN_NEW_STRUCT );
+ code.appendHalf( replUT->structEl->id );
+ }
return replUT;
}
diff --git a/src/tree.c b/src/tree.c
index 0fe9b93a..91c65f8e 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -1638,7 +1638,7 @@ Tree *listRemoveEnd( Program *prg, List *list )
Tree *listRemoveHead( Program *prg, List *list )
{
- Tree *tree = list->head->value;
+ Tree *tree = list->head;
listDetachFirst( list );
return tree;
}