Skip to content

Commit

Permalink
計算結果を手軽に表示する「?? 計算式文」を実装する #1745
Browse files Browse the repository at this point in the history
  • Loading branch information
kujirahand committed Sep 22, 2024
1 parent d6da6a0 commit d1e59e3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/src/nako_lex_rules.mts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const rules: NakoLexRule[] = [
{ name: '(', pattern: /^\(/ },
{ name: ')', pattern: /^\)/, readJosi: true },
{ name: '|', pattern: /^\|/ },
{ name: '??', pattern: /^\?\?/ }, // 「表示」のエイリアス #1745
{ name: 'string', pattern: /^🌿/, cbParser: src => cbString('🌿', '🌿', src) },
{ name: 'string_ex', pattern: /^🌴/, cbParser: src => cbString('🌴', '🌴', src) },
{ name: 'string_ex', pattern: /^「/, cbParser: src => cbString('「', '」', src) },
Expand Down
31 changes: 30 additions & 1 deletion core/src/nako_parser3.mts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export class NakoParser extends NakoParserBase {
if (this.check('エラー監視')) { return this.yTryExcept() }
if (this.accept(['抜ける'])) { return { type: 'break', josi: '', ...map, end: this.peekSourceMap() } }
if (this.accept(['続ける'])) { return { type: 'continue', josi: '', ...map, end: this.peekSourceMap() } }
if (this.check('??')) { return this.yPrint() }
// 実行モードの指定
if (this.accept(['DNCLモード'])) { return this.yDNCLMode(1) }
if (this.accept(['DNCL2モード'])) { return this.yDNCLMode(2) }
Expand Down Expand Up @@ -671,7 +672,7 @@ export class NakoParser extends NakoParserBase {
* @param kara
* @returns {AstCallFunc | null}
*/
yRange(kara: Ast): AstCallFunc | null {
yRange (kara: Ast): AstCallFunc | null {
// 範囲オブジェクト?
if (!this.check('…')) { return null }
const map = this.peekSourceMap()
Expand All @@ -694,6 +695,34 @@ export class NakoParser extends NakoParserBase {
}
}

/**
* 表示(関数)を返す 「??」のエイリアスで利用 (#1745)
* @returns {AstCallFunc | null}
*/
yPrint (): AstCallFunc | null {
const map = this.peekSourceMap()
const t = this.get() // skip '??'
if (!t || t.value !== '??') {
throw NakoSyntaxError.fromNode('『??』で指定してください。', map)
}
const arg: Ast|null = this.yGetArg()
if (!arg) {
throw NakoSyntaxError.fromNode('『??(計算式)』で指定してください。', map)
}
const meta = this.funclist.get('表示')
if (!meta) { throw new Error('関数『表示』が見つかりません。plugin_systemをシステムに追加してください。') }
return {
type: 'func',
name: '表示',
blocks: [arg],
josi: '',
meta,
asyncFn: false,
...map,
end: this.peekSourceMap()
}
}

yGetArg (): Ast|null {
// 値を一つ読む
const value1 = this.yValue()
Expand Down
1 change: 1 addition & 0 deletions core/src/nako_token.mts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export type TokenType = '?'
| '厳チェック' // 厳しくチェック (#1698)
| '」' // error - エラーチェックのため
| '』' // error - エラーチェックのため
| '??' // 「表示」のエイリアス

// トークン
export interface Token {
Expand Down
7 changes: 6 additions & 1 deletion core/test/plugin_system_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('plugin_system_test', async () => {
const g = await nako.runAsync(code, 'main.nako3')
assert.strictEqual(g.log, res)
}
const cmpex = async (/** @type {string} */ code, /** @type { name: String, message: string } */ exinfo) => {
const cmpex = async (/** @type {string} */ code, /** @type { name: string, message: string } */ exinfo) => {
const nako = new NakoCompiler()
nako.getLogger().debug('code=' + code)
try {
Expand Down Expand Up @@ -693,4 +693,9 @@ describe('plugin_system_test', async () => {
await cmp('A=[0,1,2,3];Aから0...5を参照してJSONエンコードして表示', '[0,1,2,3]') // 範囲を超えて指定もエラーにならない
await cmp('A=[0,1,2,3];Aから5...9を参照してJSONエンコードして表示', '[]') // 範囲を超えて指定もエラーにならない
})
it('「?? 計算式文」 #1745', async () => {
await cmp('??1+1', '2')
await cmp('??1+2*3', '7')
await cmp('??(1+2)*3', '9')
})
})

0 comments on commit d1e59e3

Please sign in to comment.