Skip to content

Commit

Permalink
Fixed structs, also added simple debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
goodpaul6 committed Dec 6, 2024
1 parent ee3d01c commit 1d8ecad
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
49 changes: 49 additions & 0 deletions tiny/src/std.c
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,53 @@ static TINY_FOREIGN_FUNCTION(GetExecutingLine) {
return Tiny_NewInt(line);
}

static TINY_FOREIGN_FUNCTION(DebugBreak) {
// HACK(Apaar): Very stupid debugger; unsafe

for (;;) {
int pc = thread->pc;
char disBuf[512] = {0};

Tiny_DisasmOne(thread->state, &pc, disBuf, sizeof(disBuf));

printf("%s\n", disBuf);
printf("> ");

char cmd[256] = {0};

if (!fgets(cmd, sizeof(cmd), stdin)) {
break;
}

if (strcmp(cmd, "top2\n") == 0) {
for (int i = 1; i <= 2; ++i) {
int pos = thread->sp - i;

if (pos < 0) {
break;
}

Print(thread->stack[pos], true);
putchar('\n');
}
} else if (strcmp(cmd, "s\n") == 0) {
Tiny_ExecuteCycle(thread);
} else if (strcmp(cmd, "c\n") == 0) {
// HACK(Apaar): We can't return because we've kinda
// probably put stuff on the stack and CALLF resets
// the stack after the foreign function exits sooooo
// we just kinda keep going here

Tiny_Run(thread);
break;
} else {
printf("no such command\n");
}
}

return Tiny_Null;
}

void Tiny_BindStandardLib(Tiny_State *state) {
Tiny_BindConstInt(state, "INT_MAX", INT_MAX);

Expand Down Expand Up @@ -1159,4 +1206,6 @@ void Tiny_BindStandardLib(Tiny_State *state) {
Tiny_BindFunction(state, "get_executing_line", GetExecutingLine);

Tiny_BindMacro(state, "json", JsonMacroFunction);

Tiny_BindFunction(state, "debug_break", DebugBreak);
}
3 changes: 2 additions & 1 deletion tiny/src/tiny.c
Original file line number Diff line number Diff line change
Expand Up @@ -1254,8 +1254,9 @@ inline static bool ExecuteCycle(Tiny_StateThread *thread) {
case TINY_OP_STRUCT_SET: {
++thread->pc;
Word i = state->program[thread->pc++];
Tiny_Value val = DoPop(thread);

Tiny_Value vstruct = DoPop(thread);
Tiny_Value val = DoPop(thread);

assert(vstruct.type == TINY_VAL_STRUCT);
assert(i >= 0 && i < vstruct.obj->ostruct.n);
Expand Down

0 comments on commit 1d8ecad

Please sign in to comment.