aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Moon Flower <moon@coffin.ir> 2025-08-09 14:55:50 +0330
committerGravatar Moon Flower <moon@coffin.ir> 2025-08-09 14:55:50 +0330
commite152a124c373c84af98fb100ed631b554db9a582 (patch)
tree474dd9169af934031956d348a83a39c44af4112e
parentremove ref (diff)
macros!!
-rw-r--r--code.pasm6
-rw-r--r--pu.c114
-rw-r--r--pu.out.cbin1016 -> 1405 bytes
3 files changed, 62 insertions, 58 deletions
diff --git a/code.pasm b/code.pasm
index d916970..e07d45b 100644
--- a/code.pasm
+++ b/code.pasm
@@ -1,7 +1,5 @@
-# charcode for c
-con 99
+(1 ar4 con 60 ar2 r4a mem con 1 ar4 con 60 ar3 con 0 ar2 con 2 hlt)
+
-# putchar code
-ar4 con 60 ar2 r4a mem con 1 ar4 con 60 ar3 con 0 ar2 con 2 hlt
hlt \ No newline at end of file
diff --git a/pu.c b/pu.c
index 4099ae4..8e19f10 100644
--- a/pu.c
+++ b/pu.c
@@ -8,12 +8,16 @@
#pragma clang diagnostic ignored "-Wc23-extensions"
#pragma clang diagnostic ignored "-Wnull-character"
#pragma clang diagnostic ignored "-Wimplicit-int"
+#pragma clang diagnostic ignored "-Wparentheses"
+#pragma clang diagnostic ignored "-Wempty-body"
+// NOTE: If you want the CPU to crash when there is an illegal instruction, replace the useless "4"
+// in the code with "__builtin_trap()".
// clang-format off
-#define S 65536
+#define S 1<<20
#define H !--c?
#define J while
-m[S],c,a;pu(){J((c=m[m[7]++]))H*m=m[m[7]++]:c--<9?!a?m[c]=*m:0:8>(c-=8)?*m=m[c]:!(c-=8)?m[m[1]]=*m:H*m=a:H a+=*m:H a-=*m:H a=!a:H a=0:0;}
+m[S],c,a;pu(){J(c=m[m[7]++])H*m=m[m[7]++]:c--<9?!a?m[c]=*m:0:8>(c-=8)?*m=m[c]:!(c-=8)?m[m[1]]=*m:H*m=a:H a+=*m:H a-=*m:H a=!a:H a=0:4;}
// clang-format on
// a is acc
@@ -41,8 +45,8 @@ m[S],c,a;pu(){J((c=m[m[7]++]))H*m=m[m[7]++]:c--<9?!a?m[c]=*m:0:8>(c-=8)?*m=m[c]:
#define W case
#define Q goto M;
char R[]={
-#embed"t"
-};K;char*as(char*I,int*O){char*V=R;J((K=*I)){V=R;switch(K){W 32:W 10:W 9:I++;Q;W 35:J(*++I!=10);Q}strncmp("con ",I,4)?:({*O++=1,*O++=atoi(I+=4);J(*I>47&*I<58)I++;Q});J(*V)V+=3,(strncmp(V-3,I,3)?:({*O++=*V;I+=3;Q})),V+=2;return I;M:}return 0;}
+#embed "t"
+};K;char*Y[1<<9];void*as(char*I,int*O){char*V;J(K=*I){V=R;switch(K){W 32:W 10:W 9:I++;Q;W 35:J(*++I!=10);Q W 40:I++;K=strtol(I,&I,10);Y[K]=I;J(*I++!=41);Q W 41:return O;W 64:O=as(Y[strtol(++I,&I,10)],O);if((long)O<0)return O;Q W 39:*O++=1;I++;*O++=*I++;Q}strncmp("con",I,3)?:({*O++=1,*O++=strtol(I+=3,&I,10);Q});J(*V)V+=3,(strncmp(V-3,I,3)?:({*O++=*V;I+=3;Q})),V+=2;return(char*)-(long)I;M:}return 0;}
// clang-format on
#undef Q
@@ -84,7 +88,6 @@ int emu() {
}
m[ 0 ] = 0;
- m[ 7 ]++;
}
}
@@ -92,48 +95,45 @@ char R_backup[] = "hlt\0 ar1\2 ar2\3 ar3\4 ar4\5 ar5\6 ar6\7 ar7\x8 ar8\x9 r1a\x
"r4a\xD r5a\xE r6a\xF r7a\20 r8a\21 mem\22 aca\23 add\24 sub\25 not\26 clr\27";
void error_debug(char *fail_point, char *code, int sz) {
- if (fail_point != 0) {
- int error_pos = (int) (fail_point - code);
- int i;
-
- int line = 1, col = 1;
- for (i = 0; i < error_pos && i < sz; i++) {
- if (code[ i ] == '\n') {
- line++;
- col = 1;
- } else {
- col++;
- }
- }
-
- fprintf(
- stderr,
- "\x1b[1m\x1b[31merror:\x1b[0m "
- "\x1b[1massembly failed\x1b[0m "
- "at \x1b[36mline %d\x1b[0m, \x1b[36mcolumn %d\x1b[0m "
- "(\x1b[33mposition %d\x1b[0m):\n",
- line,
- col,
- error_pos);
-
- int line_start = error_pos;
- while (line_start > 0 && code[ line_start - 1 ] != '\n') { line_start--; }
-
- int line_end = error_pos;
- while (line_end < sz && code[ line_end ] != '\n' && code[ line_end ] != '\0') {
- line_end++;
- }
-
- fprintf(stderr, " \x1b[1m");
- for (i = line_start; i < line_end; i++) { fputc(code[ i ], stderr); }
- fprintf(stderr, "\x1b[0m\n");
-
- fprintf(stderr, " ");
- for (i = line_start; i < error_pos; i++) {
- fputc((code[ i ] == '\t') ? '\t' : ' ', stderr);
+ if ((long) fail_point >= 0) return;
+
+ fail_point = (char *) -(long) fail_point;
+ int error_pos = (int) (fail_point - code);
+ int i;
+
+ int line = 1, col = 1;
+ for (i = 0; i < error_pos && i < sz; i++) {
+ if (code[ i ] == '\n') {
+ line++;
+ col = 1;
+ } else {
+ col++;
}
- fprintf(stderr, "\x1b[1m\x1b[31m^\x1b[0m \x1b[90mparsing stopped here\x1b[0m\n");
}
+
+ fprintf(
+ stderr,
+ "\x1b[1m\x1b[31merror:\x1b[0m "
+ "\x1b[1massembly failed\x1b[0m "
+ "at \x1b[36mline %d\x1b[0m, \x1b[36mcolumn %d\x1b[0m "
+ "(\x1b[33mposition %d\x1b[0m):\n",
+ line,
+ col,
+ error_pos);
+
+ int line_start = error_pos;
+ while (line_start > 0 && code[ line_start - 1 ] != '\n') { line_start--; }
+
+ int line_end = error_pos;
+ while (line_end < sz && code[ line_end ] != '\n' && code[ line_end ] != '\0') { line_end++; }
+
+ fprintf(stderr, " \x1b[1m");
+ for (i = line_start; i < line_end; i++) { fputc(code[ i ], stderr); }
+ fprintf(stderr, "\x1b[0m\n");
+
+ fprintf(stderr, " ");
+ for (i = line_start; i < error_pos; i++) { fputc((code[ i ] == '\t') ? '\t' : ' ', stderr); }
+ fprintf(stderr, "\x1b[1m\x1b[31m^\x1b[0m \x1b[90mparsing stopped here\x1b[0m\n");
}
int main(int argc, char **argv) {
@@ -148,7 +148,9 @@ int main(int argc, char **argv) {
if (argc == 1) {
printf(
- "Usage:\n %s run <filename>: Execute a file\n %s repl: Open in REPL mode",
+ "Usage:\n"
+ " %s run <filename>: Execute a file\n"
+ " %s repl: Open in REPL mode",
argv[ 0 ],
argv[ 0 ]);
exit(1);
@@ -179,6 +181,9 @@ int main(int argc, char **argv) {
memset(m, 0, 8);
m[ 7 ] = 1024;
emu();
+ int i;
+ for (i = 0; i < 16; i++) printf("%i ", m[ 1024 + i ]);
+ printf("\n\n");
continue;
}
@@ -205,7 +210,7 @@ int main(int argc, char **argv) {
code = saved;
size = sz;
printf("loaded %i bytes.\n", sz);
-
+
goto SKIP_STRAIGHT;
}
@@ -242,17 +247,14 @@ int main(int argc, char **argv) {
code = realloc(code, size += len);
memcpy(&code[ size - len ], line, len);
- SKIP_STRAIGHT:
+ SKIP_STRAIGHT:
char *exit_code = as(code, &m[ 1024 ]);
- #define COLOR_RED "\e[0;31m"
- #define COLOR_RESET "\e[0m"
-
if (exit_code != 0) {
- printf(COLOR_RED "\n");
+ printf("\n");
error_debug(exit_code, code, size);
- printf("\n" COLOR_RESET);
+ printf("\n");
free(code);
code = old_code;
@@ -268,6 +270,7 @@ int main(int argc, char **argv) {
if (argc == 3 && strcmp(argv[ 1 ], "run") != 0) {
printf("Invalid mode - Only supported modes are 'run' and 'repl'\n");
+ return;
}
// read code
@@ -285,7 +288,10 @@ int main(int argc, char **argv) {
// assemble
char *fail_point = as(code, &m[ 1024 ]);
- error_debug(fail_point, code, sz);
+ if (fail_point != 0) {
+ error_debug(fail_point, code, sz);
+ return;
+ }
// run emulation
emu();
diff --git a/pu.out.c b/pu.out.c
index 4f7b348..c62b50a 100644
--- a/pu.out.c
+++ b/pu.out.c
Binary files differ