adamc@16: (* Copyright (c) 2008, Adam Chlipala adamc@16: * All rights reserved. adamc@16: * adamc@16: * Redistribution and use in source and binary forms, with or without adamc@16: * modification, are permitted provided that the following conditions are met: adamc@16: * adamc@16: * - Redistributions of source code must retain the above copyright notice, adamc@16: * this list of conditions and the following disclaimer. adamc@16: * - Redistributions in binary form must reproduce the above copyright notice, adamc@16: * this list of conditions and the following disclaimer in the documentation adamc@16: * and/or other materials provided with the distribution. adamc@16: * - The names of contributors may not be used to endorse or promote products adamc@16: * derived from this software without specific prior written permission. adamc@16: * adamc@16: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@16: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@16: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@16: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@16: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@16: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@16: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@16: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@16: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@16: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@16: * POSSIBILITY OF SUCH DAMAGE. adamc@16: *) adamc@16: adamc@16: structure CoreEnv :> CORE_ENV = struct adamc@16: adamc@16: open Core adamc@16: adamc@16: structure U = CoreUtil adamc@16: adamc@16: structure IM = IntBinaryMap adamc@16: adamc@16: adamc@16: (* AST utility functions *) adamc@16: adamc@16: val liftConInCon = adamc@16: U.Con.mapB {kind = fn k => k, adamc@16: con = fn bound => fn c => adamc@16: case c of adamc@16: CRel xn => adamc@16: if xn < bound then adamc@16: c adamc@16: else adamc@16: CRel (xn + 1) adamc@16: | _ => c, adamc@16: bind = fn (bound, U.Con.Rel _) => bound + 1 adamc@16: | (bound, _) => bound} adamc@16: adamc@16: val lift = liftConInCon 0 adamc@16: adamc@16: adamc@16: (* Back to environments *) adamc@16: adamc@16: exception UnboundRel of int adamc@16: exception UnboundNamed of int adamc@16: adamc@16: type env = { adamc@16: relC : (string * kind) list, adamc@16: namedC : (string * kind * con option) IM.map, adamc@16: adamc@16: relE : (string * con) list, adamc@109: namedE : (string * con * exp option * string) IM.map adamc@16: } adamc@16: adamc@16: val empty = { adamc@16: relC = [], adamc@16: namedC = IM.empty, adamc@16: adamc@16: relE = [], adamc@16: namedE = IM.empty adamc@16: } adamc@16: adamc@16: fun pushCRel (env : env) x k = adamc@16: {relC = (x, k) :: #relC env, adamc@16: namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env), adamc@16: adamc@16: relE = map (fn (x, c) => (x, lift c)) (#relE env), adamc@109: namedE = IM.map (fn (x, c, eo, s) => (x, lift c, eo, s)) (#namedE env)} adamc@16: adamc@16: fun lookupCRel (env : env) n = adamc@16: (List.nth (#relC env, n)) adamc@16: handle Subscript => raise UnboundRel n adamc@16: adamc@16: fun pushCNamed (env : env) x n k co = adamc@16: {relC = #relC env, adamc@16: namedC = IM.insert (#namedC env, n, (x, k, co)), adamc@16: adamc@16: relE = #relE env, adamc@16: namedE = #namedE env} adamc@16: adamc@16: fun lookupCNamed (env : env) n = adamc@16: case IM.find (#namedC env, n) of adamc@16: NONE => raise UnboundNamed n adamc@16: | SOME x => x adamc@16: adamc@16: fun pushERel (env : env) x t = adamc@16: {relC = #relC env, adamc@16: namedC = #namedC env, adamc@16: adamc@16: relE = (x, t) :: #relE env, adamc@16: namedE = #namedE env} adamc@16: adamc@16: fun lookupERel (env : env) n = adamc@16: (List.nth (#relE env, n)) adamc@16: handle Subscript => raise UnboundRel n adamc@16: adamc@109: fun pushENamed (env : env) x n t eo s = adamc@16: {relC = #relC env, adamc@16: namedC = #namedC env, adamc@16: adamc@16: relE = #relE env, adamc@109: namedE = IM.insert (#namedE env, n, (x, t, eo, s))} adamc@16: adamc@16: fun lookupENamed (env : env) n = adamc@16: case IM.find (#namedE env, n) of adamc@16: NONE => raise UnboundNamed n adamc@16: | SOME x => x adamc@16: adamc@16: fun declBinds env (d, _) = adamc@16: case d of adamc@16: DCon (x, n, k, c) => pushCNamed env x n k (SOME c) adamc@109: | DVal (x, n, t, e, s) => pushENamed env x n t (SOME e) s adamc@109: | DExport _ => env adamc@16: adamc@16: end