1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#include "generator.h"
#include "da.h"
#include <stdio.h>
static struct replacement
generate_replacement (struct arena *a, const struct compensation *comp)
{
ASSERT (comp->polyline.count > 0);
struct replacement rep = {0};
int len = 1;
for (uint32_t i = 0; i < comp->polyline.count; ++i) {
struct vec2 to = comp->polyline.items[i];
int n = snprintf (NULL, 0, "G0 X%f Y%f\n", to.x, to.y);
len += n;
}
char *head = arena_allocate (a, len, 1);
rep.new.beg = head;
rep.new.end = head + len - 1;
for (uint32_t i = 0; i < comp->polyline.count; ++i) {
struct vec2 to = comp->polyline.items[i];
int n = snprintf (head, len, "G0 X%f Y%f\n", to.x, to.y);
head += n;
len -= n;
}
switch (comp->replace) {
case CR_FROM_BEG_TO_BEG: {
rep.old.beg = comp->from->line.beg;
rep.old.end = comp->to ->line.beg;
} break;
case CR_FROM_BEG_TO_END: {
rep.old.beg = comp->from->line.beg;
rep.old.end = comp->to ->line.end;
} break;
case CR_FROM_END_TO_BEG: {
rep.old.beg = comp->from->line.end;
rep.old.end = comp->to ->line.beg;
} break;
case CR_FROM_END_TO_END: {
rep.old.beg = comp->from->line.end;
rep.old.end = comp->to ->line.end;
} break;
}
return rep;
}
struct replacements
generate_replacements (struct arena *a, const struct compensations *comps)
{
struct replacements reps = {0};
for (uint32_t i = 0; i < comps->count; ++i) {
const struct compensation *comp = &comps->items[i];
DA_APPEND (reps, generate_replacement (a, comp));
}
return reps;
}
void
write_code_with_replacements (int fd, struct sv orig,
const struct replacements *reps)
{
const char *head = orig.beg;
for (uint32_t i = 0; i < reps->count; ++i) {
const struct replacement *rep = &reps->items[i];
struct sv before = {
.beg = head,
.end = rep->old.beg,
};
head = rep->old.end;
// puts ("(v orig --)");
printf (SV_FMT, SV_FMTA (before));
// puts ("(^ orig --)");
// puts ("(v old --)");
// printf (SV_FMT, SV_FMTA (rep->old));
// puts ("(^ old --)");
// puts ("(v new --)");
printf (SV_FMT, SV_FMTA (rep->new));
// puts ("(^ new --)");
}
struct sv before = {
.beg = head,
.end = orig.end,
};
// puts ("(v last orig --)");
printf (SV_FMT, SV_FMTA (before));
// puts ("(^ last orig --)");
}
|