undar-lang/parser.h

88 lines
1.8 KiB
C

#ifndef UNDAR_PARSER_H
#define UNDAR_PARSER_H
#include "libc.h"
#include "list.h"
#include "lexer.h"
#include "emit.h"
typedef enum symbol_type_e {
VOID,
BOOL,
BYTE,
INT,
NAT,
REAL,
STR,
U8,
I8,
I16,
U16,
I32,
U32,
F32,
ARRAY,
FUNCTION,
PLEX,
METHOD,
TRAIT,
CONST
} SymbolType;
typedef struct symbol_s Symbol;
typedef struct scope_s Scope;
typedef struct parser_s Parser;
typedef struct parse_rule_s ParseRule;
#define MAX_SYMBOL_NAME_LENGTH 64
struct symbol_s {
char name[MAX_SYMBOL_NAME_LENGTH]; /* ptr to name */
u8 name_length; /* length of the name, max 64 */
SymbolType type; /* the type for this symbol */
SymbolType secondary_type; /* return type for functions/methods, or const if type/plex */
u32 ref; /* either constant value or pointer to other thing */
u32 size; /* either size of the plex or length of the array/list/etc. */
List *args; /* function params or plex constructor args */
List *fields; /* either plex variable fields, or method signatures */
};
struct scope_s {
Scope *parent; /* pointer to this scopes parent to "bubble up"*/
List *symbols; /* list of symbols that live in this scope */
};
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)();
struct parse_rule_s {
ParseFn prefix;
ParseFn infix;
Precedence precedence;
};
struct parser_s {
Scope *current_scope;
Token current;
Token previous;
};
bool compile(char *source);
extern void emit_return();
extern void emit_constant();
#endif