lang/tok.h

135 lines
1.9 KiB
C
Raw Normal View History

2021-12-21 01:18:22 +01:00
#ifndef __TOK_H__
#define __TOK_H__
#include <stdint.h>
#include "util.h"
2021-12-28 13:55:01 +01:00
enum Type {
TypeVoid = 0,
TypeFloat,
TypeInt,
TypeBool,
TypeChar,
TypePtr,
TypeArr,
TypeEnumSize,
};
typedef enum Type Type;
2021-12-21 01:18:22 +01:00
2021-12-25 12:16:06 +01:00
extern size_t type_size[TypeEnumSize];
2021-12-25 12:32:52 +01:00
extern const char *type_str[TypeEnumSize];
2021-12-25 12:16:06 +01:00
2021-12-21 01:18:22 +01:00
typedef struct Value {
Type type;
union {
2021-12-25 12:16:06 +01:00
pseudo_void Void;
2021-12-21 01:18:22 +01:00
double Float;
ssize_t Int;
2021-12-23 21:06:49 +01:00
bool Bool;
2021-12-23 21:26:53 +01:00
char Char;
struct {
Type type;
void *val;
} Ptr;
2021-12-25 12:16:06 +01:00
struct {
Type type;
2021-12-28 13:55:01 +01:00
bool is_string : 1;
bool dynamically_allocated : 1;
2021-12-25 12:16:06 +01:00
void *vals;
size_t len, cap;
} Arr;
2021-12-21 01:18:22 +01:00
};
} Value;
void free_value(Value *v, bool purge);
2021-12-25 12:16:06 +01:00
void print_value(const Value *v, bool raw);
2021-12-21 01:18:22 +01:00
enum Operator {
OpLCurl = '{',
OpRCurl = '}',
OpLParen = '(',
OpRParen = ')',
2021-12-30 17:59:28 +01:00
OpLBrack = '[',
OpRBrack = ']',
2021-12-21 01:18:22 +01:00
OpComma = ',',
OpAdd = '+',
OpSub = '-',
OpMul = '*',
OpDiv = '/',
2021-12-23 21:06:49 +01:00
OpNot = '!',
OpAddrOf = '&',
2021-12-21 01:18:22 +01:00
OpBeginNonchars = 256,
2021-12-23 21:06:49 +01:00
OpEq,
2021-12-26 12:19:54 +01:00
OpNeq,
2021-12-23 21:06:49 +01:00
OpLt,
OpGt,
OpLe,
OpGe,
2021-12-23 21:42:09 +01:00
OpAnd,
OpOr,
2021-12-21 01:18:22 +01:00
OpNewLn,
OpEOF,
OperatorEnumSize,
};
typedef enum Operator Operator;
#define PREC_DELIM -1
extern int8_t op_prec[OperatorEnumSize];
extern const char *op_str[OperatorEnumSize];
typedef struct Identifier {
enum {
IdentName,
IdentAddr,
} kind;
union {
char *Name;
size_t Addr;
};
} Identifier;
typedef struct Tok {
size_t ln, col;
enum {
TokOp,
TokVal,
TokIdent,
TokAssign,
TokDeclare,
TokIf,
TokElse,
2021-12-21 01:18:22 +01:00
TokWhile,
TokKindEnumSize,
} kind;
union {
Operator Op;
Value Val;
Identifier Ident;
};
} Tok;
extern const char *tok_str[TokKindEnumSize];
typedef struct TokListItem {
struct TokListItem *prev, *next;
Tok tok;
} TokListItem;
typedef struct TokList {
TokListItem *begin, *end;
Pool *p;
2021-12-21 01:18:22 +01:00
} TokList;
void toklist_init(TokList *l);
void toklist_term(TokList *l);
void toklist_append(TokList *l, Tok t);
void toklist_del(TokList *l, TokListItem *from, TokListItem *to);
void print_toks(TokList *l);
#endif /* TOK_H */