- filed a bug w/ constant evaluation

SVN=125966
This commit is contained in:
Robert Griesemer 2008-07-03 15:16:51 -07:00
parent add9c8cc18
commit b43ad96ed6
3 changed files with 243 additions and 167 deletions

8
test/bugs/bug063.go Normal file
View File

@ -0,0 +1,8 @@
// $G $D/$F.go
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
const c = 0 ^ 0

View File

@ -317,6 +317,20 @@ BUG: known to fail incorrectly
=========== bugs/bug062.go =========== bugs/bug062.go
BUG: known to succeed incorrectly BUG: known to succeed incorrectly
=========== bugs/bug063.go
bugs/bug063.go:4: illegal combination of literals XOR 7
bugs/bug063.go:4: expression must be a constant
bugs/bug063.go:4: expression must be a constant
bugs/bug063.go:4: expression must be a constant
bugs/bug063.go:4: expression must be a constant
bugs/bug063.go:4: expression must be a constant
bugs/bug063.go:4: expression must be a constant
bugs/bug063.go:4: expression must be a constant
bugs/bug063.go:4: expression must be a constant
bugs/bug063.go:4: expression must be a constant
bugs/bug063.go:4: fatal error: too many errors
BUG: should compile without problems
=========== fixedbugs/bug000.go =========== fixedbugs/bug000.go
=========== fixedbugs/bug001.go =========== fixedbugs/bug001.go

View File

