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

Commit

Permalink
syntax: add DeBruijnCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
HoshinoTented authored and ice1000 committed Mar 8, 2024
1 parent 448f80e commit 16f1281
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
5 changes: 2 additions & 3 deletions base/src/main/java/org/aya/tyck/TyckState.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.tyck;

import kala.collection.mutable.MutableList;
import org.aya.syntax.core.term.Term;
import org.aya.syntax.ref.DeBruijnCtx;
import org.aya.syntax.ref.LocalCtx;
import org.jetbrains.annotations.NotNull;

public record TyckState(
@NotNull LocalCtx ctx,
@NotNull MutableList<Term> dCtx
@NotNull DeBruijnCtx dCtx
) {
}
8 changes: 0 additions & 8 deletions base/src/main/java/org/aya/tyck/tycker/StateBased.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.tyck.tycker;

import org.aya.generic.Modifier;
import org.aya.syntax.concrete.stmt.decl.TeleDecl;
import org.aya.syntax.core.def.FnDef;
import org.aya.syntax.core.def.TeleDef;
import org.aya.syntax.core.term.*;
import org.aya.syntax.core.term.call.Callable;
import org.aya.syntax.ref.DefVar;
import org.aya.tyck.Result;
import org.aya.tyck.TyckState;
import org.aya.util.Arg;
import org.jetbrains.annotations.NotNull;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2023 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.syntax.core.term;

Expand Down
43 changes: 43 additions & 0 deletions syntax/src/main/java/org/aya/syntax/ref/DeBruijnCtx.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.syntax.ref;

import kala.collection.mutable.MutableList;
import org.aya.syntax.core.term.Term;
import org.aya.util.error.InternalException;
import org.jetbrains.annotations.NotNull;

import java.util.function.Supplier;

/**
* It is NOT DeBrujin
*
* @param ctx reversed ctx, the last one is the type of 0.
*/
public record DeBruijnCtx(@NotNull MutableList<Term> ctx) {
/**
* Maps {@code 0} to {@code type}
*/
public void push(@NotNull Term type) {
ctx.append(type);
}

public @NotNull Term get(int index) {
if (index < 0) throw new InternalException("index < 0");
if (index >= ctx.size()) throw new InternalException("index >= ctx.size()");
var realIndex = ctx.size() - 1 - index;
return ctx.get(realIndex);
}

public <R> @NotNull R with(@NotNull Term type, @NotNull Supplier<R> subscope) {
push(type);
var ret = subscope.get();
pop();
return ret;
}

public @NotNull Term pop() {
if (ctx.isEmpty()) throw new InternalException("empty ctx");
return ctx.removeLast();
}
}

0 comments on commit 16f1281

Please sign in to comment.