unit-compile.tri (4623B)
1 !import prelude * 2 !import parser as P 3 !import consumers_source as C 4 !import consumers_unit_source as U 5 !import consumers_base_source as B 6 !import consumers_validate as V 7 !import consumers_lexical_worker as L 8 !export * 9 -- Compile reusable entries from a multi-consumer unit with an acyclic static 10 -- code-reference graph. Runtime recurrence/delegation is a different graph. 11 -- No configuration executes here, and computed literal trees are never walked. 12 -- Entry points: compileScpDefinitions, compileScpUnit, compileScpModuleBody; 13 -- callers supply available qualified entries explicitly. uc* helpers are local 14 -- scheduling/elaboration machinery, not another runtime reference resolver. 15 16 ucLocalNames definitions = map (definition : pair 17 (append "Self." (fst definition)) t) definitions 18 ucProblem definition diagnostic = (P.parserDiagnosticWithContext 19 (fst definition) diagnostic) 20 ucChecked definition = (bindResult (V.vValidate (fst (snd definition))) 21 (_ _ : L.lCompile (fst (snd definition)))) 22 -- Published row payload keeps open code, elaborated authored consumer, and 23 -- source locations distinct. The authored value is for the independent oracle. 24 ucCompiledRow definition worker = (pair (fst definition) 25 (pair (snd (snd worker)) (snd definition))) 26 ucRound_ self definitions deferred available declared built progressed = (lazyList 27 (_ : lazyList 28 (_ : ok (reverse built) t) 29 (first rest : lazyBool 30 (_ : self (reverse deferred) t available declared built false) 31 (_ : err (P.parserDiagnostic 24 "acyclic local code references" 32 (map fst (reverse deferred)) (fst (snd (snd first))) t) t) 33 progressed) deferred) 34 (definition rest : lazyResult 35 (problem unused : lazyBool 36 (_ : self rest (pair definition deferred) available declared built progressed) 37 (_ : err (ucProblem definition problem) t) 38 (equal? (P.parserDiagnosticCode problem) 23)) 39 (resolved unused : lazyResult 40 (problem unused : err (pair (fst definition) 41 (pair problem (B.bsDiagnosticLocation problem 42 (snd (snd (snd definition)))))) t) 43 (worker unused : self rest deferred 44 (pair (pair (append "Self." (fst definition)) (snd (snd worker))) available) 45 declared (pair (ucCompiledRow resolved worker) built) true) 46 (ucChecked resolved)) 47 (U.usResolveDefinition definition available declared)) definitions) 48 -- The local namespace cannot be supplied or overridden by import bindings. 49 ucSelfNamespace? name = (lazyList (_ : false) (a tail : 50 lazyList (_ : false) (b tail : lazyList (_ : false) (c tail : 51 lazyList (_ : false) (d tail : lazyList (_ : false) (e rest : 52 foldl and? true [(equal? a 83) (equal? b 101) (equal? c 108) 53 (equal? d 102) (equal? e 46)]) tail) tail) tail) tail) name) 54 ucBindings_ self bindings seen = (lazyList (_ : ok t t) 55 (binding rest : lazyBool 56 (_ : lazyBool 57 (_ : err (P.parserDiagnostic 21 "fresh imported qualified name" 58 (fst binding) t t) t) 59 (_ : lazyBool 60 (_ : err (P.parserDiagnostic 22 "non-Self import namespace" 61 (fst binding) t t) t) 62 (_ : self rest (pair binding seen)) (ucSelfNamespace? (fst binding))) 63 (C.cNameUsed seen (fst binding))) 64 (_ : err "invalid imported tree binding" t) (V.vPair? binding)) bindings) 65 ucBindings bindings = y ucBindings_ bindings t 66 -- Preflight every name against the complete declared interface. Otherwise a 67 -- pending local reference could hide an unknown reference later in its output 68 -- and be misreported as a cycle. Placeholder trees are discarded, never compiled. 69 ucReferences_ self definitions declared = (lazyList (_ : ok t t) 70 (definition rest : lazyResult 71 (problem unused : err (ucProblem definition problem) t) 72 (_ unused : self rest declared) 73 (U.usResolveDefinition definition declared declared)) definitions) 74 ucReferences definitions declared = y ucReferences_ definitions declared 75 compileScpDefinitions definitions available = (bindResult (ucBindings available) 76 (_ _ : let declared = append (ucLocalNames definitions) available in 77 bindResult (ucReferences definitions declared) 78 (_ _ : y ucRound_ definitions t available declared t false))) 79 compileScpUnit source available = (bindResult (U.parseScpUnit source) 80 (definitions _ : compileScpDefinitions definitions available)) 81 -- Native module bodies may be empty: exports/re-exports remain explicit and 82 -- the shared selector rejects an empty public interface. No sentinel consumer. 83 compileScpModuleBody source available = (bindResult (U.parseScpModuleBody source) 84 (definitions _ : compileScpDefinitions definitions available))