@ -69,6 +69,7 @@ const (
COR = iota; COR = iota;
// keywords // keywords
KEYWORDS_BEG = iota;
BREAK = iota; BREAK = iota;
CASE = iota; CASE = iota;
CONST = iota; CONST = iota;
@ -97,6 +98,7 @@ const (
TRUE = iota; TRUE = iota;
TYPE = iota; TYPE = iota;
VAR = iota; VAR = iota;
KEYWORDS_END = iota;
) )
@ -108,96 +110,95 @@ var (
export TokenName export TokenName
func TokenName(tok int) string { func TokenName(tok int) string {
switch (tok) { switch (tok) {
case ILLEGAL: return "ILLEGAL"; case ILLEGAL: return "illegal";
case EOF: return "EOF"; case EOF: return "eof";
case IDENT: return "IDENT"; case IDENT: return "ident";
case STRING: return "STRING"; case STRING: return "string";
case NUMBER: return "NUMBER"; case NUMBER: return "number";
case COMMA: return "COMMA"; case COMMA: return ",";
case COLON: return "COLON"; case COLON: return ":";
case SEMICOLON: return "SEMICOLON"; case SEMICOLON: return ";";
case PERIOD: return "PERIOD"; case PERIOD: return ".";
case LPAREN: return "LPAREN"; case LPAREN: return "(";
case RPAREN: return "RPAREN"; case RPAREN: return ")";
case LBRACK: return "LBRACK"; case LBRACK: return "[";
case RBRACK: return "RBRACK"; case RBRACK: return "]";
case LBRACE: return "LBRACE"; case LBRACE: return "{";
case RBRACE: return "RBRACE"; case RBRACE: return "}";
case ASSIGN: return "ASSIGN"; case ASSIGN: return "=";
case DEFINE: return "DEFINE"; case DEFINE: return ":=";
case INC: return "INC"; case INC: return "++";
case DEC: return "DEC"; case DEC: return "--";
case NOT: return "NOT"; case NOT: return "!";
case AND: return "AND"; case AND: return "&";
case OR: return "OR"; case OR: return "|";
case XOR: return "XOR"; case XOR: return "^";
case ADD: return "ADD"; case ADD: return "+";
case SUB: return "SUB"; case SUB: return "-";
case MUL: return "MUL"; case MUL: return "*";
case REM: return "REM"; case QUO: return "/";
case QUO: return "QUO"; case REM: return "%";
case REM: return "REM";
case EQL: return "EQL"; case EQL: return "==";
case NEQ: return "NEQ"; case NEQ: return "!=";
case LSS: return "LSS"; case LSS: return "<";
case LEQ: return "LEQ"; case LEQ: return "<=";
case GTR: return "GTR"; case GTR: return ">";
case GEQ: return "GEQ"; case GEQ: return ">=";
case SHL: return SHL; case SHL: return "<<";
case SHR: return SHR; case SHR: return ">>";
case ADD_ASSIGN: return "ADD_ASSIGN"; case ADD_ASSIGN: return "+=";
case SUB_ASSIGN: return "SUB_ASSIGN"; case SUB_ASSIGN: return "-=";
case MUL_ASSIGN: return "MUL_ASSIGN"; case MUL_ASSIGN: return "+=";
case QUO_ASSIGN: return "QUO_ASSIGN"; case QUO_ASSIGN: return "/=";
case REM_ASSIGN: return "REM_ASSIGN"; case REM_ASSIGN: return "%=";
case AND_ASSIGN: return "AND_ASSIGN"; case AND_ASSIGN: return "&=";
case OR_ASSIGN: return "OR_ASSIGN"; case OR_ASSIGN: return "|=";
case XOR_ASSIGN: return "XOR_ASSIGN"; case XOR_ASSIGN: return "^=";
case SHL_ASSIGN: return "SHL_ASSIGN"; case SHL_ASSIGN: return "<<=";
case SHR_ASSIGN: return "SHR_ASSIGN"; case SHR_ASSIGN: return ">>=";
case CAND: return "CAND"; case CAND: return "&&";
case COR: return "COR"; case COR: return "||";
case BREAK: return "BREAK"; case BREAK: return "break";
case CASE: return "CASE"; case CASE: return "case";
case CONST: return "CONST"; case CONST: return "const";
case CONTINUE: return "CONTINUE"; case CONTINUE: return "continue";
case DEFAULT: return "DEFAULT"; case DEFAULT: return "default";
case ELSE: return "ELSE"; case ELSE: return "else";
case EXPORT: return "EXPORT"; case EXPORT: return "export";
case FALLTHROUGH: return "FALLTHROUGH"; case FALLTHROUGH: return "fallthrough";
case FALSE: return "FALSE"; case FALSE: return "false";
case FOR: return "FOR"; case FOR: return "for";
case FUNC: return "FUNC"; case FUNC: return "func";
case GO: return "GO"; case GO: return "go";
case GOTO: return "GOTO"; case GOTO: return "goto";
case IF: return "IF"; case IF: return "if";
case IMPORT: return "IMPORT"; case IMPORT: return "import";
case INTERFACE: return "INTERFACE"; case INTERFACE: return "interface";
case MAP: return "MAP"; case MAP: return "map";
case NEW: return "NEW"; case NEW: return "new";
case NIL: return "NIL"; case NIL: return "nil";
case PACKAGE: return "PACKAGE"; case PACKAGE: return "package";
case RANGE: return "RANGE"; case RANGE: return "range";
case RETURN: return "RETURN"; case RETURN: return "return";
case SELECT: return "SELECT"; case SELECT: return "select";
case STRUCT: return "STRUCT"; case STRUCT: return "struct";
case SWITCH: return "SWITCH"; case SWITCH: return "switch";
case TRUE: return "TRUE"; case TRUE: return "true";
case TYPE: return "TYPE"; case TYPE: return "type";
case VAR: return "VAR"; case VAR: return "var";
} }
return "???"; return "???";
@ -238,50 +239,104 @@ type Scanner struct {
func (S *Scanner) Next () { func (S *Scanner) Next () {
const (
Bit1 = 7;
Bitx = 6;
Bit2 = 5;
Bit3 = 4;
Bit4 = 3;
T1 = 0x00; // (1 << (Bit1 + 1) - 1) ^ 0xFF; // 0000 0000
Tx = 0x80; // (1 << (Bitx + 1) - 1) ^ 0xFF; // 1000 0000
T2 = 0xC0; // (1 << (Bit2 + 1) - 1) ^ 0xFF; // 1100 0000
T3 = 0xE0; // (1 << (Bit3 + 1) - 1) ^ 0xFF; // 1110 0000
T4 = 0xF0; // (1 << (Bit4 + 1) - 1) ^ 0xFF; // 1111 0000
Rune1 = 1 << (Bit1 + 0*Bitx) - 1; // 0000 0000 0111 1111
Rune2 = 1 << (Bit2 + 1*Bitx) - 1; // 0000 0111 1111 1111
Rune3 = 1 << (Bit3 + 2*Bitx) - 1; // 1111 1111 1111 1111
Maskx = 0x3F; // 1 << Bitx - 1; // 0011 1111
Testx = 0xC0; // Maskx ^ 0xFF; // 1100 0000
Bad = 0xFFFD; // Runeerror
);
src := S.src; // TODO only needed because of 6g bug src := S.src; // TODO only needed because of 6g bug
if S.pos < len(src) { lim := len(src);
S.ch = int(S.src[S.pos]); pos := S.pos;
S.pos++;
if (S.ch >= 128) { // 1-byte sequence
panic "UTF-8 not handled" // 0000-007F => T1
} if pos >= lim {
} else { goto eof;
S.ch = -1;
} }
c0 := int(src[pos + 0]);
if c0 < Tx {
S.ch = c0;
S.pos = pos + 1;
return;
}
// 2-byte sequence
// 0080-07FF => T2 Tx
if pos + 1 >= lim {
goto eof;
}
c1 := int(src[pos + 1]) ^ Tx;
if c1 & Testx != 0 {
goto bad;
}
if c0 < T3 {
if c0 < T2 {
goto bad;
}
r := (c0 << Bitx | c1) & Rune2;
if r <= Rune1 {
goto bad;
}
S.ch = r;
S.pos = pos + 2;
return;
}
// 3-byte encoding
// 0800-FFFF => T3 Tx Tx
if pos + 2 >= lim {
goto eof;
}
c2 := int(src[pos + 2]) ^ Tx;
if c2 & Testx != 0 {
goto bad;
}
if c0 < T4 {
r := (((c0 << Bitx | c1) << Bitx) | c2) & Rune3;
if r <= Rune2 {
goto bad;
}
S.ch = r;
S.pos = pos + 3;
return;
}
// bad encoding
bad:
S.ch = Bad;
S.pos += 1;
return;
// end of file
eof:
S.ch = -1;
} }
func Init () { func Init () {
Keywords = new(map [string] int); Keywords = new(map [string] int);
Keywords["break"] = BREAK; for i := KEYWORDS_BEG; i <= KEYWORDS_END; i++ {
Keywords["case"] = CASE; Keywords[TokenName(i)] = i;
Keywords["const"] = CONST; }
Keywords["continue"] = CONTINUE;
Keywords["default"] = DEFAULT;
Keywords["else"] = ELSE;
Keywords["export"] = EXPORT;
Keywords["fallthrough"] = FALLTHROUGH;
Keywords["false"] = FALSE;
Keywords["for"] = FOR;
Keywords["func"] = FUNC;
Keywords["go"] = GO;
Keywords["goto"] = GOTO;
Keywords["if"] = IF;
Keywords["import"] = IMPORT;
Keywords["interface"] = INTERFACE;
Keywords["map"] = MAP;
Keywords["new"] = NEW;
Keywords["nil"] = NIL;
Keywords["package"] = PACKAGE;
Keywords["range"] = RANGE;
Keywords["return"] = RETURN;
Keywords["select"] = SELECT;
Keywords["struct"] = STRUCT;
Keywords["switch"] = SWITCH;
Keywords["true"] = TRUE;
Keywords["type"] = TYPE;
Keywords["var"] = VAR;
} }
@ -342,8 +397,8 @@ func (S *Scanner) ScanIdentifier () int {
func (S *Scanner) ScanMantissa () { func (S *Scanner) ScanMantissa () {
for is_dec_digit(S.ch) { for is_dec_digit(S.ch) {
S.Next(); S.Next();
} }
} }
@ -351,13 +406,13 @@ func (S *Scanner) ScanMantissa () {
func (S *Scanner) ScanNumber () int { func (S *Scanner) ScanNumber () int {
// TODO complete this routine // TODO complete this routine
if S.ch == '.' { if S.ch == '.' {
S.Next(); S.Next();
} }
S.ScanMantissa(); S.ScanMantissa();
if S.ch == 'e' || S.ch == 'E' { if S.ch == 'e' || S.ch == 'E' {
S.Next(); S.Next();
if S.ch == '-' || S.ch == '+' { if S.ch == '-' || S.ch == '+' {
S.Next(); S.Next();
} }
S.ScanMantissa(); S.ScanMantissa();
} }
@ -462,9 +517,9 @@ func (S *Scanner) ScanRawString () int {
func (S *Scanner) Select2 (tok0, tok1 int) int { func (S *Scanner) Select2 (tok0, tok1 int) int {
if S.ch == '=' { if S.ch == '=' {
S.Next(); S.Next();
return tok1; return tok1;
} }
return tok0; return tok0;
} }
@ -472,11 +527,11 @@ func (S *Scanner) Select2 (tok0, tok1 int) int {
func (S *Scanner) Select3 (tok0, tok1, ch2, tok2 int) int { func (S *Scanner) Select3 (tok0, tok1, ch2, tok2 int) int {
if S.ch == '=' { if S.ch == '=' {
S.Next(); S.Next();
return tok1; return tok1;
} }
if S.ch == ch2 { if S.ch == ch2 {
S.Next(); S.Next();
return tok2; return tok2;
} }
return tok0; return tok0;
@ -485,13 +540,13 @@ func (S *Scanner) Select3 (tok0, tok1, ch2, tok2 int) int {
func (S *Scanner) Select4 (tok0, tok1, ch2, tok2, tok3 int) int { func (S *Scanner) Select4 (tok0, tok1, ch2, tok2, tok3 int) int {
if S.ch == '=' { if S.ch == '=' {
S.Next(); S.Next();
return tok1; return tok1;
} }
if S.ch == ch2 { if S.ch == ch2 {
S.Next(); S.Next();
if S.ch == '=' { if S.ch == '=' {
S.Next(); S.Next();
return tok3; return tok3;
} }
return tok2; return tok2;
@ -513,46 +568,45 @@ func (S *Scanner) Scan () (tok, beg, end int) {
default: default:
S.Next(); S.Next();
switch ch { switch ch {
case -1: tok = EOF; case -1: tok = EOF;
case '"': tok = S.ScanString(); case '"': tok = S.ScanString();
case '\'': tok = S.ScanChar(); case '\'': tok = S.ScanChar();
case '`': tok = S.ScanRawString(); case '`': tok = S.ScanRawString();
case ':': tok = S.Select2(COLON, DEFINE); case ':': tok = S.Select2(COLON, DEFINE);
case '.': case '.':
if is_dec_digit(S.ch) { if is_dec_digit(S.ch) {
tok = S.ScanNumber(); tok = S.ScanNumber();
} else { } else {
tok = PERIOD; tok = PERIOD;
} }
case ',': tok = COMMA; case ',': tok = COMMA;
case ';': tok = SEMICOLON; case ';': tok = SEMICOLON;
case '(': tok = LPAREN; case '(': tok = LPAREN;
case ')': tok = RPAREN; case ')': tok = RPAREN;
case '[': tok = LBRACK; case '[': tok = LBRACK;
case ']': tok = RBRACK; case ']': tok = RBRACK;
case '{': tok = LBRACE; case '{': tok = LBRACE;
case '}': tok = RBRACE; case '}': tok = RBRACE;
case '+': tok = S.Select3(ADD, ADD_ASSIGN, '+', INC); case '+': tok = S.Select3(ADD, ADD_ASSIGN, '+', INC);
case '-': tok = S.Select3(SUB, SUB_ASSIGN, '-', DEC); case '-': tok = S.Select3(SUB, SUB_ASSIGN, '-', DEC);
case '*': tok = S.Select2(MUL, MUL_ASSIGN); case '*': tok = S.Select2(MUL, MUL_ASSIGN);
case '/': case '/':
if S.ch == '/' || S.ch == '*' { if S.ch == '/' || S.ch == '*' {
S.SkipComment(); S.SkipComment();
// cannot simply return because of 6g bug // cannot simply return because of 6g bug
tok, beg, end = S.Scan(); tok, beg, end = S.Scan();
return tok, beg, end; return tok, beg, end;
} }
tok = S.Select2(QUO, QUO_ASSIGN); tok = S.Select2(QUO, QUO_ASSIGN);
case '%': tok = S.Select2(REM, REM_ASSIGN); case '%': tok = S.Select2(REM, REM_ASSIGN);
case '^': tok = S.Select2(XOR, XOR_ASSIGN); case '^': tok = S.Select2(XOR, XOR_ASSIGN);
case '<': tok = S.Select4(LSS, LEQ, '<', SHL, SHL_ASSIGN); case '<': tok = S.Select4(LSS, LEQ, '<', SHL, SHL_ASSIGN);
case '>': tok = S.Select4(GTR, GEQ, '>', SHR, SHR_ASSIGN); case '>': tok = S.Select4(GTR, GEQ, '>', SHR, SHR_ASSIGN);
case '=': tok = S.Select2(ASSIGN, EQL); case '=': tok = S.Select2(ASSIGN, EQL);
case '!': tok = S.Select2(NOT, NEQ); case '!': tok = S.Select2(NOT, NEQ);
case '&': tok = S.Select3(AND, AND_ASSIGN, '&', CAND); case '&': tok = S.Select3(AND, AND_ASSIGN, '&', CAND);
case '|': tok = S.Select3(OR, OR_ASSIGN, '|', COR); case '|': tok = S.Select3(OR, OR_ASSIGN, '|', COR);
default: tok = ILLEGAL; default: tok = ILLEGAL;
} }
} }