summaryrefslogtreecommitdiff
path: root/src/types.h
blob: 92417877598a8446623e071325e6a6d036a590e2 (plain)
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
#pragma once

#include <stdint.h>
#include <stdio.h>

typedef enum {
    MoveLeft,
    MoveRight,
    Increment,
    Decrement,
    StartLoop,
    EndLoop,
    PutChar,
    GetChar,
    Allocate,
    Free,
    EnterTape,
    LeaveTape,
    PutString,
    GetString,
    Clear,
    Zero,
    Eof
} Symbol;

typedef struct {
    uint32_t len;
    uint32_t index;
    struct instruction {
        Symbol s;
        uint32_t j;
    } * data;
} Program;

void program_init(Program* program, FILE* file);
void program_get(Program* program, Symbol* symbol);
uint32_t program_get_jump(Program* program);
void program_next(Program* program);
void program_seek(Program* program, uint32_t index);
void program_free(Program* program);

typedef struct {
    uint32_t len;
    uint32_t index;
    union stored {
        void* p;
        uint32_t i;
    } * data;
} Stack;

void stack_init(Stack* stack, uint32_t len);
void stack_push(Stack* stack, void* value);
void* stack_pop(Stack* stack);
void stack_pushi(Stack* stack, uint32_t value);
uint32_t stack_popi(Stack* stack);
void stack_free(Stack* stack);

typedef struct {
    uint8_t len;
    uint8_t index;
    uint8_t* data;
} Tape;

void tape_init(Tape* tape, uint8_t len);
void tape_free(Tape* tape);
void tape_left(Tape* tape);
void tape_right(Tape* tape);
void tape_increment(Tape* tape);
void tape_decrement(Tape* tape);
uint8_t tape_get(Tape* tape);
void tape_set(Tape* tape, uint8_t value);
void* tape_ptr(Tape* tape);

void interpret(Program* program);