-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSexp.h
101 lines (84 loc) · 2.67 KB
/
Sexp.h
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
96
97
98
99
100
101
#ifndef SEXP_H
#define SEXP_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SYMBOL_LENGTH 30
#define MAX_DISPLAY_SEXP 400
#define MAX_FILE_NAME 100
#define HEAP_BLOCK_SIZE 500
#define ENV_SIZE 1000
typedef enum Sexp_kind Sexp_kind;
enum Sexp_kind {
Symbol, Nil, Cons
};
typedef struct Sexp Sexp;
struct Sexp {
Sexp_kind kind;
int memflag; // 0: unallocated, 1: unmarked, 2: marked
union {
struct {
char name[MAX_SYMBOL_LENGTH];
} symbol;
struct {
Sexp* Sexp1;
Sexp* Sexp2;
} cons;
} u;
};
Sexp* construct_nil(Sexp* s);
Sexp* construct_symbol(Sexp* s, char* name);
Sexp* construct_cons(Sexp* s, Sexp* s1, Sexp* s2);
Sexp* copy_Sexp(Sexp* dest, Sexp* src);
typedef struct RootSet RootSet;
struct RootSet {
Sexp* set[ENV_SIZE*4];
size_t length;
};
void construct_rootSet(RootSet* rootSet);
enum ParseResult_kind {
Success, ErrorAt, Empty
};
typedef enum ParseResult_kind ParseResult_kind;
typedef struct ParseResult ParseResult;
struct ParseResult {
ParseResult_kind kind;
unsigned int position;
Sexp* success_Sexp;
};
ParseResult* construct_PR_success(ParseResult* p, unsigned int position, Sexp* s);
ParseResult* construct_PR_error(ParseResult* p, unsigned int position);
ParseResult* construct_PR_empty(ParseResult* p, Sexp* s);
typedef struct HeapBlock HeapBlock;
struct HeapBlock {
Sexp** objects;
HeapBlock* nextBlock;
};
HeapBlock Heap;
size_t HeapSize;
size_t total_bytes_allocated;
size_t total_bytes_freed;
size_t bytes_freed_GC;
size_t total_garbage_collections;
size_t total_heapblocks_allocated;
void init_heapblock(HeapBlock* heapblock);
void free_heapblock(HeapBlock* heapblock);
void create_heap();
void delete_heap();
HeapBlock* allocate_heap_block();
Sexp* allocate_Sexp(RootSet* rootSet);
void mark(Sexp* node);
int mark_and_sweep(RootSet* rootSet);
void showSexp (Sexp* s, char* result);
void showTail (Sexp* s, char* result);
void readSexp (char* cs, ParseResult* parse_res, RootSet* rootSet);
void readExp (char* cs, size_t i, size_t len, ParseResult* parse_res, RootSet* rootSet);
void readSymbol(char* cs, size_t i, size_t len, ParseResult* parse_res, RootSet* rootSet);
void readTail (char* cs, size_t i, size_t len, ParseResult* parse_res, RootSet* rootSet);
void readSexpAndTail(Sexp* s, char* cs, size_t i, size_t len, ParseResult* parse_res, RootSet* rootSet);
void readQuoteSexpAndTail(Sexp* s, char* cs, size_t i, size_t len, ParseResult* parse_res, RootSet* rootSet);
void readClose (char* cs, size_t i, size_t len, ParseResult* parse_res, RootSet* rootSet);
// debug
void print_Sexp(Sexp* s);
void print_rootSet(RootSet* rootSet);
#endif