static void binaryExprClean(Node *node) { BinaryExpr *e = (BinaryExpr *)node; #ifdef LOG_CLEAN info("binaryExprClean\n"); #endif if (e->lhs) { nodeFree((Node *)e->lhs); } if (e->rhs) { nodeFree((Node *)e->rhs); } }
static void funcCallClean(Node *node) { FuncCall *e = (FuncCall *)node; #ifdef LOG_CLEAN info("funcCallClean\n"); #endif stringFree(&e->id); nodeFree((Node *)e->args); }
static void stmtClean(Node *node) { Stmt *st = (Stmt *)node; #ifdef LOG_CLEAN info("stmtClean\n"); #endif stringFree(&st->id); if (st->expr) { nodeFree((Node *)st->expr); } }
static void exprListClean(Node *node) { ExprList *el = (ExprList *)node; #ifdef LOG_CLEAN info("exprListClean\n"); #endif for (int i = 0; i < el->exprs.size; ++i) { Expr **arr = (Expr **)el->exprs.data; nodeFree((Node *)arr[i]); } arrayFree(&el->exprs); }
static void formulaClean(Node *node) { Formula *f = (Formula *)node; #ifdef LOG_CLEAN info("formulaClean\n"); #endif for(int i = 0; i < f->stmts.size; ++i) { Stmt **arr = (Stmt **)f->stmts.data; nodeFree((Node *)arr[i]); } arrayFree(&f->stmts); }
void parserFree(void *p) { Parser *yacc = (Parser *)p; if (!yacc) return; if (yacc->lex) { lexerFree(yacc->lex); } if (yacc->ast) { nodeFree((Node *)yacc->ast); } free(yacc); }
/** * removes the node at the specified element from the linked list. * * @function linkedListRemoveByIndex * * @date 2015-02-09 * * @revision none * * @designer Eric Tsang * * @programmer Eric Tsang * * @note none * * @signature BOOL linkedListRemoveByIndex(LinkedList* dis, int index) * * @param dis linked list pointer that operation is being performed on. * @param index index in the linked list to remove the node from. * * @return TRUE if the operation succeeded, FALSE otherwise. */ BOOL linkedListRemoveByIndex(LinkedList* dis, int index) { Node* curr; BOOL removed = FALSE; if(index/2 < linkedListSize(dis)) { curr = dis->head; while(index > 0 && curr != 0) { curr = curr->next; --index; } if(index == 0) { nodeFree(dis, curr); removed = TRUE; } } else { curr = dis->tail; while(index < linkedListSize(dis)-1 && curr != 0) { curr = curr->prev; ++index; } if(index == linkedListSize(dis)-1) { nodeFree(dis, curr); removed = TRUE; } } return removed; }
/** * removes a node in the linked list that's pointing at the specified data. * * @function linkedListRemoveElement * * @date 2015-02-09 * * @revision none * * @designer Eric Tsang * * @programmer Eric Tsang * * @note none * * @signature BOOL linkedListRemoveElement(LinkedList* dis, void* data) * * @param dis linked list pointer that operation is being performed on. * @param data pointer to the data to be removed from the linked list. * * @return TRUE if the element was removed; FALSE otherwise. */ BOOL linkedListRemoveElement(LinkedList* dis, void* data) { Node* curr; BOOL removed = FALSE; for(curr = dis->head; curr != 0; curr = curr->next) { if(curr->data == data) { nodeFree(dis, curr); removed = TRUE; break; } } return removed; }
static Node *parseUnaryExpr(Parser *p) { Node *expr = 0; if (p->tok == TK_INT) { char ch = p->tokval[p->toklen]; p->tokval[p->toklen] = '\0'; #ifdef LOG_PARSE info("解析得到IntExpr\n"); #endif expr = (Node *)intExprNew(atoi(p->tokval)); p->tokval[p->toklen] = ch; p->tok = lexerGetToken(p->lex, &p->tokval, &p->toklen); } else if (p->tok == TK_DECIMAL) { char ch = p->tokval[p->toklen]; p->tokval[p->toklen] = '\0'; #ifdef LOG_PARSE info("解析得到DecimalExpr\n"); #endif expr = (Node *)decimalExprNew(atof(p->tokval)); p->tokval[p->toklen] = ch; p->tok = lexerGetToken(p->lex, &p->tokval, &p->toklen); } else if (p->tok == TK_ID) { // ID | funcCall expr = (Node *)parseIdOrFuncCall(p); } else if (p->tok == TK_LP) { p->tok = lexerGetToken(p->lex, &p->tokval, &p->toklen); expr = parseExpr(p); if (p->tok == TK_RP) { #ifdef LOG_PARSE info("解析得到(expr)\n"); #endif p->tok = lexerGetToken(p->lex, &p->tokval, &p->toklen); } else { if (expr) { nodeFree(expr); expr = 0; } } } return expr; }
static Node *parseExpr(Parser *p) { Node *expr = 0; expr = parseUnaryExpr(p); if (expr) { int prec = getPrec(p->tok); if (prec > 0) { Node *e2 = parseBinaryExpr(p, &expr, prec); if (e2) { #ifdef LOG_PARSE info("解析得到BinaryExpr\n"); #endif expr = e2; } else { nodeFree(expr); expr = 0; } } } return expr; }
static Node *parseBinaryExpr(Parser *p, Node **lhs, int prec) { enum Token op; Node *rhs; int prec2; op = p->tok; p->tok = lexerGetToken(p->lex, &p->tokval, &p->toklen); rhs = parseUnaryExpr(p); if (!rhs) { handleParserError(p, 1, "解析BinaryExpr出错"); return 0; } prec2 = getPrec(p->tok); if (prec2 < 0) { Node *e = (Node *)binaryExprNew(*lhs, op, rhs); if (e) { return e; } } else if (prec2 <= prec) { Node *newlhs = (Node *)binaryExprNew(*lhs, op, rhs); if (newlhs) { *lhs = newlhs; return parseBinaryExpr(p, lhs, prec2); } } else { Node *newrhs = parseBinaryExpr(p, &rhs, prec2); if (newrhs) { rhs = newrhs; Node *e2 = (Node *)binaryExprNew(*lhs, op, rhs); if (e2) { return e2; } } } nodeFree(rhs); return 0; }