VapourSynth-llvmexpr
Loading...
Searching...
No Matches
InfixConverter.cpp
Go to the documentation of this file.
1
19
20#include "InfixConverter.hpp"
24
25#include <format>
26#include <stdexcept>
27
29 const std::string& infix_expr, int num_inputs, infix2postfix::Mode mode,
30 const std::map<std::string, std::string>* predefined_macros,
31 int num_intermediate_inputs) {
32 try {
33 std::string preprocessed_source = infix_expr;
34 std::vector<infix2postfix::LineMapping> line_map;
35 int library_line_count = 0;
36
37 if (predefined_macros != nullptr) {
38 infix2postfix::Preprocessor preprocessor(infix_expr);
39
40 for (const auto& [name, value] : *predefined_macros) {
41 preprocessor.addPredefinedMacro(name, value);
42 }
43
44 auto preprocess_result = preprocessor.process();
45
46 if (!preprocess_result.success) {
47 std::string error_msg = "Preprocessing errors:\n";
48 for (const auto& error : preprocess_result.errors) {
49 error_msg += error + "\n";
50 }
51 throw std::runtime_error(error_msg);
52 }
53
54 preprocessed_source = preprocess_result.source;
55 line_map = preprocess_result.line_map;
56 library_line_count = preprocess_result.library_line_count;
57 }
58
59 infix2postfix::Tokenizer tokenizer(preprocessed_source);
60 auto tokens = tokenizer.tokenize();
61
62 infix2postfix::AnalysisEngine engine(tokens, mode, num_inputs,
63 num_intermediate_inputs, line_map,
64 library_line_count);
65 bool success = engine.runAnalysis();
66
67 if (!success) {
68 std::string diagnostics = engine.formatDiagnostics();
69 throw std::runtime_error(diagnostics);
70 }
71
72 return engine.generateCode();
73 } catch (const std::exception& e) {
74 throw std::runtime_error(
75 std::format("Infix to postfix conversion error: {}", e.what()));
76 }
77}
std::string convert_infix_to_postfix(const std::string &infix_expr, int num_inputs, infix2postfix::Mode mode, const std::map< std::string, std::string > *predefined_macros, int num_intermediate_inputs)
std::string formatDiagnostics() const
void addPredefinedMacro(std::string name, const std::string &value="")
std::vector< Token > tokenize()
Definition Tokenizer.cpp:66