lang/tok.h

129 lines
1.8 KiB
C
Raw Normal View History

2021-12-21 01:18:22 +01:00
#ifndef __TOK_H__
#define __TOK_H__
#include <stdint.h>
#include <unistd.h>
#include "util.h"
2021-12-21 01:18:22 +01:00
typedef struct Type {
enum {
TypeVoid = 0,
TypeFloat,
TypeInt,
2021-12-23 21:06:49 +01:00
TypeBool,
2021-12-23 21:26:53 +01:00
TypeChar,
2021-12-25 12:16:06 +01:00
TypeArr,
TypeEnumSize,
2021-12-21 01:18:22 +01:00
} kind;
/*union {
};*/
} Type;
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;
2021-12-25 12:16:06 +01:00
struct {
bool is_string : 1;
Type type;
void *vals;
size_t len, cap;
} Arr;
2021-12-21 01:18:22 +01:00
};
} Value;
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 = ')',
OpComma = ',',
OpAdd = '+',
OpSub = '-',
OpMul = '*',
OpDiv = '/',
2021-12-23 21:06:49 +01:00
OpNot = '!',
2021-12-21 01:18:22 +01:00
OpBeginNonchars = 256,
2021-12-23 21:06:49 +01:00
OpEq,
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 */