/* ************************************************************************* */\r
\r
valueLeaf root; /* radice dell'AST */\r
+valueLeaf *typeRoot=NULL; /* radice degli user type */\r
\r
int numErr=0;\r
\r
valueLeaf *treeId2Alloc(vEnum id,valueLeaf *param1,valueLeaf *param2,const YYLTYPE *locp);\r
valueLeaf *treeId3Alloc(vEnum id,valueLeaf *param1,valueLeaf *param2,valueLeaf *param3,const YYLTYPE *locp);\r
\r
+int eqUserType(valueLeaf *type1,valueLeaf *type2);\r
+int getUserType(valueLeaf *type,valueLeaf **res);\r
+void createUserType(valueLeaf *type);\r
+\r
void prettyPrint(valueLeaf *cur,int indent);\r
\r
/* ************************************************************************* */\r
_type_ Value_STRING _init_ Separator_COLUMN _decl_lst_\r
{\r
$$=treeId3Alloc(E_DECL_VAR,$1,$2,$3,&@1);\r
- $$->next=$5;\r
+ $$->next=$5; \r
}|\r
_stm_lst_\r
{ $$=$1; };\r
{\r
$$=treeId1Alloc(E_TYPE_STRUCT,$3,&@1);\r
$3->next=$5;\r
+\r
+ createUserType($$);\r
}|\r
Type_DWORD\r
{ $$=treeId0Alloc(E_TYPE_DWORD,&@1); }|\r
return;\r
}\r
\r
+/* *************************** gestione degli user type *********** */\r
+\r
+int eqUserType(valueLeaf *type1,valueLeaf *type2)\r
+{\r
+ valueLeaf *tmp1,*tmp2;\r
+\r
+ if(type1->id!=type2->id)\r
+ { return(0); }\r
+\r
+ switch(type1->id)\r
+ {\r
+ case E_TYPE_DWORD:\r
+ case E_TYPE_WORD:\r
+ case E_TYPE_BYTE:\r
+ return(1);\r
+\r
+ case E_TYPE_ARRAY:\r
+ if(type1->param1->inumValue!=type2->param1->inumValue)\r
+ { return(0); }\r
+\r
+ return(eqUserType(type1->param2,type2->param2));\r
+\r
+ case E_TYPE_STRUCT:\r
+ tmp1=type1->param1;\r
+ tmp2=type2->param1;\r
+\r
+ while(tmp1&&tmp2)\r
+ {\r
+ if(!eqUserType(tmp1,tmp2))\r
+ { return(0); }\r
+\r
+ tmp1=tmp1->next;\r
+ tmp2=tmp2->next;\r
+ }\r
+\r
+ return(!(tmp1||tmp2));\r
+\r
+ /* dummy */\r
+ default:\r
+ return(0);\r
+ }\r
+}\r
+\r
+int getUserType(valueLeaf *type,valueLeaf **res)\r
+{\r
+ valueLeaf *tmpRes=typeRoot;\r
+ valueLeaf *lastRes;\r
+ int index=1;\r
+\r
+ while(tmpRes)\r
+ {\r
+ if(eqUserType(type,tmpRes))\r
+ {\r
+ if(res)\r
+ { *res=tmpRes; }\r
+ return(index);\r
+ }\r
+\r
+ lastRes=tmpRes;\r
+ tmpRes=tmpRes->next;\r
+ index++;\r
+ }\r
+\r
+ if(res)\r
+ { *res=lastRes; }\r
+ return(0);\r
+}\r
+\r
+void createUserType(valueLeaf *type)\r
+{\r
+ if(!typeRoot)\r
+ {\r
+ if(!(typeRoot=(valueLeaf *) myMalloc(sizeof(valueLeaf))))\r
+ {\r
+ printf("[%s:%d] ParsError:OutOfMemory%c",__FILE__,__LINE__,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+\r
+ memcpy(typeRoot,type,sizeof(valueLeaf));\r
+ typeRoot->next=NULL;\r
+ }\r
+ else\r
+ {\r
+ valueLeaf *res;\r
+\r
+ if(!getUserType(type,&res))\r
+ {\r
+ if(!(res->next=(valueLeaf *) myMalloc(sizeof(valueLeaf))))\r
+ {\r
+ printf("[%s:%d] ParsError:OutOfMemory%c",__FILE__,__LINE__,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+\r
+ memcpy(res->next,type,sizeof(valueLeaf));\r
+ res->next->next=NULL;\r
+ }\r
+ }\r
+}\r
+\r
+/* *************************** prettyPrint ************************ */\r
+\r
void prettyPrintString(char *str)\r
{\r
printf("([");\r
}\r
}\r
\r
+/* *************************** cPrint ***************************** */\r
+\r
+void cPrintIndent(int indent)\r
+{\r
+ int index;\r
+\r
+ for(index=0;index<indent;index++)\r
+ { printf("\t"); }\r
+}\r
+\r
+void cPrintTypesAux(valueLeaf *type,int field)\r
+{\r
+ if(type->id==E_TYPE_STRUCT)\r
+ { printf("\tuser%d_t\tfield%d;\n\n",getUserType(type,NULL),field); }\r
+ else if(type->id==E_TYPE_ARRAY)\r
+ {\r
+ char bufferDim[256];\r
+ valueLeaf *tmp=type;\r
+\r
+ memset(bufferDim,0,256);\r
+\r
+ while(tmp->id==E_TYPE_ARRAY)\r
+ {\r
+ sprintf(&bufferDim[strlen(bufferDim)],"[%d]",tmp->param1->inumValue+1);\r
+ tmp=tmp->param2;\r
+ }\r
+\r
+ if(tmp->id==E_TYPE_STRUCT)\r
+ { printf("\tuser%d_t\tfield%d%s;\n\n",getUserType(tmp,NULL),field,bufferDim); }\r
+ else if(tmp->id==E_TYPE_DWORD)\r
+ { printf("\t_DWORD_\tfield%d%s;\n\n",field,bufferDim); }\r
+ else if(tmp->id==E_TYPE_WORD)\r
+ { printf("\t_WORD_\tfield%d%s;\n\n",field,bufferDim); }\r
+ else\r
+ { printf("\t_BYTE_\tfield%d%s;\n\n",field,bufferDim); }\r
+ }\r
+ else if(type->id==E_TYPE_DWORD)\r
+ { printf("\t_DWORD_\tfield%d;\n\n",field); }\r
+ else if(type->id==E_TYPE_WORD)\r
+ { printf("\t_WORD_\tfield%d;\n\n",field); }\r
+ else\r
+ { printf("\t_BYTE_\tfield%d;\n\n",field); }\r
+\r
+ if(type->next)\r
+ { cPrintTypesAux(type->next,field+1); }\r
+}\r
+\r
+void cPrintTypes(void)\r
+{\r
+ int index=1;\r
+ valueLeaf *cur=typeRoot;\r
+\r
+ while(cur)\r
+ {\r
+ printf("typedef struct\n");\r
+ printf("{\n");\r
+ cPrintTypesAux(cur->param1,0);\r
+ printf("} user%d_t;\n\n",index);\r
+\r
+ cur=cur->next;\r
+ index++;\r
+ }\r
+}\r
+\r
+void cPrint(valueLeaf *cur,int indent)\r
+{\r
+ /* ** valori ** */\r
+ if(cur->id==E_VALUE_INUM)\r
+ { printf("%lu",cur->inumValue); }\r
+ else if(cur->id==E_VALUE_BYTE)\r
+ { printf("0x%02lX",(_DWORD_) cur->byteValue); }\r
+ else if(cur->id==E_VALUE_WORD)\r
+ { printf("0x%04lX",(_DWORD_) cur->wordValue); }\r
+ else if(cur->id==E_VALUE_DWORD)\r
+ { printf("0x%08lX",cur->dwordValue); }\r
+ else if((cur->id==E_VALUE_ARRAY)||(cur->id==E_VALUE_STRUCT))\r
+ {\r
+ valueLeaf *tmp=cur->param1;\r
+\r
+ printf("{ ");\r
+ while(tmp)\r
+ {\r
+ cPrint(tmp,indent);\r
+ tmp=tmp->next;\r
+ if(tmp)\r
+ { printf(", "); }\r
+ }\r
+ printf(" }");\r
+ }\r
+\r
+ /* ** variabili ** */\r
+ else if((cur->id==E_VAR_ROOT)||(cur->id==E_VAR_STRUCT)||(cur->id==E_VAR_ARRAY))\r
+ {\r
+ valueLeaf *tmp=cur;\r
+\r
+ while(tmp->id!=E_VAR_ROOT)\r
+ {\r
+ tmp->next->last=tmp;\r
+ tmp=tmp->next;\r
+ }\r
+\r
+ while(1)\r
+ {\r
+ if(tmp->id==E_VAR_ROOT)\r
+ { printf("%s",tmp->param1->stringValue); }\r
+ else if(tmp->id==E_VAR_STRUCT)\r
+ { printf(".field%lu",tmp->param1->inumValue); }\r
+ else if(tmp->id==E_VAR_ARRAY)\r
+ { printf("["); cPrint(tmp->param1,indent); printf("]"); }\r
+\r
+ if(tmp==cur)\r
+ { break; }\r
+ else\r
+ { tmp=tmp->last; }\r
+ }\r
+ }\r
+\r
+ /* ** espressioni ** */\r
+ else if(cur->id==E_EXPR_VAR)\r
+ { cPrint(cur->param1,indent); }\r
+ else if(cur->id==E_EXPR_NOT)\r
+ { printf("!("); cPrint(cur->param1,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_COM)\r
+ { printf("~("); cPrint(cur->param1,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_NEG)\r
+ { printf("-("); cPrint(cur->param1,indent); printf(")"); }\r
+ else if((cur->id==E_EXPR_W2B)||(cur->id==E_EXPR_DW2B))\r
+ { printf("((_BYTE_) ("); cPrint(cur->param1,indent); printf("))"); }\r
+ else if((cur->id==E_EXPR_B2W)||(cur->id==E_EXPR_DW2W))\r
+ { printf("((_WORD_) ("); cPrint(cur->param1,indent); printf("))"); }\r
+ else if((cur->id==E_EXPR_B2DW)||(cur->id==E_EXPR_W2DW))\r
+ { printf("((_DWORD_) ("); cPrint(cur->param1,indent); printf("))"); }\r
+ else if(cur->id==E_EXPR_ADD)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") + ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_SUB)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") - ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_MUL)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") * ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_DIV)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") / ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_SHL)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") << ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_SHR)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") >> ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_AND)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") & ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_OR)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") | ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_XOR)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") ^ ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_LT)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") < ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_LTE)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") <= ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_GT)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") > ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_GTE)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") >= ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_EQ)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") == ("); cPrint(cur->param2,indent); printf(")"); }\r
+ else if(cur->id==E_EXPR_NEQ)\r
+ { printf("("); cPrint(cur->param1,indent); printf(") != ("); cPrint(cur->param2,indent); printf(")"); }\r
+\r
+ /* ** statement ** */\r
+ else if(cur->id==E_STM_ASG)\r
+ {\r
+ cPrintIndent(indent); cPrint(cur->param1,indent); printf(" = "); cPrint(cur->param2,indent); printf(";\n\n");\r
+\r
+ if(cur->next)\r
+ { cPrint(cur->next,indent); }\r
+ }\r
+ else if(cur->id==E_STM_WHILE)\r
+ {\r
+ cPrintIndent(indent); printf("while("); cPrint(cur->param1,indent); printf(")\n");\r
+ cPrintIndent(indent+1); printf("{\n");\r
+ cPrint(cur->param2,indent+1);\r
+ cPrintIndent(indent+1); printf("}\n\n");\r
+\r
+ if(cur->next)\r
+ { cPrint(cur->next,indent); }\r
+ }\r
+ else if(cur->id==E_STM_IF)\r
+ {\r
+ valueLeaf *tmp=cur->param1;\r
+\r
+ while(tmp)\r
+ {\r
+ cPrintIndent(indent); printf("%s(",(tmp==cur->param1) ? "if" : "else if"); cPrint(tmp->param1,indent); printf(")\n");\r
+ cPrintIndent(indent+1); printf("{\n");\r
+ cPrint(tmp->param2,indent+1);\r
+ cPrintIndent(indent+1); printf("}\n");\r
+\r
+ tmp=tmp->next;\r
+ }\r
+\r
+ if(cur->param2)\r
+ {\r
+ cPrintIndent(indent); printf("else\n");\r
+ cPrintIndent(indent+1); printf("{\n");\r
+ cPrint(cur->param2,indent+1);\r
+ cPrintIndent(indent+1); printf("}\n");\r
+ }\r
+\r
+ printf("\n");\r
+\r
+ if(cur->next)\r
+ { cPrint(cur->next,indent); }\r
+ }\r
+\r
+ /* ** dichiarazioni ** */\r
+ else if((cur->id==E_DECL_CONST)||(cur->id==E_DECL_VAR))\r
+ {\r
+ valueLeaf *tmp=cur->param1;\r
+ char bufferDim[256];\r
+\r
+ memset(bufferDim,0,256);\r
+\r
+ while(tmp->id==E_TYPE_ARRAY)\r
+ {\r
+ sprintf(&bufferDim[strlen(bufferDim)],"[%d]",tmp->param1->inumValue+1);\r
+ tmp=tmp->param2;\r
+ }\r
+\r
+ cPrintIndent(indent);\r
+ if(cur->id==E_DECL_CONST)\r
+ { printf("const "); }\r
+\r
+ if(tmp->id==E_TYPE_STRUCT)\r
+ { printf("user%d_t\t",getUserType(tmp,NULL)); }\r
+ else if(tmp->id==E_TYPE_DWORD)\r
+ { printf("_DWORD_\t"); }\r
+ else if(tmp->id==E_TYPE_WORD)\r
+ { printf("_WORD_\t"); }\r
+ else\r
+ { printf("_BYTE_\t"); }\r
+\r
+ printf("%s%s",cur->param2->stringValue,bufferDim);\r
+\r
+ if(cur->param3)\r
+ { printf(" = "); cPrint(cur->param3,indent); }\r
+ printf(";\n\n");\r
+\r
+ if(cur->next)\r
+ { cPrint(cur->next,indent); }\r
+ }\r
+}\r
+\r
+/* *************************** main ******************************* */\r
+\r
int main(int argc,char **argv)\r
{\r
int ret;\r
\r
if((!ret)&&(!numErr))\r
{\r
+ printf("(* matita language *)\n\n");\r
printf("definition parsingResult \\def\n");\r
printf(" (PREAST_ROOT\n");\r
prettyPrint(&root,2);\r
- printf(" ).");\r
+ printf(" ).\n\n");\r
+\r
+ printf("(* C language\n\n");\r
+ printf("#ifndef\t__STDC__\n");\r
+ printf("typedef\tunsigned __int32\t_DWORD_;\n");\r
+ printf("typedef\tunsigned __int16\t_WORD_;\n");\r
+ printf("typedef\tunsigned __int8\t_BYTE_;\n");\r
+ printf("#else\n");\r
+ printf("typedef\t__uint32_t\t_DWORD_;\n");\r
+ printf("typedef\t__uint16_t\t_WORD_;\n");\r
+ printf("typedef\t__uint8_t\t_BYTE_;\n");\r
+ printf("#endif\n\n");\r
+ cPrintTypes();\r
+ printf("void main(void)\n");\r
+ printf("{\n");\r
+ cPrint(&root,1);\r
+ printf("\treturn;\n\n");\r
+ printf("}\n\n");\r
+ printf("*)");\r
}\r
\r
return(ret);\r