refactor code

This commit is contained in:
CismonX 2020-06-05 16:54:30 +08:00
parent 138f9978f8
commit f7272a2ed6
No known key found for this signature in database
GPG Key ID: 315D6652268C5007
6 changed files with 16 additions and 10 deletions

View File

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

View File

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

View File

@ -40,7 +40,7 @@ u6a_runtime_info(FILE* restrict istream, const char* file_name);
bool bool
u6a_runtime_init(struct u6a_runtime_options* options); 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); u6a_runtime_execute(FILE* restrict istream, FILE* restrict output_stream);
void void

View File

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

View File

@ -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_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 { union u6a_vm_var {
struct u6a_vm_var_fn fn; struct u6a_vm_var_fn fn;

View File

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