2011-06-15 13:19:50 -05:00
|
|
|
|
2011-07-05 04:48:19 -05:00
|
|
|
import util::interner;
|
2012-01-13 02:56:53 -06:00
|
|
|
import lexer::reader;
|
2010-08-18 13:35:12 -05:00
|
|
|
|
2011-05-09 16:17:28 -05:00
|
|
|
type str_num = uint;
|
|
|
|
|
2012-01-19 16:24:03 -06:00
|
|
|
enum binop {
|
2012-01-19 19:56:05 -06:00
|
|
|
PLUS,
|
|
|
|
MINUS,
|
|
|
|
STAR,
|
|
|
|
SLASH,
|
|
|
|
PERCENT,
|
|
|
|
CARET,
|
|
|
|
AND,
|
|
|
|
OR,
|
|
|
|
LSL,
|
|
|
|
LSR,
|
|
|
|
ASR,
|
2010-09-09 17:59:29 -05:00
|
|
|
}
|
|
|
|
|
2012-01-19 16:24:03 -06:00
|
|
|
enum token {
|
2010-09-09 17:59:29 -05:00
|
|
|
/* Expression-operator symbols. */
|
2012-01-19 19:56:05 -06:00
|
|
|
EQ,
|
|
|
|
LT,
|
|
|
|
LE,
|
|
|
|
EQEQ,
|
|
|
|
NE,
|
|
|
|
GE,
|
|
|
|
GT,
|
|
|
|
ANDAND,
|
|
|
|
OROR,
|
|
|
|
NOT,
|
|
|
|
TILDE,
|
|
|
|
BINOP(binop),
|
|
|
|
BINOPEQ(binop),
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Structural symbols */
|
2012-01-19 19:56:05 -06:00
|
|
|
AT,
|
|
|
|
DOT,
|
|
|
|
ELLIPSIS,
|
|
|
|
COMMA,
|
|
|
|
SEMI,
|
|
|
|
COLON,
|
|
|
|
MOD_SEP,
|
|
|
|
RARROW,
|
|
|
|
LARROW,
|
|
|
|
DARROW,
|
|
|
|
LPAREN,
|
|
|
|
RPAREN,
|
|
|
|
LBRACKET,
|
|
|
|
RBRACKET,
|
|
|
|
LBRACE,
|
|
|
|
RBRACE,
|
|
|
|
POUND,
|
|
|
|
POUND_LBRACE,
|
|
|
|
POUND_LT,
|
2010-09-09 17:59:29 -05:00
|
|
|
|
2012-01-25 17:38:09 -06:00
|
|
|
DOLLAR_LPAREN,
|
|
|
|
DOLLAR_NUM(uint),
|
|
|
|
|
2010-09-09 17:59:29 -05:00
|
|
|
/* Literals */
|
2012-01-19 19:56:05 -06:00
|
|
|
LIT_INT(i64, ast::int_ty),
|
|
|
|
LIT_UINT(u64, ast::uint_ty),
|
|
|
|
LIT_FLOAT(str_num, ast::float_ty),
|
|
|
|
LIT_STR(str_num),
|
|
|
|
LIT_BOOL(bool),
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Name components */
|
2012-01-19 19:56:05 -06:00
|
|
|
IDENT(str_num, bool),
|
|
|
|
IDX(int),
|
|
|
|
UNDERSCORE,
|
|
|
|
BRACEQUOTE(str_num),
|
|
|
|
EOF,
|
2012-01-25 17:38:09 -06:00
|
|
|
|
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 {
|
2012-01-19 00:37:22 -06: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-13 02:56:53 -06:00
|
|
|
fn to_str(r: reader, t: token) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt t {
|
2012-01-19 00:37:22 -06: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
|
|
|
/* Structural symbols */
|
2012-01-19 00:37:22 -06:00
|
|
|
AT {
|
2011-09-02 17:34:58 -05:00
|
|
|
ret "@";
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
DOT { ret "."; }
|
|
|
|
ELLIPSIS { ret "..."; }
|
|
|
|
COMMA { ret ","; }
|
|
|
|
SEMI { ret ";"; }
|
|
|
|
COLON { ret ":"; }
|
|
|
|
MOD_SEP { 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-09-02 17:34:58 -05:00
|
|
|
|
2012-01-25 17:38:09 -06:00
|
|
|
DOLLAR_LPAREN { ret "$("; }
|
|
|
|
DOLLAR_NUM(u) {
|
|
|
|
ret "$" + uint::to_str(u as uint, 10u);
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
/* Literals */
|
2012-01-19 00:37:22 -06:00
|
|
|
LIT_INT(c, ast::ty_char) {
|
2011-12-07 14:06:12 -06:00
|
|
|
// FIXME: escape.
|
|
|
|
let tmp = "'";
|
|
|
|
str::push_char(tmp, c as char);
|
2012-02-12 01:49:03 -06:00
|
|
|
str::push_char(tmp, '\'');
|
2011-12-07 14:06:12 -06:00
|
|
|
ret tmp;
|
|
|
|
}
|
|
|
|
LIT_INT(i, t) {
|
|
|
|
ret int::to_str(i as int, 10u) + ast_util::int_ty_to_str(t);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-12-07 14:06:12 -06:00
|
|
|
LIT_UINT(u, t) {
|
|
|
|
ret uint::to_str(u as uint, 10u) + ast_util::uint_ty_to_str(t);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-12-07 14:06:12 -06:00
|
|
|
LIT_FLOAT(s, t) {
|
2012-01-13 02:56:53 -06:00
|
|
|
ret interner::get::<str>(*r.interner, s) +
|
2011-12-07 14:06:12 -06:00
|
|
|
ast_util::float_ty_to_str(t);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
LIT_STR(s) { // FIXME: escape.
|
2012-01-13 02:56:53 -06:00
|
|
|
ret "\"" + interner::get::<str>(*r.interner, s) + "\"";
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
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
|
|
|
/* Name components */
|
|
|
|
IDENT(s, _) {
|
2012-01-13 02:56:53 -06:00
|
|
|
ret interner::get::<str>(*r.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); }
|
2012-01-19 00:37:22 -06:00
|
|
|
UNDERSCORE { ret "_"; }
|
2011-09-02 17:34:58 -05:00
|
|
|
BRACEQUOTE(_) { ret "<bracequote>"; }
|
2012-01-19 00:37:22 -06:00
|
|
|
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 {
|
2012-01-19 00:37:22 -06:00
|
|
|
LPAREN { true }
|
|
|
|
LBRACE { true }
|
|
|
|
LBRACKET { true }
|
2011-07-27 07:19:39 -05:00
|
|
|
IDENT(_, _) { true }
|
2012-01-19 00:37:22 -06:00
|
|
|
UNDERSCORE { true }
|
|
|
|
TILDE { true }
|
2011-12-07 14:06:12 -06:00
|
|
|
LIT_INT(_, _) { true }
|
|
|
|
LIT_UINT(_, _) { true }
|
|
|
|
LIT_FLOAT(_, _) { true }
|
2011-07-27 07:19:39 -05:00
|
|
|
LIT_STR(_) { true }
|
2012-01-19 00:37:22 -06:00
|
|
|
POUND { true }
|
|
|
|
AT { true }
|
|
|
|
NOT { true }
|
|
|
|
BINOP(MINUS) { true }
|
|
|
|
BINOP(STAR) { true }
|
|
|
|
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
|
|
|
|
// End:
|