gitoriaLog in with ident

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain5fdbb6b2ident mission 009: report — scratch folder notemremain/plugins/core/AST.hl

13.3 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? }`. `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. // the names of a read/write list, as plain strings
  147. static namesOf = (list) => {
  148. let out = []
  149. for (r of list) { if (!out.includes(r.name)) { out.push(r.name) } }
  150. return out
  151. }
  152. // the subset of a read/write list that resolves to a member of the file itself
  153. static membersOf = (list) => {
  154. let out = []
  155. for (r of list) {
  156. if (r.kind == 'member' && !r.isStatic && !out.includes(r.name)) { out.push(r.name) }
  157. }
  158. return out
  159. }
  160. // ---- one file's answer ---------------------------------------------------------
  161. // every `on` node inside a member's syntax tree, flattened, each at its site
  162. static nestedHandlers = (member, node, &into) => {
  163. if (node == null) { return into }
  164. if (node.tag == 'on_statement') {
  165. into.push({ member = member; event = node.event; scope = node.scope; realm = node.realm; site = node.site; reads = node.reads; writes = node.writes; nested = true; })
  166. return into
  167. }
  168. if (node.entries != null) { for (c of node.entries) { nestedHandlers(member, c, into) } }
  169. if (node.args != null) { for (c of node.args) { nestedHandlers(member, c, into) } }
  170. if (node.elements != null) { for (c of node.elements) { nestedHandlers(member, c, into) } }
  171. if (node.value != null) { nestedHandlers(member, node.value, into) }
  172. if (node.list != null) { nestedHandlers(member, node.list, into) }
  173. if (node.body != null) { nestedHandlers(member, node.body, into) }
  174. if (node.condition != null) { nestedHandlers(member, node.condition, into) }
  175. if (node.consequent != null) { nestedHandlers(member, node.consequent, into) }
  176. if (node.alternate != null) { nestedHandlers(member, node.alternate, into) }
  177. if (node.object != null) { nestedHandlers(member, node.object, into) }
  178. if (node.property != null) { nestedHandlers(member, node.property, into) }
  179. if (node.callee != null) { nestedHandlers(member, node.callee, into) }
  180. if (node.left != null) { nestedHandlers(member, node.left, into) }
  181. if (node.right != null) { nestedHandlers(member, node.right, into) }
  182. if (node.operand != null) { nestedHandlers(member, node.operand, into) }
  183. return into
  184. }
  185. static one = (key, member) => {
  186. let gen = analysis()
  187. let f = hlFile(gen, key)
  188. let out = { key = key; class = f.class; members = []; derivations = []; handlers = []; methods = []; reaches = []; }
  189. for (m of hlMembers(gen, key)) {
  190. // (Hybriel has no `continue`: the body is guarded instead.)
  191. if (member == null || m.name == member) {
  192. 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; }
  193. if (m.hasValue) {
  194. let n = hlSyntax(gen, key, m.node)
  195. entry.default = n.site
  196. entry.reads = n.reads
  197. entry.writes = n.writes
  198. entry.syntax = tree(key, m.node)
  199. // A DERIVATION IS AN INITIALIZER READ THE OTHER WAY: the same table,
  200. // listed by the names the value depends on.
  201. out.derivations.push({ name = m.name; site = n.site; reads = n.reads; })
  202. nestedHandlers(m.name, entry.syntax, out.handlers)
  203. for (r of n.reads) {
  204. if (r.kind == 'class' && r.key != null && !out.reaches.includes(r.key)) { out.reaches.push(r.key) }
  205. }
  206. }
  207. out.members.push(entry)
  208. }
  209. }
  210. // The DECLARED handlers and methods answer for their whole body. A member
  211. // filter narrows the member table only — a handler is not a member, and
  212. // hiding the writes would make the filtered answer a lie about the file.
  213. for (h of hlEvents(gen, key).handles) {
  214. let n = hlSyntax(gen, key, h.node)
  215. 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; })
  216. }
  217. for (mm of hlMethods(gen, key)) {
  218. // A literal's inline `on` is hoisted as a method over the SAME body
  219. // (parser `inlineHandlerEntry`); it already answered at its site above.
  220. if (!mm.inlineHandler) {
  221. let n = hlSyntax(gen, key, mm.node)
  222. out.methods.push({ name = mm.name; params = mm.params; site = n.site; reads = n.reads; writes = n.writes; })
  223. }
  224. }
  225. // WHAT THIS FILE REACHES: every import the language resolved to another file
  226. // of the project, and every composed reference a member's syntax read.
  227. if (state.graph.files[key] != null) {
  228. for (imp of state.graph.files[key].imports) {
  229. if (imp.key != null && !out.reaches.includes(imp.key)) { out.reaches.push(imp.key) }
  230. }
  231. }
  232. if (f.wrapper != null && !out.reaches.includes(f.wrapper)) { out.reaches.push(f.wrapper) }
  233. for (k of f.inherits) { if (!out.reaches.includes(k)) { out.reaches.push(k) } }
  234. return out
  235. }
  236. // ---- the query -----------------------------------------------------------------
  237. // `query(file, recursive = true, member = null)`:
  238. // files — one answer per file reached, keyed by the analysis key
  239. // order — the keys in the order they were reached, `file` first
  240. // Recursive follows what each file reaches (its imports, its wrapper, what it
  241. // inherits, and the composed references its members read), each file once.
  242. static query = (file, recursive, member) => {
  243. let rec = recursive == null ? true : recursive
  244. let want = member == null ? null : member
  245. analysis()
  246. let out = { root = file; files = {}; order = []; }
  247. let queue = [file]
  248. let seen = {}
  249. while (queue.length > 0) {
  250. let key = queue.shift()
  251. if (seen[key] == null) {
  252. seen[key] = true
  253. // A key the analysis does not hold is not this package's to parse —
  254. // the root is answered regardless, because the caller named it.
  255. if (state.graph.files[key] != null || key == file) {
  256. let answer = one(key, want)
  257. out.files[key] = answer
  258. out.order.push(key)
  259. if (rec) {
  260. for (k of answer.reaches) { if (seen[k] == null) { queue.push(k) } }
  261. }
  262. }
  263. }
  264. }
  265. return out
  266. }
  267. // EVERY FILE THE ANALYSIS HOLDS, once. A project's root reaches nearly all of
  268. // them; this reaches the rest too, which is what a whole-project report wants.
  269. static queryAll = (member) => {
  270. analysis()
  271. let out = { root = null; files = {}; order = []; }
  272. for (k of state.graph.files.keys()) {
  273. out.files[k] = one(k, member)
  274. out.order.push(k)
  275. }
  276. return out
  277. }
  278. // ---- what `hybriel --graph` carries, per file -----------------------------------
  279. // THE ONE DOCUMENT ASKS THIS PACKAGE (creator, 2026-09-14: the language scans
  280. // EVERYTHING by default and a filter only narrows). `--graph` prints the
  281. // language graph, and every file record in it carries the four tables below —
  282. // the same objects `query()` puts in `files[key]`, handed to the writer as they
  283. // are. There is no second walk and no second rendering: a person reads that
  284. // JSON, and a program imports `query` and gets the same answer as values.
  285. static tables = (path, member) => {
  286. if (path == null) { analysis() } else { analyseFrom(path) }
  287. let q = queryAll(member)
  288. let out = {}
  289. for (k of q.order) {
  290. let f = q.files[k]
  291. out[k] = { members = f.members; derivations = f.derivations; methods = f.methods; handlers = f.handlers; }
  292. }
  293. return out
  294. }

