lang/ir.h
r4 e7f4773cba make while loop parsing more elegant
It is now possible to parse parts of the program and save the IR for
later addition. This is currently used for while loops but will also make other
control flow elements easier to implement in the future.
2021-12-22 12:52:16 +01:00

86 lines
1.1 KiB
C

#ifndef __IR_H__
#define __IR_H__
#include "tok.h"
enum IRInstr {
IRSet,
IRNeg,
IRAdd,
IRSub,
IRMul,
IRDiv,
IRPrint,
IRJmp,
IRJnz,
IRInstrEnumSize,
};
typedef enum IRInstr IRInstr;
extern const char *irinstr_str[IRInstrEnumSize];
typedef struct IRParam {
enum {
IRParamNull = 0,
IRParamLiteral,
IRParamAddr,
} kind;
union {
Value Literal;
size_t Addr;
};
} IRParam;
typedef struct IRArgs {
struct IRArgs *next;
IRParam param;
} IRArgs;
typedef struct IRTok {
size_t ln, col;
IRInstr instr;
union {
struct {
size_t addr;
IRParam val;
} Unary;
struct {
size_t addr;
IRParam lhs, rhs;
} Arith;
struct {
IRArgs *args;
size_t args_size;
} Print;
struct {
size_t iaddr;
} Jmp;
struct {
size_t iaddr;
IRParam condition;
} CJmp;
};
} IRTok;
typedef struct IRToks {
size_t len, cap;
IRTok *toks;
} IRToks;
void irtoks_init_long(IRToks *v);
void irtoks_init_short(IRToks *v);
void irtoks_term(IRToks *v);
void irtoks_app(IRToks *v, IRTok t);
void irtoks_app_irtoks(IRToks *v, IRToks *other);
void print_ir(IRToks *v);
#endif /* IR_H */