mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 00:47:28 +01:00
Add ast.FlagsTypeChecked.
This commit is contained in:
1 parent
bd6e555cce
commit
15485dadc1
4 files changed
+203
-114
No files matched your search
+10
-1
@@ -67,6 +67,7 @@ type Flags uint32
|
||||
|
||||
const (
|
||||
FlagsSuspendible = Flags(0x00000001)
|
||||
FlagsTypeChecked = Flags(0x00000002)
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
@@ -88,7 +89,9 @@ type Node struct {
|
||||
list1 []*Node
|
||||
}
|
||||
|
||||
func (n *Node) Kind() Kind { return n.kind }
|
||||
func (n *Node) Kind() Kind { return n.kind }
|
||||
func (n *Node) TypeChecked() bool { return n.flags&FlagsTypeChecked != 0 }
|
||||
func (n *Node) SetTypeChecked() { n.flags |= FlagsTypeChecked }
|
||||
|
||||
func (n *Node) Assert() *Assert { return (*Assert)(n) }
|
||||
func (n *Node) Assign() *Assign { return (*Assign)(n) }
|
||||
@@ -150,6 +153,9 @@ func (n *Raw) List1() []*Node { return n.list1 }
|
||||
|
||||
func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l }
|
||||
|
||||
// MaxExprDepth is an advisory limit for an Expr's recursion depth.
|
||||
const MaxExprDepth = 255
|
||||
|
||||
// Expr is an expression, such as "i", "+j" or "k + l[m(n, o)].p":
|
||||
// - FlagsSuspendible is "f(x)" vs "f?(x)"
|
||||
// - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDColon|IDDot>
|
||||
@@ -351,6 +357,9 @@ func NewJump(keyword t.ID) *Jump {
|
||||
}
|
||||
}
|
||||
|
||||
// MaxTypeExprDepth is an advisory limit for a TypeExpr's recursion depth.
|
||||
const MaxTypeExprDepth = 63
|
||||
|
||||
// TypeExpr is a type expression, such as "u32", "u32[:8]", "pkg.foo", "ptr T"
|
||||
// or "[8] T":
|
||||
// - ID0: <0|package name|IDPtr|IDOpenBracket>
|
||||
|
||||
+60
-31
@@ -15,10 +15,15 @@ func (n *Expr) String(m *t.IDMap) string {
|
||||
if n.id0 == 0 {
|
||||
return m.ByID(n.id1)
|
||||
}
|
||||
return string(n.appendString(nil, m, false))
|
||||
return string(n.appendString(nil, m, false, 0))
|
||||
}
|
||||
|
||||
func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool) []byte {
|
||||
func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool, depth uint32) []byte {
|
||||
if depth > MaxExprDepth {
|
||||
return append(buf, "!expr_recursion_depth_too_large!"...)
|
||||
}
|
||||
depth++
|
||||
|
||||
if n != nil {
|
||||
switch n.id0.Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
|
||||
case 0:
|
||||
@@ -27,7 +32,7 @@ func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool) []byte {
|
||||
buf = append(buf, m.ByID(n.id1)...)
|
||||
|
||||
case t.IDOpenParen:
|
||||
buf = n.lhs.Expr().appendString(buf, m, true)
|
||||
buf = n.lhs.Expr().appendString(buf, m, true, depth)
|
||||
if n.flags&FlagsSuspendible != 0 {
|
||||
buf = append(buf, '?')
|
||||
}
|
||||
@@ -36,44 +41,44 @@ func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool) []byte {
|
||||
if i != 0 {
|
||||
buf = append(buf, ", "...)
|
||||
}
|
||||
buf = o.Expr().appendString(buf, m, false)
|
||||
buf = o.Expr().appendString(buf, m, false, depth)
|
||||
}
|
||||
buf = append(buf, ')')
|
||||
|
||||
case t.IDOpenBracket:
|
||||
buf = n.lhs.Expr().appendString(buf, m, true)
|
||||
buf = n.lhs.Expr().appendString(buf, m, true, depth)
|
||||
buf = append(buf, '[')
|
||||
buf = n.rhs.Expr().appendString(buf, m, false)
|
||||
buf = n.rhs.Expr().appendString(buf, m, false, depth)
|
||||
buf = append(buf, ']')
|
||||
|
||||
case t.IDColon:
|
||||
buf = n.lhs.Expr().appendString(buf, m, true)
|
||||
buf = n.lhs.Expr().appendString(buf, m, true, depth)
|
||||
buf = append(buf, '[')
|
||||
buf = n.mhs.Expr().appendString(buf, m, false)
|
||||
buf = n.mhs.Expr().appendString(buf, m, false, depth)
|
||||
buf = append(buf, ':')
|
||||
buf = n.rhs.Expr().appendString(buf, m, false)
|
||||
buf = n.rhs.Expr().appendString(buf, m, false, depth)
|
||||
buf = append(buf, ']')
|
||||
|
||||
case t.IDDot:
|
||||
buf = n.lhs.Expr().appendString(buf, m, true)
|
||||
buf = n.lhs.Expr().appendString(buf, m, true, depth)
|
||||
buf = append(buf, '.')
|
||||
buf = append(buf, m.ByID(n.id1)...)
|
||||
}
|
||||
|
||||
case t.FlagsUnaryOp:
|
||||
buf = append(buf, opStrings[0xFF&n.id0.Key()]...)
|
||||
buf = n.rhs.Expr().appendString(buf, m, true)
|
||||
buf = n.rhs.Expr().appendString(buf, m, true, depth)
|
||||
|
||||
case t.FlagsBinaryOp:
|
||||
if parenthesize {
|
||||
buf = append(buf, '(')
|
||||
}
|
||||
buf = n.lhs.Expr().appendString(buf, m, true)
|
||||
buf = n.lhs.Expr().appendString(buf, m, true, depth)
|
||||
buf = append(buf, opStrings[0xFF&n.id0.Key()]...)
|
||||
if n.id0 == t.IDXBinaryAs {
|
||||
buf = append(buf, n.rhs.TypeExpr().String(m)...)
|
||||
} else {
|
||||
buf = n.rhs.Expr().appendString(buf, m, true)
|
||||
buf = n.rhs.Expr().appendString(buf, m, true, depth)
|
||||
}
|
||||
if parenthesize {
|
||||
buf = append(buf, ')')
|
||||
@@ -88,7 +93,7 @@ func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool) []byte {
|
||||
if i != 0 {
|
||||
buf = append(buf, op...)
|
||||
}
|
||||
buf = o.Expr().appendString(buf, m, true)
|
||||
buf = o.Expr().appendString(buf, m, true, depth)
|
||||
}
|
||||
if parenthesize {
|
||||
buf = append(buf, ')')
|
||||
@@ -135,22 +140,46 @@ var opStrings = [256]string{
|
||||
|
||||
// String returns a string form of n.
|
||||
func (n *TypeExpr) String(m *t.IDMap) string {
|
||||
if n != nil {
|
||||
s := ""
|
||||
switch n.PackageOrDecorator() {
|
||||
case 0:
|
||||
s = m.ByID(n.Name())
|
||||
case t.IDPtr:
|
||||
return "ptr " + n.Inner().String(m)
|
||||
case t.IDOpenBracket:
|
||||
return "[" + n.ArrayLength().String(m) + "] " + n.Inner().String(m)
|
||||
default:
|
||||
s = m.ByID(n.PackageOrDecorator()) + "." + m.ByID(n.Name())
|
||||
}
|
||||
if n.InclMin() == nil && n.ExclMax() == nil {
|
||||
return s
|
||||
}
|
||||
return s + "[" + n.InclMin().String(m) + ":" + n.ExclMax().String(m) + "]"
|
||||
if n == nil {
|
||||
return ""
|
||||
}
|
||||
return "!invalid_type!"
|
||||
if n.PackageOrDecorator() == 0 && n.InclMin() == nil && n.ExclMax() == nil {
|
||||
return m.ByID(n.Name())
|
||||
}
|
||||
return string(n.appendString(nil, m, 0))
|
||||
}
|
||||
|
||||
func (n *TypeExpr) appendString(buf []byte, m *t.IDMap, depth uint32) []byte {
|
||||
if depth > MaxTypeExprDepth {
|
||||
return append(buf, "!type_expr_recursion_depth_too_large!"...)
|
||||
}
|
||||
depth++
|
||||
if n == nil {
|
||||
return append(buf, "!invalid_type!"...)
|
||||
}
|
||||
|
||||
switch n.PackageOrDecorator() {
|
||||
case 0:
|
||||
buf = append(buf, m.ByID(n.Name())...)
|
||||
case t.IDPtr:
|
||||
buf = append(buf, "ptr "...)
|
||||
return n.Inner().appendString(buf, m, depth)
|
||||
case t.IDOpenBracket:
|
||||
buf = append(buf, '[')
|
||||
buf = n.ArrayLength().appendString(buf, m, false, 0)
|
||||
buf = append(buf, "] "...)
|
||||
return n.Inner().appendString(buf, m, depth)
|
||||
default:
|
||||
buf = append(buf, m.ByID(n.PackageOrDecorator())...)
|
||||
buf = append(buf, '.')
|
||||
buf = append(buf, m.ByID(n.Name())...)
|
||||
}
|
||||
if n.InclMin() != nil || n.ExclMax() != nil {
|
||||
buf = append(buf, '[')
|
||||
buf = n.InclMin().appendString(buf, m, false, 0)
|
||||
buf = append(buf, ':')
|
||||
buf = n.ExclMax().appendString(buf, m, false, 0)
|
||||
buf = append(buf, ']')
|
||||
}
|
||||
return buf
|
||||
}
|
||||
Reference in new issue
Block a user