aboutsummaryrefslogtreecommitdiff
path: root/base.h
blob: ae62268e5299dff9e4e7fe887ec312229006c072 (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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright (C) 2025 Aryadev Chavali

 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the Unlicense for details.

 * You may distribute and modify this code under the terms of the Unlicense,
 * which you should have received a copy of along with this program.  If not,
 * please go to <https://unlicense.org/>.

 * Created: 2025-08-19
 * Description: All the definitions required for the lisp system
 */

#ifndef BASE_H
#define BASE_H

#include <stdint.h>

/// The bare fucking minimum
#define MAX(A, B)      ((A) > (B) ? (A) : (B))
#define MIN(A, B)      ((A) < (B) ? (A) : (B))
#define ARRSIZE(A)     (sizeof(A) / sizeof((A)[0]))
#define NTH_BYTE(X, N) (((X) >> (8 * N)) & ((1 << 8) - 1))

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;

/// String Views for my String Needs
typedef struct
{
  u64 size;
  char *data;
} sv_t;

#define SV(DATA, SIZE) ((sv_t){.data = (DATA), .size = (SIZE)})
#define SV_FMT(SV)     (int)(SV).size, (SV).data
#define PR_SV          "%.*s"

sv_t sv_copy(sv_t old);

/// Dynamic arrays

typedef struct
{
  u64 size, capacity;
  u8 bytes[];
} vec_t;

#define VEC_GET(P)  (((vec_t *)(P)) - 1)
#define VEC_SIZE(P) (VEC_GET(P)->size)
#define VEC_CAP(P)  (VEC_GET(P)->capacity)
#define VEC_MULT    2

void vec_make(void **ptr, u64 size);
void vec_free(void **data);
void vec_ensure_remaining(void **ptr, u64 space);
void vec_append_byte(void **ptr, u8 byte);
void vec_append(void **ptr, void *data, u64 size);
void vec_clone(void **dest, void **src);

/// Symbol table
typedef struct
{
  u64 count;     // How many strings?
  u64 capacity;  // How many entry buckets?
  sv_t *entries; // this is actually a vector on the inside lol
} sym_table_t;

#define SYM_TABLE_INIT_SIZE 1024

u64 djb2(sv_t string);
void sym_table_init(sym_table_t *table);
char *sym_table_find(sym_table_t *table, sv_t sv);
void sym_table_cleanup(sym_table_t *table);

/// Pointer tagging scheme for lisps
#define NIL 0
typedef struct Obj lisp_t;

typedef enum Tag
{
  TAG_NIL  = 0b00000000,
  TAG_INT  = 0b00000001, // special so we can encode 63 bit integers
  TAG_SYM  = 0b00000100,
  NUM_TAGS = 3,
} tag_t;

enum Shift
{
  SHIFT_INT = 1,
  SHIFT_SYM = 8,
};

enum Mask
{
  MASK_INT = 0b00000001,
  MASK_SYM = 0b11111111,
};

#define TAG(PTR, TYPE)    ((lisp_t *)(((PTR) << SHIFT_##TYPE) | TAG_##TYPE))
#define IS_TAG(PTR, TYPE) (((u64)(PTR) & MASK_##TYPE) == TAG_##TYPE)
#define UNTAG(PTR, TYPE)  (((u64)PTR) >> SHIFT_##TYPE)

#define INT_MAX ((1L << 62) - 1)
#define INT_MIN (-(1L << 62))

lisp_t *tag_int(i64 i);
lisp_t *tag_sym(char *str);
i64 as_int(lisp_t *);
char *as_sym(lisp_t *);

#endif