diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2018-07-09 20:47:07 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2018-07-09 20:47:07 +0200 |
| commit | 710eefe85a976c438da255499fbefd1a6c989ef6 (patch) | |
| tree | 3467ac95a3c574bdf41105e012df2e4c540ed859 /pkg/ast/parser.go | |
| parent | f25e57704183544b0d540ef0035acfa6fb9071d7 (diff) | |
pkg/compiler: support negative integers
Currently we have to use 0xffffffffffffffff to represent -1,
and we can't express e.g. -20:20 int range.
Support negative consts to fix both problems.
Diffstat (limited to 'pkg/ast/parser.go')
| -rw-r--r-- | pkg/ast/parser.go | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/pkg/ast/parser.go b/pkg/ast/parser.go index e4c5d8742..d43011796 100644 --- a/pkg/ast/parser.go +++ b/pkg/ast/parser.go @@ -413,7 +413,7 @@ func (p *parser) parseType() *Type { switch p.tok { case tokInt: allowColon = true - arg.Value, arg.valueFmt = p.parseIntValue() + arg.Value, arg.ValueFmt = p.parseIntValue() case tokIdent: allowColon = true arg.Ident = p.lit @@ -429,7 +429,7 @@ func (p *parser) parseType() *Type { arg.Pos2 = p.pos switch p.tok { case tokInt: - arg.Value2, arg.value2Fmt = p.parseIntValue() + arg.Value2, arg.Value2Fmt = p.parseIntValue() case tokIdent: arg.Ident2 = p.lit default: @@ -479,7 +479,7 @@ func (p *parser) parseInt() *Int { } switch p.tok { case tokInt: - i.Value, i.valueFmt = p.parseIntValue() + i.Value, i.ValueFmt = p.parseIntValue() case tokIdent: i.Ident = p.lit default: @@ -489,16 +489,19 @@ func (p *parser) parseInt() *Int { return i } -func (p *parser) parseIntValue() (uint64, intFmt) { +func (p *parser) parseIntValue() (uint64, IntFmt) { if p.lit[0] == '\'' { - return uint64(p.lit[1]), intFmtChar + return uint64(p.lit[1]), IntFmtChar } if v, err := strconv.ParseUint(p.lit, 10, 64); err == nil { - return v, intFmtDec + return v, IntFmtDec + } + if v, err := strconv.ParseInt(p.lit, 10, 64); err == nil { + return uint64(v), IntFmtNeg } if len(p.lit) > 2 && p.lit[0] == '0' && p.lit[1] == 'x' { if v, err := strconv.ParseUint(p.lit[2:], 16, 64); err == nil { - return v, intFmtHex + return v, IntFmtHex } } panic(fmt.Sprintf("scanner returned bad integer %q", p.lit)) |
