Skip to content

Commit

Permalink
範囲オブジェクト記法『A…B』を追加 #1704
Browse files Browse the repository at this point in the history
  • Loading branch information
kujirahand committed Jul 28, 2024
1 parent 6e38afb commit 5cb6dc8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/src/nako_lex_rules.mts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ export const rules: NakoLexRule[] = [
{ name: 'range_comment', pattern: /^\/\*/, cbParser: cbRangeComment },
{ name: 'def_test', pattern: /^:/ },
{ name: 'def_func', pattern: /^/ },
{ name: '…', pattern: /^/ }, // 範囲オブジェクト(#1704)
{ name: '…', pattern: /^\.{2,3}/ }, // 範囲オブジェクト(#1704)
// 多倍長整数リテラルの判定。整数の末尾に「n」がついているだけな為、数値判定より上に書かないとただの整数にされる
{ name: 'bigint', pattern: /^0[xX][0-9a-fA-F]+(_[0-9a-fA-F]+)*n/, readJosi: true },
{ name: 'bigint', pattern: /^0[oO][0-7]+(_[0-7]+)*n/, readJosi: true },
{ name: 'bigint', pattern: /^0[bB][0-1]+(_[0-1]+)*n/, readJosi: true },
{ name: 'bigint', pattern: /^\d+(_\d+)*?n/, readJosi: true },
// 数値の判定 --- この後nako_lexerにて単位を読む処理が入る(#994)
// 16進/8進/2進法の数値判定 --- この後nako_lexerにて単位を読む処理が入る(#994)
{ name: 'number', pattern: /^0[xX][0-9a-fA-F]+(_[0-9a-fA-F]+)*/, readJosi: true, cb: parseNumber },
{ name: 'number', pattern: /^0[oO][0-7]+(_[0-7]+)*/, readJosi: true, cb: parseNumber },
{ name: 'number', pattern: /^0[bB][0-1]+(_[0-1]+)*/, readJosi: true, cb: parseNumber },
// 下の三つは小数点が挟まっている場合、小数点から始まっている場合、小数点がない場合の十進法の数値にマッチします
// 下の三つは小数点が挟まっている場合、小数点から始まっている場合、小数点がない場合の十進法の数値にマッチする
{ name: 'number', pattern: /^\d+(_\d+)*\.(\d+(_\d+)*)?([eE][+|-]?\d+(_\d+)*)?/, readJosi: true, cb: parseNumber },
{ name: 'number', pattern: /^\.\d+(_\d+)*([eE][+|-]?\d+(_\d+)*)?/, readJosi: true, cb: parseNumber },
{ name: 'number', pattern: /^\d+(_\d+)*([eE][+|-]?\d+(_\d+)*)?/, readJosi: true, cb: parseNumber },
Expand Down
21 changes: 21 additions & 0 deletions core/src/nako_parser3.mts
Original file line number Diff line number Diff line change
Expand Up @@ -617,10 +617,31 @@ export class NakoParser extends NakoParserBase {
return this.infixToAST(args)
}

yRange (kara: Ast): Ast|null {
// 範囲オブジェクト?
if (!this.check('…')) { return null }
const map = this.peekSourceMap()
this.get() // skip '…'
const made = this.yValue()
if (!kara || !made) {
throw NakoSyntaxError.fromNode('範囲オブジェクトの指定エラー。『A…B』の書式で指定してください。', map)
}
return {
type: 'func',
name: '範囲',
args: [kara, made],
josi: made.josi,
...map,
end: this.peekSourceMap()
}
}

yGetArg (): Ast|null {
// 値を一つ読む
const value1 = this.yValue()
if (value1 === null) { return null }
// 範囲オブジェクト?
if (this.check('…')) { return this.yRange(value1) }
// 計算式がある場合を考慮
return this.yGetArgOperator(value1)
}
Expand Down
1 change: 1 addition & 0 deletions core/src/nako_types.mts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export interface NakoEvent {
callback: (event: any) => void
}

/// 範囲オブジェクト (#1704)
export interface RangeObject {
先頭: number,
末尾: number,
Expand Down
5 changes: 5 additions & 0 deletions core/test/literal_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ describe('literal_test', async () => {
await cmp('-0b10111010110nの変数型確認して表示', 'bigint')
await cmp('123_456_789nの変数型確認して表示', 'bigint')
})
it('範囲オブジェクトのテスト #1704', async () => {
await cmp('A=1…5;「{A@"先頭"}:{A@"末尾"}」を表示', '1:5')
await cmp('A=1...5;「{A@"先頭"}:{A@"末尾"}」を表示', '1:5')
await cmp('A=1;B=5;C=A...B;「{C@"先頭"}:{C@"末尾"}」を表示', '1:5')
})
})

0 comments on commit 5cb6dc8

Please sign in to comment.