From 710eefe85a976c438da255499fbefd1a6c989ef6 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 9 Jul 2018 20:47:07 +0200 Subject: 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. --- pkg/ast/format.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'pkg/ast/format.go') 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) } } -- cgit mrf-deployment