diff options
| author | 2023-02-09 16:16:41 +0000 | |
|---|---|---|
| committer | 2023-02-09 16:16:41 +0000 | |
| commit | c0770d8ece675f5e6f2249b331c7550933915829 (patch) | |
| tree | 49ed1779f0941a60ce2941c312d3f53f4ab81616 /creole.c | |
| parent | test refactoring (diff) | |
add compile t est
Diffstat (limited to 'creole.c')
| -rw-r--r-- | creole.c | 71 |
1 files changed, 37 insertions, 34 deletions
@@ -342,27 +342,17 @@ creole_parse_line(struct creole_ins *ins, struct creole_reader *r) return CREOLE_COMPILE_OK; } -#if 0 - /************************************************************************** * High level compiling interface *************************************************************************/ -static void clear_instruction(struct creole_env *env, - struct creole_ins *ins) -{ - memset(ins, 0, sizeof(ins)); - env->prgptr--; -} - static int typecheck(enum creole_word_flag fl, enum creole_arg_type typ) { switch (typ) { - case TYPE_NONE: return 0; case TYPE_IMM: return fl == CREOLE_IMMEDIATE; case TYPE_REG: return fl == CREOLE_REGISTER; case TYPE_VAL: return fl == CREOLE_IMMEDIATE - | fl == CREOLE_REGISTER; + || fl == CREOLE_REGISTER; case TYPE_LAB: return fl == CREOLE_IMMEDIATE; default: return 0; } @@ -373,65 +363,78 @@ static enum creole_compiler_ret typecheck_ins(struct creole_env *env, { unsigned i; - for (i = 0; i < opcode_info[env->opcode].arglen; i++) { - if (!typecheck(ins->w[i], - opcode_info[env->opcode].argtype[i])) + for (i = 0; i < opcode_info[ins->opcode].arglen; i++) { + if (!typecheck(ins->w_flags[i], + opcode_info[ins->opcode].argtype[i])) return CREOLE_TYPE_ERROR; } return CREOLE_COMPILE_OK; } +static void clear_ins(struct creole_ins *i) +{ + i->opcode = 0; + i->w[0] = i->w[1] = i->w[2] = i->w_flags[0] = + i->w_flags[1] = i->w_flags[2] = 0; +} + static enum creole_compiler_ret handle_compiletime_immediate(struct creole_env *env, - struct creole_ins *ins) + struct creole_ins *cur_ins) { - switch (ins->opcode) { + switch (cur_ins->opcode) { case CREOLE_CLB: - if (ins->w[0] >= ins->lablen) + if (cur_ins->w[0] >= env->lablen) return CREOLE_LABEL_OVERFLOW; - ins->lab[ins->w[0]] = env->prgptr; + env->lab[cur_ins->w[0]] = env->prgptr; /* Delete instruction because it is a compile time * instruction. Place next instruction in its place. */ - clear_instruction(env, ins); - return CREOLE_COMPILE_OK; + clear_ins(cur_ins); + return CREOLE_COMPILE_CLEARED_INSTRUCTION; case CREOLE_NOOP: - clear_instruction(env, ins); - return CREOLE_COMPILE_OK; + clear_ins(cur_ins); + return CREOLE_COMPILE_CLEARED_INSTRUCTION; default: - return typecheck_ins(env, ins); + return typecheck_ins(env, cur_ins); } } -int creole_compile(struct creole_env *env, struct creole_reader *r) +enum creole_compiler_ret +creole_compile(struct creole_env *env, struct creole_reader *r) { struct creole_ins *cur_ins = env->prg; int rcode; while (env->prgptr < env->prglen) { - if (!creole_parse_line(cur_ins, r)) - return CREOLE_PARSE_ERROR; - /* Increase prgptr here. If the instruction is a compile - * time instruction, then this will be decremented since - * the instruction will not be executed. - */ - env->prgptr++; + rcode = creole_parse_line(cur_ins, r); + if (rcode != CREOLE_COMPILE_OK) + return rcode; rcode = handle_compiletime_immediate(env, cur_ins); - if (rcode != CREOLE_COMPILE_OK) + switch (rcode) { + case CREOLE_COMPILE_CLEARED_INSTRUCTION: + break; + case CREOLE_COMPILE_OK: + cur_ins++; + env->prgptr++; + break; + default: return rcode; + } if (read_eof(r)) break; - cur_ins += 1; } - if (env->prgptr == env->prglen && *line) + if (env->prgptr == env->prglen && !read_eof(r)) return CREOLE_PROGRAM_OVERFLOW; env->prgend = env->prgptr; env->prgptr = 0; return CREOLE_COMPILE_OK; } +#if 0 + static creole_word read_word(struct creole_ins *ins, int i) { if (env->w_flags[i] == CREOLE_REGISTER) |
