From cefc57fe866a0417e7231de3685105f684710be6 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sun, 19 Feb 2017 21:52:48 -0800 Subject: [PATCH] Move error cost comparisons into their own source file --- project.gyp | 1 + src/runtime/error_costs.c | 28 ++++++++++++++++++++++++++++ src/runtime/error_costs.h | 34 +++++++++------------------------- 3 files changed, 38 insertions(+), 25 deletions(-) create mode 100644 src/runtime/error_costs.c diff --git a/project.gyp b/project.gyp index 081a3a88..2687a4cf 100644 --- a/project.gyp +++ b/project.gyp @@ -106,6 +106,7 @@ ], 'sources': [ 'src/runtime/document.c', + 'src/runtime/error_costs.c', 'src/runtime/language.c', 'src/runtime/lexer.c', 'src/runtime/node.c', diff --git a/src/runtime/error_costs.c b/src/runtime/error_costs.c new file mode 100644 index 00000000..5e038b81 --- /dev/null +++ b/src/runtime/error_costs.c @@ -0,0 +1,28 @@ +#include "runtime/error_costs.h" +#include + +static inline unsigned error_status__min_cost(ErrorStatus status) { + return status.cost + ERROR_COST_PER_SKIPPED_TREE * status.count * status.count; +} + +static inline unsigned error_status__max_cost(ErrorStatus status) { + return status.cost + + ERROR_COST_PER_SKIPPED_TREE * status.count * status.count + + (6 * ERROR_COST_PER_SKIPPED_TREE * status.count + + 12 * ERROR_COST_PER_SKIPPED_TREE) / + (1 + status.push_count / 2); +} + +int error_status_compare(ErrorStatus a, ErrorStatus b) { + if ((a.count + 1 < b.count) || (a.count < b.count && a.cost <= b.cost)) + return -1; + if ((a.count > b.count + 1) || (b.count < a.count && b.cost <= a.cost)) + return 1; + + if (error_status__max_cost(a) < error_status__min_cost(b)) + return -1; + if (error_status__min_cost(a) > error_status__max_cost(b)) + return 1; + + return 0; +} diff --git a/src/runtime/error_costs.h b/src/runtime/error_costs.h index a2187a25..aa5f3a92 100644 --- a/src/runtime/error_costs.h +++ b/src/runtime/error_costs.h @@ -1,41 +1,25 @@ #ifndef RUNTIME_ERROR_COSTS_H_ #define RUNTIME_ERROR_COSTS_H_ +#ifdef __cplusplus +extern "C" { +#endif + #define ERROR_STATE 0 #define ERROR_COST_PER_SKIPPED_TREE 10 #define ERROR_COST_PER_SKIPPED_LINE 3 #define ERROR_COST_PER_SKIPPED_CHAR 0 typedef struct { - unsigned cost; unsigned count; + unsigned cost; unsigned push_count; } ErrorStatus; -static inline unsigned error_status_min_cost(ErrorStatus status) { - return status.cost + ERROR_COST_PER_SKIPPED_TREE * status.count * status.count; -} - -static inline unsigned error_status_max_cost(ErrorStatus status) { - return status.cost + - ERROR_COST_PER_SKIPPED_TREE * status.count * status.count + - (6 * ERROR_COST_PER_SKIPPED_TREE * status.count + - 12 * ERROR_COST_PER_SKIPPED_TREE) / - (1 + status.push_count / 2); -} - -static inline int error_status_compare(ErrorStatus a, ErrorStatus b) { - if ((a.count + 1 < b.count) || (a.count < b.count && a.cost <= b.cost)) - return -1; - if ((a.count > b.count + 1) || (b.count < a.count && b.cost <= a.cost)) - return 1; - - if (error_status_max_cost(a) < error_status_min_cost(b)) - return -1; - if (error_status_min_cost(a) > error_status_max_cost(b)) - return 1; - - return 0; +int error_status_compare(ErrorStatus a, ErrorStatus b); + +#ifdef __cplusplus } +#endif #endif