VapourSynth-llvmexpr
Loading...
Searching...
No Matches
PostfixHelper.cpp
Go to the documentation of this file.
1
19
20#include "PostfixHelper.hpp"
21#include "../Tokenizer.hpp"
22
23#include <format>
24#include <stdexcept>
25
26namespace infix2postfix {
27
28int compute_postfix_stack_effect(const std::string& postfix_expr,
29 PostfixMode mode, int line, int num_inputs,
30 int num_intermediate_inputs) {
32 if (mode == PostfixMode::Expr) {
33 expr_mode = (num_intermediate_inputs > 0) ? ::ExprMode::VkExpr
35 }
36
37 std::vector<::Token> tokens;
38 try {
39 tokens = ::tokenize(postfix_expr, num_inputs, expr_mode,
40 num_intermediate_inputs);
41 } catch (const std::exception& e) {
42 throw std::runtime_error(
43 std::format("Line {}: Failed to tokenize postfix expression: {}",
44 line, e.what()));
45 }
46
47 int stack = 0;
48 for (const auto& token : tokens) {
49 ::TokenBehavior behavior = ::get_token_behavior(token);
50 stack += behavior.stack_effect;
51 if (stack < 0) {
52 throw std::runtime_error(
53 std::format("Line {}: Stack underflow while processing '{}'",
54 line, token.text));
55 }
56 }
57 return stack;
58}
59
60} // namespace infix2postfix
std::vector< Token > tokenize(const std::string &expr, int num_inputs, ExprMode mode, int num_intermediate_inputs)
TokenBehavior get_token_behavior(const Token &token)
ExprMode
int compute_postfix_stack_effect(const std::string &postfix_expr, PostfixMode mode, int line, int num_inputs, int num_intermediate_inputs)