2011-06-15 13:19:50 -05:00
|
|
|
|
2011-07-05 04:48:19 -05:00
|
|
|
import ast::ty_mach;
|
2011-08-21 23:44:41 -05:00
|
|
|
import ast_util::ty_mach_to_str;
|
2011-07-06 09:46:17 -05:00
|
|
|
import std::map::new_str_hash;
|
2011-07-05 04:48:19 -05:00
|
|
|
import util::interner;
|
2011-10-07 09:22:53 -05:00
|
|
|
import std::{int, uint, str, option};
|
|
|
|
import option::{some, none};
|
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 {
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2010-09-09 17:59:29 -05:00
|
|
|
/* 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);
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2010-09-09 17:59:29 -05:00
|
|
|
/* Structural symbols */
|
2010-09-21 01:56:43 -05:00
|
|
|
AT;
|
|
|
|
DOT;
|
2011-07-11 18:13:17 -05:00
|
|
|
ELLIPSIS;
|
2010-09-21 01:56:43 -05:00
|
|
|
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;
|
|
|
|
LARROW;
|
2011-06-13 17:48:55 -05:00
|
|
|
DARROW;
|
2010-09-21 01:56:43 -05:00
|
|
|
LPAREN;
|
|
|
|
RPAREN;
|
|
|
|
LBRACKET;
|
|
|
|
RBRACKET;
|
|
|
|
LBRACE;
|
|
|
|
RBRACE;
|
|
|
|
POUND;
|
2011-06-29 20:07:04 -05:00
|
|
|
POUND_LBRACE;
|
|
|
|
POUND_LT;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
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);
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2010-09-09 17:59:29 -05:00
|
|
|
/* Name components */
|
2011-05-14 05:54:31 -05:00
|
|
|
IDENT(str_num, bool);
|
2010-09-09 17:59:29 -05:00
|
|
|
IDX(int);
|
2010-09-21 01:56:43 -05:00
|
|
|
UNDERSCORE;
|
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
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
fn binop_to_str(o: binop) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt o {
|
2011-09-02 17:34:58 -05:00
|
|
|
PLUS. { ret "+"; }
|
|
|
|
MINUS. { ret "-"; }
|
|
|
|
STAR. { ret "*"; }
|
|
|
|
SLASH. { ret "/"; }
|
|
|
|
PERCENT. { ret "%"; }
|
|
|
|
CARET. { ret "^"; }
|
|
|
|
AND. { ret "&"; }
|
|
|
|
OR. { ret "|"; }
|
|
|
|
LSL. { ret "<<"; }
|
|
|
|
LSR. { ret ">>"; }
|
|
|
|
ASR. { ret ">>>"; }
|
2010-08-20 17:57:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
fn to_str(r: lexer::reader, t: token) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt t {
|
2011-09-02 17:34:58 -05:00
|
|
|
EQ. { ret "="; }
|
|
|
|
LT. { ret "<"; }
|
|
|
|
LE. { ret "<="; }
|
|
|
|
EQEQ. { ret "=="; }
|
|
|
|
NE. { ret "!="; }
|
|
|
|
GE. { ret ">="; }
|
|
|
|
GT. { ret ">"; }
|
|
|
|
NOT. { ret "!"; }
|
|
|
|
TILDE. { ret "~"; }
|
|
|
|
OROR. { ret "||"; }
|
|
|
|
ANDAND. { ret "&&"; }
|
2011-07-27 07:19:39 -05:00
|
|
|
BINOP(op) { ret binop_to_str(op); }
|
2011-09-02 17:34:58 -05:00
|
|
|
BINOPEQ(op) { ret binop_to_str(op) + "="; }
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
|
2011-09-12 05:39:38 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
/* Structural symbols */
|
|
|
|
AT. {
|
2011-09-02 17:34:58 -05:00
|
|
|
ret "@";
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
DOT. { ret "."; }
|
|
|
|
ELLIPSIS. { ret "..."; }
|
|
|
|
COMMA. { ret ","; }
|
|
|
|
SEMI. { ret ";"; }
|
|
|
|
COLON. { ret ":"; }
|
|
|
|
MOD_SEP. { ret "::"; }
|
|
|
|
QUES. { ret "?"; }
|
|
|
|
RARROW. { ret "->"; }
|
|
|
|
LARROW. { ret "<-"; }
|
|
|
|
DARROW. { ret "<->"; }
|
|
|
|
LPAREN. { ret "("; }
|
|
|
|
RPAREN. { ret ")"; }
|
|
|
|
LBRACKET. { ret "["; }
|
|
|
|
RBRACKET. { ret "]"; }
|
|
|
|
LBRACE. { ret "{"; }
|
|
|
|
RBRACE. { ret "}"; }
|
|
|
|
POUND. { ret "#"; }
|
|
|
|
POUND_LBRACE. { ret "#{"; }
|
|
|
|
POUND_LT. { ret "#<"; }
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
|
2011-09-12 05:39:38 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
/* Literals */
|
|
|
|
LIT_INT(i) {
|
2011-08-27 02:19:30 -05:00
|
|
|
ret int::to_str(i, 10u);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-08-27 02:19:30 -05:00
|
|
|
LIT_UINT(u) { ret uint::to_str(u, 10u); }
|
2011-07-27 07:19:39 -05:00
|
|
|
LIT_MACH_INT(tm, i) {
|
2011-09-02 17:34:58 -05:00
|
|
|
ret int::to_str(i, 10u) + "_" + ty_mach_to_str(tm);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
LIT_MACH_FLOAT(tm, s) {
|
2011-09-02 17:34:58 -05:00
|
|
|
ret interner::get::<str>(*r.get_interner(), s) + "_" +
|
|
|
|
ty_mach_to_str(tm);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
LIT_FLOAT(s) { ret interner::get::<str>(*r.get_interner(), s); }
|
2011-07-27 07:19:39 -05:00
|
|
|
LIT_STR(s) { // FIXME: escape.
|
2011-09-02 17:34:58 -05:00
|
|
|
ret "\"" + interner::get::<str>(*r.get_interner(), s) + "\"";
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
LIT_CHAR(c) {
|
|
|
|
// FIXME: escape.
|
2011-09-02 17:34:58 -05:00
|
|
|
let tmp = "'";
|
2011-09-01 19:27:58 -05:00
|
|
|
str::push_char(tmp, c);
|
|
|
|
str::push_byte(tmp, '\'' as u8);
|
2011-07-27 07:19:39 -05:00
|
|
|
ret tmp;
|
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
LIT_BOOL(b) { if b { ret "true"; } else { ret "false"; } }
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
|
2011-09-12 05:39:38 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
/* Name components */
|
|
|
|
IDENT(s, _) {
|
2011-09-02 17:34:58 -05:00
|
|
|
ret interner::get::<str>(*r.get_interner(), s);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
IDX(i) { ret "_" + int::to_str(i, 10u); }
|
|
|
|
UNDERSCORE. { ret "_"; }
|
|
|
|
BRACEQUOTE(_) { ret "<bracequote>"; }
|
|
|
|
EOF. { ret "<eof>"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-03 13:48:14 -05:00
|
|
|
|
|
|
|
|
2011-08-24 15:41:50 -05:00
|
|
|
pure fn can_begin_expr(t: token) -> bool {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt t {
|
|
|
|
LPAREN. { true }
|
|
|
|
LBRACE. { true }
|
|
|
|
LBRACKET. { true }
|
|
|
|
IDENT(_, _) { true }
|
|
|
|
UNDERSCORE. { true }
|
|
|
|
TILDE. { true }
|
|
|
|
LIT_INT(_) { true }
|
|
|
|
LIT_UINT(_) { true }
|
|
|
|
LIT_MACH_INT(_, _) { true }
|
|
|
|
LIT_FLOAT(_) { true }
|
|
|
|
LIT_MACH_FLOAT(_, _) { true }
|
|
|
|
LIT_STR(_) { true }
|
|
|
|
LIT_CHAR(_) { true }
|
|
|
|
POUND. { true }
|
|
|
|
AT. { true }
|
|
|
|
NOT. { true }
|
|
|
|
BINOP(MINUS.) { true }
|
|
|
|
BINOP(STAR.) { true }
|
2011-07-28 01:32:27 -05:00
|
|
|
MOD_SEP. { true }
|
2011-07-27 07:19:39 -05:00
|
|
|
_ { false }
|
2011-07-03 13:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2011-10-07 09:22:53 -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:
|