Browse Source

refactor code

master
CismonX 1 year ago
parent
commit
f7272a2ed6
No known key found for this signature in database GPG Key ID: 315D6652268C5007
  1. 2
      configure.ac
  2. 9
      src/runtime.c
  3. 2
      src/runtime.h
  4. 4
      src/u6a.c
  5. 2
      src/vm_defs.h
  6. 7
      src/vm_stack.c

2
configure.ac

@ -26,7 +26,7 @@ case "${host_os}" in
esac
dnl Checks for programs.
AC_PROG_CC_C99
AC_PROG_CC_STDC
dnl Checks for header files.
AC_CHECK_HEADERS([arpa/inet.h inttypes.h stddef.h stdint.h stdlib.h string.h unistd.h],

9
src/runtime.c

@ -206,7 +206,7 @@ u6a_runtime_init(struct u6a_runtime_options* options) {
return false;
}
U6A_HOT union u6a_vm_var
U6A_HOT struct u6a_vm_var_fn
u6a_runtime_execute(FILE* restrict istream, FILE* restrict ostream) {
struct u6a_vm_var_fn acc = { 0 }, top = { 0 };
struct u6a_vm_ins* ins = text + text_subst_len;
@ -345,7 +345,7 @@ u6a_runtime_execute(FILE* restrict istream, FILE* restrict ostream) {
VM_JMP(0x03);
case u6a_vf_e:
// Every program should terminate with explicit `e` function
return U6A_VM_VAR_FN(arg);
return arg;
default:
CHECK_FORCE(u6a_err_invalid_vm_func, func.token.fn);
}
@ -365,6 +365,9 @@ u6a_runtime_execute(FILE* restrict istream, FILE* restrict ostream) {
ACC_FN_REF(u6a_vf_d1_s, u6a_vm_pool_alloc2(func, arg));
} else {
acc = u6a_vm_stack_xch(acc);
if (UNLIKELY(U6A_VM_VAR_FN_IS_EMPTY(acc))) {
goto runtime_error;
}
}
break;
case u6a_vo_del:
@ -387,7 +390,7 @@ u6a_runtime_execute(FILE* restrict istream, FILE* restrict ostream) {
}
runtime_error:
return U6A_VM_VAR_PTR(NULL);
return U6A_VM_VAR_FN_EMPTY;
}
void

2
src/runtime.h

@ -40,7 +40,7 @@ u6a_runtime_info(FILE* restrict istream, const char* file_name);
bool
u6a_runtime_init(struct u6a_runtime_options* options);
union u6a_vm_var
struct u6a_vm_var_fn
u6a_runtime_execute(FILE* restrict istream, FILE* restrict output_stream);
void

4
src/u6a.c

@ -153,8 +153,8 @@ int main(int argc, char** argv) {
exit_code = EC_ERR_INIT;
goto terminate;
}
union u6a_vm_var exec_result = u6a_runtime_execute(stdin, stdout);
if (UNLIKELY(exec_result.ptr == NULL)) {
struct u6a_vm_var_fn exec_result = u6a_runtime_execute(stdin, stdout);
if (UNLIKELY(U6A_VM_VAR_FN_IS_EMPTY(exec_result))) {
exit_code = EC_ERR_RUNTIME;
goto terminate;
}

2
src/vm_defs.h

@ -89,6 +89,8 @@ struct u6a_vm_var_fn {
};
#define U6A_VM_VAR_FN_REF(fn_, ref_) (struct u6a_vm_var_fn) { .token.fn = (fn_), .ref = (ref_) }
#define U6A_VM_VAR_FN_EMPTY (struct u6a_vm_var_fn) { 0 }
#define U6A_VM_VAR_FN_IS_EMPTY(fn_) ( ((union u6a_vm_var)(fn_)).ptr == NULL )
union u6a_vm_var {
struct u6a_vm_var_fn fn;

7
src/vm_stack.c

@ -108,7 +108,7 @@ u6a_vm_stack_top() {
if (UNLIKELY(vs->top == UINT32_MAX)) {
vs = vs->prev;
if (UNLIKELY(vs == NULL)) {
return (struct u6a_vm_var_fn) { 0 };
return U6A_VM_VAR_FN_EMPTY;
}
active_stack = vs;
}
@ -232,12 +232,13 @@ u6a_vm_stack_xch(struct u6a_vm_var_fn v0) {
} else {
struct vm_stack* prev = vs->prev;
if (UNLIKELY(prev == NULL)) {
return (struct u6a_vm_var_fn) { 0 };
u6a_err_stack_underflow(err_stage);
return U6A_VM_VAR_FN_EMPTY;
}
if (--prev->refcnt > 0) {
prev = vm_stack_dup(prev);
if (UNLIKELY(prev == NULL)) {
return (struct u6a_vm_var_fn) { 0 };
return U6A_VM_VAR_FN_EMPTY;
}
}
if (vs->top == 0) {

Loading…
Cancel
Save