File: Token.cpp

package info (click to toggle)
freemat 4.2%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 142,116 kB
  • sloc: ansic: 126,788; cpp: 62,015; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 107
file content (203 lines) | stat: -rw-r--r-- 6,114 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (c) 2009 Samit Basu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "Token.hpp"
#include "Serialize.hpp"
#include <errno.h>
#include <limits.h>

Token::Token(TokenValueType tok, unsigned pos, QString text) :
  m_tok(tok), m_pos(pos), m_text(text) { }

Token::Token() {
  m_tok = TOK_INVALID;
}

bool Token::isBinaryOperator() const {
  return ((m_tok == '+') || (m_tok == '-') ||
	  (m_tok == '*') || (m_tok == '/') ||
	  (m_tok == '\\') || (m_tok == '^') ||
	  (m_tok == '>') || (m_tok == '<') || 
	  (m_tok == ':') || (m_tok == TOK_LE) ||
	  (m_tok == TOK_GE) || (m_tok == TOK_EQ) ||
	  (m_tok == TOK_NE) || (m_tok == TOK_SOR) ||
	  (m_tok == TOK_SAND) || (m_tok == TOK_DOTTIMES) ||
	  (m_tok == TOK_DOTRDIV) || (m_tok == TOK_DOTLDIV) ||
	  (m_tok == TOK_DOTPOWER) || (m_tok == '|') ||
	  (m_tok == '&'));
}

bool Token::isUnaryOperator() const {
  return ((m_tok == '+') || (m_tok == '-') || (m_tok == '~')
	  || (m_tok == TOK_UNARY_MINUS) || 
	  (m_tok == TOK_UNARY_PLUS) ||
	  (m_tok == TOK_INCR) ||
	  (m_tok == TOK_DECR));
}


bool Token::isRightAssociative() const {
  return (m_tok == '^');
}

//QTextStream& operator<<(QTextStream& o, const Token& b) {
//  o << TokenToString(b) << " (" << (b.position() >> 16)
//    << "," << (b.position() & 0xffff) << ")\r\n";
//  return o;
//}

DebugStream& operator<<(DebugStream& o, const Token& b) {
  o << TokenToString(b) << " (" << (b.position() >> 16)
    << "," << (LineNumber(b.position())) << ")\n";
  return o;
}


QString TokenToString(const Token& b) {
  switch(b.value()) {
  case TOK_IDENT: return "(ident)"+b.text();
  case TOK_SPACE: return "space";
  case TOK_STRING: return "(string)"+b.text();
  case TOK_KEYWORD: return "keyword";
  case TOK_BREAK: return "break";
  case TOK_CASE: return "case";
  case TOK_CATCH: return "catch";
  case TOK_CONTINUE: return "continue";
  case TOK_DBSTEP: return "dbstep";
  case TOK_ELSE: return "else";
  case TOK_ELSEIF: return "elseif";
  case TOK_END: return "end";
  case TOK_FOR: return "for";
  case TOK_FUNCTION: return "function";
  case TOK_GLOBAL: return "global";
  case TOK_IF: return "if";
  case TOK_KEYBOARD: return "keyboard";
  case TOK_OTHERWISE: return "otherwise";
  case TOK_PERSISTENT: return "persistent";
  case TOK_QUIT: return "quit";
  case TOK_RETALL: return "retall";
  case TOK_RETURN: return "return";
  case TOK_SWITCH: return "switch";
  case TOK_TRY: return "try";
  case TOK_WHILE: return "while";
    // Generated (synthetic) token;
  case TOK_MULTI: return "multi";
  case TOK_SPECIAL: return "special";
  case TOK_VARIABLE: return "variable";
  case TOK_DYN: return "dyn";
  case TOK_BLOCK: return "block";
  case TOK_EOF: return "eof";
  case TOK_MATDEF: return "matdef";
  case TOK_CELLDEF: return "celldef";
  case TOK_PARENS: return "()";
  case TOK_BRACES: return "{}";
  case TOK_BRACKETS: return "[]";
  case TOK_ROWDEF: return "row";
  case TOK_UNARY_MINUS: return "u-";
  case TOK_UNARY_PLUS: return "u+";
  case TOK_EXPR: return "expr";
  case TOK_DOTTIMES: return ".*";
  case TOK_DOTRDIV: return "./";
  case TOK_DOTLDIV: return ".\\";
  case TOK_DOTPOWER: return ".^";
  case TOK_DOTTRANSPOSE: return ".'";
  case TOK_LE: return "<=";
  case TOK_GE: return ">=";
  case TOK_EQ: return "==";
  case TOK_NE: return "~=";
  case TOK_SOR: return "||";
  case TOK_SAND: return "&&";
  case TOK_QSTATEMENT: return "qstmnt";
  case TOK_STATEMENT: return "stmnt";
  case TOK_REALF: return "(real single)" + b.text();
  case TOK_IMAGF: return "(imag single)" + b.text();
  case TOK_REAL: return "(real)" + b.text();
  case TOK_IMAG: return "(imag)" + b.text();
  case TOK_FUNCTION_DEFS: return "functions:";
  case TOK_SCRIPT: return "script:";
  case TOK_DBTRACE: return "dbtrace";
  case TOK_ANONYMOUS_FUNC: return "anon func";
  case TOK_NEST_FUNC: return "nest func";
  case TOK_TYPE_DECL: return "type decl";
  case TOK_DBUP: return "dbup";
  case TOK_DBDOWN: return "dbdown";
  case TOK_REINDEX: return "reindex";
  case TOK_INCR: return "++";
  case TOK_DECR: return "--";
  case TOK_INCR_PREFIX: return "++(pre)";
  case TOK_DECR_PREFIX: return "--(pre)";
  case TOK_INCR_POSTFIX: return "++(post)";
  case TOK_DECR_POSTFIX: return "--(post)";
  case TOK_PLUS_EQ: return "+=";
  case TOK_MINUS_EQ: return "-=";
  }
  return QString(1,QChar(b.value()))+QString(" val = %1").arg(b.value());
}

void Token::freeze(Serialize *s) const {
  s->putShort(m_tok);
  s->putInt(m_pos);
  s->putString(m_text);
}

Token::Token(Serialize *s) {
  m_tok = s->getShort();
  m_pos = s->getInt();
  m_text = s->getString();
  fillArray();
}

void Token::fillArray() {
  Array retval;
  switch(m_tok) {
  default:
    return;
  case TOK_REAL:
    {
      QString mt(m_text);
      if (mt.toUpper().endsWith("D")) mt.chop(1);
      retval = Array(double(mt.toDouble()));
      break;
    }
  case TOK_IMAG:
    {
      QString mt(m_text);
      if (mt.toUpper().endsWith("D")) mt.chop(1);
      retval = Array(double(0),double(mt.toDouble()));
      break;
    }
  case TOK_REALF:
    {
      QString mt(m_text);
      if (mt.toUpper().endsWith("F")) mt.chop(1);
      retval = Array(float(mt.toFloat()));
      break;
    }
  case TOK_IMAGF:
    {
      QString mt(m_text);
      if (mt.toUpper().endsWith("F")) mt.chop(1);
      retval = Array(float(0),float(mt.toFloat()));
      break;
    }
  case TOK_STRING:
    retval = Array(m_text);
    break;
  }
  m_array = retval;
}