ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
token.cpp
Go to the documentation of this file.
1 #include "core/token.hpp"
2 
3 using namespace std;
4 
5 Token::Token(const string &function_string, Type type):
6  function_(0.),
7  string_rep_(function_string),
8  type_(type){
9  if(type_ == Type::unknown){
10  type_ = GetType(function_string);
11  }
12  }
13 
14 Token::Token(const NamedFunc &function):
15  function_(function),
16  string_rep_(function.Name()),
17  type_(function.IsScalar() ? Type::resolved_scalar : Type::resolved_vector){
18 }
19 
20 Token::Type Token::GetType(const string &x){
21  //Note that '<', '>', and '!' give "unknown" since they can be part of digraph with '='
22  switch(x.size()){
23  case 0: return Type::unknown;
24  case 1:
25  switch(x[0]){
26  case '+': return Type::ambiguous_plus;
27  case '-': return Type::ambiguous_minus;
28  case '*': return Type::multiply;
29  case '/': return Type::divide;
30  case '%': return Type::modulus;
31  case '(': return Type::open_paren;
32  case ')': return Type::close_paren;
33  case '[': return Type::open_square;
34  case ']': return Type::close_square;
35  default: return Type::unknown;
36  }
37  case 2:
38  if(x == "=="){
39  return Type::equal;
40  }else if(x == "!="){
41  return Type::not_equal;
42  }else if(x == "<="){
43  return Type::less_equal;
44  }else if(x == ">="){
45  return Type::greater_equal;
46  }else if(x == "&&"){
47  return Type::logical_and;
48  }else if(x == "||"){
49  return Type::logical_or;
50  }else{
51  return Type::unknown;
52  }
53  default:
54  return Type::unknown;
55  }
56  return Type::unknown;
57 }
58 
59 ostream & operator << (ostream &stream, const Token &token){
60  stream << "Token{" << token.string_rep_ << "}{" << static_cast<unsigned>(token.type_) << '}';
61  return stream;
62 }
STL namespace.
Combines a callable function taking a Baby and returning a scalar or vector with its string represent...
Definition: named_func.hpp:13
static Type GetType(char x)
std::string string_rep_
Definition: token.hpp:33
Type type_
Definition: token.hpp:34
ostream & operator<<(ostream &stream, const Token &token)
Definition: token.cpp:59
Type
Definition: token.hpp:10
NamedFunc function_
Definition: token.hpp:32
Definition: token.hpp:9
Token(const std::string &function_string="", Type type=Type::unknown)
Definition: token.cpp:5