112 lines
1.8 KiB
C
112 lines
1.8 KiB
C
#ifndef UNDAR_PARSER_H
|
|
#define UNDAR_PARSER_H
|
|
|
|
#include "libc.h"
|
|
#include "lexer.h"
|
|
|
|
typedef enum { GLOBAL, LOCAL, VAR } ScopeType;
|
|
typedef enum {
|
|
VOID,
|
|
BOOL,
|
|
I8,
|
|
I16,
|
|
I32,
|
|
U8,
|
|
U16,
|
|
U32,
|
|
F8,
|
|
F16,
|
|
F32,
|
|
STR,
|
|
PLEX,
|
|
ARRAY,
|
|
FUNCTION
|
|
} SymbolType;
|
|
|
|
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 token_stack_s TokenStack;
|
|
typedef struct queue_s TokenQueue;
|
|
|
|
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;
|
|
SymbolType type;
|
|
ScopeType scope;
|
|
u32 ref; // vm->mp if global, vm->pc local, register if var
|
|
u32 size; // size of symbol
|
|
};
|
|
|
|
#define MAX_SYMBOLS 256
|
|
struct symbol_tab_s {
|
|
Symbol symbols[MAX_SYMBOLS];
|
|
u8 count;
|
|
i32 parent;
|
|
};
|
|
|
|
struct scope_tab_s {
|
|
SymbolTable *scopes;
|
|
u32 count;
|
|
u32 capacity;
|
|
i32 scope_ref;
|
|
u32 depth;
|
|
};
|
|
|
|
struct token_stack_s {
|
|
Token *stack;
|
|
i32 capacity;
|
|
i32 count;
|
|
};
|
|
|
|
struct queue_s {
|
|
Token *queue;
|
|
i32 capacity;
|
|
i32 start;
|
|
i32 end;
|
|
i32 count;
|
|
};
|
|
|
|
bool push(TokenStack *ts, Token t);
|
|
Token pop(TokenStack *ts);
|
|
Token top(TokenStack *ts);
|
|
bool enqueue(TokenQueue *tq, Token t);
|
|
Token dequeue(TokenQueue *tq);
|
|
Token peek_queue(TokenQueue *tq);
|
|
bool compile(char *source);
|
|
|
|
#endif
|