gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/plugins/core/AST.hl

13.9 KB

  1. // hl:core/AST — WHAT A FILE READS AND WHAT IT WRITES, from the compiler's own AST.
  2. //
  3. // The language has always been able to hand a caller the SYNTAX of a node
  4. // (`hlSyntax`) and the list of members a file declares (`hlMembers`). What it
  5. // never answered is what that syntax DOES with names: which members an
  6. // expression reads, which members a handler writes. Every consumer that needed
  7. // it wrote its own walk — hl:web's tree builder decides per tag what counts as
  8. // a read, and its browser half rediscovers the same set at runtime by walking
  9. // the DOM against the tree and JSON-comparing state afterwards (FRAMEWORK_AUDIT
  10. // §3). Two copies of one answer drift, and neither could see a write at all.
  11. //
  12. // The answer is the language's, so it lives here. `query(file)` is a table of
  13. // one file's (and, recursively, its project's) members, derivations, handlers
  14. // and methods, each with the names it reads and the names it writes, every node
  15. // carrying a stable site id `file:line:col`.
  16. //
  17. // NOTHING HERE KNOWS WHAT A VIEW IS. There is no attr, text, region, key or
  18. // component vocabulary in this file and none in the runtime rule beneath it: a
  19. // member is a member. A caller that wants one member's answer — hl:web wants
  20. // `View`'s — passes that member's NAME as the filter. That is the only way to
  21. // narrow the query, and it is a name, not a concept.
  22. //
  23. // import { query } from 'hl:core/AST'
  24. // let q = query('components/home.hl') // recursive, every member
  25. // let v = query('components/home.hl', true, 'View')
  26. //
  27. // THE GENERATION. Reflection reads a parse that is valid until the next
  28. // analysis; this package runs one of its own (`hlProject()`) the first time it
  29. // is asked and keeps the handle. `refresh()` runs a new one — a dev reload, or
  30. // a caller that changed a file. A static cannot be reassigned, so the handle
  31. // lives in a static hybrid, which can.
  32. static state = { gen = 0; graph = null; }
  33. // The live analysis generation, started on first use.
  34. static analysis = () => {
  35. if (state.gen == 0) {
  36. let g = hlProject()
  37. state.graph = g
  38. state.gen = g.analysisGen
  39. }
  40. return state.gen
  41. }
  42. // A NEW ANALYSIS: the parses this package answers from are replaced, and every
  43. // handle a caller still holds from the old one is refused by the language.
  44. static refresh = () => {
  45. let g = hlProject()
  46. state.graph = g
  47. state.gen = g.analysisGen
  48. return state.gen
  49. }
  50. // …of a project this run did not boot: a directory, a manifest, or an entry
  51. // file. The keys every answer below is filed under are that project's.
  52. static analyseFrom = (path) => {
  53. let g = hlProject(path)
  54. state.graph = g
  55. state.gen = g.analysisGen
  56. return state.gen
  57. }
  58. // The project graph the generation belongs to (`hlProject()`'s answer).
  59. static project = () => {
  60. analysis()
  61. return state.graph
  62. }
  63. // ---- the reflection surface, without the generation argument ------------------
  64. // `hlSyntax` and `hlMembers` are the ambient names the framework still calls;
  65. // here they are statics of a package, which is where they belong.
  66. static syntax = (file, node) => { return hlSyntax(analysis(), file, node) }
  67. static members = (file) => { return hlMembers(analysis(), file) }
  68. static methods = (file) => { return hlMethods(analysis(), file) }
  69. static handlers = (file) => { return hlEvents(analysis(), file).handles }
  70. static denotes = (file, name) => { return hlDenotes(analysis(), file, name) }
  71. static file = (file) => { return hlFile(analysis(), file) }
  72. // ---- the syntax of one node, as a tree ----------------------------------------
  73. // Every node carries its tag, its site id and the two answers the language gives
  74. // for it: the free names it READS and the names it WRITES. The shape below is
  75. // the shape `hlSyntax` returns, followed one child at a time — there is no
  76. // interpretation of any tag, only its children.
  77. //
  78. // AN `on` HANDLER IS A LEAF HERE ON PURPOSE. Its body is not evaluated by the
  79. // literal that carries it, so the names inside are the HANDLER's reads and
  80. // writes, not the literal's; the node answers for them and the walk stops.
  81. static tree = (file, node) => {
  82. let n = hlSyntax(analysis(), file, node)
  83. if (n == null) { return null }
  84. let out = { tag = n.tag; site = n.site; line = n.line; col = n.col; reads = n.reads; writes = n.writes; }
  85. if (n.tag == 'object_expression') {
  86. let kids = []
  87. for (h of n.entries) { kids.push(tree(file, h)) }
  88. out.entries = kids
  89. } else if (n.tag == 'object_property') {
  90. out.key = n.key
  91. out.value = tree(file, n.value)
  92. } else if (n.tag == 'view_for') {
  93. // A `for` ENTRY NAMES ITS LIST AND ITS ROW VARIABLE. The list is read in
  94. // the scope standing around the `for`; the row variable is bound inside
  95. // the body and every read of it there resolves as kind 'row'.
  96. out.row = n.varName
  97. out.list = tree(file, n.list)
  98. out.body = tree(file, n.body)
  99. } else if (n.tag == 'view_if') {
  100. out.condition = tree(file, n.condition)
  101. out.consequent = tree(file, n.consequent)
  102. out.alternate = tree(file, n.alternate)
  103. } else if (n.tag == 'on_statement') {
  104. out.event = n.event
  105. out.scope = n.scope
  106. out.realm = n.realm
  107. out.handler = n.handler
  108. } else if (n.tag == 'identifier' || n.tag == 'new_expression') {
  109. out.name = n.name
  110. } else if (n.tag == 'literal') {
  111. out.kind = n.kind
  112. out.text = n.text
  113. } else if (n.tag == 'member_expression') {
  114. out.object = tree(file, n.object)
  115. out.property = tree(file, n.property)
  116. out.computed = n.computed
  117. } else if (n.tag == 'call_expression') {
  118. out.callee = tree(file, n.callee)
  119. let args = []
  120. for (h of n.args) { args.push(tree(file, h)) }
  121. out.args = args
  122. } else if (n.tag == 'binary_expression') {
  123. out.operator = n.operator
  124. out.left = tree(file, n.left)
  125. out.right = tree(file, n.right)
  126. } else if (n.tag == 'unary_expression') {
  127. out.operator = n.operator
  128. out.operand = tree(file, n.operand)
  129. } else if (n.tag == 'conditional_expression') {
  130. out.condition = tree(file, n.condition)
  131. out.consequent = tree(file, n.consequent)
  132. out.alternate = tree(file, n.alternate)
  133. } else if (n.tag == 'array_expression') {
  134. let els = []
  135. for (h of n.elements) { els.push(tree(file, h)) }
  136. out.elements = els
  137. }
  138. return out
  139. }
  140. // ---- names ---------------------------------------------------------------------
  141. // A read or a write is `{ name, kind, isStatic, path?, key?, browser? }`. `kind` is the
  142. // language's own resolution of the bare name: 'member', 'method', 'handler',
  143. // 'class' (a default import), 'import' (a braced one), 'plugin', 'row' (bound by
  144. // an enclosing `for` entry), 'local' (a lambda, handler or method parameter, or
  145. // a `let`), 'none' (nothing this file declares — an ambient or a typo).
  146. // `browser` is only on kind 'plugin' (a name an hl: import binds): true when the browser has a side for
  147. // that package (a client.hl or a client.js: hl:time, hl:web), false when it has none (hl:proc, hl:fs).
  148. // It comes from the same check the JavaScript target uses, so a name from a package with no browser
  149. // side can be counted as server data.
  150. // the names of a read/write list, as plain strings
  151. static namesOf = (list) => {
  152. let out = []
  153. for (r of list) { if (!out.includes(r.name)) { out.push(r.name) } }
  154. return out
  155. }
  156. // the subset of a read/write list that resolves to a member of the file itself
  157. static membersOf = (list) => {
  158. let out = []
  159. for (r of list) {
  160. if (r.kind == 'member' && !r.isStatic && !out.includes(r.name)) { out.push(r.name) }
  161. }
  162. return out
  163. }
  164. // ---- one file's answer ---------------------------------------------------------
  165. // every `on` node inside a member's syntax tree, flattened, each at its site
  166. static nestedHandlers = (member, node, &into) => {
  167. if (node == null) { return into }
  168. if (node.tag == 'on_statement') {
  169. into.push({ member = member; event = node.event; scope = node.scope; realm = node.realm; site = node.site; reads = node.reads; writes = node.writes; nested = true; })
  170. return into
  171. }
  172. if (node.entries != null) { for (c of node.entries) { nestedHandlers(member, c, &into) } }
  173. if (node.args != null) { for (c of node.args) { nestedHandlers(member, c, &into) } }
  174. if (node.elements != null) { for (c of node.elements) { nestedHandlers(member, c, &into) } }
  175. if (node.value != null) { nestedHandlers(member, node.value, &into) }
  176. if (node.list != null) { nestedHandlers(member, node.list, &into) }
  177. if (node.body != null) { nestedHandlers(member, node.body, &into) }
  178. if (node.condition != null) { nestedHandlers(member, node.condition, &into) }
  179. if (node.consequent != null) { nestedHandlers(member, node.consequent, &into) }
  180. if (node.alternate != null) { nestedHandlers(member, node.alternate, &into) }
  181. if (node.object != null) { nestedHandlers(member, node.object, &into) }
  182. if (node.property != null) { nestedHandlers(member, node.property, &into) }
  183. if (node.callee != null) { nestedHandlers(member, node.callee, &into) }
  184. if (node.left != null) { nestedHandlers(member, node.left, &into) }
  185. if (node.right != null) { nestedHandlers(member, node.right, &into) }
  186. if (node.operand != null) { nestedHandlers(member, node.operand, &into) }
  187. return into
  188. }
  189. static one = (key, member) => {
  190. let gen = analysis()
  191. let f = hlFile(gen, key)
  192. let out = { key = key; class = f.class; members = []; derivations = []; handlers = []; methods = []; reaches = []; }
  193. for (m of hlMembers(gen, key)) {
  194. // (Hybriel has no `continue`: the body is guarded instead.)
  195. if (member == null || m.name == member) {
  196. let entry = { name = m.name; type = m.type; mandatory = m.mandatory; isStatic = m.isStatic; site = key + ':' + m.line + ':' + m.col; default = null; reads = []; writes = []; syntax = null; }
  197. if (m.hasValue) {
  198. let n = hlSyntax(gen, key, m.node)
  199. entry.default = n.site
  200. entry.reads = n.reads
  201. entry.writes = n.writes
  202. entry.syntax = tree(key, m.node)
  203. // A DERIVATION IS AN INITIALIZER READ THE OTHER WAY: the same table,
  204. // listed by the names the value depends on.
  205. out.derivations.push({ name = m.name; site = n.site; reads = n.reads; })
  206. nestedHandlers(m.name, entry.syntax, &out.handlers)
  207. for (r of n.reads) {
  208. if (r.kind == 'class' && r.key != null && !out.reaches.includes(r.key)) { out.reaches.push(r.key) }
  209. }
  210. }
  211. out.members.push(entry)
  212. }
  213. }
  214. // The DECLARED handlers and methods answer for their whole body. A member
  215. // filter narrows the member table only — a handler is not a member, and
  216. // hiding the writes would make the filtered answer a lie about the file.
  217. for (h of hlEvents(gen, key).handles) {
  218. let n = hlSyntax(gen, key, h.node)
  219. out.handlers.push({ member = null; event = h.event; scope = h.scope; realm = h.realm; params = h.params; site = n.site; reads = n.reads; writes = n.writes; nested = false; })
  220. }
  221. for (mm of hlMethods(gen, key)) {
  222. // A literal's inline `on` is hoisted as a method over the SAME body
  223. // (parser `inlineHandlerEntry`); it already answered at its site above.
  224. if (!mm.inlineHandler) {
  225. let n = hlSyntax(gen, key, mm.node)
  226. out.methods.push({ name = mm.name; params = mm.params; site = n.site; reads = n.reads; writes = n.writes; })
  227. }
  228. }
  229. // WHAT THIS FILE REACHES: every import the language resolved to another file
  230. // of the project, and every composed reference a member's syntax read.
  231. if (state.graph.files[key] != null) {
  232. for (imp of state.graph.files[key].imports) {
  233. if (imp.key != null && !out.reaches.includes(imp.key)) { out.reaches.push(imp.key) }
  234. }
  235. }
  236. if (f.wrapper != null && !out.reaches.includes(f.wrapper)) { out.reaches.push(f.wrapper) }
  237. for (k of f.inherits) { if (!out.reaches.includes(k)) { out.reaches.push(k) } }
  238. return out
  239. }
  240. // ---- the query -----------------------------------------------------------------
  241. // `query(file, recursive = true, member = null)`:
  242. // files — one answer per file reached, keyed by the analysis key
  243. // order — the keys in the order they were reached, `file` first
  244. // Recursive follows what each file reaches (its imports, its wrapper, what it
  245. // inherits, and the composed references its members read), each file once.
  246. static query = (file, recursive, member) => {
  247. let rec = recursive == null ? true : recursive
  248. let want = member == null ? null : member
  249. analysis()
  250. let out = { root = file; files = {}; order = []; }
  251. let queue = [file]
  252. let seen = {}
  253. while (queue.length > 0) {
  254. let key = queue.shift()
  255. if (seen[key] == null) {
  256. seen[key] = true
  257. // A key the analysis does not hold is not this package's to parse —
  258. // the root is answered regardless, because the caller named it.
  259. if (state.graph.files[key] != null || key == file) {
  260. let answer = one(key, want)
  261. out.files[key] = answer
  262. out.order.push(key)
  263. if (rec) {
  264. for (k of answer.reaches) { if (seen[k] == null) { queue.push(k) } }
  265. }
  266. }
  267. }
  268. }
  269. return out
  270. }
  271. // EVERY FILE THE ANALYSIS HOLDS, once. A project's root reaches nearly all of
  272. // them; this reaches the rest too, which is what a whole-project report wants.
  273. static queryAll = (member) => {
  274. analysis()
  275. let out = { root = null; files = {}; order = []; }
  276. for (k of state.graph.files.keys()) {
  277. out.files[k] = one(k, member)
  278. out.order.push(k)
  279. }
  280. return out
  281. }
  282. // ---- what `hybriel --graph` carries, per file -----------------------------------
  283. // THE ONE DOCUMENT ASKS THIS PACKAGE (creator, 2026-09-14: the language scans
  284. // EVERYTHING by default and a filter only narrows). `--graph` prints the
  285. // language graph, and every file record in it carries the four tables below —
  286. // the same objects `query()` puts in `files[key]`, handed to the writer as they
  287. // are. There is no second walk and no second rendering: a person reads that
  288. // JSON, and a program imports `query` and gets the same answer as values.
  289. // `trees == false` (`--graph --no-ast`) leaves each member's syntax tree out —
  290. // `syntax` is null, every other field is the same bytes. null or true keeps it.
  291. static tables = (path, member, trees) => {
  292. if (path == null) { analysis() } else { analyseFrom(path) }
  293. let q = queryAll(member)
  294. let out = {}
  295. for (k of q.order) {
  296. let f = q.files[k]
  297. if (trees == false) { for (m of f.members) { m.syntax = null } }
  298. out[k] = { members = f.members; derivations = f.derivations; methods = f.methods; handlers = f.handlers; }
  299. }
  300. return out
  301. }

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre