Separated out the definitions to clean up a bit
We're going to be using C to compile our Lisp eventually, so I need to have a header file for compilation to work. We can still separate out the implementation details into separate files (then generate a shared library for usage in the link stage) but we need this header file to be singular and clean.
This commit is contained in:
84
base.h
Normal file
84
base.h
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
/* 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]))
|
||||||
|
|
||||||
|
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);
|
||||||
|
sv_t sym_table_find(sym_table_t *table, sv_t sv);
|
||||||
|
void sym_table_cleanup(sym_table_t *table);
|
||||||
|
|
||||||
|
#endif
|
||||||
59
main.c
59
main.c
@@ -1,46 +1,22 @@
|
|||||||
/* Copyright (C) 2025 Aryadev Chavali
|
/* Copyright (C) 2025 Aryadev Chavali
|
||||||
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Unlicense
|
* FOR A PARTICULAR PURPOSE. See the Unlicense for details.
|
||||||
* for details.
|
|
||||||
|
|
||||||
* You may distribute and modify this code under the terms of the
|
* You may distribute and modify this code under the terms of the Unlicense,
|
||||||
* Unlicense, which you should have received a copy of along with this
|
* which you should have received a copy of along with this program. If not,
|
||||||
* program. If not, please go to <https://unlicense.org/>.
|
* please go to <https://unlicense.org/>.
|
||||||
|
|
||||||
* Created: 2025-08-19
|
* Created: 2025-08-19
|
||||||
* Description: Entrypoint
|
* Description: Entrypoint
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX(A, B) ((A) > (B) ? (A) : (B))
|
#include "./base.h"
|
||||||
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
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_make(void **ptr, u64 size)
|
||||||
{
|
{
|
||||||
@@ -100,16 +76,6 @@ void vec_clone(void **dest, void **src)
|
|||||||
VEC_SIZE(*dest) = VEC_SIZE(*src);
|
VEC_SIZE(*dest) = VEC_SIZE(*src);
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
sv_t sv_copy(sv_t old)
|
||||||
{
|
{
|
||||||
char *newstr = calloc(1, old.size * sizeof(*newstr));
|
char *newstr = calloc(1, old.size * sizeof(*newstr));
|
||||||
@@ -117,13 +83,6 @@ sv_t sv_copy(sv_t old)
|
|||||||
return SV(newstr, old.size);
|
return SV(newstr, old.size);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
u64 djb2(sv_t string)
|
u64 djb2(sv_t string)
|
||||||
{
|
{
|
||||||
u64 hash = 5381;
|
u64 hash = 5381;
|
||||||
@@ -132,8 +91,6 @@ u64 djb2(sv_t string)
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SYM_TABLE_INIT_SIZE 1024
|
|
||||||
|
|
||||||
void sym_table_init(sym_table_t *table)
|
void sym_table_init(sym_table_t *table)
|
||||||
{
|
{
|
||||||
table->capacity = MAX(table->capacity, SYM_TABLE_INIT_SIZE);
|
table->capacity = MAX(table->capacity, SYM_TABLE_INIT_SIZE);
|
||||||
@@ -192,7 +149,7 @@ int main(void)
|
|||||||
"euismod", "tellus", "id", "erat",
|
"euismod", "tellus", "id", "erat",
|
||||||
};
|
};
|
||||||
|
|
||||||
for (u64 i = 0; i < sizeof(words) / sizeof(words[0]); ++i)
|
for (u64 i = 0; i < ARRSIZE(words); ++i)
|
||||||
{
|
{
|
||||||
sv_t sv = sym_table_find(&table, SV(words[i], strlen(words[i])));
|
sv_t sv = sym_table_find(&table, SV(words[i], strlen(words[i])));
|
||||||
printf("%s => %p\n", words[i], sv.data);
|
printf("%s => %p\n", words[i], sv.data);
|
||||||
|
|||||||
Reference in New Issue
Block a user