This repository was archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtape.h
More file actions
53 lines (45 loc) · 1.32 KB
/
tape.h
File metadata and controls
53 lines (45 loc) · 1.32 KB
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
/*
* tape.c: Machine tape data structures
*
* Author: Giorgio Pristia
*/
#ifndef TAPE_H
#define TAPE_H
#include <string.h>
#include "types.h"
struct tape{
symbol *tail[2];
size_t size[2];
long head;
};
typedef struct tape tape;
/*
* Initialize a new tape containing the string passed
* blank are replaced with zeroes and term, if present,
* marks the end of the string (to ignore trailing \n)
*/
tape *tape_init (symbol*, symbol blank, symbol *term);
/* Branch the current tape and return a pointer to the new copy created */
tape *tape_branch(tape*);
static inline symbol tape_read(tape *t){
if(t->head < 0) return t->tail[0][~t->head];
return t->tail[1][t->head];
}
static inline tape *tape_write(tape *t, symbol write, int move){
if(t->head < 0) t->tail[0][~t->head] = write;
else t->tail[1][t->head] = write;
t->head += move;
if(t->head >= (signed)t->size[1]){
t->tail[1] = realloc(t->tail[1], (t->head + 1) * sizeof(**t->tail));
memset(t->tail[1] + t->size[1], 0, move);
t->size[1] = t->head + 1;
}
else if(t->head < -(signed)t->size[0]){
t->tail[0] = realloc(t->tail[0], -t->head * sizeof(**t->tail));
memset(t->tail[0] + t->size[0], 0, -move);
t->size[0] = -t->head;
}
return t;
}
void delete_tape(tape*);
#endif