⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ast.c

📁 Creation d un mini-interpreteur C
💻 C
字号:
/* AST.c par GROBOST.Frederick */#include <string.h>#include <assert.h>#include <stdlib.h>#include <malloc.h>#include <ast.h>struct arithExprBinary {  arithBinaryOp op;  arithExpr fg;  arithExpr fd;};struct arithExpr_ {  arithExprType type;  union {    int number;    char *ident;    struct arithExprBinary *binary;  } expr;};struct boolExprBinary {  boolBinaryOp op;  boolExpr fg;  boolExpr fd;};struct boolExprUnary {  boolUnaryOp op;  boolExpr expr;};struct boolExpr_ {  boolExprType type;  union {    struct boolExprUnary *unaryboolexpr;    struct boolExprBinary *binaryboolexpr;    struct arithExprBinary *binaryarithcmpexpr;  } expr;};struct ifThenElseStmt {  boolExpr expr;  statement stmt1;  statement stmt2;};struct whileStmt {  boolExpr expr;  statement stmt;};struct assignStmt {  char *ident;  arithExpr expr;};struct statement_ {  stmtType type;  union {    struct ifThenElseStmt *cond;    struct whileStmt *loop;    struct assignStmt *assign;    stmtList block;    char *print;  } stmt;};static struct statement_ break_ = {BREAK,NULL};static struct statement_ continue_ = {CONTINUE,NULL};static struct statement_ empty_ = {EMPTY,NULL};struct stmtList_node_ {  statement elt;  struct stmtList_node_ *next;};typedef struct stmtList_node_ *stmtList_node;struct stmtList_ {  stmtList_node head;   stmtList_node current;  stmtList_node tail;};struct declList_node_ {  declaration elt;  struct declList_node_ *next;};typedef struct declList_node_ *declList_node;struct declList_ {  declList_node head;   declList_node current;  declList_node tail;};struct program_ {  char *name;  declList decls;  stmtList stmts;};/*******************C'est Parti***************************************//* Ces fonctions vont nous permettre de creer les listes de declarations ainsi que les listes de statements *//* Creation d'un nouveau programme avec nom, declaration et statement */program program_new (char *progname, declList decls, stmtList stmts){  struct program_ * p = (program)malloc(sizeof(struct program_));  p->name=progname;  p->decls=decls;  p->stmts=stmts;  return p;}/* definition de la declaration vide */declList declListEmpty () {  struct declList_ *decl = (declList)malloc(sizeof(struct declList_)); /* on alloue la memoire pour la declaration */	 	return decl;}/* creation d'une declaration */declaration declaration_new (char *ident) {   declList_node decl=malloc(sizeof(struct declList_node_));	  decl->elt = ident;	  return decl->elt; }/* definition d'un statement vide */stmtList stmtListEmpty () {	   struct stmtList_ *stmt = (stmtList)malloc(sizeof(struct stmtList_));	 return stmt;}/* definition pour la conditionnelle */statement stmtCond_new (boolExpr e, statement stmt1, statement stmt2) {  struct statement_ *condstmt = (statement)malloc(sizeof(struct statement_));  /* allocation de memoire pour la condition if et les statement then et else */    condstmt->stmt.cond=malloc(sizeof(struct ifThenElseStmt));  condstmt->type = COND;  condstmt->stmt.cond->expr=e;  condstmt->stmt.cond->stmt1 = stmt1;  condstmt ->stmt.cond->stmt2 = stmt2;    return condstmt;}/* Pour la boucle */statement stmtWhile_new (boolExpr e, statement stmt) { struct  statement_ *whilestmt = (statement)malloc(sizeof(struct statement_)); whilestmt->stmt.loop = malloc(sizeof(struct whileStmt));   whilestmt->type = WHILE; whilestmt->stmt.loop->expr=e; whilestmt->stmt.loop->stmt = stmt; return whilestmt;}/* Pour une affectation */statement stmtAssign_new (char *ident, arithExpr e) {  struct statement_ *assignstmt = (statement)malloc(sizeof(struct statement_));  assignstmt->stmt.assign=malloc(sizeof(struct assignStmt));  assignstmt->type = ASSIGN;  assignstmt->stmt.assign->expr=e;  assignstmt->stmt.assign->ident=ident;  return assignstmt;}/* Pour un break */statement stmtBreak_new () {  struct statement_ *breakstmt = (statement)malloc(sizeof(struct statement_));  breakstmt->type=BREAK;  return breakstmt;}/* Pour un continue */statement stmtContinue_new () {  struct statement_ *continuestmt = (statement)malloc(sizeof(struct statement_));  continuestmt->type=CONTINUE;  return continuestmt;}statement stmtEmpty_new () {  statement emptystmt = (statement)malloc(sizeof(*emptystmt));  *emptystmt=empty_;  return emptystmt;}statement stmtPrint_new (char * name) {  struct statement_ *printstmt = (statement)malloc(sizeof(struct statement_));      printstmt->type = PRINT;  printstmt->stmt.print = name;  return printstmt;}statement stmtBlock_new (stmtList list) { struct statement_ *blockstmt = (statement)malloc(sizeof(struct statement_));    blockstmt->type = BLOCK;    blockstmt->stmt.block = list;  return blockstmt;}arithExpr arithExprNumber_new (int number) {  struct arithExpr_ *num = (arithExpr)malloc(sizeof(struct arithExpr_));    num->type=NUMBER;    num->expr.number = number;    return num;    }arithExpr arithExprIdent_new  (char *ident) {  struct arithExpr_ *idt = (arithExpr)malloc(sizeof(struct arithExpr_));    idt->type=IDENT;   idt->expr.ident = ident;      return idt;}arithExpr arithExprBinary_new (arithBinaryOp op, arithExpr fg, arithExpr fd) {    struct arithExpr_ *e = (arithExpr)malloc(sizeof(struct arithExpr_));    e->expr.binary=malloc(sizeof(struct arithExprBinary));  e->type=BINARY;    e->expr.binary->op = op;  e->expr.binary->fg = fg;  e->expr.binary->fd = fd;  return e;}boolExpr boolExprUnaryBool_new (boolUnaryOp op, boolExpr e) { struct boolExpr_ *bexpr = (boolExpr)malloc(sizeof(struct boolExpr_)); bexpr->expr.unaryboolexpr=malloc(sizeof(struct boolExprUnary)) ; bexpr ->type = UNARYBOOLEXPR   ; bexpr->expr.unaryboolexpr->op = op; bexpr->expr.unaryboolexpr->expr=e;  return bexpr;  }boolExpr boolExprBinaryBool_new (boolBinaryOp op, boolExpr fg, boolExpr fd) { struct boolExpr_ *bexpr = (boolExpr)malloc(sizeof(struct boolExpr_)); bexpr->expr.binaryboolexpr=malloc(sizeof(struct boolExprBinary)) ; bexpr ->type = BINARYBOOLEXPR; bexpr->expr.binaryboolexpr->op = op; bexpr->expr.binaryboolexpr->fg=fg; bexpr->expr.binaryboolexpr->fd=fd;  return bexpr;  }boolExpr boolExprBinaryCmpArith_new (arithBinaryOp op, arithExpr fg, arithExpr fd) { struct boolExpr_ *bexpr = (boolExpr)malloc(sizeof(struct boolExpr_)); bexpr->expr.binaryarithcmpexpr=malloc(sizeof(struct arithExprBinary)) ; bexpr ->type = BINARYARITHCMPEXPR   ; bexpr->expr.binaryarithcmpexpr->op = op; bexpr->expr.binaryarithcmpexpr->fg=fg; bexpr->expr.binaryarithcmpexpr->fd=fd;  return bexpr;  }stmtType stmtGetType (statement s) {  return(s->type);}arithExprType arithExprGetType (arithExpr e) {  return(e->type);}boolExprType boolExprGetType (boolExpr e) {  return(e->type);}declaration declListGetCurrent (declList l) {  return(l->current->elt);}int declListHasMoreElt (declList l) {  if(l->current)    return 1;  else    return 0;}void declListMoveCurrent (declList l) {  l->current = l->current->next;}void declListGoToHead (declList l) {  l->current = l->head;}declList addToDeclList(declList list, declaration d) {  declList_node decl = malloc(sizeof(declList_node));  decl->elt=d;  decl->next=NULL;  if(!list->tail) {    list->head=decl;    list->current=decl;    list->tail=decl;  }  else {    list->tail->next=decl;    list->tail=decl;  }  return list;}stmtList addToStmtList(stmtList list, statement d) {stmtList_node stmt = malloc(sizeof(stmtList_node));  stmt->elt=d;  stmt->next=NULL;    if(!list->tail) {    list->head=stmt;    list->current=stmt;    list->tail=stmt;  }  else {    list->tail->next=stmt;    list->tail=stmt;  }  return list;}statement stmtListGetCurrent (stmtList l) {  return l->current->elt;}int stmtListHasMoreElt (stmtList l) { if(l->current)    return 1;  else    return 0;  }void stmtListMoveCurrent (stmtList l) {  l->current = l->current->next;}void  stmtListGoToHead (stmtList l) {  l->current = l->head;}void stmtListGoToEnd (stmtList l) {  l->current = l->tail;}char* programGetName (program p) {  return p->name;}declList programGetDecls (program p) {  return p->decls;}stmtList programGetStmts (program p) {  return p->stmts;}char* stmtGetPrint (statement s) {  return s->stmt.print;}char* stmtGetAssignIdent (statement s) {  return s->stmt.assign->ident;}arithExpr stmtGetAssignExpr (statement s) {  return s->stmt.assign->expr;}boolExpr  stmtGetCondExp (statement s) {  return s->stmt.cond->expr;}statement stmtGetCondThen (statement s) {    return s->stmt.cond->stmt1;}statement stmtGetCondElse (statement s) {      return s->stmt.cond->stmt2;}boolExpr  stmtGetWhileExp (statement s) {  return s->stmt.loop->expr;}statement stmtGetWhileBody (statement s) {  return s->stmt.loop->stmt;}stmtList stmtGetBlock (statement s) {  return s->stmt.block;}int arithExprGetNumber   (arithExpr e) {  return e->expr.number;}char * arithExprGetIdent    (arithExpr e) {  return e->expr.ident;}arithBinaryOp arithExprGetBinaryOp (arithExpr e) {  return e->expr.binary->op;}arithExpr arithExprGetBinaryFg (arithExpr e) {  return e->expr.binary->fg;}arithExpr arithExprGetBinaryFd (arithExpr e) {  return e->expr.binary->fd;}arithBinaryOp boolExprGetArithBinaryOp (boolExpr e) {  return e->expr.binaryarithcmpexpr->op;}arithExpr boolExprGetArithBinaryFg (boolExpr e) {  return e->expr.binaryarithcmpexpr->fg;}arithExpr boolExprGetArithBinaryFd (boolExpr e) {  return e->expr.binaryarithcmpexpr->fd;}boolUnaryOp boolExprGetBoolUnaryOp   (boolExpr e) {  return e->expr.unaryboolexpr->op;}boolExpr boolExprGetBoolUnary     (boolExpr e) {  return e->expr.unaryboolexpr->expr;}boolBinaryOp boolExprGetBoolBinaryOp  (boolExpr e) {  return e->expr.binaryboolexpr->op;}boolExpr boolExprGetBoolBinaryFg  (boolExpr e) {  return e->expr.binaryboolexpr->fg;}boolExpr boolExprGetBoolBinaryFd  (boolExpr e) {  return e->expr.binaryboolexpr->fd;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -