0
|
1 #include <iostream>
|
|
2 #include <iomanip>
|
|
3 #include "calc-driver.h"
|
|
4 #include "calc-parser.hh"
|
|
5
|
|
6 // コンストラクタ
|
|
7
|
|
8 calc_driver::calc_driver()
|
|
9 {
|
|
10 }
|
|
11
|
|
12 // デストラクタ
|
|
13
|
|
14 calc_driver::~calc_driver()
|
|
15 {
|
|
16 }
|
|
17
|
|
18 // 計算
|
|
19
|
|
20 bool calc_driver::calc(const std::string &f)
|
|
21 {
|
|
22 file = f;
|
|
23 scan_begin(); // スキャナー初期化
|
|
24 yy::calc_parser parser(*this); // パーサー構築
|
|
25 int result = parser.parse(); // 構文解析
|
|
26 scan_end(); // スキャナー終了
|
|
27
|
|
28 if (result != 0)
|
|
29 return false; // パーサーエラー
|
|
30 return true;
|
|
31 }
|
|
32
|
|
33 // エラーメッセージを出力
|
|
34
|
|
35 void calc_driver::error(const std::string& m)
|
|
36 {
|
|
37 std::cerr << m << std::endl;
|
|
38 }
|
|
39
|
|
40 // 代入処理
|
|
41
|
|
42 void calc_driver::assign(const std::string *value, cnode *node)
|
|
43 {
|
|
44 values[*value] = node->expr(this);
|
|
45 delete value; // 後始末は自分で行う
|
|
46 delete node;
|
|
47 }
|
|
48
|
|
49 // print文
|
|
50
|
|
51 void calc_driver::print(cnode *node)
|
|
52 {
|
|
53 std::cout << node->expr(this) << std::endl;
|
|
54 delete node;
|
|
55 }
|
|
56
|
|
57 // list文
|
|
58
|
|
59 struct list_action {
|
|
60 void operator()(const std::pair<std::string, int> &it)
|
|
61 {
|
|
62 std::cout << it.first << " = " << it.second << std::endl;
|
|
63 }
|
|
64 } ;
|
|
65
|
|
66 void calc_driver::list()
|
|
67 {
|
|
68 std::for_each(values.begin(), values.end(), list_action());
|
|
69 }
|