Added rest of ALU

This commit is contained in:
Devine Lu Linvega 2024-04-28 20:07:41 -07:00
parent 25837b08f9
commit 0a1b6bfb5c
2 changed files with 18 additions and 10 deletions

View File

@ -1,5 +1,5 @@
RELEASE_flags=-DNDEBUG -O2 -g0 -s RELEASE_flags=-DNDEBUG -O2 -g0 -s
DEBUG_flags=-std=c89 -D_POSIX_C_SOURCE=199309L -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined DEBUG_flags=-std=c99 -D_POSIX_C_SOURCE=199309L -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined
.PHONY: all debug dest run debug test install uninstall format clean archive .PHONY: all debug dest run debug test install uninstall format clean archive

View File

@ -44,17 +44,25 @@ static void
device_write(char *s) device_write(char *s)
{ {
int acc = 0; int acc = 0;
char c = *s, *cap = walk(s), *reg = regs['0']; char c = *s, *cap = walk(s), **reg = regs + '0';
if(reg) { /* phase: ALU */
if(*reg) {
switch(c) { switch(c) {
case '+': /* clang-format off */
for(acc = sint(reg++); *reg; reg++) acc = acc + sint(reg); case '+': for(acc = sint(*reg++); *reg != 0; reg++) acc += sint(*reg); break;
break; case '-': for(acc = sint(*reg++); *reg != 0; reg++) acc -= sint(*reg); break;
case '*': for(acc = sint(*reg++); *reg != 0; reg++) acc *= sint(*reg); break;
case '/': for(acc = sint(*reg++); *reg != 0; reg++) acc /= sint(*reg); break;
case '%': for(acc = sint(*reg++); *reg != 0; reg++) acc %= sint(*reg); break;
case '&': for(acc = sint(*reg++); *reg != 0; reg++) acc &= sint(*reg); break;
case '^': for(acc = sint(*reg++); *reg != 0; reg++) acc ^= sint(*reg); break;
case '|': for(acc = sint(*reg++); *reg != 0; reg++) acc |= sint(*reg); break;
/* clang-format on */
} }
dst_ += snprintf(dst_, 0x10, "%d\n", acc); dst_ += snprintf(dst_, 0x10, "%d", acc);
return; return;
} }
/* phase: string */
if(*s == '(') s++, --cap; if(*s == '(') s++, --cap;
while(s < cap) { while(s < cap) {
c = *s++; c = *s++;
@ -70,7 +78,7 @@ device_write(char *s)
} }
static void static void
device_read(char *s) device_read(void)
{ {
char c; char c;
while(fread(&c, 1, 1, stdin) && c >= ' ') while(fread(&c, 1, 1, stdin) && c >= ' ')
@ -90,7 +98,7 @@ write_reg(char r, char *reg)
return; return;
case '<': /* op: input */ case '<': /* op: input */
case '~': case '~':
device_read(reg); device_read();
return; return;
case '^': /* op: join */ case '^': /* op: join */
if(*reg == '(') reg++, --cap; if(*reg == '(') reg++, --cap;