gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7c051edd7c051eddLog: hybriel#338 answermre7c051edd/plugins/web/view.hl

7.4 KB

  1. // The View's READERS — realm-neutral: the same code reads values on the server and (compiled to JS) in the
  2. // browser; the browser's bundle carries this file and nothing server-only (viewserver.hl). It reads a TREE, the
  3. // syntax of a View member reflected into plain hybrids by the server (see
  4. // server.hl `tree()`), against an INSTANCE whose members supply the values.
  5. //
  6. // Tree node shapes:
  7. // { k = 'el' tag key path = [ keys… ] site attrs = [ { name text } | { name member } ] children = [ … ] }
  8. // `site` is `file:line:col` of the literal — the id its class is minted under
  9. // `tag` is what the DOM gets (a custom key kebab-cased), `key` the source key
  10. // { k = 'text' text }
  11. // { k = 'member' name } an ordered entry naming a member of the class — a SITE
  12. // { k = 'field' name path } `p.title`: a path off a member or a `for` row variable
  13. // { k = 'for' row list body } `for (row of list) { … }`, body rendered per entry
  14. // { k = 'if' cond then other } `if (cond) { … } else { … }`
  15. // { k = 'on' event } a DOM event the owning literal handles (its inline `on`)
  16. // { k = 'component' cls key path bindings ons } `Child { count = count on done(v) { … } }`:
  17. // a composed component — its own mount renders here
  18. // { k = 'other' tag } a construct this round does not render yet (if / for / on)
  19. //
  20. // `ctx` rides through every call: `ctx.slot` is the HTML a shell's `slot` member
  21. // stands for (null when rendering a page alone), `ctx.kids` the child mounts of
  22. // the instance being rendered, keyed by the component node's path joined with
  23. // '/'. A component node renders ITS mount: its own instance, its own kids.
  24. // the language has no escapes: a newline and a tab are written as themselves
  25. static newline = "
  26. "
  27. static tab = " "
  28. // a component node's key in a mount's `kids`: its path, '/'-joined (a list has no
  29. // join in the language; the walk is written out)
  30. kidKey(&path) {
  31. let out = ''
  32. for (p of path) { out = out + '/' + p }
  33. return out
  34. }
  35. // THE KEY OF ONE ROW — "lists are keyed by the record's id" (CONCEPT §2), and by
  36. // the row's index where the row is not a record or carries no id (a list of strings
  37. // is one: demo-blog's paragraphs). ONE rule, called by everything that has to tell
  38. // one row from the next: the server's HTML walk below, the mount key of a row's
  39. // children (WebFramework `mountKids`), and the browser's keyed region (client.hl).
  40. // Two rows with the SAME id are two rows to the language and one key to this rule;
  41. // the browser's region gives the second one a key of its own, the server does not.
  42. rowKey(&entry, i) {
  43. if (entry != null) {
  44. let t = hlTypeName(&entry)
  45. if ((t == 'Hybrid' || t == 'Instance') && entry.id != null) { return '' + entry.id }
  46. }
  47. return '' + i
  48. }
  49. // THE KEYS OF A LIST, in order: rowKey of each row, and where one id stands twice the second and later
  50. // rows get ':' + their index (#119). The browser's region (client listKeys) keys a list the same way, so a
  51. // child of the second row of one id is its own mount on the server too, not the first row's.
  52. listKeys(&entries) {
  53. let out = []
  54. let seen = {}
  55. let ri = 0
  56. for (entry of entries) {
  57. let k = rowKey(&entry, ri)
  58. if (seen[k] != null) { k = k + ':' + ri }
  59. seen[k] = true
  60. out.push(k)
  61. ri = ri + 1
  62. }
  63. return out
  64. }
  65. // A LITERAL'S VALUE, TYPED — COMPONENTS.md §12, "typed, not stringified". `hlSyntax`
  66. // hands a literal back as its source text and its KIND, and a reference binding carries
  67. // the value rather than the text: `Level2 { n = 1 }` gives the child a Number, so `n * 97`
  68. // is arithmetic and not "operator '*' does not apply to String and Number" (measured
  69. // 2026-09-14, demo-nested wall 3). An attribute and a text child keep the text, because
  70. // HTML is text; only what crosses into a member of another class is typed.
  71. literalValue(kind, text) {
  72. if (kind == 'number') { return toNumber(text) }
  73. if (kind == 'boolean') { return text == 'true' }
  74. if (kind == 'null_literal') { return null }
  75. return text
  76. }
  77. // THE VALUE A REFERENCE BINDING CARRIES when it is a literal or a folded static — the
  78. // one place the two are read, so the server's mount and the browser's do it alike. A
  79. // static is folded to its VALUE and carried as one ('folded'), never printed and parsed
  80. // back; a literal carries its source text and its kind, which is all the syntax has.
  81. refValue(b) {
  82. if (b.kind == 'folded') { return b.value }
  83. if (b.kind != null) { return literalValue(b.kind, b.text) }
  84. return b.text
  85. }
  86. // THE VALUE a read names: a `for` row variable in scope wins over a member of the
  87. // same name (nesting is scope); a field path walks into it.
  88. value(&node, &page, &rows) {
  89. if (node.k == 'text') { return refValue(node) }
  90. if (node.k == 'expr') { return evalExpr(&node.x, &page, &rows) }
  91. // READ IN PLACE: `v = rows[node.name]` copied the whole row value (a block with all its spans) and each
  92. // `v = v[f]` step copied the rest again, per read, per mount; a path of up to three steps is one indexed read
  93. let name = node.name
  94. let inRow = rows[name] != null
  95. if (node.k == 'field') {
  96. let path = node.path
  97. let n = path.length
  98. if (n == 1) {
  99. if (inRow) { return rows[name] == null ? null : rows[name][path[0]] }
  100. return page[name] == null ? null : page[name][path[0]]
  101. }
  102. if (n == 2) {
  103. if (inRow) { return rows[name][path[0]] == null ? null : rows[name][path[0]][path[1]] }
  104. return page[name][path[0]] == null ? null : page[name][path[0]][path[1]]
  105. }
  106. }
  107. let v = null
  108. if (inRow) { v = rows[name] } else { v = page[name] }
  109. if (node.k == 'field') {
  110. for (f of node.path) {
  111. if (v == null) { return null }
  112. v = v[f]
  113. }
  114. }
  115. return v
  116. }
  117. // A VIEW EXPRESSION (`p { "x" + n }`, `a - b`, `n > 3 ? 'big' : 'small'`) is evaluated by
  118. // the LANGUAGE'S OWN OPERATORS: this walk only picks the operator the author wrote and
  119. // applies it, so what `+` gives null and a string, what `==` says of two types, what `&&`
  120. // returns — all of it is the runtime's answer, on the server (the interpreter or a
  121. // binary) and in the browser (the client target), and hl:web has no rule of its own.
  122. // The leaves are reads (`value`) and literals. `&&`, `||` and `?:` evaluate what they
  123. // pick only, as the language does.
  124. evalExpr(&x, &page, &rows) {
  125. if (x.k == 'bin') {
  126. let l = evalExpr(&x.l, &page, &rows)
  127. let op = x.op
  128. if (op == 'logical_and') { return l && evalExpr(&x.r, &page, &rows) }
  129. if (op == 'logical_or') { return l || evalExpr(&x.r, &page, &rows) }
  130. let r = evalExpr(&x.r, &page, &rows)
  131. if (op == 'add') { return l + r }
  132. if (op == 'subtract') { return l - r }
  133. if (op == 'multiply') { return l * r }
  134. if (op == 'divide') { return l / r }
  135. if (op == 'modulo') { return l % r }
  136. if (op == 'equal') { return l == r }
  137. if (op == 'not_equal') { return l != r }
  138. if (op == 'less_than') { return l < r }
  139. if (op == 'greater_than') { return l > r }
  140. if (op == 'less_equal') { return l <= r }
  141. if (op == 'greater_equal') { return l >= r }
  142. if (op == 'bit_and') { return l & r }
  143. if (op == 'bit_or') { return l | r }
  144. if (op == 'bit_xor') { return l ^ r }
  145. if (op == 'shift_left') { return l << r }
  146. return l >> r
  147. }
  148. if (x.k == 'un') {
  149. let o = evalExpr(&x.x, &page, &rows)
  150. if (x.op == 'logical_not') { return !o }
  151. if (x.op == 'negate') { return -o }
  152. return ~o
  153. }
  154. if (x.k == 'cond') { return evalExpr(&x.c, &page, &rows) ? evalExpr(&x.a, &page, &rows) : evalExpr(&x.b, &page, &rows) }
  155. return value(&x, &page, &rows)
  156. }

Branches

Latest commits

  • 7c051eddLog: hybriel#338 answermre
  • d049b8b8Log: HM50 live, #52 donemre
  • 6d05fa37Log: HM50 Part A mergedmre
  • 1c7dab74HM50: the shell reads the framework's online state, netProbe removed (tracker#55)mre
  • e8b91697HM50: search address follows the text via navigate(path, replace = true) (tracker#52)mre
  • 6fe1896cHM50: re-vendor Hybriel set day50 (source 0ae38232, bin d3ef9d16)mre
  • 2f391aa9Log: HM50 startedmre
  • 4b59c03dHM50 mission: doc pathmre
  • ed526d67HM50 missionmre
  • 2f986ca3Log: RT50 passmre
  • 1c4715a4RT50 mission: day50 repro retestmre
  • 57d1c0dcLog: day50 retest requestmre
  • 60b1f419Log: Anton #336 result, wake checkmre
  • 919a18aalog: scratchmre
  • 6b504440status tidymre
  • 9332bd10log: DP70 livemre
  • be11daa0DP70 brief fixmre
  • 886a7028DP70 brief, HM67 mergedmre
  • b41cdd6dMerge hm67-searchids: packed search IDsmre
  • 385904eaReport HM67 matched heap measurements and gate proofmre