44 using std::ostringstream;
47 Interpreter::Eval(StreamTokenizer &st) {
49 while (st.PeekTokenType() != StreamTokenizer::EOF_TYPE) {
52 StreamTokenizer::TokenType token_type = st.PeekTokenType();
53 VarMapBase *varmap = env_->GetVarMapForType(st.Peek());
54 bool is_type_specifier = varmap != NULL;
55 if (token_type != StreamTokenizer::IDENTIFIER && !is_type_specifier) {
56 string expected_type =
57 string(StreamTokenizer::TypeName(StreamTokenizer::IDENTIFIER)) +
59 string found_type = StreamTokenizer::TypeName(token_type);
60 WrongTokenTypeError(st.PeekTokenStart(), expected_type, found_type,
65 if (is_type_specifier) {
68 type = varmap->Name();
71 StreamTokenizer::TokenType token_type = st.PeekTokenType();
72 if (token_type != StreamTokenizer::IDENTIFIER) {
73 WrongTokenTypeError(st.PeekTokenStart(), StreamTokenizer::IDENTIFIER,
74 token_type, st.Peek());
78 string varname = st.Next();
81 token_type = st.PeekTokenType();
82 if (st.Peek() !=
"=") {
83 WrongTokenError(st.PeekTokenStart(),
"=", st.Peek(), st.PeekTokenType());
89 if (st.PeekTokenType() == StreamTokenizer::EOF_TYPE) {
91 err_ss <<
"Interpreter:" << filename_
92 <<
": error: unexpected EOF at stream position "
94 throw std::runtime_error(err_ss.str());
98 env_->ReadAndSet(varname, st, type);
100 token_type = st.PeekTokenType();
101 if (st.Peek() !=
";") {
102 WrongTokenError(st.PeekTokenStart(),
";", st.Peek(), st.PeekTokenType());
107 catch (std::runtime_error &e) {
108 cerr <<
"threw exception: " << e.what() << endl;
116 Interpreter::WrongTokenError(
size_t pos,
117 const string &expected,
119 StreamTokenizer::TokenType found_type)
const {
120 ostringstream err_ss;
121 err_ss <<
"Interpreter:" << filename_ <<
": at stream pos " << pos
122 <<
" expected token \"" << expected <<
"\" but found \"" << found
123 <<
"\" (token type: " << StreamTokenizer::TypeName(found_type)
125 throw std::runtime_error(err_ss.str());
129 Interpreter::WrongTokenTypeError(
size_t pos,
130 StreamTokenizer::TokenType expected,
131 StreamTokenizer::TokenType found,
132 const string &token)
const {
133 WrongTokenTypeError(pos,
134 StreamTokenizer::TypeName(expected),
135 StreamTokenizer::TypeName(found),
140 Interpreter::WrongTokenTypeError(
size_t pos,
141 const string &expected_type,
142 const string &found_type,
143 const string &token)
const {
144 ostringstream err_ss;
145 err_ss <<
"Interpreter:" << filename_ <<
": at stream pos " << pos
146 <<
" expected token type " << expected_type
147 <<
" but found " << found_type
148 <<
"; token=\"" << token <<
"\"";
149 throw std::runtime_error(err_ss.str());
Provides an interpreter for assigning primitives and Factory-constructible objects to named variables...