summaryrefslogtreecommitdiff
path: root/miniany/c4.c
diff options
context:
space:
mode:
Diffstat (limited to 'miniany/c4.c')
-rw-r--r--miniany/c4.c47
1 files changed, 13 insertions, 34 deletions
diff --git a/miniany/c4.c b/miniany/c4.c
index 4f00e30..d8a588a 100644
--- a/miniany/c4.c
+++ b/miniany/c4.c
@@ -30,14 +30,14 @@ int *e, *le, // current position in emitted code
// tokens and classes (operators last and in precedence order)
enum {
Num = 128, Fun, Sys, Glo, Loc, Id,
- Char, Else, Enum, If, Int, Void, Return, Sizeof, While,
+ Char, Else, Enum, If, Int, Return, Sizeof, While,
Assign, Cond, Lor, Lan, Or, Xor, And, Eq, Ne, Lt, Gt, Le, Ge, Shl, Shr, Add, Sub, Mul, Div, Mod, Inc, Dec, Brak
};
// opcodes
enum { LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,
OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,
- OPEN,READ,CLOS,PRTF,PUTS,GETC,PUTC,PUTI,PUTN,MALC,FREE,MSET,MCMP,EXIT };
+ OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,EXIT };
// types
enum { CHAR, INT, PTR };
@@ -58,7 +58,7 @@ void next()
while (le < e) {
printf("%8.4s", &"LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,"
"OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,"
- "OPEN,READ,CLOS,PRTF,PUTS,GETC,PUTC,PUTI,PUTN,MALC,FREE,MSET,MCMP,EXIT,"[*++le * 5]);
+ "OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,EXIT,"[*++le * 5]);
if (*le <= ADJ) printf(" %d\n", *++le); else printf("\n");
}
}
@@ -97,15 +97,6 @@ void next()
++p;
while (*p != 0 && *p != '\n') ++p;
}
- else if (*p != 0 && *p == '*') {
- ++p;
- while (*p != 0 && *p != '/') {
- if (*p == '\n') line++;
- if (*p == '*');
- ++p;
- }
- ++p;
- }
else {
tk = Div;
return;
@@ -125,18 +116,7 @@ void next()
}
else if (tk == '=') { if (*p == '=') { ++p; tk = Eq; } else tk = Assign; return; }
else if (tk == '+') { if (*p == '+') { ++p; tk = Inc; } else tk = Add; return; }
- else if (tk == '-') {
- if (*p == '-') {
- ++p; tk = Dec;
- }
- else if (*p >= '0' && *p <= '9') {
- if (ival = *p - '0') { p++; while (*p >= '0' && *p <= '9') ival = ival * 10 + *p++ - '0'; }
- ival = -ival;
- tk = Num;
- return;
- }
- else tk = Sub; return;
- }
+ else if (tk == '-') { if (*p == '-') { ++p; tk = Dec; } else tk = Sub; return; }
else if (tk == '!') { if (*p == '=') { ++p; tk = Ne; } return; }
else if (tk == '<') { if (*p == '=') { ++p; tk = Le; } else if (*p == '<') { ++p; tk = Shl; } else tk = Lt; return; }
else if (tk == '>') { if (*p == '=') { ++p; tk = Ge; } else if (*p == '>') { ++p; tk = Shr; } else tk = Gt; return; }
@@ -373,9 +353,9 @@ int main(int argc, char **argv)
memset(e, 0, poolsz);
memset(data, 0, poolsz);
- p = "char else enum if int void return sizeof while "
+ p = "char else enum if int return sizeof while "
"EOF EXIT_SUCCESS "
- "open read close printf putstring getchar putchar putint putnl malloc free memset memcmp exit void main";
+ "open read close printf malloc free memset memcmp getchar putstring putnl putchar putint exit void main";
i = Char; while (i <= While) { next(); id[Tk] = i++; } // add keywords to symbol table
// add library constants
next(); id[Class] = Num; id[Type] = INT; id[Val] = -1;
@@ -388,7 +368,7 @@ int main(int argc, char **argv)
if ((i = read(fd, p, poolsz-1)) <= 0) { printf("read() returned %d\n", i); return -1; }
p[i] = 0;
close(fd);
-
+
// parse declarations
line = 1;
next();
@@ -432,7 +412,6 @@ int main(int argc, char **argv)
ty = INT;
if (tk == Int) next();
else if (tk == Char) { next(); ty = CHAR; }
- else if (tk == Void) { printf("here\n"); }
while (tk == Mul) { next(); ty = ty + PTR; }
if (tk != Id) { printf("%d: bad parameter declaration\n", line); return -1; }
if (id[Class] == Loc) { printf("%d: duplicate parameter definition\n", line); return -1; }
@@ -504,7 +483,7 @@ int main(int argc, char **argv)
printf("%d> %.4s", cycle,
&"LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,"
"OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,"
- "OPEN,READ,CLOS,PRTF,PUTS,GETC,PUTC,MALC,FREE,MSET,MCMP,EXIT,"[i * 5]);
+ "OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,EXIT,"[i * 5]);
if (i <= ADJ) printf(" %d\n", *pc); else printf("\n");
}
if (i == LEA) a = (int)(bp + *pc++); // load local address
@@ -543,15 +522,15 @@ int main(int argc, char **argv)
else if (i == READ) a = read(sp[2], (char *)sp[1], *sp);
else if (i == CLOS) a = close(*sp);
else if (i == PRTF) { t = sp + pc[1]; a = printf((char *)t[-1], t[-2], t[-3], t[-4], t[-5], t[-6]); }
- else if (i == PUTS) { t = sp + pc[1]; a = printf("%s", (char *)t[-1]); }
- else if (i == GETC) a = getchar();
- else if (i == PUTC) a = putchar(*sp);
- else if (i == PUTI) a = printf("%d", (int)t[-1]);
- else if (i == PUTN) a = puts("");
else if (i == MALC) a = (int)malloc(*sp);
else if (i == FREE) free((void *)*sp);
else if (i == MSET) a = (int)memset((char *)sp[2], sp[1], *sp);
else if (i == MCMP) a = memcmp((char *)sp[2], (char *)sp[1], *sp);
+ else if (i == GETC) a = getchar();
+ else if (i == PUTS) { t = sp + pc[1]; a = printf("%s", (char *)t[-1]); }
+ else if (i == PUTN) a = printf("\n");
+ else if (i == PUTC) a = putchar(*sp);
+ else if (i == PUTI) { t = sp + pc[1]; a = printf("%d", (int)t[-1]); }
else if (i == EXIT) { printf("exit(%d) cycle = %d\n", *sp, cycle); return *sp; }
else { printf("unknown instruction = %d! cycle = %d\n", i, cycle); return -1; }
}