Interpreter

Interpreterパターンは、翻訳者(Interpreter)によって翻訳された指示に従って処理する構造。

23.Interpreter パターン | TECHSCORE(テックスコア)

main.cppで、

式を解析して、計算結果を出力する簡単なインタプリター実装した。

$ 
$ vi main.cpp 
$ g++ -o main main.cpp term.cpp expression.cpp 
$ ./main 
str = ((1 + 2) * 3 - 4) / 5
str = ((1 + 2) * 3 - 4) / 5
str = ((1 + 2) * 3 - 4)
str = (1 + 2) * 3
str = (1 + 2) * 3
str = (1 + 2)
str = 1
str = 1
operand1_str = 1
operand2_str = 
operator_ = 
operand1_str = 1
operand2_str = 
operator_ = 
str = 2
str = 2
operand1_str = 2
operand2_str = 
operator_ = 
operand1_str = 2
operand2_str = 
operator_ = 
operand1_str = 1
operand2_str = 2
operator_ = +
str = 3
str = 3
operand1_str = 3
operand2_str = 
operator_ = 
operand1_str = 3
operand2_str = 
operator_ = 
operand1_str = (1 + 2)
operand2_str = 3
operator_ = *
operand1_str = (1 + 2) * 3
operand2_str = 
operator_ = 
str = 4
str = 4
operand1_str = 4
operand2_str = 
operator_ = 
operand1_str = 4
operand2_str = 
operator_ = 
operand1_str = (1 + 2) * 3
operand2_str = 4
operator_ = -
str = 5
str = 5
operand1_str = 5
operand2_str = 
operator_ = 
operand1_str = 5
operand2_str = 
operator_ = 
operand1_str = ((1 + 2) * 3 - 4)
operand2_str = 5
operator_ = /
operand1_str = ((1 + 2) * 3 - 4) / 5
operand2_str = 
operator_ = 
result = 1
$

とりあえず、この場合は動いた。

Sample/designpattern/interpreter/interpreter/src/interpreter at master · bg1bgst333/Sample · GitHub