int main(int argc, char ** argv) { FILE *input; Exp parse_tree; if (argc > 1) { input = fopen(argv[1], "r"); if (!input) { fprintf(stderr, "Error opening input file.\n"); exit(1); } } else input = stdin; /* The default entry point is used. For other options see Parser.h */ parse_tree = pExp(input); if (parse_tree) { printf("\nParse Succesful!\n"); printf("\n[Abstract Syntax]\n"); printf("%s\n\n", showExp(parse_tree)); printf("[Linearized Tree]\n"); printf("%s\n\n", printExp(parse_tree)); return 0; } return 1; }
int main(int argc, const char * argv[]) { //方法定义要写在调用之前,c99规范 printExp(); return 0; }
//@@@ print Tr_exp void print(Tr_exp et, FILE * out) { if (et->kind == Tr_ex) printExp(unEx(et), out); if (et->kind == Tr_nx) printStm(unNx(et), out); if (et->kind == Tr_cx) printStm(unCx(et).stm, out); }
/* * Print an expression to stdout. */ void printExp(Exp *exp) { if(isApp(exp)) { printf("("); printExp(appFun(exp)); printf(" "); printExp(appArg(exp)); printf(")"); } else if(isAbs(exp)) { printf("(\\ "); printExp(absBody(exp)); printf(")"); } else if(isVar(exp)) { if(varBind(exp) >= 0) { printf("V%d", varBind(exp)); } else { printf("F%d", -varBind(exp)); } } else if(isCon(exp) && (conTy(exp) == C_Bool) && (conVal(exp) == true)) { printf("true"); } else if(isCon(exp) && (conTy(exp) == C_Bool) && (conVal(exp) == false)) { printf("false"); } else if(isCon(exp) && (conTy(exp) == C_Char)) { printf("\'%c\'", conVal(exp)); } else if(isCon(exp)/* && (conTy(exp) == C_Int)*/) { printf("%d", conVal(exp)); } else if(isOpn(exp)) { switch(opnType(exp)) { case O_Cond : printf("cond") ; break; case O_Add : printf("+") ; break; case O_Sub : printf("-") ; break; case O_Mul : printf("*") ; break; case O_Div : printf("/") ; break; case O_Mod : printf("%%") ; break; case O_Lss : printf("<") ; break; case O_LsE : printf("<=") ; break; case O_NEq : printf("/=") ; break; case O_Gtr : printf(">") ; break; case O_GtE : printf(">=") ; break; case O_Equ : printf("==") ; break; case O_And : printf("and") ; break; case O_Or : printf("or") ; break; case O_Xor : printf("xor") ; break; case O_Not : printf("not") ; break; case O_IsZ : printf("iszero") ; break; case O_Empty : printf("[]") ; break; case O_Cons : printf(":") ; break; case O_Null : printf("null") ; break; case O_Head : printf("head") ; break; case O_Tail : printf("tail") ; break; case O_Fix : printf("fix") ; break; case O_InjL : printf("injl") ; break; case O_InjR : printf("injr") ; break; case O_RemL : printf("reml") ; break; case O_RemR : printf("remr") ; break; case O_IsLeft : printf("isleft") ; break; case O_Tuple : printf("tuple") ; break; case O_Fst : printf("fst") ; break; case O_Snd : printf("snd") ; break; } } else { printf("Error - unrecognised expression type in printExp()\n"); assert(false); } }
/* * Print an expression to stdout, folled by a newline character. */ void printlnExp(Exp *exp) { printExp(exp); printf("\n"); }
/* Handles browse classes dialog box */ LRESULT CALLBACK BrowseClassesDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) { INT i; static Class currClass; Class theClass; Inst theInst; Name theMember; WORD NotifyCode, wId; HBITMAP hBitmap; RECT aRect, DlgRect; HBITMAP hBmp; BITMAP bm; DRAWITEMSTRUCT FAR *lpdis; LPMEASUREITEMSTRUCT lpmis; LPCOMPAREITEMSTRUCT lpcis; BOOL Selected = FALSE; static HBITMAP hCBm, hCSelBm, hIBm, hISelBm, hMBm, hMSelBm; String string; CHAR string1[256]; NotifyCode = HIWORD (wParam); wId = LOWORD (wParam); switch (msg) { case WM_INITDIALOG: CenterDialogInParent(hDlg); SetDialogFont (hDlg, hDialogFont); SendDlgItemMessage(hDlg, LB_CLASS, LB_SETHORIZONTALEXTENT, (WPARAM)300, 0L); SendDlgItemMessage(hDlg, LB_MEMBERS, LB_SETHORIZONTALEXTENT, (WPARAM)400, 0L); /* Create list of classes and set current class */ for(i=CLASSMIN; i<classMax(); i++) { SendDlgItemMessage(hDlg, LB_CLASS ,LB_ADDSTRING, 0, (LPARAM)(LPSTR) i); } SendDlgItemMessage(hDlg, LB_CLASS, LB_SETCURSEL, 0, 0L); currClass = (Class) SendDlgItemMessage(hDlg, LB_CLASS ,LB_GETITEMDATA, (WPARAM) SendDlgItemMessage(hDlg, LB_CLASS ,LB_GETCURSEL, 0, 0L), 0L); SetClass(hDlg, currClass); /* Create Bitmaps */ hCBm = LoadBitmap (hThisInstance, "CLASSBMP"); MapBitmap (hCBm, RGB(0,128,128), GetSysColor(COLOR_WINDOW)); hCSelBm = LoadBitmap (hThisInstance, "CLASSBMP"); MapBitmap (hCSelBm, RGB(0,128,128), GetSysColor(COLOR_HIGHLIGHT)); hIBm = LoadBitmap (hThisInstance, "INSTANCEBMP"); MapBitmap (hIBm, RGB(0,128,128), GetSysColor(COLOR_WINDOW)); hISelBm = LoadBitmap (hThisInstance, "INSTANCEBMP"); MapBitmap (hISelBm, RGB(0,128,128), GetSysColor(COLOR_HIGHLIGHT)); hMBm = LoadBitmap (hThisInstance, "MEMBERBMP"); MapBitmap (hMBm, RGB(0,128,128), GetSysColor(COLOR_WINDOW)); hMSelBm = LoadBitmap (hThisInstance, "MEMBERBMP"); MapBitmap (hMSelBm, RGB(0,128,128), GetSysColor(COLOR_HIGHLIGHT)); return TRUE; case WM_DESTROY: /* Destroy Bitmaps */ DeleteObject (hCBm); DeleteObject (hCSelBm); DeleteObject (hIBm); DeleteObject (hISelBm); DeleteObject (hMBm); DeleteObject (hMSelBm); break; case WM_CTLCOLORBTN: case WM_CTLCOLORDLG: case WM_CTLCOLOREDIT: case WM_CTLCOLORLISTBOX: case WM_CTLCOLORMSGBOX: case WM_CTLCOLORSCROLLBAR: case WM_CTLCOLORSTATIC: break; case WM_PAINT: { HDC hDC; PAINTSTRUCT Ps; BeginPaint(hDlg, &Ps); hDC = Ps.hdc; /* Paint classes Bitmap */ GetWindowRect(hDlg, &DlgRect); GetWindowRect(GetDlgItem(hDlg, ID_PLACEBITMAP), &aRect); hBitmap = LoadMappedBitmap(hThisInstance, "CLASSESDLGBMP"); DrawBitmap(hDC, hBitmap, aRect.left-DlgRect.left-GetSystemMetrics(SM_CXDLGFRAME), aRect.top-DlgRect.top-GetSystemMetrics(SM_CYDLGFRAME)-GetSystemMetrics(SM_CYCAPTION)); DeleteObject(hBitmap); EndPaint(hDlg, &Ps); } break; case WM_COMPAREITEM: { lpcis = (COMPAREITEMSTRUCT FAR *) lParam; switch (wParam) { case LB_CLASS: return strcmp (textToStr(cclass(lpcis->itemData1).text), textToStr(cclass(lpcis->itemData2).text)); case LB_INSTANCES: if (nonNull(inst(lpcis->itemData1).specifics)) { printContext(stdstr,inst(lpcis->itemData1).specifics); fprintf(stdstr," => "); } printPred(stdstr,inst(lpcis->itemData1).head); fprintf(stdstr,"\n"); strcpy (string1, stdstrbuff); if (nonNull(inst(lpcis->itemData2).specifics)) { printContext(stdstr,inst(lpcis->itemData2).specifics); fprintf(stdstr," => "); } printPred(stdstr,inst(lpcis->itemData2).head); fprintf(stdstr,"\n"); return strcmp (string1, stdstrbuff); case LB_MEMBERS: printExp (stdstr, lpcis->itemData1); fprintf(stdstr,"\n"); strcpy (string1, stdstrbuff); printExp (stdstr, lpcis->itemData2); fprintf(stdstr,"\n"); return strcmp (string1, stdstrbuff); } } break; case WM_MEASUREITEM: lpdis = (DRAWITEMSTRUCT FAR *) lParam; if (lpdis->CtlID == LB_CLASS || lpdis->CtlID == LB_INSTANCES || lpdis->CtlID == LB_MEMBERS ) { lpmis = (LPMEASUREITEMSTRUCT) lParam; /* Set the height of the list box items to Bitmap height */ hBmp = LoadBitmap(hThisInstance, "CLASSBMP"); GetObject(hBmp, sizeof(BITMAP), &bm); DeleteObject(hBmp); lpmis->itemHeight = bm.bmHeight+1; lpmis->itemWidth = 50000; return TRUE; } break; case WM_DRAWITEM: lpdis = (DRAWITEMSTRUCT FAR *) lParam; if (lpdis->CtlID == LB_CLASS || lpdis->CtlID == LB_INSTANCES || lpdis->CtlID == LB_MEMBERS ) { if (lpdis->itemID == (UINT)-1) { return TRUE; } switch (lpdis->itemAction) { case ODA_DRAWENTIRE: case ODA_SELECT: case ODA_FOCUS: if ((lpdis->itemState & ODS_SELECTED) /*&& (lpdis->itemState & ODS_FOCUS)*/) { SetBkColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHT)); SetTextColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT)); Selected = TRUE; } else { SetBkColor(lpdis->hDC, GetSysColor(COLOR_WINDOW)); SetTextColor(lpdis->hDC, GetSysColor(COLOR_WINDOWTEXT)); } break; default: return FALSE; } switch (lpdis->CtlID) { case LB_CLASS: theClass = (Class) SendDlgItemMessage(hDlg, lpdis->CtlID, LB_GETITEMDATA, lpdis->itemID, 0); printPred(stdstr,cclass(theClass).head); fprintf (stdstr, " -- in %s\n", textToStr(module(cclass(theClass).mod).text)); ExtTextOut(lpdis->hDC, lpdis->rcItem.left+21, lpdis->rcItem.top, ETO_OPAQUE, &(lpdis->rcItem), stdstrbuff, strlen(stdstrbuff), NULL); hBmp = Selected ? hCSelBm : hCBm; break; case LB_INSTANCES: theInst = (Inst) SendDlgItemMessage(hDlg, lpdis->CtlID, LB_GETITEMDATA, lpdis->itemID, 0); if (nonNull(inst(theInst).specifics)) { printContext(stdstr,inst(theInst).specifics); fprintf(stdstr," => "); } printPred(stdstr,inst(theInst).head); fprintf (stdstr, " -- in %s \n", textToStr(module(moduleOfScript(scriptThisInst(theInst))).text)); ExtTextOut(lpdis->hDC, lpdis->rcItem.left+21, lpdis->rcItem.top, ETO_OPAQUE, &(lpdis->rcItem), stdstrbuff, strlen(stdstrbuff), NULL); hBmp = Selected ? hISelBm : hIBm; break; case LB_MEMBERS: theMember = (Name) SendDlgItemMessage(hDlg, lpdis->CtlID, LB_GETITEMDATA, lpdis->itemID, 0); printExp(stdstr,theMember); fprintf(stdstr, " :: "); printType(stdstr,name(theMember).type); fprintf(stdstr,"\n"); ExtTextOut(lpdis->hDC, lpdis->rcItem.left+21, lpdis->rcItem.top, ETO_OPAQUE, &(lpdis->rcItem), stdstrbuff, strlen(stdstrbuff), NULL); hBmp = Selected ? hMSelBm : hMBm; break; } DrawBitmap (lpdis->hDC, hBmp, (lpdis->rcItem.left)+4, lpdis->rcItem.top); /* If selected draw rectangle */ if ((lpdis->itemState & ODS_SELECTED)&&(lpdis->itemState & ODS_FOCUS)) { DrawFocusRect(lpdis->hDC, &(lpdis->rcItem)); } return TRUE; } case WM_COMMAND: switch (wId) { case LB_CLASS: switch(NotifyCode) { case LBN_SELCHANGE: /* select a new class */ currClass = (Class) SendDlgItemMessage(hDlg, LB_CLASS ,LB_GETITEMDATA, SendDlgItemMessage(hDlg, LB_CLASS ,LB_GETCURSEL, 0, 0L), 0L); SetClass(hDlg, currClass); break; case LBN_DBLCLK: { /* Open in text editor script file with class definition */ currClass = (Class) SendDlgItemMessage(hDlg, LB_CLASS ,LB_GETITEMDATA, SendDlgItemMessage(hDlg, LB_CLASS ,LB_GETCURSEL, 0, 0L), 0L); currClass = (Class) SendDlgItemMessage(hDlg, LB_CLASS ,LB_GETITEMDATA, SendDlgItemMessage(hDlg, LB_CLASS ,LB_GETCURSEL, 0, 0L), 0L); setLastEdit(getScriptName(scriptThisClass(currClass)), cclass(currClass).line); runEditor(); } break; } break; case LB_MEMBERS: case LB_INSTANCES: switch(NotifyCode) {/* Open in text editor script file with instance definition */ case LBN_DBLCLK: { Inst currInst; currInst = (Inst) SendDlgItemMessage(hDlg, LB_INSTANCES, LB_GETITEMDATA, SendDlgItemMessage(hDlg, LB_INSTANCES ,LB_GETCURSEL, 0, 0L), 0L); /* Find instance module */ setLastEdit(getScriptName(scriptThisInst(currInst)), inst(currInst).line); runEditor(); } break; } break; case ID_HIERARCHY: /* Draw classes hierarchy */ DrawClassesHierarchy(); break; case ID_EDITCLASS: /* Pushed on Edit class button */ if (SendDlgItemMessage(hDlg, LB_CLASS, LB_GETCURSEL, 0, 0L) != LB_ERR) DlgSendMessage(hDlg, WM_COMMAND, LB_CLASS, MAKELONG(0, LBN_DBLCLK)); break; case ID_EDITINSTANCE: /* Pushed on Edit instance button */ if (SendDlgItemMessage(hDlg, LB_INSTANCES, LB_GETCURSEL, 0, 0L) != LB_ERR) DlgSendMessage(hDlg, WM_COMMAND, LB_INSTANCES, MAKELONG(0, LBN_DBLCLK)); break; case IDCANCEL: /* Close dialog */ case IDOK: EndDialog(hDlg, TRUE); return TRUE; default: return TRUE; } } return FALSE; }
/* A new Tycon was selected */ static local VOID SetTycon(HWND hDlg, UINT currTycon, List tycons) { Tycon tc; Int j; Type t; Inst in; tc = nth(currTycon,tycons); numCfuns = 0; numSfuns = 0; t = tc; for(j=0; j<tycon(tc).arity; ++j) t = ap(t, mkOffset(j)); switch(tycon(tc).what) { case SYNONYM: fprintf (stdstr, "type "); printType(stdstr, t); fprintf (stdstr, " = "); printType(stdstr, tycon(tc).defn); fprintf (stdstr, "\n"); SendDlgItemMessage(hDlg, LB_CONS ,LB_RESETCONTENT, 0, 0L); SendDlgItemMessage(hDlg, LB_DEF ,LB_RESETCONTENT, 0, 0L); SendDlgItemMessage(hDlg, LB_DEF ,LB_ADDSTRING, 0, (LONG)(LPSTR) stdstrbuff); break; case NEWTYPE: case DATATYPE: { List cs = tycon(tc).defn; if (tycon(tc).what==DATATYPE) fprintf(stdstr, "data "); else fprintf(stdstr, "newtype "); printType(stdstr, t); fprintf (stdstr, "\n"); SendDlgItemMessage(hDlg, LB_DEF ,LB_RESETCONTENT, 0, 0L); SendDlgItemMessage(hDlg, LB_DEF ,LB_ADDSTRING, 0, (LONG)(LPSTR) stdstrbuff); SendDlgItemMessage(hDlg, LB_CONS ,LB_RESETCONTENT, 0, 0L); /* Clear the redraw flag */ SendDlgItemMessage(hDlg, LB_CONS ,WM_SETREDRAW, FALSE, 0L); for (; nonNull(cs); cs=tl(cs)) { printExp (stdstr, hd(cs)); fprintf (stdstr, " :: "); printType(stdstr, name(hd(cs)).type); fprintf (stdstr, "\n"); SendDlgItemMessage(hDlg, LB_CONS ,LB_ADDSTRING, 0, (LONG)(LPSTR) stdstrbuff); SendDlgItemMessage(hDlg, LB_CONS, LB_SETCURSEL, 0, 0L); if (isCfun(hd(cs))) numCfuns++; else numSfuns++; } /* Set the redraw flag and force repaint. */ SendDlgItemMessage(hDlg, LB_CONS ,WM_SETREDRAW, TRUE, 0L); InvalidateRect(GetDlgItem(hDlg, LB_CONS), NULL, TRUE); break; } case RESTRICTSYN: fprintf (stdstr, "type"); printType(stdstr, t); fprintf (stdstr, " = <restricted>\n"); SendDlgItemMessage(hDlg, LB_CONS ,LB_RESETCONTENT, 0, 0L); SendDlgItemMessage(hDlg, LB_DEF ,LB_RESETCONTENT, 0, 0L); SendDlgItemMessage(hDlg, LB_DEF ,LB_ADDSTRING, 0, (LONG)(LPSTR) stdstrbuff); break; } /* Set instances */ SendDlgItemMessage(hDlg, LB_TYCONSINST ,LB_RESETCONTENT, 0, 0L); /* Clear the redraw flag */ SendDlgItemMessage(hDlg, LB_TYCONSINST, WM_SETREDRAW, FALSE, 0L); if (nonNull(in=findFirstInst(tc))) { do { SendDlgItemMessage(hDlg, LB_TYCONSINST, LB_ADDSTRING, 0, (LONG)(LPSTR) in); SendDlgItemMessage(hDlg, LB_TYCONSINST, LB_SETCURSEL, 0, 0L); in = findNextInst(tc,in); } while (nonNull(in)); } /* Set the redraw flag and force repaint. */ SendDlgItemMessage(hDlg, LB_TYCONSINST, WM_SETREDRAW, TRUE, 0L); InvalidateRect(GetDlgItem(hDlg, LB_TYCONSINST), NULL, TRUE); }