summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2021-07-27 18:41:06 +0000
committerAndreas Baumann <mail@andreasbaumann.cc>2021-07-27 18:41:06 +0000
commit24194fbe5ea73f7df8b7afaea327e726ec51bfb4 (patch)
treed673baa34639d7ce028a7b0f51ca09c2d96dda83
parentd101bbceade4510afe3c3af768c44539aece2dd8 (diff)
downloadcompilertests-24194fbe5ea73f7df8b7afaea327e726ec51bfb4.tar.gz
compilertests-24194fbe5ea73f7df8b7afaea327e726ec51bfb4.tar.bz2
printing line lumbers
-rw-r--r--miniany/c4.c12
-rw-r--r--miniany/cc.c15
-rw-r--r--miniany/libc-freestanding.c68
-rw-r--r--miniany/libc-hosted.c19
4 files changed, 106 insertions, 8 deletions
diff --git a/miniany/c4.c b/miniany/c4.c
index 692ed1f..4f00e30 100644
--- a/miniany/c4.c
+++ b/miniany/c4.c
@@ -37,7 +37,7 @@ enum {
// 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,MALC,FREE,MSET,MCMP,EXIT };
+ OPEN,READ,CLOS,PRTF,PUTS,GETC,PUTC,PUTI,PUTN,MALC,FREE,MSET,MCMP,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,MALC,FREE,MSET,MCMP,EXIT,"[*++le * 5]);
+ "OPEN,READ,CLOS,PRTF,PUTS,GETC,PUTC,PUTI,PUTN,MALC,FREE,MSET,MCMP,EXIT,"[*++le * 5]);
if (*le <= ADJ) printf(" %d\n", *++le); else printf("\n");
}
}
@@ -375,7 +375,7 @@ int main(int argc, char **argv)
p = "char else enum if int void return sizeof while "
"EOF EXIT_SUCCESS "
- "open read close printf puts getchar putchar malloc free memset memcmp exit void main";
+ "open read close printf putstring getchar putchar putint putnl malloc free memset memcmp 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;
@@ -543,9 +543,11 @@ 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\n", (char *)t[-1]); }
+ else if (i == PUTS) { t = sp + pc[1]; a = printf("%s", (char *)t[-1]); }
else if (i == GETC) a = getchar();
- else if (i == PUTC) putchar(*sp);
+ 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);
diff --git a/miniany/cc.c b/miniany/cc.c
index df89ff7..1637627 100644
--- a/miniany/cc.c
+++ b/miniany/cc.c
@@ -13,7 +13,6 @@ int getChar( )
if( c == '\n' ) {
col = 1;
row++;
- putchar( '$' );
}
return c;
}
@@ -25,11 +24,21 @@ int main( int argc, char **argv )
col = 1;
row = 1;
- puts( "Hello CC" );
+ putstring( "Hello CC" );
+ putnl( );
c = getChar( );
+ putint( row );
+ putstring( ": " );
while( c != EOF ) {
- putchar( c );
+ if( c == '\n' ) {
+ putchar( '$' );
+ putchar( c );
+ putint( row );
+ putstring( ": " );
+ } else {
+ putchar( c );
+ }
c = getChar( );
}
diff --git a/miniany/libc-freestanding.c b/miniany/libc-freestanding.c
index b4e48f8..8b1a378 100644
--- a/miniany/libc-freestanding.c
+++ b/miniany/libc-freestanding.c
@@ -6,6 +6,8 @@
*
*/
+#include <stddef.h>
+
int strlen( char *s )
{
char *p;
@@ -139,6 +141,72 @@ int putchar( int c )
return c;
}
+static void strreverse( char *s )
+{
+ char *end = s + strlen( s ) - 1;
+
+ while( s < end ) {
+ *s ^= *end;
+ *end ^= *s;
+ *s ^= *end;
+ s++;
+ end--;
+ }
+}
+
+char *itoa( int v, char *s, int base )
+{
+ static char digit[] = "0123456789ABCDEF";
+ int sign = 0;
+ char *p = s;
+
+ if( base < 2 || base > 16 ) {
+ return NULL;
+ }
+
+ if( v < 0 ) {
+ v = -v;
+ sign = 1;
+ }
+
+ do {
+ *p++ = digit[v % base];
+ } while( ( v /= base ) > 0 );
+
+ if( sign ) {
+ *p++ = '-';
+ }
+ *p = '\0';
+
+ strreverse( s );
+
+ return s;
+}
+
+int putint( int i )
+{
+ char buf[17];
+
+ itoa( i, buf, 10 );
+ print_string( buf );
+
+ return i;
+}
+
+int putstring( char *s )
+{
+ print_string( s );
+
+ return 0;
+}
+
+int putnl( )
+{
+ putchar( '\n' );
+
+ return 0;
+}
+
void exit( int status )
{
syscall1( SYSCALL_EXIT, status );
diff --git a/miniany/libc-hosted.c b/miniany/libc-hosted.c
index 8cb4d46..4c663bf 100644
--- a/miniany/libc-hosted.c
+++ b/miniany/libc-hosted.c
@@ -5,3 +5,22 @@
#include <stdio.h>
#include <stdlib.h>
+
+int putstring( char *s )
+{
+ printf( "%s", s );
+
+ return 0;
+}
+
+int putint( int i )
+{
+ printf( "%d", i );
+
+ return i;
+}
+
+int putnl( void )
+{
+ return puts( "" );
+}