Branches

Latest commits

  • 5fdbb6b2ident mission 009: report — scratch folder notemre
  • 74235815ident mission 009 (4/4): docs (README files map + same-output test, STATUS, LOG), report, tests/letcount.py + realdata-baseline/comparemre
  • fe183516ident mission 009 (3/4): let only where reassigned — 293 never-reassigned lets are plain declarations; kept: reassigned, loop bodies, names of a file member, a name declared twice in one function; same outputmre
  • d2e7f91bident mission 009 (2/4): one lib file per topic (login, accounts, identities, apps, invites, selector, notify + helpers, util), function routes as thin wrappers in lib/api.hl, project.hl = the map; same outputmre
  • 91017164ident mission 009 (1/4): file moves — the root .hl files into lib/ (api.hl → lib/api-helpers.hl), styles.hl → components/styles.hl; imports adjusted, no other changemre
  • f8bdcbc2ident: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); all gates greenmre
  • ff78726cident: Hybriel master 190aa11d (fc838894 GC correctness, #127, #126 closure scopes); gates all greenmre
  • a3a7d21aident: Hybriel master 8efba065 (#126 GC by bytes, #48 lambda params copy); session-writing lambdas take &sessionmre
  • 98226b41antcolony#40: mission references point to the moved missionsmre
  • ff805b9aantcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
  • 51a7bcdfident: Hybriel master 73267707 (#122); /code uses the new page() signature; pending address passed as parameter; once-checksmre
  • 836f644fident#24: installable app (manifest, service worker, data-free offline /start), own iconmre
  • 8bebbbf2deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • cc063ea2deploy.sh: never send .git or .gitignore to Byrodinmre
  • 81b15b7bState of 2026-09-27, before the move to gitoriamre