grimoire/c/meltdown.c

74 lines
1.5 KiB
C

#include <stdint.h>
#include <stdio.h>
#include <string.h>
uint16_t compile(const char *prog, uint8_t *tape, uint16_t tape_len, uint8_t **idx, uint8_t eval) {
uint16_t elapsed = 1;
while(*prog) {
uint16_t traveled = 1;
if(eval && *prog == '>') {
if(*idx == tape + tape_len) {
*idx = tape;
} else {
*idx += 1;
}
} else if(eval && *prog == '<') {
if(*idx == tape) {
*idx = tape + tape_len;
} else {
*idx -= 1;
}
} else if(eval && *prog == '+') {
if(**idx == (2 << 7) - 1) {
**idx = 0;
} else {
**idx += 1;
}
} else if(eval && *prog == '-') {
if(**idx == 0) {
**idx = (2 << 7) - 1;
} else {
**idx -= 1;
}
} else if(eval && *prog == '.') {
putchar(**idx);
} else if(eval && *prog == ',') {
**idx = getchar();
} else if(*prog == '[') {
uint8_t should_eval = **idx > 0;
if(!eval) {
should_eval = 0;
}
traveled = compile(prog + 1, tape, tape_len, idx, should_eval);
} else if(*prog == ']') {
if(eval && **idx > 0) {
return 0;
} else {
return elapsed + 1;
}
}
elapsed += traveled;
prog += traveled;
}
return 0;
}
int main(int argc, char **argv) {
if(argc != 2) {
return 1;
}
const char *prog = argv[1];
uint8_t tape[30000] = { 0 };
uint16_t tape_len = sizeof(tape);
uint8_t *idx = tape;
uint8_t eval = 1;
compile(prog, tape, tape_len, &idx, eval);
}