📄 indent51.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXLINE 512
#define OPCODE_NUM 52
int divToken(char linet[]);
int getline(char line[]);
void copy(char to[], char from[]);
int SToUpper(char line[], int n);
int AddBlank(char line[][512]);
char *OpCodes[OPCODE_NUM] = {
"NOP", "MOVX", "MOVC", "MOV",
"PUSH", "POP", "XCHD", "XCH",
"SWAP", "ADDC", "DJNZ", "ADD",
"SUBB", "INC", "DEC", "MUL",
"DIV", "DA", "ANL", "ORL",
"XRL", "CLR", "CPL", "RL",
"RLC", "RR", "RRC", "SETB",
"ACALL", "LCALL", "RETI", "RET",
"AJMP", "LJMP", "SJMP", "JZ",
"JNZ", "JC", "JNC", "JBC",
"JNB", "JB", "CJNE", "JMP",
"DATA", "BIT", "EQU", "DS",
"DB", "DW", "END", "ORG"
};
main(){
int len;
char line[MAXLINE], linet[MAXLINE], *reline;
printf("%s\n", ";indented by indent51(written by Tary 2006-05-17)");
do {
len = getline(line);
SToUpper(line, len);
copy(linet, line);
if(divToken(linet))
printf("%s\n", linet);
else
printf("%s\n", line);
} while(len >= 0);
return 0;
}
int SToUpper(char line[], int n){
int i;
for (i = 0; i < n && !(line[i] == ';' || line[i] == '\n'); ++i)
line[i] = toupper(line[i]);
return 1;
}
int divToken(char linet[]) {
char *si, *CPos, *CPCom;
char Sign[50] = "\0",
OpCode[10] = {0},
OpAnd[3][512] = {{0}, {0}, {0}},
Comment[MAXLINE] = "\t\t";
int i, j, Ocur = 0;
for(i = 0; i < OPCODE_NUM; ++i){
j = strlen(OpCodes[i]);
if(NULL != (CPos = strstr(linet, OpCodes[i])) &&
(CPos == linet || *(CPos-1) == ':' ||
*(CPos-1) == '\t' || *(CPos-1) == ' ') &&
(*(CPos+j) == '\t' || CPos[j] == ' '||
CPos[j] == ';' || CPos[j] == '\n')) {
Ocur = 1;
break;
}
}
if (!Ocur) return 0;
strcpy(OpCode, OpCodes[i]);
for (i = 0, si = linet; (*si == ' ' || *si == '\t'); ++si);
if (CPos == si) goto ProcOpAnd;
for (; si < CPos; si++){
if (*si == ' ' || *si == '\t') continue;
Sign[i++] = *si;
}
ProcOpAnd:
Sign[i++] = '\t';
Sign[i] = '\0';
CPos += strlen(OpCode);
for (si = CPos; (*si == ' ' || *si == '\t')
&& *si != '\n'; ++si);
Ocur = 0;
if (*si != '\n') {
CPos = si;
if (NULL != (CPCom = strchr(si, ';'))){
Ocur = 1;
strcat(Comment, CPCom);
while(*--CPCom == '\t' || *CPCom == ' ');
*(CPCom+1) = '\0';
}
if (';' != *(CPos = si)) strcpy (OpAnd[2], CPos);
}else ;
linet[0] = 0;
strcat (linet, Sign);
j = AddBlank(OpAnd);
if (j > 0) strcat(OpCode, "\t");
strcat (linet, OpCode);
/*printf("j=%d\n", j);*/
for (i = 0; i < j; ++i) {
/*printf("%d %s\n", i, OpAnd[i]);*/
strcat (linet, OpAnd[i]);
}
if (Ocur) strcat (linet, Comment);
return 1;
}
int AddBlank(char line[][512]){
char *CPos, *CPB;
int i;
if (strlen(line[2]) == 0) return 0;
if ((CPos = strchr(line[2], ',')) == NULL) {
strcpy(line[0], line[2]);
return 1;
}
*CPos++ = '\0';
strcpy(line[0], line[2]);
strcat(line[0], ", ");
/*printf("1=%s2=%s\n", line[0], CPos);*/
if ((CPos = strchr((CPB = CPos), ',')) == NULL) {
while(*CPB == ' ') ++CPB;
strcpy(line[1], CPB);
return 2;
}
*CPos++ = '\0';
while(*CPB == ' ') ++CPB;
strcpy(line[1], CPB);
strcat(line[1], ", ");
while(*CPos == ' ') ++CPos;
strcpy(line[2], CPos);
return 3;
}
/*read a line and return the length of it*/
int getline(char s[]){
int c, i;
for(i = 0; (c = fgetc(stdin)) != EOF && c != '\n'; ++i)
s[i] = c;
s[i] = '\0';
return (c == EOF)? -1: i;
}
/*copy string from array arg2 to array arg1*/
void copy(char to[], char from[]){
int i;
i = 0;
while((to[i] = from[i]) != '\0')
++i;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -