view src/ploki/expr.h @ 8916:0234daffd946

<oerjan> addquote <int-e> I couldn\'t help thinking that maybe if one considers the ramifications in full detail it will turn out that overthinking is often not helpful and therefore, not something to be proud of.
author HackBot
date Sun, 14 Aug 2016 02:31:47 +0000
parents ac0403686959
children
line wrap: on
line source

#ifndef EXPR_H_
#define EXPR_H_

#include "config.h"
#include "Str.h"
#include "op.h"
#include "re.h"
#include "stack.h"
#include "strhash.h"
#include "val.h"
#include "variable.h"

#include <stddef.h>
#include <setjmp.h>

enum t_expr {
	literE,
	varE,
	varhashE,
	symbolE,
	unopE,
	binopE,
	listE
};

enum t_func {
	F_CALL = -1,
	F_NUL,
	F_ABS,
	F_ACOS,
	F_ASIN,
	F_ATAN,
	F_ATAN2,
	F_CATCH,
	F_CHR,
	F_COS,
	F_DEFINED,
	F_EOF,
	F_ERROR,
	F_EXP,
	F_FREEZE,
	F_GETC,
	F_GETENV,
	F_GETS,
	F_HANG,
	F_INT,
	F_IO,
	F_LENGTH,
	F_LOG,
	F_LOG10,
	F_LOWER,
	F_MATCH,
	F_MOEND,
	F_MOSTART,
	F_NEG,
	F_NOT,
	F_NUM,
	F_OPEN,
	F_OPENR,
	F_OPENW,
	F_ORD,
	F_QUOTE,
	F_RE_ESC,
	F_REMOVE,
	F_RENAME,
	F_REVERSE,
	F_SEEK,
	F_SIN,
	F_SQRT,
	F_STR,
	F_TAN,
	F_TELL,
	F_TYPEOF,
	F_UPPER
};

enum t_symbol {
	S_NUL,
	S_ARG,
	S_ARGC,
	S_ARGV,
	S_ERR,
	S_EULER,
	S_LUDOLF,
	S_MATCH,
	S_RAND,
	S_RESULT,
	S_STDIN,
	S_STDOUT,
	S_STDERR
};

enum t_binop {
	B_AMPERSAND,
	B_ANGLE,
	B_BACKSPARK,
	B_BRACELET,
	B_DOUBLE_OH_SEVEN,
	B_EMBRACE,
	B_FLATWORM,
	B_HALF_MESH,
	B_HYBRID,
	B_INTERSECTION,
	B_RIGHT_ANGLE,
	B_SHARK_FIN,
	B_SLAT,
	B_SPARK,
	B_SPARK_SPOT,
	B_SPIKE,
	B_SPLAT,
	B_SPOT,
	B_SQIGGLE,
	B_TAIL,
	B_TWO_SPOT,
	B_U_TURN,
	B_U_TURN_BACK,
	B_WORM,
	B_XMATCH
};

struct expr {
	enum t_expr type;
	int op;
	union {
		t_vr_cookie tent;
		struct val *val;
		t_strhash *hash;
	} v;
	struct expr *right;
	union {
		struct expr *expr;
		struct op *op;
		t_regex *rx;
		size_t bonus;
	} left;
};

void expr_init(void);
void expr_end(void);

void free_expr(struct expr *);
struct expr *get_expr(struct op *);
struct expr *get_lval(struct op *);
struct expr *get_iobj(struct op *);

void eval_push(struct expr *);
void eval_into(struct expr *, struct val *);
struct val *eval_pop(void);
struct val *eval_expr(struct expr *);

ATTR_PURE
enum t_binop expr_binop(int);

void expr_pp(enum t_binop, struct val *, struct val *);

typedef struct {
	jmp_buf buf;
	size_t depth;
} t_context;

stack_declare(t_context, extern)
extern stack(t_context) Context;

#define OPERATORS "_+-*/%^[]<>=!{}:;&|~`'.,"
#define OPERATOR_P(c) strchr(OPERATORS, c)

#define TOLABEL(v)                            \
do {                                          \
	V_NUM(v);                                 \
	V_xxx_OFF(v);                             \
	(v)->num = floor((v)->num + .5);          \
	(v)->type = V_NUM_K;                      \
	v_ok_str(v);                              \
	ko_grep(v->ko, isdigit);                  \
	while (ko_at(v->ko, 0) == '0') {          \
		ko_shift(v->ko, 1);                   \
	}                                         \
	(v)->type = V_STR_K;                      \
} while (0)

#endif /* EXPR_H_ */