Skip to content

Commit

Permalink
Revert "Merge branch 'refactor-env' into horizon"
Browse files Browse the repository at this point in the history
This reverts commit 19cb033, reversing
changes made to c761319.
  • Loading branch information
jpolitz committed Jul 6, 2018
1 parent a7d28dc commit 4f3f512
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 221 deletions.
3 changes: 1 addition & 2 deletions src/arr/compiler/anf-loop-compiler.arr
Original file line number Diff line number Diff line change
Expand Up @@ -2027,8 +2027,7 @@ end

fun compile-provides(provides):
cases(CS.Provides) provides:
# MARK(joe/ben): modules below
| provides(thismod-uri, _, values, aliases, data-defs) =>
| provides(thismod-uri, values, aliases, data-defs) =>
value-fields = for cl-map-sd(v from values):
cases(CS.ValueExport) values.get-value(v):
| v-just-type(t) => j-field(v, compile-provided-type(t))
Expand Down
49 changes: 38 additions & 11 deletions src/arr/compiler/ast-util.arr
Original file line number Diff line number Diff line change
Expand Up @@ -1042,10 +1042,36 @@ fun get-named-provides(resolved :: CS.NameResolution, uri :: URI, compile-env ::
# TODO(joe): give more info than this to type checker? only needed dynamically, right?
ann-to-typ(ann)
| a-dot(l, obj, field) =>
# TODO(joe): maybe-b = resolved.module-bindings.get-now(obj.key())
# Then use the information to provide the right a-dot type by looking
# it up on the module.
T.t-top(l, false)
maybe-b = resolved.type-bindings.get-now(obj.key())
cases(Option) maybe-b:
| none =>
T.t-top(l, false)
| some(b) =>
cases(CS.TypeBinder) b.binder:
| tb-module(dot-uri) =>
T.t-name(module-uri(dot-uri), A.s-name(l, field), l, false)
| else =>

# NOTE(joe): This case comes up in the way we expose modules
# across repl entries. If one entry does `import string-dict as
# SD` others will see it as a type-let bound type (tb-type-let
# in compile-structs). If those entries use
# `SD.MutableStringDict` as an annotation on a provided value,
# then it will look like an a-dot is happening to a type-let
# bound type. The fundamental issue is that type-env-from-env
# in resolve-names can't distinguish between modules and
# aliases coming from globals, so these necessarily get
# conflated. Ideally, we'd have more information in Globals to
# tell us this, and that would let all of the appropriate setup
# happen in resolve scope to make this case truly never happen.
# Instead, this is necessary.

T.t-top(l, false)

# raise("Fatal error: used a-dot on a non-module annotation. Should be caught in resolve-scope. " + to-repr(A.a-dot(l, obj, field)) + "\n" + to-repr(b) + "\n")

end
end
| a-checked(checked, residual) =>
raise("a-checked should only be generated by the type-checker")
end
Expand Down Expand Up @@ -1111,7 +1137,12 @@ fun get-named-provides(resolved :: CS.NameResolution, uri :: URI, compile-env ::
target-binding = resolved.type-bindings.get-value-now(a.in-name.key())
typ = cases(Option) target-binding.ann:
| none => T.t-top(l, false)
| some(target-ann) => ann-to-typ(target-ann)
| some(target-ann) =>
if A.is-Ann(a):
ann-to-typ(target-ann)
else:
T.t-top(l, false)
end
end
alias-typs.set-now(a.out-name.toname(), typ)
end
Expand All @@ -1122,7 +1153,6 @@ fun get-named-provides(resolved :: CS.NameResolution, uri :: URI, compile-env ::
end
CS.provides(
uri,
SD.make-string-dict(), # MARK(joe/ben): fill with module info
val-typs.freeze(),
alias-typs.freeze(),
data-typs.freeze()
Expand Down Expand Up @@ -1211,13 +1241,11 @@ transform-data-dict = transform-dict-helper(canonicalize-data-type)

fun transform-provides(provides, compile-env, transformer):
cases(CS.Provides) provides:
# MARK(joe/ben): modules
| provides(from-uri, _, values, aliases, data-definitions) =>
| provides(from-uri, values, aliases, data-definitions) =>
new-vals = transform-value-dict(values, from-uri, transformer)
new-aliases = transform-dict(aliases, from-uri, transformer)
new-data-definitions = transform-data-dict(data-definitions, from-uri, transformer)
# MARK(joe/ben): fill in the string-dict below with module info
CS.provides(from-uri, [SD.string-dict:], new-vals, new-aliases, new-data-definitions)
CS.provides(from-uri, new-vals, new-aliases, new-data-definitions)
end
end

Expand Down Expand Up @@ -1350,7 +1378,6 @@ fun get-typed-provides(typed :: TCS.Typed, uri :: URI, compile-env :: CS.Compile
end
CS.provides(
uri,
[SD.string-dict:], # MARK(joe/ben): fill with real module provides
val-typs.freeze(),
alias-typs.freeze(),
data-typs.freeze()
Expand Down
28 changes: 27 additions & 1 deletion src/arr/compiler/compile-lib.arr
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,32 @@ fun const-dict<a>(strs :: List<String>, val :: a) -> SD.StringDict<a>:
end
end

fun get-provides(p :: PyretCode, uri :: URI) -> Provides:
parsed = get-ast(p, uri)
vals-part =
cases (A.Provide) parsed._provide:
| s-provide-none(l) => mtd
| s-provide-all(l) =>
const-dict(A.toplevel-ids(parsed).map(_.toname()), CS.v-just-there)
| s-provide(l, e) =>
cases (A.Expr) e:
| s-obj(_, mlist) => const-dict(mlist.map(_.name), CS.v-just-there)
| else => raise("Non-object expression in provide: " + l.format(true))
end
end
types-part =
cases(A.ProvideTypes) parsed.provided-types:
| s-provide-types-none(l) => mtd
| s-provide-types-all(l) =>
type-ids = A.block-type-ids(parsed.block)
type-strs = type-ids.map(lam(i): i.name.toname() end)
const-dict(type-strs, CS.t-just-there)
| s-provide-types(l, anns) =>
const-dict(anns.map(_.name), CS.t-just-there)
end
CS.provides(vals-part, types-part)
end

type ToCompile = { locator :: Locator, dependency-map :: SD.MutableStringDict<Locator> }

fun dict-map<a, b>(sd :: SD.MutableStringDict, f :: (String, a -> b)):
Expand All @@ -218,7 +244,7 @@ fun dict-map<a, b>(sd :: SD.MutableStringDict, f :: (String, a -> b)):
end
end

dummy-provides = lam(uri): CS.provides(uri, SD.make-string-dict(), SD.make-string-dict(), SD.make-string-dict(), SD.make-string-dict()) end
dummy-provides = lam(uri): CS.provides(uri, SD.make-string-dict(), SD.make-string-dict(), SD.make-string-dict()) end

fun compile-worklist<a>(dfind, locator, context):
compile-worklist-known-modules(dfind, locator, context, SD.make-mutable-string-dict())
Expand Down
95 changes: 25 additions & 70 deletions src/arr/compiler/compile-structs.arr
Original file line number Diff line number Diff line change
Expand Up @@ -57,45 +57,30 @@ data NativeModule:
end

data BindOrigin:
| bind-origin(local-bind-site :: Loc, definition-bind-site :: Loc, new-definition :: Boolean, uri-of-definition :: URI)
end

fun bo-local(loc):
cases(SL.Srcloc) loc:
| builtin(source) =>
bind-origin(loc, loc, true, source)
| else =>
bind-origin(loc, loc, true, loc.source)
end
end

# NOTE(joe): If source information ends up in provides, we can add an extra arg
# here to provide better definition site info for names from other modules
fun bo-module(loc, uri):
bind-origin(loc, SL.builtin(uri), false, uri)
end

fun bo-global(uri):
bind-origin(SL.builtin(uri), SL.builtin(uri), false, uri)
| bo-local(loc :: Loc)
| bo-module(uri :: URI)
end

data ValueBinder:
| vb-letrec
| vb-let
| vb-var
| vb-module(uri :: URI) # The A in import ast as A (with URI determined from compile env)
end

data ValueBind:
| value-bind(
origin :: BindOrigin,
binder :: ValueBinder,
atom :: A.Name,
ann :: A.Ann)
ann :: A.Ann,
expr :: Option<A.Expr>)
end

data TypeBinder:
| tb-type-let
| tb-type-var
| tb-module(uri :: URI)
end

data TypeBind:
Expand All @@ -106,13 +91,6 @@ data TypeBind:
ann :: Option<A.Ann>)
end

data ModuleBind:
| module-bind(
origin :: BindOrigin,
atom :: A.Name,
uri :: URI)
end

data ScopeResolution:
| resolved-scope(ast :: A.Program, errors :: List<CompileError>)
end
Expand All @@ -121,7 +99,6 @@ data NameResolution:
| resolved-names(
ast :: A.Program,
errors :: List<CompileError>,
module-bindings :: SD.MutableStringDict<ModuleBind>,
bindings :: SD.MutableStringDict<ValueBind>,
type-bindings :: SD.MutableStringDict<TypeBind>,
datatypes :: SD.MutableStringDict<A.Expr>)
Expand Down Expand Up @@ -228,17 +205,6 @@ sharing:
| some(shadow provides) => provides
end
end,
method provides-by-module-name(self, name):
self.globals.modules.get(name)
.and-then(self.provides-by-dep-key(_))
.and-then(_.value)
end,
method provides-by-module-name-value(self, name):
cases(Option) self.provides-by-module-name(name):
| none => raise("Could not find module " + name)
| some(shadow provides) => provides
end
end,
method value-by-dep-key(self, dep-key, name):
uri = self.my-modules.get-value(dep-key)
self.value-by-uri(uri, name)
Expand All @@ -263,9 +229,9 @@ sharing:
end
end

# globals maps from names to the appropriate dependency (e.g. in my-modules)
# The strings in globals should be the appropriate dependency (e.g. in my-modules)
data Globals:
| globals(modules :: StringDict<String>, values :: StringDict<String>, types :: StringDict<String>)
| globals(values :: StringDict<String>, types :: StringDict<String>)
end

data ValueExport:
Expand All @@ -277,7 +243,6 @@ end
data Provides:
| provides(
from-uri :: URI,
modules :: StringDict<URI>,
values :: StringDict<ValueExport>,
aliases :: StringDict<T.Type>,
data-definitions :: StringDict<T.DataType>
Expand Down Expand Up @@ -417,17 +382,15 @@ fun provides-from-raw-provides(uri, raw):
ddict = for fold(ddict from SD.make-string-dict(), d from raw.datatypes):
ddict.set(d.name, datatype-from-raw(uri, d.typ))
end
# MARK(joe/ben): modules
provides(uri, [SD.string-dict:], vdict, adict, ddict)
provides(uri, vdict, adict, ddict)
end




fun provides-to-raw-provides-ast(provs, env):
cases(Provides) provs:
# MARK(joe/ben): modules
| provides(uri, _, values, aliases, data-defs) =>
| provides(uri, values, aliases, data-defs) =>
#|
value-fields = for CL.map_list(v from values.keys().to-list()):
J.j-field(v, type-to-raw-ast(values.get-value(v), compile-env))
Expand Down Expand Up @@ -1256,23 +1219,19 @@ data CompileError:
ED.text(" is being used as a value:")]
usage = ED.cmcode(self.id.l)
cases(BindOrigin) self.origin:
| bind-origin(lbind, ldef, newdef, uri) =>
if newdef:
[ED.error: intro, usage,
[ED.para:
ED.text("But it is "),
ED.highlight(ED.text("defined as a type"), [ED.locs: ldef], 1),
ED.text(":")],
ED.cmcode(ldef)]
else:
# MARK(joe/ben): This may be able to use lbind and ldef when they
# are more refined; come back to this
[ED.error: intro, usage,
[ED.para:
ED.text("But it is defined as a type in "),
ED.embed(uri),
ED.text(".")]]
end
| bo-local(loc) =>
[ED.error: intro, usage,
[ED.para:
ED.text("But it is "),
ED.highlight(ED.text("defined as a type"), [ED.locs: loc], 1),
ED.text(":")],
ED.cmcode(loc)]
| bo-module(uri) =>
[ED.error: intro, usage,
[ED.para:
ED.text("But it is defined as a type in "),
ED.embed(uri),
ED.text(".")]]
end
end,
method render-reason(self):
Expand Down Expand Up @@ -2668,8 +2627,6 @@ fun t-forall1(f):
end

runtime-provides = provides("builtin://global",
# MARK(joe/ben): modules
[string-dict:],
[string-dict:
"test-print", t-forall1(lam(a): t-arrow([list: a], a) end),
"print", t-forall1(lam(a): t-arrow([list: a], a) end),
Expand Down Expand Up @@ -2870,11 +2827,9 @@ shadow runtime-types = for SD.fold-keys(rt from runtime-types, k from runtime-pr
rt.set(k, "builtin(global)")
end

# MARK(joe/ben): modules
no-builtins = compile-env(globals([string-dict: ], [string-dict: ], [string-dict: ]), [mutable-string-dict:],[string-dict:])
no-builtins = compile-env(globals([string-dict: ], [string-dict: ]), [mutable-string-dict:],[string-dict:])

# MARK(joe/ben): modules
standard-globals = globals([string-dict:], runtime-values, runtime-types)
standard-globals = globals(runtime-values, runtime-types)

minimal-imports = extra-imports(empty)

Expand Down
4 changes: 2 additions & 2 deletions src/arr/compiler/desugar.arr
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ end

fun mk-id-ann(loc, base, ann) block:
a = names.make-atom(base)
generated-binds.set-now(a.key(), C.value-bind(C.bo-local(loc), C.vb-let, a, ann))
generated-binds.set-now(a.key(), C.value-bind(C.bo-local(loc), C.vb-let, a, ann, none))
{ id: a, id-b: A.s-bind(loc, false, a, ann), id-e: A.s-id(loc, a) }
end

fun mk-id-var-ann(loc, base, ann) block:
a = names.make-atom(base)
generated-binds.set-now(a.key(), C.value-bind(C.bo-local(loc), C.vb-var, a, ann))
generated-binds.set-now(a.key(), C.value-bind(C.bo-local(loc), C.vb-var, a, ann, none))
{ id: a, id-b: A.s-bind(loc, false, a, ann), id-e: A.s-id-var(loc, a) }
end

Expand Down
Loading

0 comments on commit 4f3f512

Please sign in to comment.