Blender V5.3
expression.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
13#include "pratt_parser.hh"
14#include "token.hh"
15#include "token_stream.hh"
16
17#include <stdexcept>
18
20
26 static void lexical_analysis(LexerBase &lex, std::string_view input)
27 {
31 }
32};
33
40class ExpressionParser : public Parser<ExpressionLexer, NullParser> {
41 public:
42 int64_t eval() const
43 {
44 assert(size_ > 0);
45 EvalContext ctx((*this)[0]);
46
47 int64_t v = ctx.expr(EvalContext::BindingPower(0));
48 if (ctx.peek() != Invalid) {
49 throw std::runtime_error("Trailing input");
50 }
51 return v;
52 }
53
54 private:
55 struct EvalContext : public PrattParser<EvalContext, int64_t, Token> {
58 using BindingPower = Base::BindingPower;
59
60 private:
61 Token tok;
62
63 public:
64 EvalContext(Token tok) : tok(tok) {}
65
66 int64_t identifier(const Token & /*t*/)
67 {
68 /* Undefined identifier (not macro substituted). Evaluate to 0. */
69 return 0;
70 }
71
72 int64_t number_literal(const Token &t)
73 {
74 return int64_t(std::stoull(std::string(t.str()), nullptr, 0));
75 }
76
77 int64_t string_literal(const Token & /*t*/)
78 {
79 throw std::runtime_error("Invalid expression: strings not supported");
80 }
81
82 int64_t prefix(const Token &t, BindingPower p)
83 {
84 switch (t.type()) {
85 case Plus:
86 return +expr(p);
87 case Minus:
88 return -expr(p);
89 case Not: {
90 int64_t v = expr(p);
91 /* Note that '!' token is of MultiTok class and can contain many unary '!'. */
92 return (t.str().size() & 1) ? !v : !!v;
93 }
94 case BitwiseNot:
95 return ~expr(p);
96 default:
97 throw std::runtime_error("Invalid prefix operator");
98 }
99 }
100
101 int64_t parenthesis(const Token & /*t*/, BindingPower p)
102 {
103 /* Parse the whole parenthesis expression. */
104 int64_t v = expr(p);
105 /* Consume the closing parenthesis. */
106 if (consume().type() != ParClose) {
107 throw std::runtime_error("Expected ')'");
108 }
109 return v;
110 }
111
112 int64_t member(int64_t /*left*/, const Token & /*t*/)
113 {
114 throw std::runtime_error("Invalid operator: member access not supported");
115 }
116
117 int64_t function_call(int64_t /*left*/, const Token & /*t*/)
118 {
119 throw std::runtime_error("Invalid expression: function call not supported");
120 }
121
122 int64_t subscript(int64_t /*left*/, const Token & /*t*/)
123 {
124 throw std::runtime_error("Invalid operator: subscript not supported");
125 }
126
127 int64_t suffix(int64_t /*left*/, const Token & /*t*/)
128 {
129 throw std::runtime_error("Invalid operator: suffix not supported");
130 }
131
132 int64_t comma(int64_t /*left*/, const Token & /*t*/, BindingPower /*p*/)
133 {
134 throw std::runtime_error("Invalid operator: comma not supported");
135 }
136
137 int64_t binary(int64_t left, const Token &t, BindingPower p)
138 {
139 switch (t.type()) {
140 case Multiply:
141 return left * expr(p);
142 case Divide:
143 if (int64_t right = expr(p); right == 0) {
144 throw std::runtime_error("Division by zero");
145 }
146 else {
147 return left / right;
148 }
149 case Modulo:
150 if (int64_t right = expr(p); right == 0) {
151 throw std::runtime_error("Modulo by zero");
152 }
153 else {
154 return left % right;
155 }
156 case Plus:
157 return left + expr(p);
158 case Minus:
159 return left - expr(p);
160 case LShift:
161 return left << expr(p);
162 case RShift:
163 return left >> expr(p);
164 case LThan:
165 return left < expr(p);
166 case LEqual:
167 return left <= expr(p);
168 case GThan:
169 return left > expr(p);
170 case GEqual:
171 return left >= expr(p);
172 case Equal:
173 return left == expr(p);
174 case NotEqual:
175 return left != expr(p);
176 case And:
177 return left & expr(p);
178 case Xor:
179 return left ^ expr(p);
180 case Or:
181 return left | expr(p);
182 case LogicalAnd: {
183 /* Avoid short circuit. */
184 int64_t right = expr(p);
185 return left && right;
186 }
187 case LogicalOr: {
188 /* Avoid short circuit. */
189 int64_t right = expr(p);
190 return left || right;
191 }
192 default:
193 throw std::runtime_error("Invalid operator");
194 }
195 }
196
197 int64_t ternary(int64_t left, const Token & /*t*/, BindingPower p_first, BindingPower p_second)
198 {
199 int64_t tval = expr(p_first);
200 if (consume().type() != Colon) {
201 error(ErrorType::ExpectedColon, Colon);
202 }
203 int64_t fval = expr(p_second);
204 return left ? tval : fval;
205 }
206
207 int64_t error(ErrorType err, lexit::TokenType type)
208 {
209 switch (err) {
210 case ErrorType::ExpectedParenthesis:
211 throw std::runtime_error("Expected ')'");
212 case ErrorType::ExpectedColon:
213 throw std::runtime_error("Expected ':'");
214 case ErrorType::InvalidOperator:
215 throw std::runtime_error("Invalid operator " + to_str(type));
216 case ErrorType::InvalidExpression:
217 throw std::runtime_error("Invalid expression");
218 }
219 throw std::runtime_error("Parse error");
220 }
221
222 TokenType peek() const
223 {
224 return tok.type();
225 }
226
227 Token consume()
228 {
229 Token t = tok;
230 tok = tok.next();
231 return t;
232 }
233
234 TokenType to_type(const Token &t) const
235 {
236 return t.type();
237 }
238 };
239};
240
241} // namespace blender::gpu::shader::parser
long long int int64_t
ResultT expr(BindingPower right)
__int64 int64_t
Definition BKE_text.h:90
ATTR_WARN_UNUSED_RESULT const BMVert * v
BMesh const char void * data
TokenType
Definition types.hh:41
@ RShift
Definition types.hh:126
@ GThan
Definition types.hh:149
@ LThan
Definition types.hh:150
@ LogicalOr
Definition types.hh:93
@ Not
Definition types.hh:64
@ Modulo
Definition types.hh:152
@ And
Definition types.hh:146
@ LogicalAnd
Definition types.hh:81
@ BitwiseNot
Definition types.hh:151
@ Multiply
Definition types.hh:145
@ ParClose
Definition types.hh:54
@ LEqual
Definition types.hh:104
@ Plus
Definition types.hh:68
@ Colon
Definition types.hh:65
@ NotEqual
Definition types.hh:89
@ Equal
Definition types.hh:90
@ Divide
Definition types.hh:70
@ Xor
Definition types.hh:148
@ LShift
Definition types.hh:125
@ Minus
Definition types.hh:69
@ Invalid
Definition types.hh:42
@ GEqual
Definition types.hh:94
@ Or
Definition types.hh:147
static void lexical_analysis(LexerBase &lex, std::string_view input)
Definition expression.hh:26
static const std::array< CharClass, 128 > default_char_class_table
void merge_complex_literals(bool bsl_template_angle_bracket_lexing=false)
Merge complex literals such as floats, strings and comments.
Definition lexit.cc:1010
void process(const std::string_view str, const CharClass char_class_table[128])
Definition lexit.hh:154
uint32_t size_
Definition lexit.hh:135
std::string_view str() const
Definition lexit.hh:381
const TokenType & type() const
Definition lexit.hh:354
Token next(int i=1) const
Definition lexit.hh:372