Move error cost comparisons into their own source file
This commit is contained in:
parent
5b4e6df3ff
commit
cefc57fe86
3 changed files with 38 additions and 25 deletions
|
|
@ -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',
|
||||
|
|
|
|||
28
src/runtime/error_costs.c
Normal file
28
src/runtime/error_costs.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include "runtime/error_costs.h"
|
||||
#include <math.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue