Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
more files
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed Jan 4, 2024
1 parent 6e6288b commit ed4bfa2
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 7 deletions.
5 changes: 4 additions & 1 deletion base/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
requires org.commonmark;

exports org.aya.prelude;
exports org.aya.syntax.core;
exports org.aya.syntax.concrete.decl;
exports org.aya.syntax.core.def;
exports org.aya.syntax.core.term;
exports org.aya.syntax.ref;
exports org.aya.syntax.concrete.expr;
}
4 changes: 4 additions & 0 deletions base/src/main/java/org/aya/syntax/concrete/decl/Decl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.aya.syntax.concrete.decl;

public interface Decl {
}
16 changes: 16 additions & 0 deletions base/src/main/java/org/aya/syntax/concrete/expr/Expr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.aya.syntax.concrete.expr;

import org.aya.syntax.ref.LocalVar;
import org.aya.util.error.SourceNode;
import org.aya.util.error.SourcePos;
import org.jetbrains.annotations.NotNull;

public sealed interface Expr {
record Node(@NotNull SourcePos sourcePos, @NotNull Expr expr) implements SourceNode {}

record Lam(@NotNull LocalVar var, @NotNull Node body) implements Expr {}

record Ref(@NotNull LocalVar var) implements Expr {}

record App(@NotNull Node fun, @NotNull Node arg) implements Expr {}
}
4 changes: 4 additions & 0 deletions base/src/main/java/org/aya/syntax/core/def/Def.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.aya.syntax.core.def;

public interface Def {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aya.syntax.core;
package org.aya.syntax.core.term;

import org.aya.syntax.ref.LocalVar;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aya.syntax.core;
package org.aya.syntax.core.term;

import org.aya.syntax.ref.LocalVar;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.aya.syntax.core;
package org.aya.syntax.core.term;

import org.aya.syntax.ref.LocalVar;
import org.jetbrains.annotations.NotNull;

public record LamTerm(Term body) implements Term {
public record LamTerm(Term body) implements Term, Term.StableWHNF {
@Override public @NotNull Term bindAt(@NotNull LocalVar var, int depth) {
var newBody = body.bindAt(var, depth + 1);
if (newBody == body) return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aya.syntax.core;
package org.aya.syntax.core.term;

import org.aya.syntax.ref.LocalVar;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aya.syntax.core;
package org.aya.syntax.core.term;

import org.aya.syntax.ref.LocalVar;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -35,4 +35,6 @@ public sealed interface Term extends Serializable
default @NotNull Term instantiate(Term arg) {
return replace(0, arg);
}

interface StableWHNF {}
}
12 changes: 12 additions & 0 deletions base/src/main/java/org/aya/syntax/ref/AnyVar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.aya.syntax.ref;

import org.jetbrains.annotations.Debug;
import org.jetbrains.annotations.NotNull;

/**
* @author kiva
*/
@Debug.Renderer(hasChildren = "false", text = "name()")
public interface AnyVar {
@NotNull String name();
}
67 changes: 67 additions & 0 deletions base/src/main/java/org/aya/syntax/ref/DefVar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.aya.syntax.ref;

import kala.collection.immutable.ImmutableSeq;
import org.aya.syntax.concrete.decl.Decl;
import org.aya.syntax.core.def.Def;
import org.aya.util.binop.Assoc;
import org.aya.util.binop.OpDecl;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;

public final class DefVar<Core extends Def, Concrete extends Decl> implements AnyVar {
private final @NotNull String name;
/** Initialized in parsing, so it might be null for deserialized user definitions. */
public @UnknownNullability Concrete concrete;
/** Initialized in type checking or core deserialization, so it might be null for unchecked user definitions. */
public @UnknownNullability Core core;
/** Initialized in the resolver or core deserialization */
public @Nullable ImmutableSeq<String> module;
public @Nullable ImmutableSeq<String> fileModule; // TODO: unify `module` and `fileModule`
/** Initialized in the resolver or core deserialization */
public @Nullable OpDecl opDecl;

@Contract(pure = true) public @Nullable Assoc assoc() {
if (opDecl == null) return null;
if (opDecl.opInfo() == null) return null;
return opDecl.opInfo().assoc();
}

@Contract(pure = true) public @NotNull String name() {
return name;
}

private DefVar(Concrete concrete, Core core, @NotNull String name) {
this.concrete = concrete;
this.core = core;
this.name = name;
}

/** Used in user definitions. */
public static <Core extends Def, Concrete extends Decl>
@NotNull DefVar<Core, Concrete> concrete(@NotNull Concrete concrete, @NotNull String name) {
return new DefVar<>(concrete, null, name);
}

/** Used in the serialization of core and primitive definitions. */
public static <Core extends Def, Concrete extends Decl>
@NotNull DefVar<Core, Concrete> empty(@NotNull String name) {
return new DefVar<>(null, null, name);
}

@Override public boolean equals(Object o) {
return this == o;
}

public boolean isInModule(@NotNull ImmutableSeq<String> moduleName) {
var maybeSubmodule = module;
if (maybeSubmodule == null) return false;
if (maybeSubmodule.sizeLessThan(moduleName.size())) return false;
return maybeSubmodule.sliceView(0, moduleName.size()).sameElements(moduleName);
}

public @NotNull ImmutableSeq<String> qualifiedName() {
return module == null ? ImmutableSeq.of(name) : module.appended(name);
}
}
4 changes: 4 additions & 0 deletions base/src/test/java/org/aya/syntax/core/BindTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.aya.syntax.core;

import org.aya.syntax.core.term.AppTerm;
import org.aya.syntax.core.term.FreeTerm;
import org.aya.syntax.core.term.LamTerm;
import org.aya.syntax.core.term.LocalTerm;
import org.aya.syntax.ref.LocalVar;
import org.aya.util.error.SourcePos;
import org.junit.jupiter.api.Test;
Expand Down

0 comments on commit ed4bfa2

Please sign in to comment.