/* MAP-ELEMENTS for internal subroutines */ LVAL subr_map_elements P1C(mapfun, f) { LVAL arglist, result, fcn, first_compound, type; int rlen; first_compound = findcompound(FALSE); if (first_compound == NIL) result = (*f)(); else { xlstkcheck(3); xlsave(arglist); xlsave(fcn); xlsave(result); fcn = cvsubr(f, SUBR, 0); type = compoundseqtype(first_compound); arglist = makearglist(xlargc, xlargv); rlen = findrlen(arglist); fixuparglist(arglist); result = map(type, fcn, arglist, rlen); result = makecompound(first_compound, result); #ifdef MULVALS xlnumresults = 1; xlresults[0] = result; #endif /* MULVALS */ xlpopn(3); } return(result); }
void makeset(TagType tag, int n) { Cell *temp; makecompound(PAIR, n+2); temp = newcell(tag); temp->value = n; temp->left = pop(); temp->right = template_nil; push(temp); }
static void parseapplication(void) { if(tokentype == TYPEID) { int count = 1; push(gettemplate(tokenval)); gettoken(); while(tokentype == NUMBER || tokentype == IDENTIFIER || tokentype == TYPEID || tokentype == CHARACTER || tokentype == STRING || tokentype == LPAR || tokentype == LBRACK || tokentype == LACC) { parseterm(); count++; } makecompound(STRUCT, count); } else if(tokentype == OPERATOR) parsename(); else parseterm(); while(tokentype == NUMBER || tokentype == IDENTIFIER || tokentype == TYPEID || tokentype == CHARACTER || tokentype == STRING || tokentype == LPAR || tokentype == LBRACK || tokentype == LACC) { parseterm(); makeinverse(APPLY); } if(tokentype == OPERATOR && strcmp(tokenval, ":") == 0) { gettoken(); if(tokentype == RPAR) { push(gettemplate(":")); make(APPLY); } else { parseapplication(); makeinverse(LIST); } } }
static void makerecordfield(Cell *recordtype, Cell *field, Cell *fieldtype) { char *fieldname = getfunction(field->value)->name; Cell *var = newcell(VARIABLE); var->value = 1; var->left = field; push(fieldtype); push(recordtype); make(APPLY); if(!inserttypeexpr(fieldname, pop())) parseerror(12); push(var); push(var); push(field); make(ALIAS); makecompound(RECORD, 1); push(field); make(APPLY); make(LIST); if(!insert(fieldname, 1, FUNC, pop(), NULL)) parseerror(18); }
/* Built in MAP-ELEMENTS */ LVAL xsmap_elements(V) { LVAL arglist, result, fcn, first_compound, type; int rlen; if (xlargc < 2) xltoofew(); first_compound = findcompound(TRUE); if (first_compound == NIL) result = xfuncall(); else { xlstkcheck(2) xlsave(arglist); xlsave(result); fcn = xlgetarg(); type = compoundseqtype(first_compound); arglist = makearglist(xlargc, xlargv); rlen = findrlen(arglist); fixuparglist(arglist); result = map(type, fcn, arglist, rlen); result = makecompound(first_compound,result); xlpopn(2); } return(result); }
/******************************************************************** initialisation of hashtable with system functions *********************************************************************/ static void initsyslib(void) { Cell *obj = gettemplate("objecttype"); inserttypestring("object", "[char] -> objecttype"); insertabstype("object", obj); parsetypeexpr("(*, [char] -> [[char]] -> * -> (*, [[char]]))"); makeconstant(FUNC, obj->value); makecompound(STRUCT, 1); make(TYPESYNONYM); inserttypeexpr("objecttype", pop()); insertabstype("objecttype", obj); insert("_section", 3, FUNC , NULL, apply_SECTION); insert("if" , 3, FUNC , NULL, applyIF); insert("^" , 2, FUNC , NULL, applyPOWER); insert("neg" , 1, FUNC , NULL, applyNEG); insert("*" , 2, FUNC , NULL, applyTIMES); insert("/" , 2, FUNC , NULL, applyDIV); insert("//" , 2, FUNC , NULL, applyDIVIDE); insert("%" , 2, FUNC , NULL, applyMOD); insert("+" , 2, FUNC , NULL, applyPLUS); insert("-" , 2, FUNC , NULL, applyMINUS); insert("=" , 2, FUNC , NULL, applyEQ); insert("~=" , 2, FUNC , NULL, applyNE); insert("<" , 2, FUNC , NULL, applyLT); insert("<=" , 2, FUNC , NULL, applyLE); insert(">" , 2, FUNC , NULL, applyGT); insert(">=" , 2, FUNC , NULL, applyGE); insert("&" , 2, FUNC , NULL, applyUPDATE); insert("True" , 0, BOOLEAN , NULL, NULL); insert("False" , 0, BOOLEAN , NULL, NULL); insert("pi" , 0, REAL , NULL, NULL); insert("Nil" , 0, NIL , NULL, NULL); insert("" , 1, FUNC , NULL, NULL); insert("strict" , 2, FUNC , NULL, applySTRICT); inserttypestring("_section" , "(* -> ** -> ***) -> ** -> * -> ***"); inserttypestring("if" , "bool -> * -> * -> *"); inserttypestring("^" , "num -> num -> num"); inserttypestring("neg" , "num -> num"); inserttypestring("*" , "num -> num -> num"); inserttypestring("/" , "num -> num -> num"); inserttypestring("//" , "num -> num -> num"); inserttypestring("%" , "num -> num -> num"); inserttypestring("+" , "num -> num -> num"); inserttypestring("-" , "num -> num -> num"); inserttypestring("=" , "* -> * -> bool"); inserttypestring("~=" , "* -> * -> bool"); inserttypestring("<" , "* -> * -> bool"); inserttypestring("<=" , "* -> * -> bool"); inserttypestring(">" , "* -> * -> bool"); inserttypestring(">=" , "* -> * -> bool"); inserttypestring("&" , "* -> * -> *"); inserttypestring("True" , "bool"); inserttypestring("False" , "bool"); inserttypestring("pi" , "num"); inserttypestring("Nil" , "[*]"); inserttypestring("strict" , "(* -> **) -> * -> **"); insertsys("strict"); insertsys("^"); insertsys("neg"); insertsys("*"); insertsys("/"); insertsys("//"); insertsys("%"); insertsys("+"); insertsys("-"); insertsys("="); insertsys("~="); insertsys("<"); insertsys("<="); insertsys(">"); insertsys(">="); insertoperator("." , 1, Right); insertoperator(":" , 1, Right); insertoperator("&" , 1, Left); insertoperator("!" , 2, Left); insertoperator("^" , 2, Right); insertoperator("*" , 3, Left); insertoperator("/" , 3, Left); insertoperator("//" , 3, Left); insertoperator("%" , 3, Left); insertoperator("++" , 4, Right); insertoperator("--" , 4, Left); insertoperator("+" , 4, Left); insertoperator("-" , 4, Left); insertoperator("=" , 5, Right); insertoperator("~=" , 5, Left); insertoperator("<" , 5, Left); insertoperator("<=" , 5, Left); insertoperator(">" , 5, Left); insertoperator(">=" , 5, Left); insertoperator("/\\" , 6, Right); insertoperator("\\/" , 7, Right); template_divide = gettemplate("//"); template_div = gettemplate("/"); template_mod = gettemplate("%"); template_power = gettemplate("^"); template_update = gettemplate("&"); }
static void parsestructdef(void) { char structname[stringsize]; char *headname; int count; Cell *head = pop(); setchecktypevariables(COLLECT); push(template_match); for(; head->tag==APPLY; head=head->left) { if(head->right->tag != UNDEFINED && head->right->tag != FUNC) parseerror(9); push(maketypevariable(getfunction(head->right->value)->name)); make(STRUCT); } if(head->tag != UNDEFINED && head->tag != FUNC) parseerror(10); headname = getfunction(head->value)->name; makeconstant(FUNC, head->value); make(STRUCT); setchecktypevariables(CHECK); gettoken(); head = top(); if(tokentype == LACC) { count = 0; do { gettoken(); if(tokentype != IDENTIFIER) parseerror(25); push(gettemplate(tokenval)); gettoken(); if(tokentype != COLONS) parseerror(15); gettoken(); parsetype(TYPEEXPR); makerecordfield(head, getN(2), getN(1)); makeinverse(TYPEDEF); count++; } while(tokentype == COMMA); makecompound(RECORD, count); makeinverse(TYPEDEF); if(tokentype != RACC) parseerror(33); gettoken(); } else { for(;;) { if(tokentype != TYPEID) parseerror(11); strcpy(structname, tokenval); count = 0; gettoken(); while(tokentype == IDENTIFIER || tokentype == OPERATOR || tokentype == LBRACK || tokentype == LPAR) { parsetype(TYPETERM); count++; } push(head); while(count-- > 0) makeinverse(APPLY); if(!inserttypeexpr(structname, pop())) parseerror(12); if(tokentype != BAR) break; gettoken(); } } if(!inserttypeexpr(headname, pop())) parseerror(12); setchecktypevariables(NOCHECK); }
static void parsetype(TypeType typetype) { switch(tokentype) { case IDENTIFIER: if(strcmp(tokenval, "num") == 0) { push(newcell(INT)); gettoken(); } else if(strcmp(tokenval, "char") == 0) { push(newcell(CHAR)); gettoken(); } else if(strcmp(tokenval, "bool") == 0) { push(newcell(BOOLEAN)); gettoken(); } else { int count = 1; push(gettemplate(tokenval)); gettoken(); if(typetype == TYPEEXPR) while(tokentype == IDENTIFIER || tokentype == OPERATOR || tokentype == LBRACK || tokentype == LPAR) { parsetype(TYPETERM); count++; } makecompound(STRUCT, count); } break; case OPERATOR: push(maketypevariable(tokenval)); gettoken(); break; case LPAR: gettoken(); if(tokentype == RPAR) push(newcell(NULLTUPLE)); else { parsetype(TYPEEXPR); if(tokentype == COMMA) { int count = 1; while(tokentype == COMMA) { gettoken(); parsetype(TYPEEXPR); count++; } makecompound(PAIR, count); } } if(tokentype != RPAR) parseerror(2); gettoken(); break; case LBRACK: gettoken(); parsetype(TYPEEXPR); push(template_nil); makeinverse(LIST); if(tokentype != RBRACK) parseerror(1); gettoken(); break; default: parseerror(8); } if(typetype == TYPEEXPR && tokentype == ARROW) { gettoken(); parsetype(TYPEEXPR); makeinverse(APPLY); } }
static void parseterm(void) { int count; switch(tokentype) { case NUMBER: if(strchr(tokenval, '.') == NULL) makeINT(atol(tokenval)); else makeREAL(atof(tokenval)); gettoken(); break; case IDENTIFIER: parsename(); break; case TYPEID: push(gettemplate(tokenval)); makecompound(STRUCT, 1); gettoken(); break; case CHARACTER: makeconstant(CHAR, tokenval[0]); gettoken(); break; case STRING: buildstring(tokenval); gettoken(); break; case LPAR: gettoken(); if(tokentype == OPERATOR && strcmp(tokenval, "-") != 0) { parsename(); if(tokentype != RPAR) { parseexpression(MAXPRIO); rotatestack(); push(gettemplate("_section")); make(APPLY); make(APPLY); } } else if(tokentype == RPAR) makeconstant(NULLTUPLE, 0); else { parseexpression(MAXPRIO); if(tokentype == COMMA) { count = 1; while(tokentype == COMMA) { gettoken(); parseexpression(MAXPRIO); count++; } makecompound(PAIR, count); } } if(tokentype != RPAR) parseerror(2); gettoken(); break; case LBRACK: parselist(); break; case LACC: count = 0; do { gettoken(); if(tokentype != IDENTIFIER) parseerror(25); push(gettemplate(tokenval)); gettoken(); if(strcmp(tokenval, "=") != 0) parseerror(5); gettoken(); parseexpression(MAXPRIO); makeinverse(ALIAS); count++; } while(tokentype == COMMA); makecompound(RECORD, count); if(tokentype != RACC) parseerror(33); gettoken(); break; default: parseerror(3); } }