Skip to content

Commit

Permalink
Merge pull request #1874 from kujirahand/fix_array_sytax_ref_array
Browse files Browse the repository at this point in the history
配列記法の後ろの配列アクセスに失敗する問題を修正 #1858
  • Loading branch information
kujirahand authored Dec 20, 2024
2 parents 981e8e2 + 381672f commit 11da558
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
13 changes: 11 additions & 2 deletions core/src/nako_gen.mts
Original file line number Diff line number Diff line change
Expand Up @@ -1050,9 +1050,18 @@ export class NakoGen {
}

convRefArray (node: Ast): string {
const name = this._convGen(node.name as Ast, true)
let code = ''
if (node.name === '__ARRAY__') {
const a = node.index?.shift()
if (a) {
code = this._convGen(a, true)
} else {
code = '[]'
}
} else {
code = this._convGen(node.name as Ast, true)
}
const list: Ast[] | undefined = node.index
let code = name
if (!list) { return code }
for (let i = 0; i < list.length; i++) {
const idx = this._convGen(list[i] as Ast, true)
Expand Down
26 changes: 25 additions & 1 deletion core/src/nako_parser3.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2261,7 +2261,9 @@ export class NakoParser extends NakoParserBase {
this.checkArrayIndex(this.y[1]),
this.checkArrayIndex(this.y[3])
]
const aa = ast.index.pop()
ast.index = this.checkArrayReverse(index)
if (aa) { ast.index.unshift(aa) }
ast.josi = this.y[4].josi
return this.y[4].josi === '' // 助詞があればそこで終了(false)を返す
}
Expand All @@ -2273,7 +2275,9 @@ export class NakoParser extends NakoParserBase {
this.checkArrayIndex(this.y[3]),
this.checkArrayIndex(this.y[5])
]
const aa = ast.index.pop()
ast.index = this.checkArrayReverse(index)
if (aa) { ast.index.unshift(aa) }
ast.josi = this.y[6].josi
return this.y[6].josi === '' // 助詞があればそこで終了(false)を返す
}
Expand Down Expand Up @@ -2496,8 +2500,28 @@ export class NakoParser extends NakoParserBase {
return a
}

yJSONArray(): AstBlocks | Ast | null {
// 配列を得る
const a = this.yJSONArrayRaw()
if (!a) { return null }
// 配列の直後に@や[]があるか?助詞がある場合には、別の引数の可能性があるので無視。 (例) [0,1,2]を[3,4,5]に配列***
if (a.josi === '' && this.checkTypes(['@', '['])) {
const ast: Ast = {
type: 'ref_array',
name: '__ARRAY__',
index: [a],
josi: '',
line: a.line,
end: this.peekSourceMap()
}
this.yValueWordGetIndex(ast)
return ast
}
return a
}

/** @returns {AstBlocks | null} */
yJSONArray (): AstBlocks | null {
yJSONArrayRaw (): AstBlocks | null {
const map = this.peekSourceMap()
if (this.accept(['[', ']'])) {
return {
Expand Down
10 changes: 10 additions & 0 deletions core/test/array_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ describe('array_test', async () => {
await cmp('A=[[0,1],[2,3]]。A[1]と[2]を連続表示', '2,32')
await cmp('A=[[0,1],[2,3]]。0にA[1]の[9,9]を配列一括挿入してJSONエンコードして表示', '[9,9,2,3]')
})
it('『["a","b","c","d"]@2を表示』がエラー #1858', async () => {
await cmp('[0,1,2,3,4,5]@2を表示', '2')
await cmp('["a","b","c"]@2を表示', 'c')
await cmp('[[0,1,2],[3,4,5]]@1,0を表示', '3')
})
it('『["a","b","c","d"]@2を表示』がエラー #1858', async () => {
await cmp('[0,1,2,3,4,5][2]を表示', '2')
await cmp('["a","b","c"][2]を表示', 'c')
await cmp('[[0,1,2],[3,4,5]][1,0]を表示', '3')
})
})

0 comments on commit 11da558

Please sign in to comment.