void
ws_die(size_t * pc, char * msg)
{
- printf("\n");
printf("SIM_ERROR @ PC %lu: %s\n", *pc, msg);
fflush(stdout);
exit(EXIT_FAILURE);
switch (next_code_byte(code,pc)) {
case '\n':
/* Technically another LF is required but we ignore it. */
- printf("\n");
fflush(stdout);
exit(EXIT_SUCCESS);
case ' ':
if (next_code_byte(code,pc) != '\v') ws_die(pc,"expected vtab, "
"perhaps a whitespace program, rather than vvhitespace?");
/* Jump to next instruction since labels were parsed during startup. */
- parse_label( code, pc);
+ parse_label(code, pc);
break;
case '\t':
/* Call a subroutine. */
- *((*rsp)++) = *pc;
- *pc = labels[parse_label(code, pc)];
+ {
+ size_t temp_pc = labels[parse_label(code, pc)];
+ *((*rsp)++) = *pc;
+ *pc = temp_pc;
+ }
break;
case '\n':
/* Jump unconditionally to a label. */
switch (next_code_byte(code,pc)) {
case ' ':
/* Jump to a label if TOS == 0 */
+ /* TODO: Does WS pop or peek the TOS? */
if (stack_peek(sp,0) == 0) *pc = labels[parse_label(code, pc)];
break;
case '\t':
/* Jump to a label if TOS < 0. */
+ /* TODO: Does WS pop or peek the TOS? */
if (stack_peek(sp,0) < 0) *pc = labels[parse_label(code, pc)];
break;
case '\n':
process_imp_heap(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
{
switch (next_code_byte(code,pc)) {
- case ' ' : /* Store to heap */ *(*hp + *((*sp)-1)) = **sp; *sp -= 2; break;
- case '\t': /* Retrieve from heap */ **sp = *(*hp + **sp); break;
- default : ws_die(pc, "malformed heap IMP"); break;
+ case ' ' :
+ /* Store to heap */
+ {
+ int32_t value = stack_pop(sp);
+ int32_t addr = stack_pop(sp);
+ *(*hp + addr) = value;
+ }
+ break;
+ case '\t':
+ /* Retrieve from heap */
+ stack_push(sp, *(*hp + stack_pop(sp)));
+ break;
+ default:
+ ws_die(pc, "malformed heap IMP");
+ break;
}
}
/* Output */
{
switch (next_code_byte(code,pc)) {
- case ' ' : /* Output character from TOS */ printf("%c", stack_pop(sp)); break;
- case '\t': /* Output number from TOS */ printf("%d", stack_pop(sp)); break;
+ case ' ' : /* Output char from TOS */ printf("%c", stack_pop(sp)); break;
+ case '\t': /* Output digit from TOS */ printf("%c", stack_pop(sp)+'0'); break;
default : ws_die(pc, "malformed output IMP"); break;
}
fflush(stdout);