theprogrammablemind 9.7.1 → 9.8.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/semantics.js CHANGED
@@ -75,13 +75,16 @@ class Semantic {
75
75
  async matches (args, context, options = {}) {
76
76
  args = {...args}
77
77
  this.fixUpArgs(args, context)
78
- const matches = await this.matcher(args)
79
- if (matches && (options.debug || {}).match || args.callId === this.callId) {
80
- // next line is the matcher
81
- debugger // eslint-disable-line no-debugger
82
- await this.matcher(args)
83
- }
84
- return matches
78
+ return await debug.hitScoped(args.callId, async () => {
79
+ const matches = await this.matcher(args)
80
+ if (matches) {
81
+ if ((options.debug || {}).match || args.callId === this.callId || process.env.DEBUG_CALLID == args.callId) {
82
+ debugger // eslint-disable-line no-debugger
83
+ await this.matcher(args) // DEBUG: This is the line you step into
84
+ }
85
+ }
86
+ return matches
87
+ })
85
88
  }
86
89
 
87
90
  async apply (args, context, s, options = {}) {
@@ -97,21 +100,24 @@ class Semantic {
97
100
  const contextPrime = Object.assign({}, context)
98
101
  this.fixUpArgs(args, contextPrime)
99
102
 
100
- if ((options.debug || {}).apply || args.callId === this.callId) {
101
- debugger // eslint-disable-line no-debugger
102
- }
103
- if (args.breakOnSemantics) {
104
- debugger // eslint-disable-line no-debugger
105
- }
106
- try {
107
- args.handlerStack.push(this)
108
- await this._apply(args)
109
- args.handlerStack.pop()
110
- } catch( e ) {
111
- args.handlerStack.pop()
112
- throw e
113
- }
114
- return contextPrime
103
+ return await debug.hitScoped(args.callId, async () => {
104
+ if ((options.debug || {}).apply || args.callId === this.callId || process.env.DEBUG_CALLID == args.callId) {
105
+ debugger // eslint-disable-line no-debugger // STEP ABOUT THREE TIMES TO THE DEBUG LINE
106
+ }
107
+ if (args.breakOnSemantics) {
108
+ debugger // eslint-disable-line no-debugger
109
+ }
110
+
111
+ try {
112
+ args.stack.push(this)
113
+ await this._apply(args) // DEBUG: This is the line you want to step into
114
+ args.stack.pop()
115
+ } catch( e ) {
116
+ args.stack.pop()
117
+ throw e
118
+ }
119
+ return contextPrime
120
+ })
115
121
  }
116
122
  }
117
123
 
@@ -187,6 +193,8 @@ class Semantics {
187
193
  let seenQuestion = false
188
194
  const deferred = []
189
195
  args.log = (message) => { this.logs.push(message) }
196
+ const finallyList = []; // called after the semantic is processed
197
+ args._finally = (call) => finallyList.unshift(call)
190
198
  for (const isemantic in this.semantics) {
191
199
  const semantic = this.semantics[isemantic]
192
200
  // only one question at a time
@@ -247,13 +255,14 @@ class Semantics {
247
255
  const widths = Lines.addRemainder([10, 10])
248
256
  const lines = new Lines(widths)
249
257
  lines.setElement(0, 0, 'Semantic')
258
+ lines.setElement(0, 1, `#${context.context_id || "?"}`)
250
259
  const source = `${semantic.km}/#${semantic.index}`
251
260
  lines.setElement(0, 2, `ERROR while applying (${source}) ${semantic.toLabel()}`)
252
261
  lines.newRow()
253
262
  lines.setElement(0, 2, semantic.toString())
254
263
  lines.newRow()
255
264
  lines.setElement(0, 1, 'DEBUG')
256
- lines.setElement(0, 2, `To debug this use args.callId === '${args.calls.current()}'`)
265
+ lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') in the code or DEBUG_CALLID="${args.calls.current()}"`)
257
266
  lines.newRow()
258
267
  lines.setElement(0, 1, 'TO')
259
268
  lines.setElement(0, 2, `context_id: ${context.context_id}`)
@@ -277,6 +286,7 @@ class Semantics {
277
286
  const widths = Lines.addRemainder([10, 10])
278
287
  const lines = new Lines(widths)
279
288
  lines.setElement(0, 0, 'Semantic')
289
+ lines.setElement(0, 1, `#${context.context_id || "?"}`)
280
290
  if (semantic.index > -1 && semantic.km) {
281
291
  // lines.setElement(0, 2, `KM '${semantic.km}' ordinal: ${semantic.index} ${ semantic.notes ? semantic.nodes : '' }`)
282
292
  lines.setElement(0, 2, semantic.toLabel())
@@ -288,7 +298,7 @@ class Semantics {
288
298
  lines.setElement(0, 2, semantic.toString())
289
299
  lines.newRow()
290
300
  lines.setElement(0, 1, 'DEBUG')
291
- lines.setElement(0, 2, `To debug this use args.callId === '${args.calls.current()}'`)
301
+ lines.setElement(0, 2, `To debug this use debug.breakAt('${args.calls.current()}') in the code or DEBUG_CALLID="${args.calls.current()}"`)
292
302
  lines.newRow()
293
303
  lines.setElement(0, 1, 'TO')
294
304
  lines.setElement(0, 2, `context_id: ${context.context_id}`)
@@ -319,11 +329,15 @@ class Semantics {
319
329
  }
320
330
  counter += 1
321
331
  }
332
+ for (const f of finallyList) {
333
+ await f()
334
+ }
322
335
  args.calls.pop()
323
336
  if (!applied && debug) {
324
337
  const widths = Lines.addRemainder([10, 10])
325
338
  const lines = new Lines(widths)
326
339
  lines.setElement(0, 0, 'Semantic')
340
+ lines.setElement(0, 1, `#${context.context_id || "?"}`)
327
341
  lines.setElement(0, 2, 'No semantic applied')
328
342
  lines.newRow()
329
343
  lines.setElement(0, 1, 'STACK')