(value: Value, literalLike = false, processedObjects = new Set<object>())
| 174 | * @param literalLike - `true` なら出力をリテラルに似せる |
| 175 | */ |
| 176 | export function reprValue(value: Value, literalLike = false, processedObjects = new Set<object>()): string { |
| 177 | if ((value.type === 'arr' || value.type === 'obj') && processedObjects.has(value.value)) { |
| 178 | return '...'; |
| 179 | } |
| 180 | |
| 181 | if (literalLike && value.type === 'str') return '"' + value.value.replace(/["\\\r\n]/g, x => `\\${x}`) + '"'; |
| 182 | if (value.type === 'str') return value.value; |
| 183 | if (value.type === 'num') return value.value.toString(); |
| 184 | if (value.type === 'arr') { |
| 185 | processedObjects.add(value.value); |
| 186 | const content = []; |
| 187 | |
| 188 | for (const item of value.value) { |
| 189 | content.push(reprValue(item, true, processedObjects)); |
| 190 | } |
| 191 | |
| 192 | return '[ ' + content.join(', ') + ' ]'; |
| 193 | } |
| 194 | if (value.type === 'obj') { |
| 195 | processedObjects.add(value.value); |
| 196 | const content = []; |
| 197 | |
| 198 | for (const [key, val] of value.value) { |
| 199 | content.push(`${key}: ${reprValue(val, true, processedObjects)}`); |
| 200 | } |
| 201 | |
| 202 | return '{ ' + content.join(', ') + ' }'; |
| 203 | } |
| 204 | if (value.type === 'bool') return value.value.toString(); |
| 205 | if (value.type === 'null') return 'null'; |
| 206 | if (value.type === 'fn') { |
| 207 | if (value.native) { |
| 208 | // そのうちネイティブ関数の引数も表示できるようにしたいが、ホスト向けの破壊的変更を伴うと思われる |
| 209 | return '@( ?? ) { native code }'; |
| 210 | } else { |
| 211 | return `@( ${(value.params.map(v => v.dest.type === 'identifier' ? v.dest.name : '?')).join(', ')} ) { ... }`; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return '?'; |
| 216 | } |
no test coverage detected