115 lines
2.0 KiB
C
115 lines
2.0 KiB
C
#ifndef UNDAR_COMPILER_H
|
|
#define UNDAR_COMPILER_H
|
|
|
|
#include "../../vm/libc.h"
|
|
#include "../../vm/vm.h"
|
|
|
|
typedef enum { GLOBAL, LOCAL } 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;
|
|
|
|
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;
|
|
};
|
|
|
|
|
|
typedef enum {
|
|
PREC_NONE,
|
|
PREC_ASSIGNMENT, // =
|
|
PREC_OR, // or
|
|
PREC_AND, // and
|
|
PREC_EQUALITY, // == !=
|
|
PREC_COMPARISON, // < > <= >=
|
|
PREC_TERM, // + -
|
|
PREC_FACTOR, // * /
|
|
PREC_UNARY, // ! -
|
|
PREC_CALL, // . ()
|
|
PREC_PRIMARY
|
|
} Precedence;
|
|
|
|
typedef void (*ParseFn)();
|
|
|
|
typedef struct {
|
|
ParseFn prefix;
|
|
ParseFn infix;
|
|
Precedence precedence;
|
|
} ParseRule;
|
|
|
|
|
|
bool compile(ScopeTable *st, char *source);
|
|
extern bool table_realloc(ScopeTable *table);/* implement this in arch/ not here */
|
|
extern void error(const char* msg);
|
|
|
|
#endif
|