Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
julianhyde committed Dec 15, 2023
1 parent 4b88c8a commit 2fdc83c
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 287 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/hydromatic/morel/compile/Compiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static CompiledStatement prepareDecl(TypeSystem typeSystem,
final boolean relationalize =
Prop.RELATIONALIZE.booleanValue(session.map);

final Resolver resolver = Resolver.of(resolved.typeMap, env);
final Resolver resolver = Resolver.of(resolved.typeMap, env, session);
final Core.Decl coreDecl0 = resolver.toCore(resolved.node);
tracer.onCore(0, coreDecl0);

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/hydromatic/morel/compile/Inliner.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public static Inliner of(TypeSystem typeSystem, Environment env,
default:
if (v instanceof Code) {
v = ((Code) v).eval(Compiler.EMPTY_ENV);
if (v == null) {
// Cannot inline SYS_FILE; it requires a session.
break;
}
}
return core.valueLiteral(id, v);
}
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/net/hydromatic/morel/compile/Resolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.hydromatic.morel.ast.Shuttle;
import net.hydromatic.morel.ast.Visitor;
import net.hydromatic.morel.eval.Codes;
import net.hydromatic.morel.eval.Session;
import net.hydromatic.morel.type.Binding;
import net.hydromatic.morel.type.DataType;
import net.hydromatic.morel.type.FnType;
Expand All @@ -44,6 +45,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import org.apache.calcite.util.Util;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.math.BigDecimal;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -84,6 +86,7 @@ public class Resolver {
final TypeMap typeMap;
private final NameGenerator nameGenerator;
private final Environment env;
private final @Nullable Session session;

/** Contains variable declarations whose type at the point they are used is
* different (more specific) than in their declaration.
Expand All @@ -101,22 +104,25 @@ public class Resolver {

private Resolver(TypeMap typeMap, NameGenerator nameGenerator,
Map<Pair<Core.NamedPat, Type>, Core.NamedPat> variantIdMap,
Environment env) {
Environment env, @Nullable Session session) {
this.typeMap = typeMap;
this.nameGenerator = nameGenerator;
this.variantIdMap = variantIdMap;
this.env = env;
this.session = session;
}

/** Creates a root Resolver. */
public static Resolver of(TypeMap typeMap, Environment env) {
return new Resolver(typeMap, new NameGenerator(), new HashMap<>(), env);
public static Resolver of(TypeMap typeMap, Environment env,
@Nullable Session session) {
return new Resolver(typeMap, new NameGenerator(), new HashMap<>(), env,
session);
}

/** Binds a Resolver to a new environment. */
public Resolver withEnv(Environment env) {
return env == this.env ? this
: new Resolver(typeMap, nameGenerator, variantIdMap, env);
: new Resolver(typeMap, nameGenerator, variantIdMap, env, session);
}

/** Binds a Resolver to an environment that consists of the current
Expand Down Expand Up @@ -485,6 +491,14 @@ Object valueOf(Core.Exp exp) {
final Core.Id id = (Core.Id) exp;
Binding binding = env.getOpt(id.idPat);
if (binding != null) {
if (binding.value == Codes.SYS_FILE) {
// We can't evaluate, because we don't have an EvalEnv. But we
// can go straight to the Session.
if (session == null) {
return null;
}
return session.file.get();
}
return binding.value;
}
}
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/net/hydromatic/morel/eval/Codes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2324,8 +2324,29 @@ private static Core.Exp sysEnv(TypeSystem typeSystem, Environment env,
core.tuple(typeSystem, null, args));
}

/** @see BuiltIn#SYS_FILE */
private static final TypedValue SYS_FILE = new Directory();
/** @see BuiltIn#SYS_FILE
*
* <p>A reference to {@code file} expands to an indirection via the
* {@link EvalEnv#SESSION}. Each session has its own file system; this
* ensures thread-safety and also predictability. You wouldn't want/expect
* the file system to be already 'expanded' in one session because another
* session just used it. */
public static final Code SYS_FILE =
new Code() {
@Override public List eval(EvalEnv evalEnv) {
final Session session = (Session) evalEnv.getOpt(EvalEnv.SESSION);
if (session == null) {
// We are sometimes evaluated without a session during compilation.
// Return null to indicate that a directory is not available.
return null;
}
return session.file.get().valueAs(List.class);
}

@Override public Describer describe(Describer describer) {
return describer.start("file", detail -> {});
}
};

/** @see BuiltIn#SYS_PLAN */
private static final Applicable SYS_PLAN =
Expand Down
275 changes: 0 additions & 275 deletions src/main/java/net/hydromatic/morel/eval/Directory.java

This file was deleted.

Loading

0 comments on commit 2fdc83c

Please sign in to comment.