-
Notifications
You must be signed in to change notification settings - Fork 0
/
fish-runtime.c
57 lines (50 loc) · 1.74 KB
/
fish-runtime.c
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
#include "fish-runtime.h"
#include <stdint.h>
#include <string.h>
#include "fish-stack.h"
/* reverse stack ('r' instruction) */
void
fish_reverse_stack(struct fish_stack *stack)
{
uint8_t *front = stack->data;
uint8_t *back =
(uint8_t *)stack->data + FISH_STACK_ITEM_SIZE * (stack->num_items - 1);
while (back - front >= FISH_STACK_ITEM_SIZE)
{
/* swap front and back and move inwards */
uint8_t buf[FISH_STACK_ITEM_SIZE];
memcpy(buf, front, FISH_STACK_ITEM_SIZE);
memcpy(front, back, FISH_STACK_ITEM_SIZE);
memcpy(back, buf, FISH_STACK_ITEM_SIZE);
front += FISH_STACK_ITEM_SIZE;
back -= FISH_STACK_ITEM_SIZE;
}
}
/* shift stack left ('{' instruction) */
void
fish_shift_left(struct fish_stack *stack)
{
uint8_t *front = stack->data;
uint8_t *back =
(uint8_t *)stack->data + FISH_STACK_ITEM_SIZE * (stack->num_items - 1);
/* copy front, shift everything else to front and put front in back */
uint8_t buf[FISH_STACK_ITEM_SIZE];
memcpy(buf, front, FISH_STACK_ITEM_SIZE);
memmove(front, front + FISH_STACK_ITEM_SIZE,
(stack->num_items - 1) * FISH_STACK_ITEM_SIZE);
memcpy(back, buf, FISH_STACK_ITEM_SIZE);
}
/* shift stack right ('}' instruction) */
void
fish_shift_right(struct fish_stack *stack)
{
uint8_t *front = stack->data;
uint8_t *back =
(uint8_t *)stack->data + FISH_STACK_ITEM_SIZE * (stack->num_items - 1);
/* copy back, shift everything else to front and put front in back */
uint8_t buf[FISH_STACK_ITEM_SIZE];
memcpy(buf, back, FISH_STACK_ITEM_SIZE);
memmove(front + FISH_STACK_ITEM_SIZE, front,
(stack->num_items - 1) * FISH_STACK_ITEM_SIZE);
memcpy(front, buf, FISH_STACK_ITEM_SIZE);
}