1018ac65552074f0cd9a2b13c541e0a3137ee492017355204559277ac8828abd.tri (5887B)
1 !import prelude * 2 !import parser * 3 !import source_order * 4 !import source_compile * 5 !import source_program * 6 !import source_resolve * 7 !import source_ast * 8 !import source_stable * 9 !export * 10 11 -- Build definition bindings in stable dependency order, including unused 12 -- definitions. The reducer may defer normalization of unused body trees. 13 -- Extend the association list at the head in O(1); resolution sees local 14 -- definitions before supplied external bindings. 15 sourceCompileDefinition environment definition = (lazyResult 16 (diagnostic remainder : err diagnostic remainder) 17 (core unused : sourceCompileCore core) 18 (resolveGateAst environment (sourceDefinitionBody definition))) 19 20 sourceCompileOrderedWith_ self names definitions external compiled compileDefinition = 21 lazyList 22 (_ : ok compiled t) 23 (name rest : lazyMaybe 24 (_ : err (parserDiagnostic 33 name t t t) name) 25 (definition : lazyResult 26 (diagnostic remainder : err diagnostic remainder) 27 (tree unused : self rest definitions external 28 (pair (pair name tree) compiled) compileDefinition) 29 (compileDefinition (append compiled external) definition)) 30 (sourceFindDefinition definitions name)) 31 names 32 33 sourceCompileOrderedWith compileDefinition names definitions external = 34 y sourceCompileOrderedWith_ names definitions external t compileDefinition 35 sourceCompileOrdered names definitions external = 36 sourceCompileOrderedWith sourceCompileDefinition names definitions external 37 38 -- Explicit selection: a local `main` wins, otherwise the final expression, 39 -- otherwise a missing-root diagnostic. Definitions are ordered first. 40 sourceSelectProgram external program compiled = 41 lazyMaybe 42 (_ : lazyMaybe 43 (_ : err (parserDiagnostic 32 "main" t t t) t) 44 (expression : 45 lazyResult 46 (diagnostic remainder : err diagnostic remainder) 47 (core unused : sourceCompileCore core) 48 (resolveGateAst (append compiled external) expression)) 49 (sourceProgramFinal program)) 50 (tree : ok tree t) 51 (sourceFindExternal compiled "main") 52 53 -- The reusable single-unit boundary also returns compiled definitions in 54 -- stable dependency order. Values remain raw trees, not compiler-private ASTs. 55 compileGateProgramOutput external characters = 56 parserMatchReply 57 (diagnostic remainder consumed committed : err diagnostic remainder) 58 (program remainder consumed committed : 59 lazyResult 60 (diagnostic rest : err diagnostic rest) 61 (ordered unused : 62 lazyResult 63 (diagnostic rest : err diagnostic rest) 64 (compiled unused : 65 lazyResult 66 (diagnostic rest : err diagnostic rest) 67 (root unused : ok (pair root (reverse compiled)) t) 68 (sourceSelectProgram external program compiled)) 69 (sourceCompileOrdered ordered 70 (sourceProgramDefinitions program) external)) 71 (orderGateProgram external program)) 72 (parseGateProgram characters) 73 74 -- Public single-unit boundary. Keep the environment explicit: it contains 75 -- already-resolved (name, tree) bindings, not paths or import directives. 76 -- The selected root and dependency-ordered definitions are returned together; 77 -- parsing, ordering and compilation failures retain their structured replies. 78 compileSource environment sourceCharacters = 79 compileGateProgramOutput environment sourceCharacters 80 81 -- Module units have explicit exports, not an implicit `main` or final value. 82 -- This entrypoint shares the ordinary parser, orderer, resolver and reference 83 -- backend, but rejects a final expression instead of requiring a root. 84 sourceModuleCheckShadows_ self imports definitions = 85 lazyList 86 (_ : ok t t) 87 (binding rest : 88 lazyMaybe 89 (_ : self rest definitions) 90 (definition : err 91 (parserDiagnostic 46 (fst binding) (fst binding) 92 (sourceDefinitionPosition definition) t) t) 93 (sourceFindDefinition definitions (fst binding))) 94 imports 95 96 sourceModuleCheckShadows = (imports definitions : 97 y sourceModuleCheckShadows_ imports definitions) 98 99 compileModuleDefinitionsWith orderProgram compileDefinition environment characters = 100 parserMatchReply 101 (diagnostic remainder consumed committed : err diagnostic remainder) 102 (program remainder consumed committed : 103 lazyMaybe 104 (_ : lazyResult 105 (diagnostic rest : err diagnostic rest) 106 (unused rest : lazyResult 107 (diagnostic rest : err diagnostic rest) 108 (ordered unused : lazyResult 109 (diagnostic rest : err diagnostic rest) 110 (compiled unused : ok (reverse compiled) t) 111 (sourceCompileOrderedWith compileDefinition ordered 112 (sourceProgramDefinitions program) environment)) 113 (orderProgram environment program)) 114 (sourceModuleCheckShadows environment 115 (sourceProgramDefinitions program))) 116 (expression : err 117 (parserDiagnostic 42 "definitions only" t 118 (sourceAstStart expression) t) expression) 119 (sourceProgramFinal program)) 120 (parseGateProgram characters) 121 122 -- Backend selection is explicit. Existing entrypoints retain optimized trees. 123 compileModuleDefinitions environment characters = 124 compileModuleDefinitionsWith orderGateProgram sourceCompileDefinition environment characters 125 compileStableModuleDefinitions environment characters = 126 compileModuleDefinitionsWith orderStableProgram sourceCompileStableDefinition environment characters 127 128 -- Preserve the established gate API: callers interested only in the root 129 -- do not have to inspect (or force) the compiled definition environment. 130 compileGateProgram external characters = 131 lazyResult 132 (diagnostic remainder : err diagnostic remainder) 133 (output unused : ok (fst output) t) 134 (compileGateProgramOutput external characters) 135