Mercurial > hg > Members > nobuyasu > SampleSource
comparison Bison-Flex/CALC/Bison-Flex/EUC/calc-driver.cpp @ 0:db40c85cad7a default tip
upload sample source
author | nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 09 May 2011 03:11:59 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:db40c85cad7a |
---|---|
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 void calc_driver::print(cnode *node) | |
50 { | |
51 std::cout << node->expr(this) << std::endl; | |
52 delete node; | |
53 } | |
54 | |
55 struct list_action { | |
56 void operator()(const std::pair<std::string, int> &it) | |
57 { | |
58 std::cout << it.first << " = " << it.second << std::endl; | |
59 } | |
60 } ; | |
61 | |
62 void calc_driver::list() | |
63 { | |
64 std::for_each(values.begin(), values.end(), list_action()); | |
65 } |