mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Parse "x + y".
This commit is contained in:
1 parent
8dbc476375
commit
2ff25dc122
3 files changed
+87
-12
No files matched your search
+21
-9
@@ -15,13 +15,26 @@ type Kind uint32
|
||||
const (
|
||||
KInvalid = Kind(iota)
|
||||
|
||||
// KLiteral is a literal value.
|
||||
// - ID0: value
|
||||
KLiteral
|
||||
|
||||
// KIdent is an identifier.
|
||||
// - ID0: name
|
||||
KIdent
|
||||
// KExpr is an expression, such as "i", "+j" or "k + l[m].n".
|
||||
// - ID0: <0|operator|IDOpenBracket|IDDot>
|
||||
// - ID2: <0|identifier name|literal>
|
||||
// - LHS: <nil|KExpr>
|
||||
// - RHS: <nil|KExpr|KType>
|
||||
//
|
||||
// A zero ID0 means an identifier or literal in ID2, like "foo" or "42".
|
||||
//
|
||||
// For unary operators, ID0 is the operator and RHS is the operand.
|
||||
//
|
||||
// For binary operators, ID0 is the operator and LHS and RHS are the
|
||||
// operands. The LHS may be null for the "-" and "+" operators, which are
|
||||
// also unary operators.
|
||||
//
|
||||
// For function calls, like "lhs(rhs)", ID0 is IDOpenParen.
|
||||
//
|
||||
// For indexes, like "lhs[rhs]", ID0 is IDOpenBracket.
|
||||
//
|
||||
// For selectors, like "lhs.rhs", ID0 is IDDot.
|
||||
KExpr
|
||||
|
||||
// KAssign is "Left = Right".
|
||||
// - LHS: Left
|
||||
@@ -73,11 +86,10 @@ func (k Kind) String() string {
|
||||
|
||||
var kindStrings = [...]string{
|
||||
KAssign: "KAssign",
|
||||
KExpr: "KExpr",
|
||||
KFile: "KFile",
|
||||
KFunc: "KFunc",
|
||||
KIdent: "KIdent",
|
||||
KInvalid: "KInvalid",
|
||||
KLiteral: "KLiteral",
|
||||
KParam: "KParam",
|
||||
KType: "KType",
|
||||
}
|
||||
|
||||
+54
-3
@@ -261,13 +261,64 @@ func (p *parser) parseStatement() (*a.Node, error) {
|
||||
}
|
||||
|
||||
func (p *parser) parseExpr() (*a.Node, error) {
|
||||
// TODO: parse other expressions, such as x.y, unop x and x binop y.
|
||||
lhs, err := p.parseOperand()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if x := p.peekID(); x.IsBinaryOp() {
|
||||
p.src = p.src[1:]
|
||||
parseFunc := (*parser).parseOperand
|
||||
if x == t.IDAs {
|
||||
parseFunc = (*parser).parseType
|
||||
}
|
||||
rhs, err := parseFunc(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &a.Node{
|
||||
Kind: a.KExpr,
|
||||
ID0: x,
|
||||
LHS: lhs,
|
||||
RHS: rhs,
|
||||
}, nil
|
||||
}
|
||||
return lhs, nil
|
||||
}
|
||||
|
||||
func (p *parser) parseOperand() (*a.Node, error) {
|
||||
switch x := p.peekID(); {
|
||||
case x == t.IDOpenParen:
|
||||
// TODO: parse "(x + y)".
|
||||
|
||||
case x.IsUnaryOp():
|
||||
p.src = p.src[1:]
|
||||
rhs, err := p.parseExpr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &a.Node{
|
||||
Kind: a.KExpr,
|
||||
ID0: x,
|
||||
RHS: rhs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
id, err := p.parseIdent()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch p.peekID() {
|
||||
case t.IDOpenParen:
|
||||
// TODO: parse "f(i)".
|
||||
case t.IDOpenBracket:
|
||||
// TODO: parse "a[i]".
|
||||
case t.IDDot:
|
||||
// TODO: parse "x.y".
|
||||
}
|
||||
|
||||
return &a.Node{
|
||||
Kind: a.KIdent,
|
||||
ID0: id,
|
||||
Kind: a.KExpr,
|
||||
ID2: id,
|
||||
}, nil
|
||||
}
|
||||
@@ -43,6 +43,18 @@ type ID uint32
|
||||
func (x ID) Key() Key { return Key(x >> idShift) }
|
||||
func (x ID) Flags() Flags { return Flags(x & idMask) }
|
||||
|
||||
func (x ID) IsUnaryOp() bool { return Flags(x)&FlagsUnaryOp != 0 }
|
||||
func (x ID) IsBinaryOp() bool { return Flags(x)&FlagsBinaryOp != 0 }
|
||||
func (x ID) IsAssociative() bool { return Flags(x)&FlagsAssociative != 0 }
|
||||
func (x ID) IsAssign() bool { return Flags(x)&FlagsAssign != 0 }
|
||||
func (x ID) IsLiteral() bool { return Flags(x)&FlagsLiteral != 0 }
|
||||
func (x ID) IsIdent() bool { return Flags(x)&FlagsIdent != 0 }
|
||||
func (x ID) IsImplicitSemicolon() bool { return Flags(x)&FlagsImplicitSemicolon != 0 }
|
||||
func (x ID) IsOpen() bool { return Flags(x)&FlagsOpen != 0 }
|
||||
func (x ID) IsClose() bool { return Flags(x)&FlagsClose != 0 }
|
||||
func (x ID) IsTightLeft() bool { return Flags(x)&FlagsTightLeft != 0 }
|
||||
func (x ID) IsTightRight() bool { return Flags(x)&FlagsTightRight != 0 }
|
||||
|
||||
// Token combines an ID and the line number it was seen.
|
||||
type Token struct {
|
||||
ID ID
|
||||
|
||||
Reference in new issue
Block a user