WIP allocator for compiler.

This commit is contained in:
zongor 2026-03-22 09:27:18 -07:00
parent 6310390cc4
commit 373caf7b5e
9 changed files with 1058 additions and 842 deletions

33
.clang-format Normal file
View File

@ -0,0 +1,33 @@
# Plan 9 coding conventions for C (http://man.9front.org/6/style)
BasedOnStyle: LLVM
IndentWidth: 2
TabWidth: 2
UseTab: Always
SpaceBeforeParens: Never
SpaceBeforeAssignmentOperators: true
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
RemoveBracesLLVM: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
AlwaysBreakAfterReturnType: TopLevelDefinitions
DerivePointerAlignment: false
PointerAlignment: Right
AlignOperands: Align
AlignAfterOpenBracket: Align
SortIncludes: Never
IndentCaseLabels: false

126
lexer.c
View File

@ -2,51 +2,73 @@
#include "lexer.h"
typedef struct {
typedef struct lexer_s Lexer;
struct lexer_s {
const char *start;
const char *current;
i32 line;
} Lexer;
};
Lexer lexer;
void init_lexer(const char *source) {
void
init_lexer(const char *source)
{
lexer.start = source;
lexer.current = source;
lexer.line = 1;
}
static bool is_alpha(char c) {
static bool
is_alpha(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}
static bool is_digit(char c) { return c >= '0' && c <= '9'; }
static bool
is_digit(char c)
{
return c >= '0' && c <= '9';
}
static bool is_at_end() { return *lexer.current == '\0'; }
static bool
is_at_end()
{
return *lexer.current == '\0';
}
static char advance() {
static char
advance()
{
lexer.current++;
return lexer.current[-1];
}
char peek() { return *lexer.current; }
char
peek()
{
return *lexer.current;
}
static char peek_next() {
if (is_at_end())
return '\0';
static char
peek_next()
{
if(is_at_end()) return '\0';
return lexer.current[1];
}
static bool match(char expected) {
if (is_at_end())
return false;
if (*lexer.current != expected)
return false;
static bool
match(char expected)
{
if(is_at_end()) return false;
if(*lexer.current != expected) return false;
lexer.current++;
return true;
}
static Token make_token(TokenType type) {
static Token
make_token(TokenType type)
{
Token token;
token.type = type;
token.start = lexer.start;
@ -55,7 +77,9 @@ static Token make_token(TokenType type) {
return token;
}
static Token error_token(const char *message) {
static Token
error_token(const char *message)
{
Token token;
token.type = TOKEN_ERROR;
token.start = message;
@ -64,7 +88,9 @@ static Token error_token(const char *message) {
return token;
}
static void skip_whitespace() {
static void
skip_whitespace()
{
for(;;) {
char c = peek();
switch(c) {
@ -81,15 +107,13 @@ static void skip_whitespace() {
if(peek_next() == '/') {
// Single-line comment: skip until newline or end of file
advance();
while (peek() != '\n' && !is_at_end())
advance();
while(peek() != '\n' && !is_at_end()) advance();
} else if(peek_next() == '*') {
// Multi-line comment: skip until '*/' or end of file
advance();
advance();
while(!is_at_end()) {
if (peek() == '\n')
lexer.line++;
if(peek() == '\n') lexer.line++;
if(peek() == '*' && peek_next() == '/') {
advance();
advance();
@ -107,8 +131,9 @@ static void skip_whitespace() {
}
}
static TokenType check_keyword(i32 start, i32 length, const char *rest,
TokenType type) {
static TokenType
check_keyword(i32 start, i32 length, const char *rest, TokenType type)
{
if(lexer.current - lexer.start == start + length &&
memcmp(lexer.start + start, rest, length) == 0) {
return type;
@ -117,7 +142,9 @@ static TokenType check_keyword(i32 start, i32 length, const char *rest,
return TOKEN_IDENTIFIER;
}
static TokenType identifierType() {
static TokenType
identifierType()
{
switch(lexer.start[0]) {
case 'a':
if(lexer.current - lexer.start > 1) {
@ -202,7 +229,8 @@ static TokenType identifierType() {
break;
case 'p':
if(lexer.current - lexer.start > 1) {
switch (lexer.start[1]) { case 't':
switch(lexer.start[1]) {
case 't':
return check_keyword(2, 1, "r", TOKEN_TYPE_PTR);
case 'l':
@ -305,23 +333,24 @@ static TokenType identifierType() {
return TOKEN_IDENTIFIER;
}
static Token identifier() {
while (is_alpha(peek()) || is_digit(peek()))
advance();
static Token
identifier()
{
while(is_alpha(peek()) || is_digit(peek())) advance();
return make_token(identifierType());
}
static Token number() {
while (is_digit(peek()))
advance();
static Token
number()
{
while(is_digit(peek())) advance();
/* Look for a fractional part. */
if(peek() == '.' && is_digit(peek_next())) {
/* Consume the ".". */
advance();
while (is_digit(peek()))
advance();
while(is_digit(peek())) advance();
return make_token(TOKEN_LITERAL_REAL);
}
@ -329,34 +358,33 @@ static Token number() {
return make_token(TOKEN_LITERAL_INT);
}
static Token string() {
static Token
string()
{
while(peek() != '"' && !is_at_end()) {
if (peek() == '\n')
lexer.line++;
if(peek() == '\n') lexer.line++;
advance();
}
if (is_at_end())
return error_token("Unterminated string.");
if(is_at_end()) return error_token("Unterminated string.");
/* The closing quote. */
advance();
return make_token(TOKEN_LITERAL_STR);
}
Token next_token() {
Token
next_token()
{
skip_whitespace();
lexer.start = lexer.current;
if (is_at_end())
return make_token(TOKEN_EOF);
if(is_at_end()) return make_token(TOKEN_EOF);
char c = advance();
if (is_alpha(c))
return identifier();
if(is_alpha(c)) return identifier();
char next = peek();
if ((c == '-' && is_digit(next)) || is_digit(c))
return number();
if((c == '-' && is_digit(next)) || is_digit(c)) return number();
switch(c) {
case '(':
@ -406,7 +434,9 @@ Token next_token() {
return error_token("Unexpected character.");
}
const char *token_type_to_string(TokenType type) {
const char *
token_type_to_string(TokenType type)
{
switch(type) {
case TOKEN_EOF:
return "EOF";

View File

@ -83,12 +83,13 @@ typedef enum {
TOKEN_ARROW_RIGHT
} TokenType;
typedef struct {
typedef struct token_s Token;
struct token_s {
TokenType type;
const char *start;
i32 length;
i32 line;
} Token;
};
void init_lexer(const char *source);
Token next_token();

66
libc.c
View File

@ -1,30 +1,32 @@
#include "libc.h"
void mcpy(void *to, void *from, u32 length) {
void
mcpy(void *to, void *from, u32 length)
{
u8 *src, *dest;
if(to == nil || from == nil) return;
src = (u8 *)from;
dest = (u8 *)to;
while (length-- > 0) {
*(dest++) = *(src++);
}
while(length-- > 0) *(dest++) = *(src++);
return;
}
i32 scpy(char *to, const char *from, u32 length) {
i32
scpy(char *to, const char *from, u32 length)
{
u32 i;
if(to == nil || from == nil) return -1;
if (length == 0) {return 0;}
for (i = 0; i < length - 1 && from[i] != '\0'; i++) {
to[i] = from[i];
}
if(length == 0) return 0;
for(i = 0; i < length - 1 && from[i] != '\0'; i++) to[i] = from[i];
to[i] = '\0';
return 0;
}
bool seq(const char *s1, const char *s2) {
bool
seq(const char *s1, const char *s2)
{
if(s1 == nil && s2 == nil) return true;
if(s1 == nil || s2 == nil) return false;
@ -37,7 +39,9 @@ bool seq(const char *s1, const char *s2) {
return (*s1 == '\0' && *s2 == '\0');
}
bool sleq(const char *s1, const char *s2, u32 length) {
bool
sleq(const char *s1, const char *s2, u32 length)
{
u32 i;
if(s1 == nil && s2 == nil) return true;
if(s1 == nil || s2 == nil) return false;
@ -53,20 +57,40 @@ bool sleq(const char *s1, const char *s2, u32 length) {
return (*s1 == '\0' && *s2 == '\0');
}
u32 slen(const char *str) {
u32
slen(const char *str)
{
u32 i;
if (str == nil) {return 0;}
for (i = 0; str[i] != '\0'; i++) {
;
}
if(str == nil) return 0;
for(i = 0; str[i] != '\0'; i++);
return i;
}
u32 snlen(const char *str, u32 max_len) {
u32
snlen(const char *str, u32 max_len)
{
u32 i;
if (str == nil) {return 0;}
for (i = 0; i < max_len && str[i] != '\0'; i++) {
;
}
if(str == nil) return 0;
for(i = 0; i < max_len && str[i] != '\0'; i++);
return i;
}
void *
aaloc(Arena *arena, u32 size)
{
u32 pos;
if(arena == nil) return nil;
if(arena->count + size > arena->capacity) return nil;
pos = arena->count;
arena->count += size;
return &arena->tape[pos];
}
u32
afree(Arena *arena)
{
u32 freed = arena->count;
arena->count = 0;
return freed;
}

9
libc.h
View File

@ -69,11 +69,20 @@ typedef u8 bool;
#define USED(x) ((void)(x))
typedef struct arena_s Arena;
struct arena_s {
u8 *tape;
u32 count;
u32 capacity;
};
void mcpy(void *dest, void *src, u32 n);
i32 scpy(char *to, const char *from, u32 length);
bool seq(const char *s1, const char *s2);
bool sleq(const char *s1, const char *s2, u32 length);
u32 slen(const char *str);
u32 snlen(const char *str, u32 max_len);
void *aalloc(Arena *arena, u32 size);
u32 afree(Arena *arena);
#endif

12
main.c
View File

@ -2,7 +2,8 @@
#include <stdlib.h>
#define EMBED_FILE(name) \
void emit_##name(const char *filename) { \
void emit_##name(const char *filename) \
{ \
FILE *f = fopen(filename, "wb"); \
if(f) { \
fwrite(name, 1, name##_len, f); \
@ -10,14 +11,15 @@
} \
}
int main(int argc, char **argv) {
int
main(int argc, char **argv)
{
char *name;
if (argc > 1) {
if(argc > 1)
name = argv[1];
} else {
else
name = "'u'";
}
printf("nuqneH %s?\n", name);
return EXIT_SUCCESS;

148
parser.c
View File

@ -1,22 +1,45 @@
#include "parser.h"
bool push(TokenStack *ts, Token t) {
Parser parser;
bool
advance()
{
parser.previous = parser.current;
for(;;) {
parser.current = next_token();
if(parser.current.type != TOKEN_ERROR) return true;
return false;
}
}
bool
push(TokenStack *ts, Token t)
{
if(ts->count >= ts->capacity) return false;
ts->stack[ts->count++] = t;
return true;
}
Token pop(TokenStack *ts) {
Token
pop(TokenStack *ts)
{
if(ts->count == 0) return (Token){TOKEN_ERROR, nil, -1, -1};
return ts->stack[--ts->count];
}
Token top(TokenStack *ts) {
Token
top(TokenStack *ts)
{
if(ts->count == 0) return (Token){TOKEN_ERROR, nil, -1, -1};
return ts->stack[ts->count - 1];
}
bool enqueue(TokenQueue *tq, Token t) {
bool
enqueue(TokenQueue *tq, Token t)
{
if(tq->count >= tq->capacity) return false;
tq->queue[tq->end] = t;
@ -25,7 +48,9 @@ bool enqueue(TokenQueue *tq, Token t) {
return true;
}
Token dequeue(TokenQueue *tq) {
Token
dequeue(TokenQueue *tq)
{
if(tq->count == 0) return (Token){TOKEN_ERROR, NULL, -1, -1};
Token t = tq->queue[tq->start];
@ -34,18 +59,119 @@ Token dequeue(TokenQueue *tq) {
return t;
}
Token peek_queue(TokenQueue *tq) {
Token
peek_queue(TokenQueue *tq)
{
if(tq->count == 0) return (Token){TOKEN_ERROR, NULL, -1, -1};
return tq->queue[tq->start];
}
bool expression() {
u32
idx_from_arena(Arena *arena, void *p)
{
return (u32)((u8 *)p - arena->tape);
}
bool compile(char *source) {
TokenStack operators;
TokenQueue output;
void *
ptr_from_arena(Arena *arena, u32 i)
{
return &arena->tape[i];
}
ArenaList *
al_create(Arena *arena, u32 size)
{
ArenaList *meta = aalloc(arena, sizeof(ArenaList));
if(!meta) return nil;
meta->size = size + sizeof(u32);
meta->arena = arena;
meta->head = 0;
meta->tail = 0;
return meta;
}
void *
al_append(ArenaList *list, void **out_payload)
{
void *node = aalloc(list->arena, list->size);
if(!node) return nil;
u32 idx = idx_from_arena(list->arena, node);
void *payload = node; /* Payload starts at offset 0 */
void *cdr_ptr = (u8 *)node + (list->size - sizeof(u32));
*(u32 *)cdr_ptr = 0;
if(list->tail != 0) {
void *prev_node = ptr_from_arena(list->arena, list->tail);
void *prev_cdr = (u8 *)prev_node + (list->size - sizeof(u32));
*(u32 *)prev_cdr = idx;
} else {
list->head = idx;
}
list->tail = idx;
if(out_payload) *out_payload = payload;
return payload;
}
void *
al_head(ArenaList *list)
{
if(list->head == 0) return nil;
return ptr_from_arena(list->arena, list->head);
}
void *
al_tail(ArenaList *list)
{
if(list->tail == 0) return nil;
return ptr_from_arena(list->arena, list->tail);
}
SymbolLink *
symbol_table_find(ArenaList *table, const char *name)
{
void *current = al_head(table);
Arena *arena = table->arena;
while(current != nil) {
SymbolLink *link = (SymbolLink *)current;
if(seq(link->s.name.start, name)) return link;
u32 next_idx = link->cdr;
current = (next_idx == 0) ? nil : ptr_from_arena(arena, next_idx);
}
return nil;
}
/****************************************************
* Parser
***************************************************/
bool
expression()
{
Token operator_stack[256];
TokenStack operators = {0};
operators.stack = operator_stack;
operators.capacity = 256;
Token output_queue[256];
TokenQueue output = {0};
output.queue = output_queue;
output.capacity = 256;
return true;
}
bool
compile(char *source)
{
return true;
}

100
parser.h
View File

@ -4,8 +4,7 @@
#include "libc.h"
#include "lexer.h"
typedef enum { GLOBAL, LOCAL, VAR } ScopeType;
typedef enum {
typedef enum symbol_type_e {
VOID,
BOOL,
I8,
@ -18,72 +17,60 @@ typedef enum {
F16,
F32,
STR,
PLEX,
ARRAY,
FUNCTION
FUNCTION,
PLEX,
METHOD,
TRAIT,
} SymbolType;
typedef struct arena_list_s ArenaList;
typedef struct symbol_s Symbol;
typedef struct symbol_tab_s SymbolTable;
typedef struct value_type_s ValueType;
typedef struct plex_fields_tab_s PlexFieldsTable;
typedef struct plex_def_s PlexDef;
typedef struct plex_tab_s PlexTable;
typedef struct scope_s Scope;
typedef struct scope_tab_s ScopeTable;
typedef struct symbol_link_s SymbolLink;
typedef struct token_stack_s TokenStack;
typedef struct queue_s TokenQueue;
typedef struct parser_s Parser;
struct value_type_s {
SymbolType type;
u32 name;
u32 size;
u32 table_ref; // if it is a heap object
};
struct plex_def_s {
u32 name;
u32 size;
u32 field_ref_start;
u32 field_count;
};
struct plex_fields_tab_s {
u32 *plex_refs;
ValueType *fields;
u32 count;
u32 capacity;
};
struct plex_tab_s {
PlexDef *symbols;
u32 count;
u32 capacity;
};
#define MAX_SYMBOL_NAME_LENGTH 64
struct symbol_s {
char name[MAX_SYMBOL_NAME_LENGTH];
u8 name_length;
Token name;
SymbolType type;
ScopeType scope;
u32 ref; // vm->mp if global, vm->pc local, register if var
u32 size; // size of symbol
u32 size;
i32 scope;
union type_def {
struct trait_def {
u32 field_ref_start; /* reference to field list of symbols */
u32 methods_ref_start; /* zero if none */
} trait;
struct plex_def {
u32 field_ref_start; /* reference to field list of symbols */
u32 methods_ref_start; /* zero if none */
} plex;
struct function_def {
SymbolType return_type;
u32 arguments_ref_start; /* reference to field list of symbols */
} function;
struct array_def {
SymbolType type;
u32 length; /* zero means "unbounded" */
} array;
struct field_def {
u32 offset;
} field;
} def;
};
#define MAX_SYMBOLS 256
struct symbol_tab_s {
Symbol symbols[MAX_SYMBOLS];
u8 count;
i32 parent;
struct symbol_link_s {
Symbol s;
u32 cdr; /* zero means "end of list" */
};
struct scope_tab_s {
SymbolTable *scopes;
struct arena_list_s {
Arena *arena;
u32 head;
u32 tail;
u32 size;
u32 count;
u32 capacity;
i32 scope_ref;
u32 depth;
i32 parent;
};
struct token_stack_s {
@ -100,6 +87,11 @@ struct queue_s {
i32 count;
};
struct parser_s {
Token current;
Token previous;
};
bool push(TokenStack *ts, Token t);
Token pop(TokenStack *ts);
Token top(TokenStack *ts);

View File

@ -3,7 +3,9 @@
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
int
main(int argc, char *argv[])
{
FILE *in;
int c;
long count = 0;
@ -31,11 +33,8 @@ int main(int argc, char *argv[]) {
strcpy(var_name, argv[1]);
for (p = var_name; *p; ++p) {
if (!isalnum((unsigned char)*p)) {
*p = '_';
}
}
for(p = var_name; *p; ++p)
if(!isalnum((unsigned char)*p)) *p = '_';
printf("unsigned char %s[] = {\n", var_name);