Assign values, not just messages, to status codes

This commit is contained in:
Nigel Tao committed 2018-06-10 15:48:29 +10:00
1 parent 86010a17e2
commit 634789722d
10 files changed
+103 -59

No files matched your search

+7 -5
View File
@@ -477,9 +477,9 @@ func NewVar(op t.ID, name t.ID, xType *TypeExpr, value *Expr) *Var {
// - LHS: <TypeExpr>
type Field Node
func (n *Field) Node() *Node { return (*Node)(n) }
func (n *Field) Name() t.ID { return n.id2 }
func (n *Field) XType() *TypeExpr { return n.lhs.TypeExpr() }
func (n *Field) Node() *Node { return (*Node)(n) }
func (n *Field) Name() t.ID { return n.id2 }
func (n *Field) XType() *TypeExpr { return n.lhs.TypeExpr() }
func NewField(name t.ID, xType *TypeExpr) *Field {
return &Field{
@@ -837,7 +837,7 @@ func NewFunc(flags Flags, filename string, line uint32, receiverName t.ID, funcN
}
}
// Status is "error ID2" or "suspension ID2":
// Status is "error (RHS) ID2" or "suspension (RHS) ID2":
// - FlagsPublic is "pub" vs "pri"
// - ID0: <IDError|IDSuspension>
// - ID1: <0|pkg> (set by calling SetPackage)
@@ -850,8 +850,9 @@ func (n *Status) Filename() string { return n.filename }
func (n *Status) Line() uint32 { return n.line }
func (n *Status) Keyword() t.ID { return n.id0 }
func (n *Status) QID() t.QID { return t.QID{n.id1, n.id2} }
func (n *Status) Value() *Expr { return n.rhs.Expr() }
func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, message t.ID) *Status {
func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, value *Expr, message t.ID) *Status {
return &Status{
kind: KStatus,
flags: flags,
@@ -859,6 +860,7 @@ func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, message
line: line,
id0: keyword,
id2: message,
rhs: value.Node(),
}
}
+15 -14
View File
@@ -35,20 +35,21 @@ var numTypeBounds = [...][2]*big.Int{
}
var (
minusOne = big.NewInt(-1)
zero = big.NewInt(+0)
one = big.NewInt(+1)
two = big.NewInt(+2)
three = big.NewInt(+3)
four = big.NewInt(+4)
five = big.NewInt(+5)
six = big.NewInt(+6)
seven = big.NewInt(+7)
eight = big.NewInt(+8)
sixteen = big.NewInt(+16)
thirtyTwo = big.NewInt(+32)
sixtyFour = big.NewInt(+64)
ffff = big.NewInt(0xFFFF)
minusOne = big.NewInt(-1)
zero = big.NewInt(+0)
one = big.NewInt(+1)
two = big.NewInt(+2)
three = big.NewInt(+3)
four = big.NewInt(+4)
five = big.NewInt(+5)
six = big.NewInt(+6)
seven = big.NewInt(+7)
eight = big.NewInt(+8)
sixteen = big.NewInt(+16)
thirtyTwo = big.NewInt(+32)
sixtyFour = big.NewInt(+64)
oneTwentyEight = big.NewInt(+128)
ffff = big.NewInt(0xFFFF)
maxIntBits = big.NewInt(t.MaxIntBits)
+26
View File
@@ -178,6 +178,8 @@ type Checker struct {
builtInSliceFuncs map[t.QQID]*a.Func
builtInTableFuncs map[t.QQID]*a.Func
unsortedStructs []*a.Struct
statusByValue [256]t.ID
}
func (c *Checker) PackageID() uint32 { return c.packageID }
@@ -296,6 +298,30 @@ func (c *Checker) checkStatus(node *a.Node) error {
}
}
c.statuses[qid] = n
q := &checker{
c: c,
tm: c.tm,
}
if err := q.tcheckExpr(n.Value(), 0); err != nil {
return fmt.Errorf("%v in status %s", err, qid.Str(c.tm))
}
if cv := n.Value().ConstValue(); cv == nil {
return fmt.Errorf("status %s value is not a constant expression", qid.Str(c.tm))
} else if cv.Cmp(zero) <= 0 || oneTwentyEight.Cmp(cv) <= 0 {
return fmt.Errorf("status %s value %v is out of range [0x01..0x7F]", qid.Str(c.tm), cv)
} else if n.QID()[0] == 0 {
x := uint8(cv.Int64())
if n.Keyword() == t.IDError {
x = -x
}
if other := c.statusByValue[x]; other != 0 {
return fmt.Errorf("duplicate %s value 0x%02X: %s and %s",
n.Keyword().Str(c.tm), cv, other.Str(c.tm), n.QID()[1].Str(c.tm))
}
c.statusByValue[x] = n.QID()[1]
}
n.Node().SetMType(typeExprPlaceholder)
return nil
}
+17 -1
View File
@@ -229,6 +229,22 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
case t.IDError, t.IDSuspension:
keyword := p.src[0].ID
p.src = p.src[1:]
if x := p.peek1(); x != t.IDOpenParen {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected "(", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
value, err := p.parseExpr()
if err != nil {
return nil, err
}
if x := p.peek1(); x != t.IDCloseParen {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected ")", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
message := p.peek1()
if !message.IsStrLiteral(p.tm) {
got := p.tm.ByID(message)
@@ -240,7 +256,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
return a.NewStatus(flags, p.filename, line, keyword, message).Node(), nil
return a.NewStatus(flags, p.filename, line, keyword, value, message).Node(), nil
case t.IDStruct:
p.src = p.src[1:]