// 他のcompound datumを設定するためには、このようにして設定しなければならない。
// iteratorを範囲をpush_backした場合には、iteratorはcompound datumとして
// 認識されるため、型を表すdatumを渡す必要がある。
- t->push_back(t2->begin(), t2->end(), syntax::makeCompoundDatum(syntax::DatumID::LIST));
+ t->push_back(t2.begin(), t2.end(), syntax::makeCompoundDatum(syntax::DatumID::list));
// beginでは、現在Treeが保持しているトップが取得される。
syntax::Tree::iterator lo = t->begin();
asserter->checkOnly(lo->first.toEnum(), syntax::DatumID::LIST);
// 内部はこれで取得可能。lo2はlistの内部のみを巡回することができる。
- syntax::Tree::iterator lo2 = t->in_compound(lo);
+ syntax::Tree::iterator lo2 = t->in_compound_begin(lo);
// Literalオブジェクトの表面的なデータ型としては、Objectと同様。
asserter->checkOnly(lo2->second->toValue()->toStr(), "hoge");
return asserter->isOk();
}
-// bool syntax_tree_test2(smart_ptr<simpletest::SimpleTestAsserter> asserter)
-// {
-// // 通常通り利用すると、かなり面倒なので、解析の際に各種省略系を使う
-// // テスト。
-// std::stringstream ss;
-// ss << "==========" << endl;
-// ss << ",@(hoge 12 `(hage) 'huga #f #t)" << endl;
-
-// textarrayformat::TextArrayReader reader(ss);
-// smart_ptr<istream> formats(new stringstream(reader.get()));
-// smart_ptr<utakata::utf8::UTF8InputStream> st;
-// st.add(new utakata::utf8::UTF8InputStream(formats));
+bool syntax_tree_test2(smart_ptr<simpletest::SimpleTestAsserter> asserter)
+{
+ // 通常通り利用すると、かなり面倒なので、解析の際に各種省略系を使う
+ // テスト。
+ std::stringstream ss;
+ ss << "==========" << endl;
+ ss << ",@(hoge 12 `(hage) 'huga #f #t)" << endl;
+
+ textarrayformat::TextArrayReader reader(ss);
+ smart_ptr<istream> formats(new stringstream(reader.get()));
+ smart_ptr<utakata::utf8::UTF8InputStream> st;
+ st.add(new utakata::utf8::UTF8InputStream(formats));
-// utakata::lexer::Lexer lexer;
-
-// smart_ptr<lexeme::ILexeme> m(lexer.lex(st));
-// smart_ptr<syntax::Tree> root(new syntax::Tree);
-// smart_ptr<syntax::Tree> t(root);
-// std::stack<smart_ptr<syntax::Tree> > s;
-
-// // 最初から,@なので、compoundと判断し、Treeを設定する。
-// s.push(t);
-// t.add(new syntax::Tree);
-// t->push_cons(syntax::makeAbbreviation(
-// literal::generateLiteral(m)));
-
-// // (なので、再度pushして再生成。
-// lexer.lex(st);
-
-// s.push(t);
-// t.add(new syntax::Tree);
-// m = lexer.lex(st);
-// // symbol,numberをそれぞれ設定するつもりで。
-// t->push_cons(syntax::makeLexemeDatum(
-// literal::generateLiteral(m),
-// syntax::toDatumType(m)));
-// m = lexer.lex(st);
-// t->push_cons(syntax::makeLexemeDatum(
-// literal::generateLiteral(m),
-// syntax::toDatumType(m)));
-
-// s.push(t);
-// t.add(new syntax::Tree);
-// t->push_cons(syntax::makeAbbreviation(
-// literal::generateLiteral(lexer.lex(st))));
-
-// lexer.lex(st); // (の読み飛ばし。
-
-// s.push(t);
-// t.add(new syntax::Tree);
-// m = lexer.lex(st);
-// t->push_cons(syntax::makeLexemeDatum(
-// literal::generateLiteral(m),
-// syntax::toDatumType(m)));
-
-// lexer.lex(st); // )の読み飛ばし
-// // abbrevのtreeに追加する。
-// smart_ptr<syntax::Tree> tmp = s.top(); s.pop();
-// tmp->push_cons(t->begin());
-// t = tmp;
-
-// // abbrevが完了したので追加する。
-// tmp = s.top(); s.pop();
-// tmp->push_cons(t->begin);
-// t = tmp;
-
-// tmp = s.top(); s.pop();
-// tmp->push_cons(t->begin);
-// t = tmp;
-
-// // 先頭はlistであるはず。
-// asserter->check(root->begin()->first.toEnum(), syntax::DatumID::LIST);
-// asserter->check(root->car(root->begin())->first.toEnum(),
-// syntax::DatumID::ABBREVIATIONS);
-// syntax::Tree::iterator it = root->car(root->begin());
-// ++it;
-// asserter->check(it->first.toEnum(), syntax::DatumID::LIST);
-// it = root->car(it);
-// asserter->check(root->car(it)->first.toEnum(), syntax::DatumID::SYMBOL);
-// ++it;
-// asserter->check(root->car(it)->first.toEnum(), syntax::DatumID::NUMBER);
-
-// return asserter->isOk();
-// }
+ utakata::lexer::Lexer lexer;
+
+ smart_ptr<lexeme::ILexeme> m(lexer.lex(st));
+ smart_ptr<syntax::Tree> root(new syntax::Tree);
+ smart_ptr<syntax::Tree> t(root);
+ std::stack<smart_ptr<syntax::Tree> > s;
+
+ // 最初から,@なので、compoundと判断し、Treeを設定する。
+ s.push(t);
+ t.add(new syntax::Tree);
+ t->push_back(syntax::makeAbbreviation(
+ literal::generateLiteral(m)));
+
+ // (なので、再度pushして再生成。
+ lexer.lex(st);
+
+ s.push(t);
+ t.add(new syntax::Tree);
+ m = lexer.lex(st);
+ // symbol,numberをそれぞれ設定するつもりで。
+ t->push_back(syntax::makeLexemeDatum(
+ literal::generateLiteral(m),
+ syntax::DatumID::symbol));
+ m = lexer.lex(st);
+ t->push_back(syntax::makeLexemeDatum(
+ literal::generateLiteral(m),
+ syntax::DatumID::number));
+
+ // 再度abbreviation
+ s.push(t);
+ t.add(new syntax::Tree);
+ t->push_back(syntax::makeAbbreviation(
+ literal::generateLiteral(lexer.lex(st))));
+
+ lexer.lex(st); // (の読み飛ばし。
+
+ s.push(t);
+ t.add(new syntax::Tree);
+ m = lexer.lex(st);
+ t->push_back(syntax::makeLexemeDatum(
+ literal::generateLiteral(m),
+ syntax::DatumID::symbol));
+
+ lexer.lex(st); // )の読み飛ばし
+ // abbrevのtreeに追加する。
+ smart_ptr<syntax::Tree> tmp = s.top(); s.pop();
+ tmp->push_back(t->begin(), t->end(), syntax::makeCompoundDatum(syntax::DatumID::list));
+ t = tmp;
+
+ // abbrevが完了したので追加する。
+ tmp = s.top(); s.pop();
+ tmp->push_back(t->begin(), t->end(), syntax::makeCompoundDatum(syntax::DatumID::list));
+ t = tmp;
+
+ tmp = s.top(); s.pop();
+ tmp->push_back(t->begin(), t->end(), syntax::makeCompoundDatum(syntax::DatumID::list));
+ t = tmp;
+
+ root->push_back(t->begin(), t->end(), syntax::makeCompoundDatum(syntax::DatumID::list));
+
+ // 先頭はlistであるはず。
+ asserter->check(root->begin()->first.toEnum(), syntax::DatumID::LIST, "list error");
+ syntax::Tree::iterator it = root->in_compound_begin(root->begin());
+ asserter->check(it->first.toEnum(),
+ syntax::DatumID::ABBREVIATIONS, "abbreviation?");
+ ++it;
+ asserter->check(it->first.toEnum(), syntax::DatumID::LIST);
+ it = root->in_compound_begin(it);
+ asserter->check(it->first.toEnum(), syntax::DatumID::SYMBOL);
+ ++it;
+ asserter->check(it->first.toEnum(), syntax::DatumID::NUMBER);
+ ++it;
+ // abbrevが含まれる部分は、実際にはlistで囲まれていること。
+ asserter->check(it->first.toEnum(), syntax::DatumID::LIST);
+
+ return asserter->isOk();
+}
int main(int argc, char *argv[])
{
simpletest::SimpleTestSuite suite("syntax tree test");
suite.addTester(sfcr::screate(syntax_tree_test, suite.getAsserter()));
- // suite.addTester(sfcr::screate(syntax_tree_test2, suite.getAsserter()));
+ suite.addTester(sfcr::screate(syntax_tree_test2, suite.getAsserter()));
suite.run();
return 0;