Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/herb.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "include/herb.h"
#include "include/io.h"
#include "include/lexer.h"
#include "include/macros.h"
#include "include/parser.h"
#include "include/token.h"
#include "include/util/hb_arena.h"
#include "include/util/hb_array.h"
#include "include/util/hb_buffer.h"
#include "include/version.h"
Expand All @@ -11,8 +13,11 @@
#include <stdlib.h>

hb_array_T* herb_lex(const char* source) {
hb_arena_T allocator;
hb_arena_init(&allocator, KB(32));

lexer_T lexer = { 0 };
lexer_init(&lexer, source);
lexer_init(&lexer, &allocator, source);

token_T* token = NULL;
hb_array_T* tokens = hb_array_init(128);
Expand All @@ -23,14 +28,19 @@ hb_array_T* herb_lex(const char* source) {

hb_array_append(tokens, token);

hb_arena_free(&allocator);

return tokens;
}

AST_DOCUMENT_NODE_T* herb_parse(const char* source, parser_options_T* options) {
if (!source) { source = ""; }

hb_arena_T allocator;
hb_arena_init(&allocator, MB(512));

lexer_T lexer = { 0 };
lexer_init(&lexer, source);
lexer_init(&lexer, &allocator, source);
parser_T parser = { 0 };

parser_options_T parser_options = HERB_DEFAULT_PARSER_OPTIONS;
Expand Down
3 changes: 2 additions & 1 deletion src/include/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

#include "lexer_struct.h"
#include "token_struct.h"
#include "util/hb_arena.h"

void lexer_init(lexer_T* lexer, const char* source);
void lexer_init(lexer_T* lexer, hb_arena_T* allocator, const char* source);
token_T* lexer_next_token(lexer_T* lexer);
token_T* lexer_error(lexer_T* lexer, const char* message);

Expand Down
2 changes: 2 additions & 0 deletions src/include/lexer_struct.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef HERB_LEXER_STRUCT_H
#define HERB_LEXER_STRUCT_H

#include "util/hb_arena.h"
#include "util/hb_string.h"

#include <stdbool.h>
Expand All @@ -14,6 +15,7 @@ typedef enum {
} lexer_state_T;

typedef struct LEXER_STRUCT {
hb_arena_T* allocator;
hb_string_T source;

uint32_t current_line;
Expand Down
5 changes: 4 additions & 1 deletion src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "include/token.h"
#include "include/utf8.h"
#include "include/util.h"
#include "include/util/hb_arena.h"
#include "include/util/hb_buffer.h"
#include "include/util/hb_string.h"

Expand Down Expand Up @@ -31,7 +32,9 @@ static bool lexer_stalled(lexer_T* lexer) {
return lexer->stalled;
}

void lexer_init(lexer_T* lexer, const char* source) {
void lexer_init(lexer_T* lexer, hb_arena_T* allocator, const char* source) {
lexer->allocator = allocator;

if (source != NULL) {
lexer->source = hb_string(source);
} else {
Expand Down
18 changes: 9 additions & 9 deletions src/util/hb_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
#define _GNU_SOURCE
#endif

#ifdef HB_USE_MALLOC
#include <stdlib.h>
#else
#ifdef HB_USE_MMAP
#include <sys/mman.h>
#else
#include <stdlib.h>
#endif

void* hb_system_allocate_memory(size_t size) {
#ifdef HB_USE_MALLOC
return malloc(size);
#else
#ifdef HB_USE_MMAP
void* memory = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (memory == MAP_FAILED) { return NULL; }

return memory;
#else
return malloc(size);
#endif
}

void hb_system_free_memory(void* ptr, size_t size) {
#ifdef HB_USE_MALLOC
free(ptr);
#else
#ifdef HB_USE_MMAP
munmap(ptr, size);
#else
free(ptr);
#endif
}
Loading