summaryrefslogtreecommitdiff
path: root/src/bin/pgbench/exprparse.y
blob: e68631e3e6196525f041c03c9010d7fc0165b6a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
%{
/*-------------------------------------------------------------------------
 *
 * exprparse.y
 *	  bison grammar for a simple expression syntax
 *
 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *-------------------------------------------------------------------------
 */

#include "postgres_fe.h"

#include "pgbench.h"

PgBenchExpr *expr_parse_result;

static PgBenchExpr *make_integer_constant(int64 ival);
static PgBenchExpr *make_variable(char *varname);
static PgBenchExpr *make_op(char operator, PgBenchExpr *lexpr,
		PgBenchExpr *rexpr);

%}

%expect 0
%name-prefix="expr_yy"

%union
{
	int64		ival;
	char	   *str;
	PgBenchExpr *expr;
}

%type <expr> expr
%type <ival> INTEGER
%type <str> VARIABLE

%token INTEGER VARIABLE
%token CHAR_ERROR /* never used, will raise a syntax error */

/* Precedence: lowest to highest */
%left	'+' '-'
%left	'*' '/' '%'
%right	UMINUS

%%

result: expr				{ expr_parse_result = $1; }

expr: '(' expr ')'			{ $$ = $2; }
	| '+' expr %prec UMINUS	{ $$ = $2; }
	| '-' expr %prec UMINUS	{ $$ = make_op('-', make_integer_constant(0), $2); }
	| expr '+' expr			{ $$ = make_op('+', $1, $3); }
	| expr '-' expr			{ $$ = make_op('-', $1, $3); }
	| expr '*' expr			{ $$ = make_op('*', $1, $3); }
	| expr '/' expr			{ $$ = make_op('/', $1, $3); }
	| expr '%' expr			{ $$ = make_op('%', $1, $3); }
	| INTEGER				{ $$ = make_integer_constant($1); }
	| VARIABLE 				{ $$ = make_variable($1); }
	;

%%

static PgBenchExpr *
make_integer_constant(int64 ival)
{
	PgBenchExpr *expr = pg_malloc(sizeof(PgBenchExpr));

	expr->etype = ENODE_INTEGER_CONSTANT;
	expr->u.integer_constant.ival = ival;
	return expr;
}

static PgBenchExpr *
make_variable(char *varname)
{
	PgBenchExpr *expr = pg_malloc(sizeof(PgBenchExpr));

	expr->etype = ENODE_VARIABLE;
	expr->u.variable.varname = varname;
	return expr;
}

static PgBenchExpr *
make_op(char operator, PgBenchExpr *lexpr, PgBenchExpr *rexpr)
{
	PgBenchExpr *expr = pg_malloc(sizeof(PgBenchExpr));

	expr->etype = ENODE_OPERATOR;
	expr->u.operator.operator = operator;
	expr->u.operator.lexpr = lexpr;
	expr->u.operator.rexpr = rexpr;
	return expr;
}

#include "exprscan.c"