diff --git a/libacc/acc.c b/libacc/acc.cpp similarity index 96% rename from libacc/acc.c rename to libacc/acc.cpp index 4aa2b3cc2..c2c38c49e 100644 --- a/libacc/acc.c +++ b/libacc/acc.cpp @@ -20,11 +20,14 @@ 3. This notice may not be removed or altered from any source distribution. */ +#include +#include #include #include #include #include +class compiler { /* vars: value of variables loc : local variable index glo : global variable index @@ -64,12 +67,12 @@ FILE* file; #define TAG_TOK ' ' #define TAG_MACRO 2 -pdef(t) +void pdef(int t) { *(char *)dstk++ = t; } -inp() +void inp() { if (dptr) { ch = *(char *)dptr++; @@ -82,13 +85,13 @@ inp() /* printf("ch=%c 0x%x\n", ch, ch); */ } -isid() +int isid() { return isalnum(ch) | ch == '_'; } /* read a character constant */ -getq() +void getq() { if (ch == '\\') { inp(); @@ -97,7 +100,7 @@ getq() } } -next() +void next() { int l, a; @@ -224,11 +227,11 @@ void error(char *fmt,...) fprintf(stderr, "%d: ", ftell((FILE *)file)); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); - exit(1); va_end(ap); + exit(1); } -void skip(c) +void skip(int c) { if (tok != c) { error("'%c' expected", c); @@ -236,7 +239,7 @@ void skip(c) next(); } -o(n) +void o(int n) { /* cannot use unsigned, so we must do a hack */ while (n && n != -1) { @@ -246,7 +249,7 @@ o(n) } /* output a symbol and patch all calls to it */ -gsym(t) +void gsym(int t) { int n; while (t) { @@ -261,7 +264,7 @@ gsym(t) #define psym oad /* instruction + address */ -oad(n, t) +int oad(int n, int t) { o(n); *(int *)ind = t; @@ -271,24 +274,24 @@ oad(n, t) } /* load immediate value */ -li(t) +int li(int t) { oad(0xb8, t); /* mov $xx, %eax */ } -gjmp(t) +int gjmp(int t) { return psym(0xe9, t); } /* l = 0: je, l == 1: jne */ -gtst(l, t) +int gtst(int l, int t) { o(0x0fc085); /* test %eax, %eax, je/jne xxx */ return psym(0x84 + l, t); } -gcmp(t) +int gcmp(int t) { o(0xc139); /* cmp %eax,%ecx */ li(0); @@ -297,14 +300,14 @@ gcmp(t) o(0xc0); } -gmov(l, t) +int gmov(int l, int t) { o(l + 0x83); oad((t < LOCAL) << 7 | 5, t); } /* l is one if '=' parsing wanted (quick hack) */ -unary(l) +void unary(int l) { int n, t, a, c; @@ -375,7 +378,7 @@ unary(l) n = *(int *)t; /* forward reference: try dlsym */ if (!n) - n = dlsym(0, last_id); + n = (int) dlsym(0, (char*) last_id); if (tok == '=' & l) { /* assignment */ next(); @@ -426,7 +429,7 @@ unary(l) } } -sum(l) +void sum(int l) { int t, n, a; @@ -468,19 +471,20 @@ sum(l) } } -expr() +void expr() { sum(11); } -test_expr() +int test_expr() { expr(); return gtst(0, 0); } -block(l) + +void block(int l) { int a, n, t; @@ -524,7 +528,7 @@ block(l) } } skip(')'); - block(&a); + block((int) &a); gjmp(n - ind - 5); /* jmp */ gsym(a); } else if (tok == '{') { @@ -550,7 +554,7 @@ block(l) } /* 'l' is true if local declarations */ -decl(l) +void decl(int l) { int a; @@ -599,7 +603,10 @@ decl(l) } } -main(int n, char** t) +public: +compiler(){} + +int compile(int n, char** t) { file = stdin; if (n-- > 1) { @@ -627,3 +634,10 @@ main(int n, char** t) return (*(int (*)())*(int *)(vars + TOK_MAIN)) (n, t); #endif } + +}; + +int main(int argc, char** argv) { + compiler c; + return c.compile(argc, argv); +} \ No newline at end of file diff --git a/libacc/test b/libacc/test index eb4f383be..b4cef93c6 100755 --- a/libacc/test +++ b/libacc/test @@ -1,2 +1,2 @@ #!/bin/sh -gcc acc.c -DTEST -ldl -o tests/acc && tests/acc tests/otcc.c tests/otcc.out && diff tests/otcc.out tests/otcc.out-orig +g++ acc.cpp -DTEST -ldl -o tests/acc && tests/acc tests/otcc.c tests/otcc.out && diff tests/otcc.out tests/otcc.out-orig diff --git a/libacc/tests/hello.out-orig b/libacc/tests/hello.out-orig index bb178c079..1fb7bf5cf 100644 Binary files a/libacc/tests/hello.out-orig and b/libacc/tests/hello.out-orig differ diff --git a/libacc/tests/otcc.out-orig b/libacc/tests/otcc.out-orig index ea32bc8be..2cb08ff5b 100644 Binary files a/libacc/tests/otcc.out-orig and b/libacc/tests/otcc.out-orig differ