mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Implement Expr.String.
This commit is contained in:
1 parent
d92049b8fa
commit
1dd51ac30f
4 files changed
+156
-3
No files matched your search
@@ -162,6 +162,7 @@ func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l }
|
||||
type Expr Node
|
||||
|
||||
func (n *Expr) Node() *Node { return (*Node)(n) }
|
||||
func (n *Expr) Suspendible() bool { return n.flags&FlagsSuspendible != 0 }
|
||||
func (n *Expr) ConstValue() *big.Int { return n.constValue }
|
||||
func (n *Expr) MType() *TypeExpr { return n.mType }
|
||||
func (n *Expr) ID0() t.ID { return n.id0 }
|
||||
|
||||
+118
-3
@@ -12,10 +12,125 @@ func (n *Expr) String(m *t.IDMap) string {
|
||||
if n == nil {
|
||||
return ""
|
||||
}
|
||||
if n.ID0() == 0 {
|
||||
return m.ByID(n.ID1())
|
||||
if n.id0 == 0 {
|
||||
return m.ByID(n.id1)
|
||||
}
|
||||
return "Expr.String.TODO"
|
||||
return string(n.appendString(nil, m, false))
|
||||
}
|
||||
|
||||
func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool) []byte {
|
||||
if n != nil {
|
||||
switch n.id0.Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
|
||||
case 0:
|
||||
switch n.id0 {
|
||||
case 0:
|
||||
buf = append(buf, m.ByID(n.id1)...)
|
||||
|
||||
case t.IDOpenParen:
|
||||
buf = n.lhs.Expr().appendString(buf, m, true)
|
||||
if n.flags&FlagsSuspendible != 0 {
|
||||
buf = append(buf, '?')
|
||||
}
|
||||
buf = append(buf, '(')
|
||||
for i, o := range n.list0 {
|
||||
if i != 0 {
|
||||
buf = append(buf, ", "...)
|
||||
}
|
||||
buf = o.Expr().appendString(buf, m, false)
|
||||
}
|
||||
buf = append(buf, ')')
|
||||
|
||||
case t.IDOpenBracket:
|
||||
buf = n.lhs.Expr().appendString(buf, m, true)
|
||||
buf = append(buf, '[')
|
||||
buf = n.rhs.Expr().appendString(buf, m, false)
|
||||
buf = append(buf, ']')
|
||||
|
||||
case t.IDColon:
|
||||
buf = n.lhs.Expr().appendString(buf, m, true)
|
||||
buf = append(buf, '[')
|
||||
buf = n.mhs.Expr().appendString(buf, m, false)
|
||||
buf = append(buf, ':')
|
||||
buf = n.rhs.Expr().appendString(buf, m, false)
|
||||
buf = append(buf, ']')
|
||||
|
||||
case t.IDDot:
|
||||
buf = n.lhs.Expr().appendString(buf, m, true)
|
||||
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)
|
||||
|
||||
case t.FlagsBinaryOp:
|
||||
if parenthesize {
|
||||
buf = append(buf, '(')
|
||||
}
|
||||
buf = n.lhs.Expr().appendString(buf, m, true)
|
||||
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)
|
||||
}
|
||||
if parenthesize {
|
||||
buf = append(buf, ')')
|
||||
}
|
||||
|
||||
case t.FlagsAssociativeOp:
|
||||
if parenthesize {
|
||||
buf = append(buf, '(')
|
||||
}
|
||||
op := opStrings[0xFF&n.id0.Key()]
|
||||
for i, o := range n.list0 {
|
||||
if i != 0 {
|
||||
buf = append(buf, op...)
|
||||
}
|
||||
buf = o.Expr().appendString(buf, m, true)
|
||||
}
|
||||
if parenthesize {
|
||||
buf = append(buf, ')')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
var opStrings = [256]string{
|
||||
t.IDXUnaryPlus >> t.KeyShift: "+",
|
||||
t.IDXUnaryMinus >> t.KeyShift: "-",
|
||||
t.IDXUnaryNot >> t.KeyShift: "not ",
|
||||
|
||||
t.IDXBinaryPlus >> t.KeyShift: " + ",
|
||||
t.IDXBinaryMinus >> t.KeyShift: " - ",
|
||||
t.IDXBinaryStar >> t.KeyShift: " * ",
|
||||
t.IDXBinarySlash >> t.KeyShift: " / ",
|
||||
t.IDXBinaryShiftL >> t.KeyShift: " << ",
|
||||
t.IDXBinaryShiftR >> t.KeyShift: " >> ",
|
||||
t.IDXBinaryAmp >> t.KeyShift: " & ",
|
||||
t.IDXBinaryAmpHat >> t.KeyShift: " &^ ",
|
||||
t.IDXBinaryPipe >> t.KeyShift: " | ",
|
||||
t.IDXBinaryHat >> t.KeyShift: " ^ ",
|
||||
t.IDXBinaryNotEq >> t.KeyShift: " != ",
|
||||
t.IDXBinaryLessThan >> t.KeyShift: " < ",
|
||||
t.IDXBinaryLessEq >> t.KeyShift: " <= ",
|
||||
t.IDXBinaryEqEq >> t.KeyShift: " == ",
|
||||
t.IDXBinaryGreaterEq >> t.KeyShift: " >= ",
|
||||
t.IDXBinaryGreaterThan >> t.KeyShift: " > ",
|
||||
t.IDXBinaryAnd >> t.KeyShift: " and ",
|
||||
t.IDXBinaryOr >> t.KeyShift: " or ",
|
||||
t.IDXBinaryAs >> t.KeyShift: " as ",
|
||||
|
||||
t.IDXAssociativePlus >> t.KeyShift: " + ",
|
||||
t.IDXAssociativeStar >> t.KeyShift: " * ",
|
||||
t.IDXAssociativeAmp >> t.KeyShift: " & ",
|
||||
t.IDXAssociativePipe >> t.KeyShift: " | ",
|
||||
t.IDXAssociativeHat >> t.KeyShift: " ^ ",
|
||||
t.IDXAssociativeAnd >> t.KeyShift: " and ",
|
||||
t.IDXAssociativeOr >> t.KeyShift: " or ",
|
||||
}
|
||||
|
||||
// Eq returns whether n and o are equal.
|
||||
|
||||
@@ -16,11 +16,36 @@ func TestString(t *testing.T) {
|
||||
"1",
|
||||
"x",
|
||||
|
||||
"f()",
|
||||
"f(i)",
|
||||
"f(i, j)",
|
||||
"f(i, j) + 1",
|
||||
"f(i, j)(k)",
|
||||
"f(i, j)(k, l, m + 2) + 3",
|
||||
|
||||
"x[i]",
|
||||
"x[i][j]",
|
||||
"x[i:j]",
|
||||
"x[i](j)",
|
||||
"x(i)[j]",
|
||||
|
||||
"x.y",
|
||||
"x.y.z.a",
|
||||
|
||||
"+x",
|
||||
"-(x + y)",
|
||||
"not x",
|
||||
"not not x",
|
||||
"+++-x",
|
||||
|
||||
"x + 42",
|
||||
"x and (y < z)",
|
||||
"x & (y as u8)",
|
||||
"x * ((a / b) - (i / j))",
|
||||
|
||||
"x + y + z",
|
||||
"x + (i * j.k[l] * (-m << 4) * (n & o(p, q[:r.s + 5]))) + z",
|
||||
|
||||
"x as bool",
|
||||
"x as u32",
|
||||
"x as T",
|
||||
|
||||
@@ -24,6 +24,18 @@ func Parse(m *t.IDMap, filename string, src []t.Token) (*a.File, error) {
|
||||
return p.parseFile()
|
||||
}
|
||||
|
||||
func ParseExpr(m *t.IDMap, filename string, src []t.Token) (*a.Expr, error) {
|
||||
p := &parser{
|
||||
src: src,
|
||||
m: m,
|
||||
filename: filename,
|
||||
}
|
||||
if len(src) > 0 {
|
||||
p.lastLine = src[len(src)-1].Line
|
||||
}
|
||||
return p.parseExpr()
|
||||
}
|
||||
|
||||
type parser struct {
|
||||
src []t.Token
|
||||
m *t.IDMap
|
||||
|
||||
Reference in new issue
Block a user