projects
/
vvhitespace
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
tags
|
clone url
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Added tests for output half of IO IMP.
[vvhitespace]
/
vv_interpreter.c
diff --git
a/vv_interpreter.c
b/vv_interpreter.c
index
8f98fec
..
be52497
100644
(file)
--- a/
vv_interpreter.c
+++ b/
vv_interpreter.c
@@
-21,10
+21,10
@@
void
print_usage(char ** argv)
{
void
print_usage(char ** argv)
{
- printf( "
W
hitespace Interpreter v%d (www.subgeniuskitty.com)\n"
+ printf( "
VV
hitespace Interpreter v%d (www.subgeniuskitty.com)\n"
"Usage: %s -i <file>\n"
" -h Help (prints this message)\n"
"Usage: %s -i <file>\n"
" -h Help (prints this message)\n"
- " -i <file> Specify a
W
hitespace source file to interpret.\n"
+ " -i <file> Specify a
VV
hitespace source file to interpret.\n"
, VERSION, argv[0]
);
}
, VERSION, argv[0]
);
}
@@
-49,7
+49,6
@@
stdin_empty(void)
void
ws_die(size_t * pc, char * msg)
{
void
ws_die(size_t * pc, char * msg)
{
- printf("\n");
printf("SIM_ERROR @ PC %lu: %s\n", *pc, msg);
fflush(stdout);
exit(EXIT_FAILURE);
printf("SIM_ERROR @ PC %lu: %s\n", *pc, msg);
fflush(stdout);
exit(EXIT_FAILURE);
@@
-80,6
+79,10
@@
next_code_byte(uint8_t * code, size_t * pc)
return code[(*pc)++];
}
return code[(*pc)++];
}
+/*
+ * In addition to returning the parsed label, this function advances the PC to
+ * the next instruction.
+ */
uint16_t
parse_label(uint8_t * code, size_t * pc)
{
uint16_t
parse_label(uint8_t * code, size_t * pc)
{
@@
-89,9
+92,24
@@
parse_label(uint8_t * code, size_t * pc)
label = label << 1;
if (c == ' ') label++;
}
label = label << 1;
if (c == ' ') label++;
}
+ // TODO: Where should I handle attempts to access an unitialized label?
+ // For now, leave it undefined in a nasal demon sense.
return label;
}
return label;
}
+void
+populate_labels(uint32_t * labels, uint8_t * code, size_t code_size)
+{
+ size_t cp = 0;
+ while (cp <= code_size) {
+ if (code[cp] == '\v') {
+ uint16_t temp_label = parse_label(code, &cp);
+ labels[temp_label] = cp;
+ }
+ cp++;
+ }
+}
+
void
process_imp_stack(uint8_t * code, size_t * pc, int32_t ** sp)
{
void
process_imp_stack(uint8_t * code, size_t * pc, int32_t ** sp)
{
@@
-104,13
+122,14
@@
process_imp_stack(uint8_t * code, size_t * pc, int32_t ** sp)
switch (next_code_byte(code,pc)) {
case ' ' : sign = 1; break;
case '\t': sign = -1; break;
switch (next_code_byte(code,pc)) {
case ' ' : sign = 1; break;
case '\t': sign = -1; break;
-
case '\n'
: ws_die(pc, "expected sign"); break;
+
default
: ws_die(pc, "expected sign"); break;
}
/* Now, construct the number and push to TOS. */
/* I'm assuming the numbers are read MSb first. */
int32_t temp, number = 0;
while ((temp = next_code_byte(code,pc)) != '\n') {
}
/* Now, construct the number and push to TOS. */
/* I'm assuming the numbers are read MSb first. */
int32_t temp, number = 0;
while ((temp = next_code_byte(code,pc)) != '\n') {
+ if (temp == '\v') ws_die(pc, "non-binary digit in number");
number <<= 1;
if (temp == '\t') number++;
}
number <<= 1;
if (temp == '\t') number++;
}
@@
-138,10
+157,13
@@
process_imp_stack(uint8_t * code, size_t * pc, int32_t ** sp)
case '\n':
stack_pop(sp);
break;
case '\n':
stack_pop(sp);
break;
+ default:
+ ws_die(pc, "malformed stack IMP");
+ break;
}
}
break;
}
}
break;
-
case '\t'
: ws_die(pc, "malformed stack IMP"); break;
+
default
: ws_die(pc, "malformed stack IMP"); break;
}
}
}
}
@@
-166,6
+188,9
@@
process_imp_arithmetic(uint8_t * code, size_t * pc, int32_t ** sp)
/* Multiplication */
stack_push(sp, stack_pop(sp)*stack_pop(sp));
break;
/* Multiplication */
stack_push(sp, stack_pop(sp)*stack_pop(sp));
break;
+ default:
+ ws_die(pc, "malformed arithmetic IMP");
+ break;
}
}
break;
}
}
break;
@@
-182,11
+207,11
@@
process_imp_arithmetic(uint8_t * code, size_t * pc, int32_t ** sp)
temp = stack_pop(sp);
stack_push(sp, stack_pop(sp)%temp);
break;
temp = stack_pop(sp);
stack_push(sp, stack_pop(sp)%temp);
break;
-
case '\n'
: ws_die(pc, "malformed arithmetic IMP"); break;
+
default
: ws_die(pc, "malformed arithmetic IMP"); break;
}
}
break;
}
}
break;
-
case '\n'
: ws_die(pc, "malformed arithmetic IMP"); break;
+
default
: ws_die(pc, "malformed arithmetic IMP"); break;
}
}
}
}
@@
-197,7
+222,6
@@
process_imp_flowcontrol(uint8_t * code, size_t * pc, int32_t ** sp, uint32_t * l
switch (next_code_byte(code,pc)) {
case '\n':
/* Technically another LF is required but we ignore it. */
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 ' ':
fflush(stdout);
exit(EXIT_SUCCESS);
case ' ':
@@
-205,17
+229,26
@@
process_imp_flowcontrol(uint8_t * code, size_t * pc, int32_t ** sp, uint32_t * l
switch (next_code_byte(code,pc)) {
case ' ':
/* Mark a location in the program. */
switch (next_code_byte(code,pc)) {
case ' ':
/* Mark a location in the program. */
- labels[parse_label(code, pc)] = *pc;
+ 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);
break;
case '\t':
/* Call a subroutine. */
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. */
*pc = labels[parse_label(code, pc)];
break;
break;
case '\n':
/* Jump unconditionally to a label. */
*pc = labels[parse_label(code, pc)];
break;
+ default:
+ ws_die(pc, "malformed flow control IMP");
+ break;
}
}
break;
}
}
break;
@@
-224,19
+257,27
@@
process_imp_flowcontrol(uint8_t * code, size_t * pc, int32_t ** sp, uint32_t * l
switch (next_code_byte(code,pc)) {
case ' ':
/* Jump to a label if TOS == 0 */
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. */
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':
/* Return from subroutine. */
*pc = *(--(*rsp));
break;
if (stack_peek(sp,0) < 0) *pc = labels[parse_label(code, pc)];
break;
case '\n':
/* Return from subroutine. */
*pc = *(--(*rsp));
break;
+ default:
+ ws_die(pc, "malformed flow control IMP");
+ break;
}
}
break;
}
}
break;
+ default:
+ ws_die(pc, "malformed flow control IMP");
+ break;
}
}
}
}
@@
-244,9
+285,21
@@
void
process_imp_heap(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
{
switch (next_code_byte(code,pc)) {
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;
- case '\n': 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;
}
}
}
}
@@
-258,9
+311,9
@@
process_imp_io(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
/* Output */
{
switch (next_code_byte(code,pc)) {
/* Output */
{
switch (next_code_byte(code,pc)) {
- case ' ' : /* Output char
acter from TOS */ printf("%c", stack_pop(sp));
break;
- case '\t': /* Output
number from TOS */ printf("%d", stack_pop(sp)
); break;
-
case '\n'
: ws_die(pc, "malformed output IMP"); 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);
}
}
fflush(stdout);
}
@@
-273,16
+326,14
@@
process_imp_io(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
switch (next_code_byte(code,pc)) {
case '\t': /* Input digit */ c -= '0'; /* fallthrough */
case ' ' : /* Input character */ *(*hp + *((*sp)--)) = c; break;
switch (next_code_byte(code,pc)) {
case '\t': /* Input digit */ c -= '0'; /* fallthrough */
case ' ' : /* Input character */ *(*hp + *((*sp)--)) = c; break;
-
case '\n'
: ws_die(pc, "malformed input IMP"); break;
+
default
: ws_die(pc, "malformed input IMP"); break;
}
}
break;
}
}
break;
-
case '\n'
: ws_die(pc, "malformed i/o IMP"); break;
+
default
: ws_die(pc, "malformed i/o IMP"); break;
}
}
}
}
-/* TODO: Continue cleanup here */
-
int
main(int argc, char ** argv)
{
int
main(int argc, char ** argv)
{
@@
-307,25
+358,29
@@
main(int argc, char ** argv)
}
}
if (input == NULL) {
}
}
if (input == NULL) {
- fprintf(stderr, "ERROR: Must specify a
W
hitespace source file with -f flag.\n");
+ fprintf(stderr, "ERROR: Must specify a
VV
hitespace source file with -f flag.\n");
print_usage(argv);
exit(EXIT_FAILURE);
}
/*
print_usage(argv);
exit(EXIT_FAILURE);
}
/*
- * Read just the
W
hitespace source code into memory.
+ * Read just the
VV
hitespace source code into memory.
* We will use the array indices as addresses for the virtual PC when jumping to labels.
*/
size_t ws_code_size = 0;
uint8_t temp_byte;
while (fread(&temp_byte, 1, 1, input)) {
* We will use the array indices as addresses for the virtual PC when jumping to labels.
*/
size_t ws_code_size = 0;
uint8_t temp_byte;
while (fread(&temp_byte, 1, 1, input)) {
- if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n') ws_code_size++;
+ if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n' || temp_byte == '\v') {
+ ws_code_size++;
+ }
}
rewind(input);
uint8_t * ws_code_space = malloc(ws_code_size);
ws_code_size = 0;
while (fread(&temp_byte, 1, 1, input)) {
}
rewind(input);
uint8_t * ws_code_space = malloc(ws_code_size);
ws_code_size = 0;
while (fread(&temp_byte, 1, 1, input)) {
- if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n') ws_code_space[ws_code_size++] = temp_byte;
+ if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n' || temp_byte == '\v') {
+ ws_code_space[ws_code_size++] = temp_byte;
+ }
}
fclose(input);
}
fclose(input);
@@
-333,6
+388,7
@@
main(int argc, char ** argv)
* Setup a stack and heap.
* Assume a 32-bit word size.
*/
* Setup a stack and heap.
* Assume a 32-bit word size.
*/
+ // TODO: Make everything 64-bit.
int32_t * hp = malloc(HEAPSIZE*4);
int32_t * sp = malloc(STACKSIZE*4);
int32_t * hp = malloc(HEAPSIZE*4);
int32_t * sp = malloc(STACKSIZE*4);
@@
-340,7
+396,8
@@
main(int argc, char ** argv)
* Setup the return stack and the label array.
*/
uint32_t * rsp = malloc(RETURNSTACKSIZE*4);
* Setup the return stack and the label array.
*/
uint32_t * rsp = malloc(RETURNSTACKSIZE*4);
- uint32_t labels[65536];
+ uint32_t labels[65536] = {0};
+ populate_labels(labels, ws_code_space, ws_code_size);
/*
* Main Loop
/*
* Main Loop
@@
-349,9
+406,12
@@
main(int argc, char ** argv)
size_t pc = 0; /* Virtual program counter. Operates in the ws_code_space[] address space. */
while (1) {
if (pc >= ws_code_size) {
size_t pc = 0; /* Virtual program counter. Operates in the ws_code_space[] address space. */
while (1) {
if (pc >= ws_code_size) {
- fprintf(stderr, "SIM_ERROR: PC Overrun\n Requested PC: %lu\n Max Address: %lu\n", pc, ws_code_size-1);
+ fprintf(stderr, "SIM_ERROR: PC Overrun\n Requested PC: %lu\n Max Address: %lu\n",
+ pc, ws_code_size-1);
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
+// TODO: Have the SIGTERM signal handler and normal term point return the value
+// on TOS so I can do rudimentary automated tests.
/* Decode the IMPs */
switch (ws_code_space[pc++]) {
/* Decode the IMPs */
switch (ws_code_space[pc++]) {
@@
-382,6
+442,7
@@
main(int argc, char ** argv)
}
}
break;
}
}
break;
+ default: ws_die(&pc, "unexpected VTab"); break;
}
}
}
}