aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/ast/format.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2018-07-09 20:47:07 +0200
committerDmitry Vyukov <dvyukov@google.com>2018-07-09 20:47:07 +0200
commit710eefe85a976c438da255499fbefd1a6c989ef6 (patch)
tree3467ac95a3c574bdf41105e012df2e4c540ed859 /pkg/ast/format.go
parentf25e57704183544b0d540ef0035acfa6fb9071d7 (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/format.go')
-rw-r--r--pkg/ast/format.go34
1 files changed, 18 insertions, 16 deletions
diff --git a/pkg/ast/format.go b/pkg/ast/format.go
index 5022d5da1..13cd26e2c 100644
--- a/pkg/ast/format.go
+++ b/pkg/ast/format.go
@@ -35,6 +35,21 @@ func SerializeNode(n Node) string {
return buf.String()
}
+func FormatInt(v uint64, format IntFmt) string {
+ switch format {
+ case IntFmtDec:
+ return fmt.Sprint(v)
+ case IntFmtNeg:
+ return fmt.Sprint(int64(v))
+ case IntFmtHex:
+ return fmt.Sprintf("0x%x", v)
+ case IntFmtChar:
+ return fmt.Sprintf("'%c'", v)
+ default:
+ panic(fmt.Sprintf("unknown int format %v", format))
+ }
+}
+
type serializer interface {
serialize(w io.Writer)
}
@@ -159,14 +174,14 @@ func fmtType(t *Type) string {
case t.HasString:
v = fmt.Sprintf("\"%v\"", t.String)
default:
- v = fmtIntValue(t.Value, t.valueFmt)
+ v = FormatInt(t.Value, t.ValueFmt)
}
if t.HasColon {
switch {
case t.Ident2 != "":
v += fmt.Sprintf(":%v", t.Ident2)
default:
- v += fmt.Sprintf(":%v", fmtIntValue(t.Value2, t.value2Fmt))
+ v += fmt.Sprintf(":%v", FormatInt(t.Value2, t.Value2Fmt))
}
}
v += fmtTypeList(t.Args)
@@ -206,20 +221,7 @@ func fmtInt(i *Int) string {
case i.CExpr != "":
return fmt.Sprintf("%v", i.CExpr)
default:
- return fmtIntValue(i.Value, i.valueFmt)
- }
-}
-
-func fmtIntValue(v uint64, format intFmt) string {
- switch format {
- case intFmtDec:
- return fmt.Sprint(v)
- case intFmtHex:
- return fmt.Sprintf("0x%x", v)
- case intFmtChar:
- return fmt.Sprintf("'%c'", v)
- default:
- panic(fmt.Sprintf("unknown int format %v", format))
+ return FormatInt(i.Value, i.ValueFmt)
}
}