diff --git a/lang/ast/ast.go b/lang/ast/ast.go index f56ad91a..f98211ab 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -68,8 +68,9 @@ var kindStrings = [...]string{ type Flags uint32 const ( - FlagsSuspendible = Flags(0x00000001) - FlagsTypeChecked = Flags(0x00000002) + FlagsImpure = Flags(0x00000001) + FlagsSuspendible = Flags(0x00000002) + FlagsTypeChecked = Flags(0x00000004) ) type Node struct { @@ -160,7 +161,8 @@ func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l } 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)" +// - FlagsImpure is "f(x)" vs "f!(x)" +// - FlagsSuspendible is "f(x)" vs "f?(x)", it implies FlagsImpure // - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDColon|IDDot> // - ID1: <0|ident|literal> // - LHS: @@ -189,6 +191,7 @@ const MaxExprDepth = 255 type Expr Node func (n *Expr) Node() *Node { return (*Node)(n) } +func (n *Expr) Impure() bool { return n.flags&FlagsImpure != 0 } 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 } @@ -437,7 +440,8 @@ func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthMin *Expr, max *Expr, inne } // Func is "func ID0.ID1(LHS)(RHS) { List1 }": -// - FlagsSuspendible is "ID1" vs "ID1?" +// - FlagsImpure is "ID1" vs "ID1!" +// - FlagsSuspendible is "ID1" vs "ID1?", it implies FlagsImpure // - ID0: <0|receiver> // - ID1: name // - LHS: in-parameters @@ -447,6 +451,7 @@ func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthMin *Expr, max *Expr, inne type Func Node func (n *Func) Node() *Node { return (*Node)(n) } +func (n *Func) Impure() bool { return n.flags&FlagsImpure != 0 } func (n *Func) Suspendible() bool { return n.flags&FlagsSuspendible != 0 } func (n *Func) Filename() string { return n.filename } func (n *Func) Line() uint32 { return n.line } diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 4eaff317..b1f208ac 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -87,8 +87,12 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { return nil, fmt.Errorf(`parse: built-in %q used for func name at %s:%d`, p.m.ByID(id1), p.filename, p.line()) } - if p.peek1().Key() == t.KeyQuestion { - flags |= a.FlagsSuspendible + switch p.peek1().Key() { + case t.KeyExclam: + flags |= a.FlagsImpure + p.src = p.src[1:] + case t.KeyQuestion: + flags |= a.FlagsImpure | a.FlagsSuspendible p.src = p.src[1:] } inFields, err := p.parseList(t.KeyCloseParen, (*parser).parseFieldNode) @@ -667,9 +671,13 @@ func (p *parser) parseOperand() (*a.Expr, error) { default: return lhs, nil - case t.KeyQuestion: + case t.KeyExclam, t.KeyQuestion: + if p.src[0].Key() == t.KeyExclam { + flags |= a.FlagsImpure + } else { + flags |= a.FlagsImpure | a.FlagsSuspendible + } p.src = p.src[1:] - flags |= a.FlagsSuspendible fallthrough case t.KeyOpenParen: diff --git a/lang/token/list.go b/lang/token/list.go index fad8bd06..d54e6a25 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -139,6 +139,7 @@ const ( KeyDot = Key(IDDot >> KeyShift) KeyDotDot = Key(IDDotDot >> KeyShift) KeyComma = Key(IDComma >> KeyShift) + KeyExclam = Key(IDExclam >> KeyShift) KeyQuestion = Key(IDQuestion >> KeyShift) KeyColon = Key(IDColon >> KeyShift) KeySemicolon = Key(IDSemicolon >> KeyShift) @@ -264,9 +265,10 @@ const ( IDDot = ID(0x20<