0
|
1 //
|
|
2 // 四則演算パーサーとのインターフェース
|
|
3 //
|
|
4 // Chihiro.SAKAMOTO
|
|
5 //
|
|
6 #include <iostream>
|
|
7 #include <algorithm>
|
|
8 #include "calc-driver.h"
|
|
9 #include "parser.h"
|
|
10
|
|
11 // コンストラクタ
|
|
12
|
|
13 calc_driver::calc_driver()
|
|
14 {
|
|
15 }
|
|
16
|
|
17 // デストラクタ
|
|
18
|
|
19 calc_driver::~calc_driver()
|
|
20 {
|
|
21 }
|
|
22
|
|
23 // 計算
|
|
24
|
|
25 bool calc_driver::calc(const std::string &f)
|
|
26 {
|
|
27 cparser parser; // パーサー構築
|
|
28 int result = parser.parse(this, f); // 構文解析
|
|
29
|
|
30 if (result != 0)
|
|
31 return false; // パーサーエラー
|
|
32 return true;
|
|
33 }
|
|
34
|
|
35 // エラーメッセージを出力
|
|
36
|
|
37 void calc_driver::error(const std::string& m)
|
|
38 {
|
|
39 std::cerr << m << std::endl;
|
|
40 }
|
|
41
|
|
42 // 代入処理
|
|
43
|
|
44 void calc_driver::assign(const std::string &value, cnode *node)
|
|
45 {
|
|
46 values[value] = node->expr(this);
|
|
47 }
|
|
48
|
|
49 // print文
|
|
50
|
|
51 void calc_driver::print(cnode *node)
|
|
52 {
|
|
53 std::cout << node->expr(this) << std::endl;
|
|
54 }
|
|
55
|
|
56 // 変数リストの一覧表示
|
|
57
|
|
58 struct list_action {
|
|
59 void operator()(const std::pair<std::string, int> &it)
|
|
60 {
|
|
61 std::cout << it.first << " = " << it.second << std::endl;
|
|
62 }
|
|
63 } ;
|
|
64
|
|
65 void calc_driver::list()
|
|
66 {
|
|
67 std::for_each(values.begin(), values.end(), list_action());
|
|
68 }
|