2021-12-21 01:18:22 +01:00
|
|
|
#ifndef __TOK_H__
|
|
|
|
#define __TOK_H__
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-12-21 11:40:49 +01:00
|
|
|
#include "util.h"
|
|
|
|
|
2021-12-21 01:18:22 +01:00
|
|
|
typedef struct Type {
|
|
|
|
enum {
|
|
|
|
TypeVoid = 0,
|
|
|
|
TypeFloat,
|
|
|
|
TypeInt,
|
|
|
|
} kind;
|
|
|
|
|
|
|
|
/*union {
|
|
|
|
};*/
|
|
|
|
} Type;
|
|
|
|
|
|
|
|
typedef struct Value {
|
|
|
|
Type type;
|
|
|
|
|
|
|
|
union {
|
|
|
|
double Float;
|
|
|
|
ssize_t Int;
|
|
|
|
};
|
|
|
|
} Value;
|
|
|
|
|
|
|
|
enum Operator {
|
|
|
|
OpLCurl = '{',
|
|
|
|
OpRCurl = '}',
|
|
|
|
OpLParen = '(',
|
|
|
|
OpRParen = ')',
|
|
|
|
OpComma = ',',
|
|
|
|
OpAdd = '+',
|
|
|
|
OpSub = '-',
|
|
|
|
OpMul = '*',
|
|
|
|
OpDiv = '/',
|
|
|
|
OpBeginNonchars = 256,
|
|
|
|
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,
|
|
|
|
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;
|
2021-12-21 11:40:49 +01:00
|
|
|
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 */
|