*: Split off headers into their own folder
Main reason is so we don't have that stupid arl prefix directory in our source code. Now our source code is flat, and we can still reference headers by linking from root.
This commit is contained in:
55
include/arl/lib/base.h
Normal file
55
include/arl/lib/base.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/* base.h: Basic definitions
|
||||
* Created: 2026-01-22
|
||||
* Author: Aryadev Chavali
|
||||
* License: See end of file
|
||||
* Commentary:
|
||||
|
||||
Taken from prick_aliases.h: see https://github.com/oreodave/prick.
|
||||
*/
|
||||
|
||||
#ifndef BASE_H
|
||||
#define BASE_H
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
|
||||
static_assert(sizeof(float) == 4, "f32 requires 4 byte floats");
|
||||
static_assert(sizeof(double) == 8, "f64 requires 8 byte doubles");
|
||||
typedef float f32;
|
||||
typedef double f64;
|
||||
|
||||
#define MAX(A, B) ((A) > (B) ? (A) : (B))
|
||||
#define MIN(A, B) ((A) > (B) ? (B) : (A))
|
||||
#define ARRSIZE(A) ((sizeof(A)) / sizeof((A)[0]))
|
||||
|
||||
#define FAIL(...) \
|
||||
do \
|
||||
{ \
|
||||
fprintf(stderr, "FAIL: "); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
assert(0); \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
/* Copyright (C) 2026 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 MIT License for details.
|
||||
|
||||
* You may distribute and modify this code under the terms of the MIT License,
|
||||
* which you should have received a copy of along with this program. If not,
|
||||
* please go to <https://opensource.org/license/MIT>.
|
||||
|
||||
*/
|
||||
48
include/arl/lib/sv.h
Normal file
48
include/arl/lib/sv.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/* sv.h: String Views
|
||||
* Created: 2026-01-22
|
||||
* Author: Aryadev Chavali
|
||||
* License: See end of file
|
||||
* Commentary: Absolute bajoding
|
||||
*/
|
||||
|
||||
#ifndef SV_H
|
||||
#define SV_H
|
||||
|
||||
#include <arl/lib/base.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *data;
|
||||
u64 size;
|
||||
} sv_t;
|
||||
|
||||
// Constructor and printer macros
|
||||
#define SV(PTR, SIZE) ((sv_t){.data = (PTR), .size = (SIZE)})
|
||||
#define PR_SV "%.*s"
|
||||
#define SV_FMT(SV) (int)((SV).size), (SV).data
|
||||
|
||||
// Return a new string view which has had LEN characters removed from the
|
||||
// beginning
|
||||
sv_t sv_chop_left(sv_t sv, u64 len);
|
||||
// Return a new string view which has had LEN characters removed from the end
|
||||
sv_t sv_chop_right(sv_t sv, u64 len);
|
||||
// Return the first index where SV does not present a character from EXPECTED
|
||||
// (strspn equivalent)
|
||||
u64 sv_while(const sv_t sv, const char *expected);
|
||||
// Return the first index where SV presents a character from EXPECTED (strcspn
|
||||
// equivalent)
|
||||
u64 sv_till(const sv_t sv, const char *expected);
|
||||
|
||||
#endif
|
||||
|
||||
/* Copyright (C) 2026 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 MIT License for details.
|
||||
|
||||
* You may distribute and modify this code under the terms of the MIT License,
|
||||
* which you should have received a copy of along with this program. If not,
|
||||
* please go to <https://opensource.org/license/MIT>.
|
||||
|
||||
*/
|
||||
66
include/arl/lib/vec.h
Normal file
66
include/arl/lib/vec.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/* vec.h: A dynamically sized array with SBO.
|
||||
* Created: 2026-01-22
|
||||
* Author: Aryadev Chavali
|
||||
* License: See end of file
|
||||
* Commentary:
|
||||
|
||||
Taken from prick_vec.h: see https://github.com/oreodave/prick.
|
||||
*/
|
||||
|
||||
#ifndef VEC_H
|
||||
#define VEC_H
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include <arl/lib/base.h>
|
||||
|
||||
#define VEC_INLINE_CAPACITY 32
|
||||
#define VEC_MULT 2
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u64 size, capacity;
|
||||
u8 not_inlined;
|
||||
union
|
||||
{
|
||||
void *ptr;
|
||||
alignas(max_align_t) u8 inlined[VEC_INLINE_CAPACITY];
|
||||
};
|
||||
} vec_t;
|
||||
|
||||
static_assert(sizeof(vec_t) == 64, "Expected sizeof(vec_t) to be 64");
|
||||
|
||||
void vec_append(vec_t *vec, const void *const ptr, u64 size);
|
||||
void vec_append_byte(vec_t *vec, u8 byte);
|
||||
|
||||
// Returns pointer to the start of the buffer VEC is currently using to store
|
||||
// data (either its inline buffer or the heap buffer).
|
||||
void *vec_data(vec_t *vec);
|
||||
|
||||
// Ensure VEC has at least CAPACITY capacity.
|
||||
void vec_ensure_capacity(vec_t *vec, u64 capacity);
|
||||
|
||||
// Ensure VEC has at least SIZE bytes free
|
||||
void vec_ensure_free(vec_t *vec, u64 size);
|
||||
void vec_free(vec_t *vec);
|
||||
|
||||
// Copy all data from V1 into V2.
|
||||
void vec_clone(vec_t *v2, vec_t *v1);
|
||||
|
||||
// Helper macro to use a vector as a type generic (but homogeneous) container.
|
||||
#define VEC_GET(VEC, INDEX, TYPE) (((TYPE *)vec_data(VEC))[INDEX])
|
||||
|
||||
#endif
|
||||
|
||||
/* Copyright (C) 2026 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/>.
|
||||
|
||||
*/
|
||||
75
include/arl/parser/ast.h
Normal file
75
include/arl/parser/ast.h
Normal file
@@ -0,0 +1,75 @@
|
||||
/* ast.h: General definition of the AST and nodes within it.
|
||||
* Created: 2026-01-22
|
||||
* Author: Aryadev Chavali
|
||||
* License: See end of file
|
||||
* Commentary:
|
||||
*/
|
||||
|
||||
#ifndef AST_H
|
||||
#define AST_H
|
||||
|
||||
#include <arl/lib/base.h>
|
||||
#include <arl/lib/sv.h>
|
||||
#include <arl/lib/vec.h>
|
||||
|
||||
/// Types the AST can encode
|
||||
typedef enum
|
||||
{
|
||||
AST_NODE_TYPE_PRIMITIVE = 0,
|
||||
AST_NODE_TYPE_SYMBOL,
|
||||
AST_NODE_TYPE_STRING,
|
||||
|
||||
NUM_AST_NODE_TYPES,
|
||||
} ast_node_type_t;
|
||||
|
||||
/// Primitives (values, callables, etc) as symbols
|
||||
typedef enum
|
||||
{
|
||||
AST_PRIM_NIL = 0,
|
||||
AST_PRIM_PRINTLN,
|
||||
|
||||
NUM_AST_PRIMS,
|
||||
} ast_prim_t;
|
||||
|
||||
const char *ast_prim_to_cstr(ast_prim_t);
|
||||
|
||||
/// Node of the AST as a tagged union
|
||||
typedef struct
|
||||
{
|
||||
u64 byte_location;
|
||||
ast_node_type_t type;
|
||||
union
|
||||
{
|
||||
ast_prim_t as_prim;
|
||||
sv_t as_symbol;
|
||||
sv_t as_string;
|
||||
} value;
|
||||
} ast_node_t;
|
||||
|
||||
ast_node_t ast_node_prim(u64 byte, ast_prim_t primitive);
|
||||
ast_node_t ast_node_symbol(u64 byte, sv_t symbol);
|
||||
ast_node_t ast_node_string(u64 byte, sv_t string);
|
||||
void ast_node_print(FILE *fp, ast_node_t *node);
|
||||
|
||||
/// The AST as a flat collection of nodes
|
||||
typedef struct
|
||||
{
|
||||
vec_t nodes;
|
||||
} ast_t;
|
||||
|
||||
void ast_free(ast_t *ast);
|
||||
void ast_print(FILE *fp, ast_t *ast);
|
||||
|
||||
#endif
|
||||
|
||||
/* Copyright (C) 2026 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 MIT License for details.
|
||||
|
||||
* You may distribute and modify this code under the terms of the MIT License,
|
||||
* which you should have received a copy of along with this program. If not,
|
||||
* please go to <https://opensource.org/license/MIT>.
|
||||
|
||||
*/
|
||||
49
include/arl/parser/parser.h
Normal file
49
include/arl/parser/parser.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/* parser.h: Parser which takes character buffers and yields an AST
|
||||
* Created: 2026-01-22
|
||||
* Author: Aryadev Chavali
|
||||
* License: See end of file
|
||||
* Commentary:
|
||||
*/
|
||||
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
|
||||
#include <arl/parser/ast.h>
|
||||
|
||||
/// Parser streams, utilised when generating an AST.
|
||||
typedef struct
|
||||
{
|
||||
u64 byte;
|
||||
sv_t contents;
|
||||
} parse_stream_t;
|
||||
|
||||
/// Types of errors that may occur during parsing
|
||||
typedef enum
|
||||
{
|
||||
PARSE_ERR_OK = 0,
|
||||
PARSE_ERR_EXPECTED_SPEECH_MARKS,
|
||||
PARSE_ERR_UNKNOWN_CHAR,
|
||||
} parse_err_t;
|
||||
const char *parse_err_to_string(parse_err_t err);
|
||||
|
||||
// Generates an AST from STREAM, storing it in OUT. Returns any errors it may
|
||||
// generate.
|
||||
parse_err_t parse(ast_t *out, parse_stream_t *stream);
|
||||
|
||||
// Computes the line and column that STREAM is currently pointing at in its
|
||||
// buffer, storing it in LINE and COL.
|
||||
void parse_stream_get_line_col(parse_stream_t *stream, u64 *line, u64 *col);
|
||||
|
||||
#endif
|
||||
|
||||
/* Copyright (C) 2026 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 MIT License for details.
|
||||
|
||||
* You may distribute and modify this code under the terms of the MIT License,
|
||||
* which you should have received a copy of along with this program. If not,
|
||||
* please go to <https://opensource.org/license/MIT>.
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user