Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- in reply to: https://www.youtube.com/watch?v=qK0vmuQib8Y
- regarding: overcomplicated [ ] jump logic
- // +
- ++*ptr;
- // -
- --*ptr;
- // <
- --ptr;
- // >
- ++ptr;
- // ,
- *ptr = getchar();
- // .
- putchar(*ptr);
- // [
- if(*ptr == 0)
- pc = bracket_seek(pc, pc + strlen(pc), '[', ']');
- // ]
- pc = bracket_seek_r(pc, code, ']', '[') - 1;
- // description of functions and variables
- char *ptr
- pointer to working memory
- char *pc
- pointer to current instruction (program counter)
- char *code
- pointer to beginning of instruction pool
- char *bracket_seek(char *start, char *end, char bracket, char match)
- assume program has been sanitized and stripped of non-instruction characters
- returns a position within the array pointed to by start.
- Undefined behavior will occur if misused.
- start is where the seek will begin
- end must be greater than start, seek must return NULL (representing end of program)
- if no match is found before start overtakes end
- bracket is the character being seeked from. If bracket is found before match, a counter is incremented
- match is the character for the matching bracket. If counter > 0 when match is found,
- decrement counter and continue. Else, return the current seek position
- char *bracket_seek_r(char *start, char *end, char bracket, char match)
- same as bracket_seek, except it seeks in reverse, and thus end must be
- less than start or undefined behavior will occur
- // sample implementation (untested):
- char *
- bracket_seek(char *start, char *end, char bracket, char match)
- {
- int depth = 0;
- while(++start < end){
- if(*start == bracket)
- ++depth;
- if(*start == match){
- if(depth > 0){
- --depth;
- continue;
- }
- return start;
- }
- }
- return NULL;
- }
- char *
- bracket_seek_r(char *start, char *end, char bracket, char match)
- {
- int depth = 0;
- while(--start >= end){
- if(*start == bracket)
- ++depth;
- if(*start == match){
- if(depth > 0){
- --depth;
- continue;
- }
- return start;
- }
- }
- return NULL;
- }
- // note: it would be a good idea to find a way to avoid duplicate code, e.g. by making a bracket_seek_generic function that takes a seek direction as an additional argument, and is wrapped by the two single-purpose functions (bracket_seek and bracket_seek_r), so as to minimize both excessive arguments and code duplication.
- char *
- bracket_seek_generic(char *start, char *end, char bracket, char match, int direction)
- {
- int depth = 0;
- if(direction == 0)
- return NULL;
- while( (direction > 0 && ++start < end) ||
- (direction < 0 && --start >= end) ){
- if(*start == bracket)
- ++depth;
- if(*start == match){
- if(depth > 0){
- --depth;
- continue;
- }
- return start;
- }
- }
- return NULL;
- }
- char *
- bracket_seek(char *start, char *end, char bracket, char match)
- {
- return bracket_seek_generic(start, end, bracket, match, +1);
- }
- char *
- bracket_seek_r(char *start, char *end, char bracket, char match)
- {
- return bracket_seek_generic(start, end, bracket, match, -1);
- }
- // doing this, the two bracket_seek functions act as wrappers to a bigger function. This saves on duplicate code in the sense that the implementations of bracket_seek and bracket_seek_r are obviously correct and won't need to be updated in parallel (debugging will happen entirely in bracket_seek_generic), and also avoids the problem of having too many arguments, as the functions that are meant to be used by downstream code have a much simpler interface. However, the drawback is that bracket_seek_generic has more complexity and is thus more bug-prone. Choose your battles.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement