Skip to content

Commit

Permalink
対象がローカル変数になってしまっている問題を修正 #1723
Browse files Browse the repository at this point in the history
  • Loading branch information
kujirahand committed Aug 16, 2024
1 parent 132d394 commit 9fe4e3e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 38 deletions.
29 changes: 21 additions & 8 deletions bundle_esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,42 @@ const files = [
]
const outdir = path.join(__dirname, 'release')
const watch = process.argv.includes('--watch')
const filesFullpath = files.map((f) => path.join(__dirname, f))

// create outdir
if (!fs.existsSync(outdir)) {
fs.mkdirSync(outdir)
console.log(`[esbuild] created ${outdir}`)
}
// build options
const options = {
entryPoints: files,
bundle: true,
outdir,
minify: true,
sourcemap: true,
}
if (!watch) {
await esbuild.build(options)
for (const f of files) {
const outfile = path.join(outdir, path.basename(f).replace(/\.(mjs|mts|jsx)$/, '.js'))
const options = {
entryPoints: [f],
bundle: true,
outfile,
minify: true,
sourcemap: true,
}
console.log('[esbuild] build start', options)
await esbuild.build(options)
}
// 例外的なコピー
const src = path.join(outdir, 'edit_main.js')
if (fs.existsSync(src)) {
fs.copyFileSync(src, path.join(outdir, 'editor.js'))
fs.copyFileSync(path.join(outdir, 'version_main.js'), path.join(outdir, 'version.js'))
}
} else {
// TODO: watch がうまく動かない
const options = {
entryPoints: filesFullpath,
bundle: true,
outdir,
minify: true,
sourcemap: true,
}
const ctx = await esbuild.context(options)
await ctx.watch()
}
4 changes: 2 additions & 2 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nadesiko3core",
"version": "3.6.13",
"version": "3.6.14",
"description": "Japanese Programming Language Nadesiko v3 core",
"main": "index.mjs",
"type": "module",
Expand Down Expand Up @@ -47,7 +47,7 @@
"@typescript-eslint/eslint-plugin": "^5.60.0",
"eslint-config-standard-with-typescript": "^35.0.0",
"mocha": "^10.2.0",
"typescript": "^5.1.3"
"typescript": "^5.5.3"
},
"dependencies": {
"cross-env": "^7.0.3"
Expand Down
4 changes: 2 additions & 2 deletions core/src/nako_core_version.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export interface NakoCoreVersion {
}
// 実際のバージョン定義 (自動生成されるので以下を編集しない)
const coreVersion: NakoCoreVersion = {
version: '3.6.9',
version: '3.6.14',
major: 3,
minor: 6,
patch: 9
patch: 14
}
export default coreVersion
27 changes: 18 additions & 9 deletions core/src/nako_gen.mts
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,21 @@ export class NakoGen {
*/
varname_set (name: string, jsvalue: string): string {
if (this.varslistSet.length === 3) {
// グローバル
return `__self.__varslist[${2}].set(${JSON.stringify(name)}, (${jsvalue}))`
return `__self.__varslist[2].set(${JSON.stringify(name)}, (${jsvalue}))`
} else {
// ローカル
return `__self.__vars.set(${JSON.stringify(name)}, (${jsvalue}))`
}
}

/**
* ローカル変数の設定用JavaScriptコードを生成する。
* @param {string} name
* @param {string} jsvalue
*/
varname_set_sys(name: string, jsvalue: string): string {
return `__self.__setSysVar(${JSON.stringify(name)}, (${jsvalue}))`
}

/**
* @param {string} name
* @returns {string}
Expand Down Expand Up @@ -1145,20 +1152,22 @@ export class NakoGen {
const loopKeyVar = `$nako_i${id}`
const loopValueVar = `$nako_foreach_value${id}`
const loopDataVar = `$nako_foreach_data${id}`

// 「対象」「対象キー」を取得 --- blockより早く変数を定義する必要がある
let valueVar = '対象'
let taisyoPrefex = this.varname_set_sys('対象', loopValueVar);
if (node.name) { // 対象変数がある場合、対象は設定されない
valueVar = '' + (node.name as Ast).value
const valueVar = '' + (node.name as Ast).value
this.varsSet.names.add(valueVar)
taisyoPrefex = this.varname_set(valueVar, loopValueVar)
}
this.varsSet.names.add(valueVar)
const keyVar = '対象キー'
this.varsSet.names.add(keyVar)
const keySetter = this.varname_set(keyVar, loopKeyVar)
const keySetter = this.varname_set_sys(keyVar, loopKeyVar)
// 「それ」(対象のエイリアス)の設定
let sorePrefex = ''
if (this.speedMode.invalidSore === 0) {
sorePrefex = this.varname_set('それ', loopValueVar)
}

// 反復するデータを取得
let targetData = ''
if (node.target === null) {
Expand Down Expand Up @@ -1189,7 +1198,7 @@ export class NakoGen {
' // 対象の設定\n' +
` let ${loopValueVar} = $nako_foreach_data${id}[${loopKeyVar}]\n` +
` ${sorePrefex};\n` +
` ${this.varname_set(valueVar, loopValueVar)}\n` +
` ${taisyoPrefex};\n` +
' // [convForeach::block]\n' +
` ${block}\n` +
' // [/convForeach::block]\n' +
Expand Down
3 changes: 3 additions & 0 deletions core/test/flow_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,7 @@ describe('flow_test', async () => {
await cmp('N=「」;1から3の範囲を繰り返す\nN=N&それ;\nここまで;Nを表示。', '123')
await cmp('N=「」;0…9を繰り返す\nN=N&それ;\nここまで;Nを表示。', '0123456789')
})
it('対象がローカル変数になっていた問題 #1723', async () => {
await cmp('関数 Fとは; [1]を反復; ここまで。ここまで;対象=50;F;対象を表示。', '1')
})
})
37 changes: 20 additions & 17 deletions core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
{
"ts-node": {
"esm": true,
"require": [
"tsconfig-paths/register"
]
},
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Projects */
// "incremental": true, /* Enable incremental compilation */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
Expand All @@ -17,8 +10,8 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"target": "es2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": ["dom"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
Expand All @@ -30,14 +23,15 @@
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */

/* Modules */
"module": "NodeNext", /* Specify what module code is generated. */
"module": "ESNext", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "NodeNext", /* Specify how TypeScript looks up a file from a given module specifier. */
"baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
"paths": {
},
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
"typeRoots": ["src/@types", "node_modules/@types"], /* Specify multiple folders that act like `./node_modules/@types`. */
"types": ["node"], /* Specify type package names to be included without being referenced in a source file. */
"typeRoots": ["src/@types","node_modules/@types",], /* Specify multiple folders that act like `./node_modules/@types`. */
"types": ["node", "mocha"], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "resolveJsonModule": true, /* Enable importing .json files */
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
Expand Down Expand Up @@ -102,7 +96,16 @@

/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
/*
"allowImportingTsExtensions": true,
"noEmit": true
*/
},
"exclude": ["deno/*"]
/* "extends": "./core/tsconfig.json", */
"exclude": ["core/deno/*", "deno/*", "utils/*", "core/sample/*"],
"include": ["core/*", "core/src/*", "src/*"],
"ts-node": {
"esm": true
}
}

0 comments on commit 9fe4e3e

Please sign in to comment.