2011-05-12 10:24:54 -05:00
|
|
|
import util::common::ty_mach;
|
|
|
|
import util::common::ty_mach_to_str;
|
|
|
|
import util::common::new_str_hash;
|
|
|
|
import std::_int;
|
|
|
|
import std::_uint;
|
|
|
|
import std::_str;
|
2010-08-18 13:35:12 -05:00
|
|
|
|
2011-05-09 16:17:28 -05:00
|
|
|
type str_num = uint;
|
|
|
|
|
2010-09-09 17:59:29 -05:00
|
|
|
tag binop {
|
2010-09-21 01:56:43 -05:00
|
|
|
PLUS;
|
|
|
|
MINUS;
|
|
|
|
STAR;
|
|
|
|
SLASH;
|
|
|
|
PERCENT;
|
|
|
|
CARET;
|
|
|
|
AND;
|
|
|
|
OR;
|
|
|
|
LSL;
|
|
|
|
LSR;
|
|
|
|
ASR;
|
2010-09-09 17:59:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tag token {
|
|
|
|
/* Expression-operator symbols. */
|
2010-09-21 01:56:43 -05:00
|
|
|
EQ;
|
|
|
|
LT;
|
|
|
|
LE;
|
|
|
|
EQEQ;
|
|
|
|
NE;
|
|
|
|
GE;
|
|
|
|
GT;
|
|
|
|
ANDAND;
|
|
|
|
OROR;
|
|
|
|
NOT;
|
|
|
|
TILDE;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
BINOP(binop);
|
|
|
|
BINOPEQ(binop);
|
|
|
|
|
|
|
|
/* Structural symbols */
|
2010-09-21 01:56:43 -05:00
|
|
|
AT;
|
|
|
|
DOT;
|
|
|
|
COMMA;
|
|
|
|
SEMI;
|
|
|
|
COLON;
|
2011-05-12 10:20:07 -05:00
|
|
|
MOD_SEP;
|
2010-09-21 10:30:06 -05:00
|
|
|
QUES;
|
2010-09-21 01:56:43 -05:00
|
|
|
RARROW;
|
|
|
|
SEND;
|
|
|
|
LARROW;
|
|
|
|
LPAREN;
|
|
|
|
RPAREN;
|
|
|
|
LBRACKET;
|
|
|
|
RBRACKET;
|
|
|
|
LBRACE;
|
|
|
|
RBRACE;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
2010-09-21 01:56:43 -05:00
|
|
|
POUND;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Literals */
|
|
|
|
LIT_INT(int);
|
|
|
|
LIT_UINT(uint);
|
|
|
|
LIT_MACH_INT(ty_mach, int);
|
2011-05-09 16:17:28 -05:00
|
|
|
LIT_FLOAT(str_num);
|
|
|
|
LIT_MACH_FLOAT(ty_mach, str_num);
|
|
|
|
LIT_STR(str_num);
|
2010-09-09 17:59:29 -05:00
|
|
|
LIT_CHAR(char);
|
|
|
|
LIT_BOOL(bool);
|
|
|
|
|
|
|
|
/* Name components */
|
2011-05-09 16:17:28 -05:00
|
|
|
IDENT(str_num);
|
2010-09-09 17:59:29 -05:00
|
|
|
IDX(int);
|
2010-09-21 01:56:43 -05:00
|
|
|
UNDERSCORE;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
2011-05-09 16:17:28 -05:00
|
|
|
BRACEQUOTE(str_num);
|
2010-09-21 01:56:43 -05:00
|
|
|
EOF;
|
2010-09-09 17:59:29 -05:00
|
|
|
}
|
2010-08-18 13:35:12 -05:00
|
|
|
|
2010-08-31 15:29:00 -05:00
|
|
|
fn binop_to_str(binop o) -> str {
|
2010-08-20 17:57:59 -05:00
|
|
|
alt (o) {
|
2010-09-21 01:56:43 -05:00
|
|
|
case (PLUS) { ret "+"; }
|
|
|
|
case (MINUS) { ret "-"; }
|
|
|
|
case (STAR) { ret "*"; }
|
|
|
|
case (SLASH) { ret "/"; }
|
|
|
|
case (PERCENT) { ret "%"; }
|
|
|
|
case (CARET) { ret "^"; }
|
|
|
|
case (AND) { ret "&"; }
|
|
|
|
case (OR) { ret "|"; }
|
|
|
|
case (LSL) { ret "<<"; }
|
|
|
|
case (LSR) { ret ">>"; }
|
|
|
|
case (ASR) { ret ">>>"; }
|
2010-08-20 17:57:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn to_str(lexer::reader r, token t) -> str {
|
2010-08-20 13:41:34 -05:00
|
|
|
alt (t) {
|
2010-08-31 15:29:00 -05:00
|
|
|
|
2010-09-21 01:56:43 -05:00
|
|
|
case (EQ) { ret "="; }
|
|
|
|
case (LT) { ret "<"; }
|
|
|
|
case (LE) { ret "<="; }
|
|
|
|
case (EQEQ) { ret "=="; }
|
|
|
|
case (NE) { ret "!="; }
|
|
|
|
case (GE) { ret ">="; }
|
|
|
|
case (GT) { ret ">"; }
|
|
|
|
case (NOT) { ret "!"; }
|
|
|
|
case (TILDE) { ret "~"; }
|
|
|
|
case (OROR) { ret "||"; }
|
|
|
|
case (ANDAND) { ret "&&"; }
|
2010-08-31 15:29:00 -05:00
|
|
|
|
2010-09-21 01:56:43 -05:00
|
|
|
case (BINOP(?op)) { ret binop_to_str(op); }
|
|
|
|
case (BINOPEQ(?op)) { ret binop_to_str(op) + "="; }
|
2010-08-31 15:29:00 -05:00
|
|
|
|
2010-08-20 13:41:34 -05:00
|
|
|
/* Structural symbols */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (AT) { ret "@"; }
|
|
|
|
case (DOT) { ret "."; }
|
|
|
|
case (COMMA) { ret ","; }
|
|
|
|
case (SEMI) { ret ";"; }
|
|
|
|
case (COLON) { ret ":"; }
|
2011-05-12 10:20:07 -05:00
|
|
|
case (MOD_SEP) { ret "::"; }
|
2010-09-21 10:30:06 -05:00
|
|
|
case (QUES) { ret "?"; }
|
2010-09-21 01:56:43 -05:00
|
|
|
case (RARROW) { ret "->"; }
|
|
|
|
case (SEND) { ret "<|"; }
|
|
|
|
case (LARROW) { ret "<-"; }
|
|
|
|
case (LPAREN) { ret "("; }
|
|
|
|
case (RPAREN) { ret ")"; }
|
|
|
|
case (LBRACKET) { ret "["; }
|
|
|
|
case (RBRACKET) { ret "]"; }
|
|
|
|
case (LBRACE) { ret "{"; }
|
|
|
|
case (RBRACE) { ret "}"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
2010-09-21 01:56:43 -05:00
|
|
|
case (POUND) { ret "#"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Literals */
|
2011-05-12 10:24:54 -05:00
|
|
|
case (LIT_INT(?i)) { ret _int::to_str(i, 10u); }
|
|
|
|
case (LIT_UINT(?u)) { ret _uint::to_str(u, 10u); }
|
2010-09-21 01:56:43 -05:00
|
|
|
case (LIT_MACH_INT(?tm, ?i)) {
|
2011-05-12 10:24:54 -05:00
|
|
|
ret _int::to_str(i, 10u)
|
2010-08-20 13:41:34 -05:00
|
|
|
+ "_" + ty_mach_to_str(tm);
|
|
|
|
}
|
2011-05-09 16:17:28 -05:00
|
|
|
case (LIT_MACH_FLOAT(?tm, ?s)) {
|
|
|
|
ret r.get_str(s) + "_" + ty_mach_to_str(tm);
|
|
|
|
}
|
|
|
|
|
|
|
|
case (LIT_FLOAT(?s)) { ret r.get_str(s); }
|
2010-09-21 01:56:43 -05:00
|
|
|
case (LIT_STR(?s)) {
|
2010-08-20 13:41:34 -05:00
|
|
|
// FIXME: escape.
|
2011-05-09 16:17:28 -05:00
|
|
|
ret "\"" + r.get_str(s) + "\"";
|
2010-08-20 13:41:34 -05:00
|
|
|
}
|
2010-09-21 01:56:43 -05:00
|
|
|
case (LIT_CHAR(?c)) {
|
2011-03-24 10:07:43 -05:00
|
|
|
// FIXME: escape.
|
2010-08-31 13:36:36 -05:00
|
|
|
auto tmp = "'";
|
2011-05-12 10:24:54 -05:00
|
|
|
_str::push_char(tmp, c);
|
|
|
|
_str::push_byte(tmp, '\'' as u8);
|
2010-08-20 13:41:34 -05:00
|
|
|
ret tmp;
|
|
|
|
}
|
|
|
|
|
2010-09-21 01:56:43 -05:00
|
|
|
case (LIT_BOOL(?b)) {
|
2010-08-20 13:41:34 -05:00
|
|
|
if (b) { ret "true"; } else { ret "false"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Name components */
|
2011-05-09 16:17:28 -05:00
|
|
|
case (IDENT(?s)) {
|
2011-05-13 14:30:08 -05:00
|
|
|
ret r.get_str(s);
|
2011-05-09 16:17:28 -05:00
|
|
|
}
|
2011-05-12 10:24:54 -05:00
|
|
|
case (IDX(?i)) { ret "_" + _int::to_str(i, 10u); }
|
2010-09-21 01:56:43 -05:00
|
|
|
case (UNDERSCORE) { ret "_"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
case (BRACEQUOTE(_)) { ret "<bracequote>"; }
|
2010-09-21 01:56:43 -05:00
|
|
|
case (EOF) { ret "<eof>"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-18 13:35:12 -05:00
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-03-25 17:07:27 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-08-18 13:35:12 -05:00
|
|
|
// End:
|