Created
December 12, 2021 08:26
-
-
Save eparadis/b1097a888e204568c1dacc522f20bd4a to your computer and use it in GitHub Desktop.
another frustrating dead end in C where I need fancier language features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct state { | |
int some_info; | |
void (*next)(struct state *s); | |
}; | |
void end(struct state *s) { | |
// do some clean up | |
s->some_info = 0; | |
s->next = halt; | |
} | |
void bar(struct state *s) { | |
s->some_info = 987; | |
s->next = end; | |
} | |
void branch( struct state *s) { | |
if( s->some_info == 100) { | |
// this is the branch | |
s->next = go_to_address(addr); // this is illegal. you can't compose functions like this | |
} | |
} | |
void go_to_address(struct state *s, int addr ) { | |
if( outsidePMemory(addr) ) { | |
s->next = exception; | |
} | |
s->next = get_instr_at(addr); | |
} | |
void foo(struct state *s) { | |
if( s->some_info < 0) { | |
// something no-no-bad-computer happened | |
s->next = exception; | |
return; | |
} | |
// do whatever you're gonna do | |
s->some_info *= 321; | |
s->next = NULL; // get next | |
} | |
void run() { | |
struct state s = { 123, foo}; | |
while( s.next != halt) { | |
s.next = s.next == NULL ? get_next_instr() : s.next; | |
(*s.next)(&s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment