예제 #1
0
void prettyDECL(int indent, DECL *decl)
{
  switch(decl->kind){
  case formalK:
    prettyTYPE(decl->type);
    printf("%s",decl->val.formalD.name);
    if(decl->next != NULL)
      printf(", ");
    break;
  case variableK:
    indentprintf(indent, "");
    prettyModifierKind(decl->val.variableD.modifier);
    prettyTYPE(decl->type);
    prettyIDENTIFIER(decl->val.variableD.identifiers);
    if (decl->val.variableD.initialization != NULL) {
      printf(" = ");
      prettyEXP(decl->val.variableD.initialization);
    }
    printf(";\n");
    break;
  case simplevarK:
    prettyTYPE(decl->type);
    printf("%s",decl->val.simplevarD.name);
    if (decl->val.simplevarD.initialization != NULL) {
      printf(" = ");
      prettyEXP(decl->val.simplevarD.initialization);
    }
    if(decl->next != NULL)
      printf(", ");
    break;
  }
  
  if(decl->next != NULL)
    prettyDECL(indent, decl->next);
}
void prettyLVALUE(LVALUE *lvalue)
{
	if(lvalue->id!=NULL) printf(lvalue->id);
	else printf("this");
	if(lvalue->member!=NULL){
		printf("[");
		prettyEXP(lvalue->member);  
		printf("]");
		
	}
}
예제 #3
0
파일: pretty.c 프로젝트: mojingliu/520proj
void prettyFIELDVALUE(FIELDVALUE* f)
{
    if(f == NULL) return;
    if(f->next != NULL)
    {
        prettyFIELDVALUE(f->next);
        fprintf(ofile, ", ");
    }
    prettyID(f->id);
    fprintf(ofile, " = ");
    prettyEXP(f->expr);
}
예제 #4
0
파일: pretty.c 프로젝트: mojingliu/520proj
void prettyPLUG(PLUG* p)
{
    if(p == NULL) return;
    if(p->next != NULL)
    {
        prettyPLUG(p->next);
        fprintf(ofile, ", ");
    }
    prettyID(p->id);
    fprintf(ofile, " = ");
    prettyEXP(p->expr);
}
예제 #5
0
void prettyFORINIT(int indent, FORINIT *forinit)
{
  switch (forinit->kind) {
  case declforinitK:
    prettyDECL(indent, forinit->val.declforinitF);
    break;
  case expforinitK:
    prettyEXP(forinit->val.expforinitF);
    break;
  }
  
  if (forinit->next != NULL) {
    printf(",");
    prettyFORINIT(indent, forinit->next);
  }
}
예제 #6
0
void prettyEXP(EXP *exp)
{ 
  if (exp == NULL)
   return;
  
  switch(exp->kind) {
  case intconstK:
    printf("%i",exp->val.intconstE);   
    break;
  case boolconstK:
    if (exp->val.boolconstE == 1)
      printf("true");
    else
      printf("false");       
    break;
  case stringconstK:
    printf("\"%s\"", exp->val.stringconstE);   
    break;
  case uminusK:
    printf("-");
    prettyEXP(exp->val.uminusE);    
    break;
  case notK:
    printf("!");
    prettyEXP(exp->val.notE.exp);    
    break;
  case lvalueK:
    prettyLVALUE(exp->val.lvalueE);
    break;
  case assignmentK:
    prettyLVALUE(exp->val.assignmentE.left);
    printf("=");
    prettyEXP(exp->val.assignmentE.right);   
    break;
  case equalsK:
    printf("(");
    prettyEXP(exp->val.equalsE.left);
    printf("==");
    prettyEXP(exp->val.equalsE.right);
    printf(")");    
    break;
  case nequalsK:
    printf("(");
    prettyEXP(exp->val.nequalsE.left);
    printf("!=");
    prettyEXP(exp->val.nequalsE.right);
    printf(")");        
    break;
  case lessK:
    printf("(");
    prettyEXP(exp->val.lessE.left);
    printf("<");
    prettyEXP(exp->val.lessE.right);
    printf(")");            
    break;
  case greaterK:
    printf("(");
    prettyEXP(exp->val.greaterE.left);
    printf(">");
    prettyEXP(exp->val.greaterE.right);
    printf(")");         
    break;
  case lequalsK:
    printf("(");
    prettyEXP(exp->val.lequalsE.left);
    printf("<=");
    prettyEXP(exp->val.lequalsE.right);
    printf(")");             
    break;
  case gequalsK:
    printf("(");
    prettyEXP(exp->val.gequalsE.left);
    printf(">=");
    prettyEXP(exp->val.gequalsE.right);
    printf(")");            
    break;
  case plusK:
    printf("(");
    prettyEXP(exp->val.plusE.left);
    printf("+");
    prettyEXP(exp->val.plusE.right);
    printf(")");         
    break;
  case minusK:
    printf("(");
    prettyEXP(exp->val.minusE.left);
    printf("-");
    prettyEXP(exp->val.minusE.right);
    printf(")");       
    break;
  case multK:
    printf("(");
    prettyEXP(exp->val.multE.left);
    printf("*");
    prettyEXP(exp->val.multE.right);
    printf(")");     
    break;
  case divK:
    printf("(");
    prettyEXP(exp->val.divE.left);
    printf("/");
    prettyEXP(exp->val.divE.right);
    printf(")");         
    break;
  case moduloK:
    printf("(");
    prettyEXP(exp->val.moduloE.left);
    printf("%%");
    prettyEXP(exp->val.moduloE.right);
    printf(")");        
    break;
  case andK:
    printf("(");
    prettyEXP(exp->val.andE.left);
    printf("&&");
    prettyEXP(exp->val.andE.right);
    printf(")");       
    break;
  case orK:
    printf("(");
    prettyEXP(exp->val.orE.left);
    printf("||");
    prettyEXP(exp->val.orE.right);
    printf(")");         
    break;
  case callK:
    printf("%s", exp->val.callE.name);
    printf("(");
    if (exp->val.callE.arguments != NULL)
      prettyEXP(exp->val.callE.arguments);
    printf(")");    
    break;
  case castK:
    printf("(");
    prettyTYPE(exp->val.castE.type);
    printf(")");
    prettyEXP(exp->val.castE.exp);    
    break;
  
  }
  
  if(exp->next != NULL) {
    printf(",");  
    prettyEXP(exp->next);  
  }  
}
예제 #7
0
void prettySTM(int indent, STM *stm)
{
  switch (stm->kind) {
    case skipK:
      indentprintf(indent, ";\n");
      break;
    case expK:
      indentprintf(indent, "");
      prettyEXP(stm->val.expS);
      printf(";\n");
      break;
     case declstmK:
      prettyDECL(indent, stm->val.declstmS);
      break;
    case returnK:
      indentprintf(indent, "return ");
      if (stm->val.returnS.exp != NULL)
        prettyEXP(stm->val.returnS.exp);
      printf(";\n");
      break;
    case ifK:
      indentprintf(indent, "if (");
      prettyEXP(stm->val.ifS.condition);
      printf(")");
      prettySTM(indent, stm->val.ifS.body);
      break;
    case ifelseK:
      indentprintf(indent, "if (");
      prettyEXP(stm->val.ifelseS.condition);
      printf(")");
      prettySTM(indent, stm->val.ifelseS.thenpart);
      indentprintf(indent, " else ");
      prettySTM(indent, stm->val.ifelseS.elsepart);
      break;
    case whileK:
      indentprintf(indent, "while (");
      prettyEXP(stm->val.whileS.condition);
      printf(") ");
      prettySTM(indent, stm->val.whileS.body);
      break;
    case forK:
      indentprintf(indent, "for (");
      prettyFORINIT(indent, stm->val.forS.inits);
      printf("; ");
      prettyEXP(stm->val.forS.condition);
      printf("; ");
      prettyEXP(stm->val.forS.updates);
      printf(") ");
      prettySTM(indent, stm->val.forS.body);
      break;
  case sequenceK:
      prettySTM(indent, stm->val.sequenceS.first);
      prettySTM(indent, stm->val.sequenceS.second);
      break;
  case scopeK:
      printf("\n");
      indentprintf(indent, "{ /* new scope */\n");
      #if (PRINTSYMBOL)
        prettySymbolTable(indent+2, stm->val.scopeS.sym);
      #endif
      prettySTM(indent+1, stm->val.scopeS.stm);
      indentprintf(indent, "}\n");
      break;
  case setintK:
      indentprintf(indent,"setint(");
      prettyEntity(stm->val.setintS.kind);
      printf(", ");
      prettyEXP(stm->val.setintS.modelname);
      printf(",");      
      prettyEXP(stm->val.setintS.nr);
      printf(",");
      prettyEXP(stm->val.setintS.val);
      printf(")");
      break;
  case sleepK:
      indentprintf(indent,"sleep(");
      prettyEXP(stm->val.sleepS.time);
      printf(")"); 
      break;
   }
}
예제 #8
0
void prettyEXP(EXP *e, EXP *parent)
{
switch (e->kind) {

    /* Base case */
    case idK:
         printf("%s",e->val.idE);
         break;

    /* Base case */
    case intconstK:
         printf("%i",e->val.intconstE);
         break;

    /* Case exp * exp 
     * We only print the opening and closing parentheses
     * when the child node's operator has a lower 
     * precedence than the parent's node operator.
     */
    case timesK:
         if(isLowerPrecedence(e, parent)) 
         {
            printf("(");
         }
         prettyEXP(e->val.timesE.left, e);
         printf("*");
         prettyEXP(e->val.timesE.right, e);
         if(isLowerPrecedence(e, parent)) 
         {
            printf(")");
         }
         break;
    case divK:
         if(isLowerPrecedence(e, parent)) 
         {
            printf("(");
         }
         prettyEXP(e->val.divE.left, e);
         printf("/");
         prettyEXP(e->val.divE.right, e);
         if(isLowerPrecedence(e, parent)) 
         {
            printf(")");
         }
         break;
    case modK:
         if(isLowerPrecedence(e, parent)) 
         {
            printf("(");
         }
         prettyEXP(e->val.modE.left, e);
         printf("%%");
         prettyEXP(e->val.modE.right, e);
         if(isLowerPrecedence(e, parent)) 
         {
            printf(")");
         }
         break;
    case absK:
         printf("abs(");
         prettyEXP(e->val.absE.right, e);
         printf(")");
         break;
    case negK:
         printf("-");
         prettyEXP(e->val.negE.right, e);
         break;
    case exponK:
         if(isLowerPrecedence(e, parent)) 
         {
            printf("(");
         }
         prettyEXP(e->val.exponE.left, e);
         printf("**");
         prettyEXP(e->val.exponE.right, e);
         if(isLowerPrecedence(e, parent)) 
         {
            printf(")");
         }
         break;
    case plusK:
         if(isLowerPrecedence(e, parent)) 
         {
            printf("(");
         }
         prettyEXP(e->val.plusE.left, e);
         printf("+");
         prettyEXP(e->val.plusE.right, e);
         if(isLowerPrecedence(e, parent)) 
         {
            printf(")");
         }
         break;
    case minusK:
         if(isLowerPrecedence(e, parent)) 
         {
            printf("(");
         }
         prettyEXP(e->val.minusE.left, e);
         printf("-");
         prettyEXP(e->val.minusE.right, e);
         if(isLowerPrecedence(e, parent)) 
         {
            printf(")");
         }
         break;
    default:
         printf("Wrong kind");
         break;
  }
}
void prettySTM(int indent, STM *stm)
{
	switch (stm->kind) {
    case skipK:
		indentprintf(indent, ";\n");
		break;
    case expK:
		indentprintf(indent, "");
		prettyEXP(stm->val.expS);
		printf(";\n");
		break;
	case declstmK:
		prettyDECL(indent, stm->val.declstmS);
		break;
    case returnK:
		indentprintf(indent, "return ");
		if (stm->val.returnS.exp != NULL)
			prettyEXP(stm->val.returnS.exp);
		printf(";\n");
		break;
    case ifK:
		indentprintf(indent, "if (");
		prettyEXP(stm->val.ifS.condition);
		printf(")");
		prettySTM(indent, stm->val.ifS.body);
		break;
    case ifelseK:
		indentprintf(indent, "if (");
		prettyEXP(stm->val.ifelseS.condition);
		printf(")");
		prettySTM(indent, stm->val.ifelseS.thenpart);
		indentprintf(indent, " else ");
		prettySTM(indent, stm->val.ifelseS.elsepart);
		break;
    case whileK:
		indentprintf(indent, "while (");
		prettyEXP(stm->val.whileS.condition);
		printf(") ");
		prettySTM(indent, stm->val.whileS.body);
		break;
    case forK:
		indentprintf(indent, "for (");
		prettyFORINIT(indent, stm->val.forS.inits);
		printf("; ");
		prettyEXP(stm->val.forS.condition);
		printf("; ");
		prettyEXP(stm->val.forS.updates);
		printf(") ");
		prettySTM(indent, stm->val.forS.body);
		break;
	case sequenceK:
		prettySTM(indent, stm->val.sequenceS.first);
		prettySTM(indent, stm->val.sequenceS.second);
		break;
	case scopeK:
		printf("\n");
		indentprintf(indent, "{ /* new scope */\n");
		prettySTM(indent+1, stm->val.scopeS.stm);
		indentprintf(indent, "}\n");
		break;
	case sleepK:
		indentprintf(indent,"sleep(");
		prettyEXP(stm->val.sleepS.time);
		printf(")"); 
		break;
	case breakK:
		indentprintf(indent,"break;\n");
		break;
	case continueK:
		indentprintf(indent,"continue;\n");
		break;
	}
}
예제 #10
0
파일: pretty.c 프로젝트: mojingliu/520proj
void prettyEXP(EXP* e)
{
    if(e == NULL) return;
    if(e->next != NULL)
    {
        prettyEXP(e->next);
        fprintf(ofile, ", ");
    }
    switch(e->kind)
    {
        case lvalueK:
            prettyLVALUE(e->val.lvalueE);
            break;
        case assignK:
            prettyLVALUE(e->val.assignE.lvalue);
            fprintf(ofile, " = ");
            prettyEXP(e->val.assignE.expr);
            break;
        case equalsK:
            prettyEXP(e->val.equalsE.left);
            fprintf(ofile, " == ");
            prettyEXP(e->val.equalsE.right);
            break;
        case notequalsK:
            prettyEXP(e->val.notequalsE.left);
            fprintf(ofile, " != ");
            prettyEXP(e->val.notequalsE.right);
            break;
        case ltK:
            prettyEXP(e->val.ltE.left);
            fprintf(ofile, " < ");
            prettyEXP(e->val.ltE.right);
            break;
        case gtK:
            prettyEXP(e->val.gtE.left);
            fprintf(ofile, " > ");
            prettyEXP(e->val.gtE.right);
            break;
        case lteK:
            prettyEXP(e->val.lteE.left);
            fprintf(ofile, " <= ");
            prettyEXP(e->val.lteE.right);
            break;
        case gteK:
            prettyEXP(e->val.gteE.left);
            fprintf(ofile, " >= ");
            prettyEXP(e->val.gteE.right);
            break;
        case notK:
            fprintf(ofile, "!");
            prettyEXP(e->val.exprE);
            break;
        case plusK:
            prettyEXP(e->val.plusE.left);
            fprintf(ofile, " + ");
            prettyEXP(e->val.plusE.right);
            break;
        case minusK:
            prettyEXP(e->val.minusE.left);
            fprintf(ofile, " - ");
            prettyEXP(e->val.minusE.right);
            break;
        case multK:
            prettyEXP(e->val.multE.left);
            fprintf(ofile, " * ");
            prettyEXP(e->val.multE.right);
            break;
        case divK:
            prettyEXP(e->val.divE.left);
            fprintf(ofile, " / ");
            prettyEXP(e->val.divE.right);
            break;
        case modK:
            prettyEXP(e->val.modE.left);
            fprintf(ofile, " %% ");
            prettyEXP(e->val.modE.right);
            break;
        case andK:
            prettyEXP(e->val.andE.left);
            fprintf(ofile, " && ");
            prettyEXP(e->val.andE.right);
            break;
        case orK:
            prettyEXP(e->val.orE.left);
            fprintf(ofile, " || ");
            prettyEXP(e->val.orE.right);
            break;
        case joinK:
            prettyEXP(e->val.joinE.left);
            fprintf(ofile, " << ");
            prettyEXP(e->val.joinE.right);
            break;
        case keepK:
            prettyEXP(e->val.keepE.left);
            fprintf(ofile, " \\+ ");
            prettyID(e->val.keepE.right);
            break;
        case removeK:
            prettyEXP(e->val.removeE.left);
            fprintf(ofile, " \\- ");
            prettyID(e->val.removeE.right);
            break;
        case callK:
            prettyID(e->val.callE.left);
            fprintf(ofile, "(");
            prettyEXP(e->val.callE.right);
            fprintf(ofile, ")");
            break;
        case intconstK:
            fprintf(ofile, "%d", e->val.intconstE);
            break;
        case trueK:
            fprintf(ofile, "true");
            break;
        case falseK:
            fprintf(ofile, "false");
            break;
        case stringconstK:
            fprintf(ofile, "\"%s\"", e->val.stringconstE);
            break;
        case tupleK:
            fprintf(ofile, "tuple {");
            prettyFIELDVALUE(e->val.tupleE);
            fprintf(ofile, "}");
            break;
        case parenK:
            fprintf(ofile, "(");
            prettyEXP(e->val.exprE);
            fprintf(ofile, ")");
            break;
    }
}
예제 #11
0
파일: pretty.c 프로젝트: mojingliu/520proj
void prettySTM(STM* s)
{
    int myindent;
    if(s == NULL) return;
    if(s->next != NULL)
    {
        prettySTM(s->next);
        newline();
    }
    myindent = indent;
    
    switch(s->kind) {
        case semicolonK:
            fprintf(ofile, ";");
            /* newline(); */
            break;
        case showK:
            fprintf(ofile, "show ");
            prettyDOCUMENT(s->val.showE.doc);
            if(s->val.showE.rec != NULL)
            {
                prettyRECEIVE(s->val.showE.rec);
            }
            fprintf(ofile, ";");
            /* newline(); */
            break;
        case exitK:
            fprintf(ofile, "exit ");
            prettyDOCUMENT(s->val.doc);
            fprintf(ofile, ";");
            /* newline(); */
            break;
        case returnK:
            fprintf(ofile, "return;");
            /* newline(); */
            break;
        case returnexprK:
            fprintf(ofile, "return ");
            prettyEXP(s->val.expr);
            fprintf(ofile, ";");
            /* newline(); */
            break;
        case ifK:
            fprintf(ofile, "if(");
            prettyEXP(s->val.ifE.expr);
            fprintf(ofile, ")");
            if(s->val.ifE.stm != NULL && s->val.ifE.stm->kind != compoundK)
                indent++;
            newline();
            prettySTM(s->val.ifE.stm);
            break;
        case ifelseK:
            fprintf(ofile, "if(");
            prettyEXP(s->val.ifelseE.expr);
            fprintf(ofile, ")");
            if(s->val.ifelseE.stm1 != NULL && s->val.ifelseE.stm1->kind != compoundK)
                indent++;
            newline();
            prettySTM(s->val.ifelseE.stm1);
            indent = myindent;
            newline();
            fprintf(ofile, "else");
            if(s->val.ifelseE.stm2 != NULL)
            {
                if(s->val.ifelseE.stm2->kind == ifK || s->val.ifelseE.stm2->kind == ifelseK)
                    fprintf(ofile, " ");
                else if(s->val.ifelseE.stm2->kind != compoundK)
                {
                    indent++;
                    newline();
                }
            }
            prettySTM(s->val.ifelseE.stm2);
            break;
        case whileK:
            fprintf(ofile, "while(");
            prettyEXP(s->val.whileE.expr);
            fprintf(ofile, ")");
            if(s->val.whileE.stm != NULL && s->val.whileE.stm->kind != compoundK)
                indent++;
            newline();
            prettySTM(s->val.whileE.stm);
            break;
        case compoundK:
            prettyCOMPOUNDSTM(s->val.compoundstm);
            break;
        case exprK:
            prettyEXP(s->val.expr);
            fprintf(ofile, ";");
            /* newline(); */
            break;
    }
    indent = myindent;
}