fix(parser): substitute macro parameters behind element selectors
Assisted-by: GLM 5.3 Flash
This commit is contained in:
+89
-2
@@ -349,7 +349,7 @@ func (pp *preproc) expandTokens(in []token.Token) []token.Token {
|
||||
consecutive = 0
|
||||
continue
|
||||
}
|
||||
def := pp.macros[t.Text]
|
||||
def, suffix := pp.macroFor(t.Text)
|
||||
if def == nil {
|
||||
i++
|
||||
consecutive = 0
|
||||
@@ -363,7 +363,14 @@ func (pp *preproc) expandTokens(in []token.Token) []token.Token {
|
||||
return nil
|
||||
}
|
||||
if def.args == nil {
|
||||
s = append(s[:i], append(restamp(def.body, t.Pos), s[i+1:]...)...)
|
||||
body := restamp(def.body, t.Pos)
|
||||
if suffix != "" {
|
||||
// The macro was reached only through a compound spelling
|
||||
// (ACC0.B16 over "#define ACC0 V8"), so the selector has
|
||||
// to travel with the expansion.
|
||||
body = appendSelector(body, suffix, t.Pos)
|
||||
}
|
||||
s = append(s[:i], append(body, s[i+1:]...)...)
|
||||
continue
|
||||
}
|
||||
// A parameterised macro invoked without its parentheses stands
|
||||
@@ -394,6 +401,17 @@ func (pp *preproc) expandTokens(in []token.Token) []token.Token {
|
||||
sub = append(sub, restamp(args[k], t.Pos)...)
|
||||
continue
|
||||
}
|
||||
// A parameter used with an element or lane selector: the
|
||||
// lexer folds A.S4 into one identifier, so the whole-token
|
||||
// match above cannot see the parameter. The toolchain
|
||||
// lexes the period separately and substitutes the name
|
||||
// alone; splitting at the FIRST period and pasting the
|
||||
// argument back in front of the selector is the equivalent
|
||||
// for this lexer.
|
||||
if k, sel := parameterSelector(bt.Text, def.args); k >= 0 {
|
||||
sub = append(sub, restamp(pasteSelector(args[k], sel), t.Pos)...)
|
||||
continue
|
||||
}
|
||||
}
|
||||
sub = append(sub, bt)
|
||||
}
|
||||
@@ -402,6 +420,75 @@ func (pp *preproc) expandTokens(in []token.Token) []token.Token {
|
||||
return s
|
||||
}
|
||||
|
||||
// macroFor finds the macro a use names. The lexer folds NAME.selector into
|
||||
// one identifier token, so a macro written behind a selector suffix
|
||||
// (ACC0.B16 over "#define ACC0 V8") never matches a whole-token table
|
||||
// lookup; the toolchain splits on the period and reads the two halves, so
|
||||
// the prefix before the FIRST period is tried here as well and the caller
|
||||
// re-attaches the suffix to whatever the macro expands to. Only a whole
|
||||
// name counts: AB.S4 does not reach a macro named A, and a parameterised
|
||||
// macro is not hidden behind a selector, because its invocation would need
|
||||
// the parentheses to follow the bare name.
|
||||
func (pp *preproc) macroFor(text string) (*macroDef, string) {
|
||||
if def := pp.macros[text]; def != nil {
|
||||
return def, ""
|
||||
}
|
||||
if j := strings.IndexByte(text, '.'); j > 0 {
|
||||
if def := pp.macros[text[:j]]; def != nil && def.args == nil {
|
||||
return def, text[j:]
|
||||
}
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
// appendSelector glues a selector suffix onto an object macro's expansion:
|
||||
// the selector binds to the identifier the expansion ends with, the way the
|
||||
// toolchain's operand parser reads V0 and .B16 back as one register
|
||||
// spelling. An expansion that does not end in an identifier carries the
|
||||
// selector as its own token, which the parser then reports where it cannot
|
||||
// parse it.
|
||||
func appendSelector(body []token.Token, suffix string, pos token.Position) []token.Token {
|
||||
if n := len(body); n > 0 && body[n-1].Kind == token.Ident {
|
||||
body[n-1].Text += suffix
|
||||
return body
|
||||
}
|
||||
return append(body, token.Token{Kind: token.Ident, Text: suffix, Pos: pos, End: pos})
|
||||
}
|
||||
|
||||
// parameterSelector reports the argument a compound body token names: the
|
||||
// parameter whose whole name occupies the text before the token's FIRST
|
||||
// period, with the selector that follows. k is negative when no parameter
|
||||
// matches, which leaves tokens like AB.S4 untouched even though a parameter
|
||||
// A is bound.
|
||||
func parameterSelector(text string, args []string) (int, string) {
|
||||
j := strings.IndexByte(text, '.')
|
||||
if j <= 0 {
|
||||
return -1, ""
|
||||
}
|
||||
if k := slices.Index(args, text[:j]); k >= 0 {
|
||||
return k, text[j:]
|
||||
}
|
||||
return -1, ""
|
||||
}
|
||||
|
||||
// pasteSelector joins an argument with the selector a compound body token
|
||||
// carries, textually: the selector binds to the identifier the argument
|
||||
// ends with, so A.S4 over the argument V0.B16 spells V0.B16.S4, exactly the
|
||||
// operand the toolchain's split-then-substitute leaves behind. An argument
|
||||
// with no trailing identifier carries the selector as a separate token,
|
||||
// which the parser then reports where it cannot parse it.
|
||||
func pasteSelector(val []token.Token, suffix string) []token.Token {
|
||||
if len(val) == 0 {
|
||||
return []token.Token{{Kind: token.Ident, Text: suffix}}
|
||||
}
|
||||
out := slices.Clone(val)
|
||||
if n := len(out); out[n-1].Kind == token.Ident {
|
||||
out[n-1].Text += suffix
|
||||
return out
|
||||
}
|
||||
return append(out, token.Token{Kind: token.Ident, Text: suffix})
|
||||
}
|
||||
|
||||
// collectArgs reads the actual argument tokens of an invocation; the opening
|
||||
// parenthesis is at start. Commas separate arguments except inside nested
|
||||
// parentheses. A nil result means the list was unterminated, which is a
|
||||
|
||||
Reference in New Issue
Block a user