This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
448f80e
commit 16f1281
Showing
4 changed files
with
46 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |