2010-08-18 13:35:12 -05:00
|
|
|
import util.common.ty_mach;
|
2010-08-20 13:41:34 -05:00
|
|
|
import util.common.ty_mach_to_str;
|
2010-09-21 18:22:32 -05:00
|
|
|
import util.common.new_str_hash;
|
2010-08-20 13:41:34 -05:00
|
|
|
import std._int;
|
|
|
|
import std._uint;
|
2010-08-18 13:35:12 -05:00
|
|
|
|
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);
|
|
|
|
|
2010-09-21 01:56:43 -05:00
|
|
|
AS;
|
|
|
|
WITH;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Structural symbols */
|
2010-09-21 01:56:43 -05:00
|
|
|
AT;
|
|
|
|
DOT;
|
|
|
|
COMMA;
|
|
|
|
SEMI;
|
|
|
|
COLON;
|
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
|
|
|
|
|
|
|
/* Module and crate keywords */
|
2010-09-21 01:56:43 -05:00
|
|
|
MOD;
|
|
|
|
USE;
|
|
|
|
AUTH;
|
|
|
|
META;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Metaprogramming keywords */
|
2010-09-21 01:56:43 -05:00
|
|
|
SYNTAX;
|
|
|
|
POUND;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Statement keywords */
|
2010-09-21 01:56:43 -05:00
|
|
|
IF;
|
|
|
|
ELSE;
|
|
|
|
DO;
|
|
|
|
WHILE;
|
|
|
|
ALT;
|
|
|
|
CASE;
|
|
|
|
|
|
|
|
FAIL;
|
|
|
|
DROP;
|
|
|
|
|
|
|
|
IN;
|
|
|
|
FOR;
|
|
|
|
EACH;
|
|
|
|
PUT;
|
|
|
|
RET;
|
|
|
|
BE;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Type and type-state keywords */
|
2010-09-21 01:56:43 -05:00
|
|
|
TYPE;
|
|
|
|
CHECK;
|
|
|
|
CLAIM;
|
|
|
|
PROVE;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Effect keywords */
|
2010-09-21 01:56:43 -05:00
|
|
|
IO;
|
|
|
|
STATE;
|
|
|
|
UNSAFE;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Type qualifiers */
|
2010-09-21 01:56:43 -05:00
|
|
|
NATIVE;
|
|
|
|
AUTO;
|
|
|
|
MUTABLE;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Name management */
|
2010-09-21 01:56:43 -05:00
|
|
|
IMPORT;
|
|
|
|
EXPORT;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Value / stmt declarators */
|
2010-09-21 01:56:43 -05:00
|
|
|
LET;
|
|
|
|
CONST;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Magic runtime services */
|
2010-09-21 01:56:43 -05:00
|
|
|
LOG;
|
|
|
|
SPAWN;
|
|
|
|
BIND;
|
|
|
|
THREAD;
|
|
|
|
YIELD;
|
|
|
|
JOIN;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Literals */
|
|
|
|
LIT_INT(int);
|
|
|
|
LIT_UINT(uint);
|
|
|
|
LIT_MACH_INT(ty_mach, int);
|
|
|
|
LIT_STR(str);
|
|
|
|
LIT_CHAR(char);
|
|
|
|
LIT_BOOL(bool);
|
|
|
|
|
|
|
|
/* Name components */
|
|
|
|
IDENT(str);
|
|
|
|
IDX(int);
|
2010-09-21 01:56:43 -05:00
|
|
|
UNDERSCORE;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Reserved type names */
|
2010-09-21 01:56:43 -05:00
|
|
|
BOOL;
|
|
|
|
INT;
|
|
|
|
UINT;
|
|
|
|
FLOAT;
|
|
|
|
CHAR;
|
|
|
|
STR;
|
2010-09-09 17:59:29 -05:00
|
|
|
MACH(ty_mach);
|
|
|
|
|
|
|
|
/* Algebraic type constructors */
|
2010-09-21 01:56:43 -05:00
|
|
|
REC;
|
|
|
|
TUP;
|
|
|
|
TAG;
|
|
|
|
VEC;
|
|
|
|
ANY;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Callable type constructors */
|
2010-09-21 01:56:43 -05:00
|
|
|
FN;
|
|
|
|
ITER;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Object type */
|
2010-09-21 01:56:43 -05:00
|
|
|
OBJ;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Comm and task types */
|
2010-09-21 01:56:43 -05:00
|
|
|
CHAN;
|
|
|
|
PORT;
|
|
|
|
TASK;
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
BRACEQUOTE(str);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-20 13:41:34 -05:00
|
|
|
fn to_str(token t) -> str {
|
|
|
|
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-09-21 01:56:43 -05:00
|
|
|
case (AS) { ret "as"; }
|
|
|
|
case (WITH) { ret "with"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
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 ":"; }
|
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
|
|
|
|
|
|
|
/* Module and crate keywords */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (MOD) { ret "mod"; }
|
|
|
|
case (USE) { ret "use"; }
|
|
|
|
case (AUTH) { ret "auth"; }
|
|
|
|
case (META) { ret "meta"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Metaprogramming keywords */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (SYNTAX) { ret "syntax"; }
|
|
|
|
case (POUND) { ret "#"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Statement keywords */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (IF) { ret "if"; }
|
|
|
|
case (ELSE) { ret "else"; }
|
|
|
|
case (DO) { ret "do"; }
|
|
|
|
case (WHILE) { ret "while"; }
|
|
|
|
case (ALT) { ret "alt"; }
|
|
|
|
case (CASE) { ret "case"; }
|
|
|
|
|
|
|
|
case (FAIL) { ret "fail"; }
|
|
|
|
case (DROP) { ret "drop"; }
|
|
|
|
|
|
|
|
case (IN) { ret "in"; }
|
|
|
|
case (FOR) { ret "for"; }
|
|
|
|
case (EACH) { ret "each"; }
|
|
|
|
case (PUT) { ret "put"; }
|
|
|
|
case (RET) { ret "ret"; }
|
|
|
|
case (BE) { ret "be"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Type and type-state keywords */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (TYPE) { ret "type"; }
|
|
|
|
case (CHECK) { ret "check"; }
|
|
|
|
case (CLAIM) { ret "claim"; }
|
|
|
|
case (PROVE) { ret "prove"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Effect keywords */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (IO) { ret "io"; }
|
|
|
|
case (STATE) { ret "state"; }
|
|
|
|
case (UNSAFE) { ret "unsafe"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Type qualifiers */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (NATIVE) { ret "native"; }
|
|
|
|
case (AUTO) { ret "auto"; }
|
|
|
|
case (MUTABLE) { ret "mutable"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Name management */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (IMPORT) { ret "import"; }
|
|
|
|
case (EXPORT) { ret "export"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Value / stmt declarators */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (LET) { ret "let"; }
|
|
|
|
case (CONST) { ret "const"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Magic runtime services */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (LOG) { ret "log"; }
|
|
|
|
case (SPAWN) { ret "spawn"; }
|
|
|
|
case (BIND) { ret "bind"; }
|
|
|
|
case (THREAD) { ret "thread"; }
|
|
|
|
case (YIELD) { ret "yield"; }
|
|
|
|
case (JOIN) { ret "join"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Literals */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (LIT_INT(?i)) { ret _int.to_str(i, 10u); }
|
|
|
|
case (LIT_UINT(?u)) { ret _uint.to_str(u, 10u); }
|
|
|
|
case (LIT_MACH_INT(?tm, ?i)) {
|
2010-08-20 13:41:34 -05:00
|
|
|
ret _int.to_str(i, 10u)
|
|
|
|
+ "_" + ty_mach_to_str(tm);
|
|
|
|
}
|
|
|
|
|
2010-09-21 01:56:43 -05:00
|
|
|
case (LIT_STR(?s)) {
|
2010-08-20 13:41:34 -05:00
|
|
|
// FIXME: escape.
|
|
|
|
ret "\"" + s + "\"";
|
|
|
|
}
|
2010-09-21 01:56:43 -05:00
|
|
|
case (LIT_CHAR(?c)) {
|
2010-08-20 13:41:34 -05:00
|
|
|
// FIXME: escape and encode.
|
2010-08-31 13:36:36 -05:00
|
|
|
auto tmp = "'";
|
|
|
|
tmp += c as u8;
|
|
|
|
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 */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (IDENT(?s)) { auto si = "ident:"; si += s; ret si; }
|
|
|
|
case (IDX(?i)) { ret "_" + _int.to_str(i, 10u); }
|
|
|
|
case (UNDERSCORE) { ret "_"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Reserved type names */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (BOOL) { ret "bool"; }
|
|
|
|
case (INT) { ret "int"; }
|
|
|
|
case (UINT) { ret "uint"; }
|
|
|
|
case (FLOAT) { ret "float"; }
|
|
|
|
case (CHAR) { ret "char"; }
|
|
|
|
case (STR) { ret "str"; }
|
|
|
|
case (MACH(?tm)) { ret ty_mach_to_str(tm); }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Algebraic type constructors */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (REC) { ret "rec"; }
|
|
|
|
case (TUP) { ret "tup"; }
|
|
|
|
case (TAG) { ret "tag"; }
|
|
|
|
case (VEC) { ret "vec"; }
|
|
|
|
case (ANY) { ret "any"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Callable type constructors */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (FN) { ret "fn"; }
|
|
|
|
case (ITER) { ret "iter"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Object type */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (OBJ) { ret "obj"; }
|
2010-08-20 13:41:34 -05:00
|
|
|
|
|
|
|
/* Comm and task types */
|
2010-09-21 01:56:43 -05:00
|
|
|
case (CHAN) { ret "chan"; }
|
|
|
|
case (PORT) { ret "port"; }
|
|
|
|
case (TASK) { ret "task"; }
|
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
|
|
|
|
// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|