diff options
Diffstat (limited to 'src/interp.c')
| -rw-r--r-- | src/interp.c | 195 |
1 files changed, 96 insertions, 99 deletions
diff --git a/src/interp.c b/src/interp.c index e3b00b6..0d809dc 100644 --- a/src/interp.c +++ b/src/interp.c @@ -48,14 +48,14 @@ typedef long long int vartype; /*** Globals ***/ -off_t *line_offset = NULL; +static off_t *line_offset = NULL; -unsigned lines_executed = 0, current_line = 0, num_lines; +static unsigned lines_executed = 0, current_line = 0, num_lines; -unsigned call_stack[CALL_STACK_SZ]; -unsigned stack_frame = 0; +static unsigned call_stack[CALL_STACK_SZ]; +static unsigned stack_frame = 0; -int log_fd = -1, file_des = -1; +static int file_des = -1; struct varnode_t { char name[VARNAME_MAX + 1]; @@ -64,19 +64,19 @@ struct varnode_t { struct varnode_t *next; }; -void error(const char *fmt, ...) __attribute__((noreturn,format(print,1,2))); -void vid_write(const char *str); -void vid_writef(const char *fmt, ...) __attribute__((format(printf,1,2))); -void debug(const char *fmt, ...) __attribute__((format(printf,1,2))); -bool isValidVariable(const char *c); +static void error(const char *fmt, ...) __attribute__((noreturn,format(print,1,2))); +static void vid_write(const char *str); +static void vid_writef(const char *fmt, ...) __attribute__((format(printf,1,2))); +static void debug(const char *fmt, ...) __attribute__((format(printf,1,2))); +static bool isValidVariable(const char *c); /* variables are stored in a chained hash map */ /* collisions are manageable, but should be minimized */ -struct varnode_t *var_map[VARMAP_SIZE]; +static struct varnode_t *var_map[VARMAP_SIZE]; /* simple DJB hash */ -uint32_t var_hash(const char *str) +static uint32_t var_hash(const char *str) { uint32_t hash = 5381; char c; @@ -88,7 +88,7 @@ uint32_t var_hash(const char *str) return hash; } -struct varnode_t *lookup_var(const char *name) +static struct varnode_t *lookup_var(const char *name) { uint32_t hash = var_hash(name) % VARMAP_SIZE; @@ -120,12 +120,12 @@ struct varnode_t *lookup_var(const char *name) return new; } -vartype getVariable(const char *name) +static vartype getVariable(const char *name) { return lookup_var(name)->val; } -void setVariable(const char *name, vartype val) +static void setVariable(const char *name, vartype val) { struct varnode_t *node = lookup_var(name); if(!node->constant) @@ -134,12 +134,12 @@ void setVariable(const char *name, vartype val) error("attempted to modify a constant variable"); } -void setConst(const char *name, bool c) +static void setConst(const char *name, bool c) { lookup_var(name)->constant = c; } -void incVar(const char *name) +static void incVar(const char *name) { struct varnode_t *node = lookup_var(name); if(!node->constant) @@ -148,7 +148,7 @@ void incVar(const char *name) error("attempted to modify a constant variable"); } -void decVar(const char *name) +static void decVar(const char *name) { struct varnode_t *node = lookup_var(name); if(!node->constant) @@ -159,12 +159,10 @@ void decVar(const char *name) /*** Utility functions ***/ -void exit_handler(void) +static void exit_handler(void) { if(file_des >= 0) close(file_des); - if(log_fd >= 0) - close(log_fd); if(line_offset) free(line_offset); /* free all our variables */ @@ -180,12 +178,12 @@ void exit_handler(void) } } -void vid_write(const char *str) +static void vid_write(const char *str) { printf("%s", str); } -void __attribute__((format(printf,1,2))) vid_writef(const char *fmt, ...) +static void __attribute__((format(printf,1,2))) vid_writef(const char *fmt, ...) { char fmtbuf[256]; @@ -196,7 +194,7 @@ void __attribute__((format(printf,1,2))) vid_writef(const char *fmt, ...) va_end(ap); } -void __attribute__((noreturn,format(printf,1,2))) error(const char *fmt, ...) +static void __attribute__((noreturn,format(printf,1,2))) error(const char *fmt, ...) { char fmtbuf[256]; @@ -211,7 +209,7 @@ void __attribute__((noreturn,format(printf,1,2))) error(const char *fmt, ...) exit(EXIT_FAILURE); } -void __attribute__((format(printf,1,2))) warning(const char *fmt, ...) +static void __attribute__((format(printf,1,2))) warning(const char *fmt, ...) { char fmtbuf[256]; @@ -223,7 +221,7 @@ void __attribute__((format(printf,1,2))) warning(const char *fmt, ...) } /* grabs a line from a file, -1 on error, returns # bytes read otherwise */ -int read_line(int fd, char *buf, size_t sz) +static int read_line(int fd, char *buf, size_t sz) { unsigned i = 0; int bytes_read = 0; @@ -254,7 +252,7 @@ int read_line(int fd, char *buf, size_t sz) /* index_lines() precalculates the offset of each line for faster jumping */ /* also it does a quick pass to index all the labels */ -off_t *index_lines(int fd, unsigned *numlines) +static off_t *index_lines(int fd, unsigned *numlines) { size_t sz = sizeof(off_t); off_t *data = malloc(sz); @@ -295,7 +293,7 @@ off_t *index_lines(int fd, unsigned *numlines) return data; } -void jump_line(int fd, unsigned where) +static void jump_line(int fd, unsigned where) { if(1 <= where && where <= num_lines) { @@ -306,7 +304,7 @@ void jump_line(int fd, unsigned where) current_line = where - 1; } -void sub_call(int fd, unsigned where) +static void sub_call(int fd, unsigned where) { if(stack_frame < ARRAYLEN(call_stack)) { @@ -318,7 +316,7 @@ void sub_call(int fd, unsigned where) error("call stack overflow"); } -void sub_return(int fd) +static void sub_return(int fd) { if(stack_frame > 0) { @@ -331,104 +329,104 @@ void sub_return(int fd) /* based on http://en.literateprograms.org/Shunting_yard_algorithm_%28C%29 */ -vartype eval_uminus(vartype a1, vartype a2) +static vartype eval_uminus(vartype a1, vartype a2) { (void) a2; return -a1; } -vartype eval_exp(vartype a1, vartype a2) +static vartype eval_exp(vartype a1, vartype a2) { return a2<0 ? 0 : (a2==0?1:a1*eval_exp(a1, a2-1)); } -vartype eval_mul(vartype a1, vartype a2) +static vartype eval_mul(vartype a1, vartype a2) { return a1*a2; } -vartype eval_div(vartype a1, vartype a2) +static vartype eval_div(vartype a1, vartype a2) { if(!a2) { error("division by zero"); } return a1/a2; } -vartype eval_mod(vartype a1, vartype a2) +static vartype eval_mod(vartype a1, vartype a2) { if(!a2) { error("division by zero"); } return a1%a2; } -vartype eval_add(vartype a1, vartype a2) +static vartype eval_add(vartype a1, vartype a2) { return a1+a2; } -vartype eval_sub(vartype a1, vartype a2) +static vartype eval_sub(vartype a1, vartype a2) { return a1-a2; } -vartype eval_eq(vartype a1, vartype a2) +static vartype eval_eq(vartype a1, vartype a2) { return a1 == a2; } -vartype eval_neq(vartype a1, vartype a2) +static vartype eval_neq(vartype a1, vartype a2) { return a1 != a2; } -vartype eval_leq(vartype a1, vartype a2) +static vartype eval_leq(vartype a1, vartype a2) { return a1 <= a2; } -vartype eval_geq(vartype a1, vartype a2) +static vartype eval_geq(vartype a1, vartype a2) { return a1 >= a2; } -vartype eval_lt(vartype a1, vartype a2) +static vartype eval_lt(vartype a1, vartype a2) { return a1 < a2; } -vartype eval_gt(vartype a1, vartype a2) +static vartype eval_gt(vartype a1, vartype a2) { return a1 > a2; } -vartype eval_log_neg(vartype a1, vartype a2) +static vartype eval_log_neg(vartype a1, vartype a2) { (void) a2; return !a1; } -vartype eval_log_and(vartype a1, vartype a2) +static vartype eval_log_and(vartype a1, vartype a2) { return a1 && a2; } -vartype eval_log_or(vartype a1, vartype a2) +static vartype eval_log_or(vartype a1, vartype a2) { return a1 || a2; } -vartype eval_bit_and(vartype a1, vartype a2) +static vartype eval_bit_and(vartype a1, vartype a2) { return a1 & a2; } -vartype eval_bit_xor(vartype a1, vartype a2) +static vartype eval_bit_xor(vartype a1, vartype a2) { return a1 ^ a2; } -vartype eval_bit_or(vartype a1, vartype a2) +static vartype eval_bit_or(vartype a1, vartype a2) { return a1 | a2; } -vartype eval_bit_comp(vartype a1, vartype a2) +static vartype eval_bit_comp(vartype a1, vartype a2) { (void) a2; return ~a1; } -vartype eval_lsh(vartype a1, vartype a2) +static vartype eval_lsh(vartype a1, vartype a2) { return a1 << a2; } -vartype eval_rsh(vartype a1, vartype a2) +static vartype eval_rsh(vartype a1, vartype a2) { return a1 >> a2; } -vartype eval_sqrt(vartype a1, vartype a2) +static vartype eval_sqrt(vartype a1, vartype a2) { (void) a2; return sqrt(a1); @@ -462,7 +460,7 @@ enum {ASSOC_NONE=0, ASSOC_LEFT, ASSOC_RIGHT}; /* arrays are implemented as UNARY OPERATORS */ -struct op_s { +static struct op_s { const char *op; int prec; int assoc; @@ -498,13 +496,13 @@ struct op_s { #define OPMAP_SIZE 25 -void op_hash_round(char c, uint32_t *hash) +static void op_hash_round(char c, uint32_t *hash) { *hash *= 70; *hash ^= c; } -uint32_t op_hash(const char *c) +static uint32_t op_hash(const char *c) { uint32_t hash = 4412; while(1) @@ -517,10 +515,10 @@ uint32_t op_hash(const char *c) } /* optimized hash map for fast lookup of operators */ -struct op_s op_map[OPMAP_SIZE]; -size_t longest_op = 0; +static struct op_s op_map[OPMAP_SIZE]; +static size_t longest_op = 0; -void opmap_insert(struct op_s *op) +static void opmap_insert(struct op_s *op) { if(op->len > longest_op) longest_op = op->len; @@ -532,7 +530,7 @@ void opmap_insert(struct op_s *op) memcpy(op_map+hash, op, sizeof(*op)); } -void init_optable(void) +static void init_optable(void) { memset(op_map, 0, sizeof(op_map)); for(unsigned int i = 0; i < ARRAYLEN(ops); ++i) @@ -542,7 +540,7 @@ void init_optable(void) } } -const struct op_s *getop(const char *ch, int *len) +static const struct op_s *getop(const char *ch, int *len) { unsigned int i = 0; uint32_t hash = 4412; @@ -560,13 +558,13 @@ const struct op_s *getop(const char *ch, int *len) return poss; } -const struct op_s *opstack[MAXOPSTACK]; -int nopstack; +static const struct op_s *opstack[MAXOPSTACK]; +static int nopstack; -vartype numstack[MAXNUMSTACK]; -int nnumstack; +static vartype numstack[MAXNUMSTACK]; +static int nnumstack; -void push_opstack(const struct op_s *op) +static void push_opstack(const struct op_s *op) { if(nopstack>MAXOPSTACK - 1) { error("operator stack overflow"); @@ -574,7 +572,7 @@ void push_opstack(const struct op_s *op) opstack[nopstack++] = op; } -const struct op_s *pop_opstack(void) +static const struct op_s *pop_opstack(void) { if(!nopstack) { error("operator stack empty"); @@ -582,7 +580,7 @@ const struct op_s *pop_opstack(void) return opstack[--nopstack]; } -void push_numstack(vartype num) +static void push_numstack(vartype num) { if(nnumstack>MAXNUMSTACK - 1) { error("number stack overflow"); @@ -590,7 +588,7 @@ void push_numstack(vartype num) numstack[nnumstack++] = num; } -vartype pop_numstack(void) +static vartype pop_numstack(void) { if(!nnumstack) { error("number stack empty"); @@ -598,12 +596,12 @@ vartype pop_numstack(void) return numstack[--nnumstack]; } -bool isDigit(char c) +static bool isDigit(char c) { return '0' <= c && c <= '9'; } -bool isValidNumber(char *str) +static bool isValidNumber(char *str) { //vid_writef("isValidNumber %s", str); if(str && (isDigit(*str) || *str == '-')) @@ -621,13 +619,13 @@ bool isValidNumber(char *str) return false; } -bool isSpace(char c) +static bool isSpace(char c) { //vid_writef("isSpace '%c'", c); return (c == ' ') || (c == '\t'); } -bool isValidVariable(const char *c) +static bool isValidVariable(const char *c) { //vid_writef("isValidVariable %s", c); if(!isDigit(*c) && !getop(c, NULL) && !isSpace(*c)) @@ -637,7 +635,7 @@ bool isValidVariable(const char *c) return false; } -vartype getValue(char *str, char *cur) +static vartype getValue(char *str, char *cur) { //vid_writef("getValue %s", str); if(str && isValidVariable(str)) @@ -650,7 +648,7 @@ vartype getValue(char *str, char *cur) return strtol(str, NULL, 0); } -bool isValidNumberOrVariable(const char *c) +static bool isValidNumberOrVariable(const char *c) { //vid_writef("isValidNumberOrVariable %s", c); if(isDigit(*c) || isValidVariable(c)) @@ -660,7 +658,7 @@ bool isValidNumberOrVariable(const char *c) return false; } -void shunt_op(const struct op_s *op) +static void shunt_op(const struct op_s *op) { const struct op_s *pop; vartype n1, n2; @@ -726,7 +724,7 @@ void shunt_op(const struct op_s *op) push_opstack(op); } -vartype eval_expr(char *str) +static vartype eval_expr(char *str) { //vid_write("**************** EVAL EXPR ***************"); @@ -825,7 +823,7 @@ vartype eval_expr(char *str) #define NEXT 2 #define BREAK 3 -int let_handler(char **save, int *repeats_left) +static int let_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; char *varname = strtok_r(NULL, "= \t", save); @@ -849,7 +847,7 @@ int let_handler(char **save, int *repeats_left) return OK; } -int repeat_handler(char **save, int *repeats_left) +static int repeat_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; char *tok = strtok_r(NULL, ";", save); @@ -865,7 +863,7 @@ int repeat_handler(char **save, int *repeats_left) error("expected valid expression after REPEAT"); } -int goto_handler(char **save, int *repeats_left) +static int goto_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; char *tok = strtok_r(NULL, ";", save); @@ -878,7 +876,7 @@ int goto_handler(char **save, int *repeats_left) error("expected valid expression after GOTO"); } -int call_handler(char **save, int *repeats_left) +static int call_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; char *tok = strtok_r(NULL, "", save); @@ -891,14 +889,14 @@ int call_handler(char **save, int *repeats_left) error("expected destination for CALL"); } -int ret_handler(char **save, int *repeats_left) +static int ret_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; sub_return(file_des); return NEXT; } -int inc_handler(char **save, int *repeats_left) +static int inc_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; char *tok = strtok_r(NULL, " \t", save); @@ -907,7 +905,7 @@ int inc_handler(char **save, int *repeats_left) return OK; } -int dec_handler(char **save, int *repeats_left) +static int dec_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; char *tok = strtok_r(NULL, " \t", save); @@ -916,7 +914,7 @@ int dec_handler(char **save, int *repeats_left) return OK; } -int if_handler(char **save, int *repeats_left) +static int if_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; char *tok = strtok_r(NULL, ";", save); @@ -932,7 +930,7 @@ int if_handler(char **save, int *repeats_left) return OK; } -int delay_handler(char **save, int *repeats_left) +static int delay_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; /* delay N 1000ths of a sec */ @@ -951,7 +949,7 @@ int delay_handler(char **save, int *repeats_left) return OK; } -int log_handler(char **save, int *repeats_left) +static int log_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; char *tok = strtok_r(NULL, "", save); @@ -960,7 +958,7 @@ int log_handler(char **save, int *repeats_left) return OK; } -int logvar_handler(char **save, int *repeats_left) +static int logvar_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; char *tok = strtok_r(NULL, ";", save); @@ -973,26 +971,26 @@ int logvar_handler(char **save, int *repeats_left) error("expected expression after LOGVAR"); } -int rem_handler(char **save, int *repeats_left) +static int rem_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; return BREAK; } -int quit_handler(char **save, int *repeats_left) +static int quit_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; return DONE; } -int newline_handler(char **save, int *repeats_left) +static int newline_handler(char **save, int *repeats_left) { (void) save; (void) repeats_left; vid_write("\n"); return OK; } -int logchar_handler(char **save, int *repeats_left) +static int logchar_handler(char **save, int *repeats_left) { (void) repeats_left; char *tok = strtok_r(NULL, ";", save); @@ -1006,7 +1004,7 @@ int logchar_handler(char **save, int *repeats_left) } -struct token_t { +static struct token_t { const char *tok; int (*func)(char **save, int *repeats_left); } tokens[] = { @@ -1037,7 +1035,7 @@ struct token_t { /* once again, this lookup table is implemented with a perfect hash map */ #define TOKMAP_SIZE ARRAYLEN(tokens) -struct token_t tokmap[TOKMAP_SIZE]; +static struct token_t tokmap[TOKMAP_SIZE]; /* auto-generated with mph-1.2 */ /* @@ -1165,7 +1163,7 @@ static int T2[] = { #define uchar unsigned char -int +static int tok_hash(const uchar *key) { int i; @@ -1193,7 +1191,7 @@ tok_hash(const uchar *key) return (g[f0] + g[f1] + g[f2]) % 22; } -void tokmap_insert(struct token_t *tok) +static void tokmap_insert(struct token_t *tok) { uint32_t hash = tok_hash(tok->tok) % TOKMAP_SIZE; if(hash < 0 || tokmap[hash].tok) @@ -1201,7 +1199,7 @@ void tokmap_insert(struct token_t *tok) memcpy(tokmap+hash, tok, sizeof(*tok)); } -void init_tokmap(void) +static void init_tokmap(void) { memset(tokmap, 0, sizeof(tokmap)); for(unsigned int i = 0; i < ARRAYLEN(tokens); ++i) @@ -1210,10 +1208,9 @@ void init_tokmap(void) } } -void init_globals(void) +static void init_globals(void) { line_offset = NULL; - log_fd = -1; file_des = -1; stack_frame = 0; lines_executed = 0; |