Skip to content

Commit

Permalink
refact Uniqid; fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mio-19 committed Oct 23, 2024
1 parent 853cdc9 commit b2565a9
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 96 deletions.
6 changes: 3 additions & 3 deletions syntax/src/main/scala/chester/syntax/concrete/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ case class ResolvedIdentifier(

@deprecated("not used")
case class ResolvedLocalVar(
name: Name,
varId: Uniqid,
meta: Option[ExprMeta]
name: Name,
varId: Uniqid,
meta: Option[ExprMeta]
) extends Expr {
override type ThisTree = ResolvedLocalVar

Expand Down
56 changes: 28 additions & 28 deletions syntax/src/main/scala/chester/syntax/core/Term.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1053,10 +1053,10 @@ sealed trait ReferenceCall extends Term with Uneval with TermWithUniqid derives
}

case class LocalV(
name: Name,
ty: Term,
uniqId: UniqidOf[LocalV],
meta: OptionTermMeta
name: Name,
ty: Term,
uniqId: UniqidOf[LocalV],
meta: OptionTermMeta
) extends ReferenceCall derives ReadWriter {
override type ThisTree = LocalV
override def descent(f: Term => Term, g: SpecialMap): LocalV = thisOr(
Expand All @@ -1070,10 +1070,10 @@ case class LocalV(
}

case class ToplevelV(
id: AbsoluteRef,
ty: Term,
uniqId: UniqidOf[ToplevelV],
meta: OptionTermMeta
id: AbsoluteRef,
ty: Term,
uniqId: UniqidOf[ToplevelV],
meta: OptionTermMeta
) extends ReferenceCall {
override type ThisTree = ToplevelV
override def descent(f: Term => Term, g: SpecialMap): ToplevelV = thisOr(
Expand Down Expand Up @@ -1270,11 +1270,11 @@ sealed trait TypeDefinition extends StmtTerm with TermWithUniqid derives ReadWri
}

case class RecordStmtTerm(
name: Name,
uniqId: UniqidOf[RecordStmtTerm] = Uniqid.generate[RecordStmtTerm],
fields: Vector[FieldTerm],
body: Option[BlockTerm],
meta: OptionTermMeta
name: Name,
uniqId: UniqidOf[RecordStmtTerm] = Uniqid.generate[RecordStmtTerm],
fields: Vector[FieldTerm],
body: Option[BlockTerm],
meta: OptionTermMeta
) extends TypeDefinition {
override type ThisTree = RecordStmtTerm
override def switchUniqId(r: UReplacer): RecordStmtTerm = copy(uniqId = r(uniqId))
Expand Down Expand Up @@ -1312,11 +1312,11 @@ case class RecordConstructorCallTerm(
}
}
case class TraitStmtTerm(
name: Name,
uniqId: UniqidOf[TraitStmtTerm] = Uniqid.generate[TraitStmtTerm],
extendsClause: Option[Term] = None,
body: Option[BlockTerm] = None,
meta: OptionTermMeta
name: Name,
uniqId: UniqidOf[TraitStmtTerm] = Uniqid.generate[TraitStmtTerm],
extendsClause: Option[Term] = None,
body: Option[BlockTerm] = None,
meta: OptionTermMeta
) extends TypeDefinition derives ReadWriter {
override type ThisTree = TraitStmtTerm
override def switchUniqId(r: UReplacer): TraitStmtTerm = copy(uniqId = r(uniqId))
Expand All @@ -1338,11 +1338,11 @@ case class TraitStmtTerm(
}

case class InterfaceStmtTerm(
name: Name,
uniqId: UniqidOf[InterfaceStmtTerm] = Uniqid.generate[InterfaceStmtTerm],
extendsClause: Option[Term] = None,
body: Option[BlockTerm] = None,
meta: OptionTermMeta
name: Name,
uniqId: UniqidOf[InterfaceStmtTerm] = Uniqid.generate[InterfaceStmtTerm],
extendsClause: Option[Term] = None,
body: Option[BlockTerm] = None,
meta: OptionTermMeta
) extends TypeDefinition derives ReadWriter {
override type ThisTree = InterfaceStmtTerm
override def switchUniqId(r: UReplacer): InterfaceStmtTerm = copy(uniqId = r(uniqId))
Expand All @@ -1363,11 +1363,11 @@ case class InterfaceStmtTerm(
}
}
case class ObjectStmtTerm(
name: Name,
uniqId: UniqidOf[ObjectStmtTerm],
extendsClause: Option[Term],
body: Option[BlockTerm],
meta: OptionTermMeta
name: Name,
uniqId: UniqidOf[ObjectStmtTerm],
extendsClause: Option[Term],
body: Option[BlockTerm],
meta: OptionTermMeta
) extends TypeDefinition derives ReadWriter {
override def switchUniqId(r: UReplacer): ObjectStmtTerm = copy(uniqId = r(uniqId))
override def toDoc(using options: PrettierOptions): Doc = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ case class RecordMember()

@deprecated("deprecated")
case class TyckedRecord(
name: Name,
varId: Uniqid,
members: Vector[RecordMember],
meta: Option[TermMeta] = None
name: Name,
varId: Uniqid,
members: Vector[RecordMember],
meta: Option[TermMeta] = None
) extends TyckedDefinitionNamed

@deprecated("deprecated")
case class TyckedDef(
name: Name,
varId: Uniqid,
body: Judge,
meta: Option[TermMeta] = None
name: Name,
varId: Uniqid,
body: Judge,
meta: Option[TermMeta] = None
) extends TyckedDefinitionNamed
8 changes: 3 additions & 5 deletions syntax/src/main/scala/chester/uniqid/Uniqid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ private val uniqIdCounter = AtomicInteger(0)
type Uniqid = UniqidOf[Any]

private val rwUniqID: ReadWriter[UniqidOf[Any]] =
readwriter[java.lang.Integer].bimap(_.id, x=>UniqidOf(x.toInt.refineUnsafe))
readwriter[java.lang.Integer].bimap(_.id, x => UniqidOf(x.toInt.refineUnsafe))

implicit inline def rwUniqIDOf[T]: ReadWriter[UniqidOf[T]] = rwUniqID.asInstanceOf[ReadWriter[UniqidOf[T]]]

case class UniqidOf[+A] private[uniqid] (id: Int :| Positive0) extends AnyVal {

}
case class UniqidOf[+A] private[uniqid] (id: Int :| Positive0) extends AnyVal {}
type UniqidOffset = Int :| Positive0

private implicit val UniqIdOffsetRW: ReadWriter[UniqidOffset] =
Expand Down Expand Up @@ -99,7 +97,7 @@ object Uniqid {

def is(x: Any): Boolean = x match {
case UniqidOf(_) => true
case _ => false
case _ => false
}

def calculateRange[T <: ContainsUniqid](x: T): UniqIdRange = {
Expand Down
28 changes: 14 additions & 14 deletions tyck/src/main/scala/chester/tyck/Context.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ object TyAndVal {}

/** for pure values only like let and def. record is not included */
case class ContextItem(
name: Name,
uniqId: UniqidOf[ReferenceCall],
ref: ReferenceCall,
ty: Term,
reference: Option[SymbolCollector] = None
name: Name,
uniqId: UniqidOf[ReferenceCall],
ref: ReferenceCall,
ty: Term,
reference: Option[SymbolCollector] = None
)
object ContextItem {}
case class Imports()
Expand All @@ -92,16 +92,16 @@ object Imports {
}

case class Context(
map: Map[Name, UniqidOf[ReferenceCall]] = Map.empty[Name, UniqidOf[ReferenceCall]], // empty[...] are needed because compiler bugs
contextItems: Map[UniqidOf[ReferenceCall], ContextItem] =
map: Map[Name, UniqidOf[ReferenceCall]] = Map.empty[Name, UniqidOf[ReferenceCall]], // empty[...] are needed because compiler bugs
contextItems: Map[UniqidOf[ReferenceCall], ContextItem] =
Map.empty[UniqidOf[ReferenceCall], ContextItem], // empty[...] are needed because compiler bugs
knownMap: Map[UniqidOf[ReferenceCall], TyAndVal] = Map.empty[UniqidOf[ReferenceCall], TyAndVal], // empty[...] are needed because compiler bugs
typeDefinitionNames: Map[Name, UniqidOf[TypeDefinition]] = Map.empty,
typeDefinitions: Map[UniqidOf[TypeDefinition], TypeDefinition] = Map.empty,
imports: Imports = Imports.Empty,
loadedModules: LoadedModules = LoadedModules.Empty,
operators: OperatorsContext = OperatorsContext.Default,
currentModule: ModuleRef = DefaultModule
knownMap: Map[UniqidOf[ReferenceCall], TyAndVal] = Map.empty[UniqidOf[ReferenceCall], TyAndVal], // empty[...] are needed because compiler bugs
typeDefinitionNames: Map[Name, UniqidOf[TypeDefinition]] = Map.empty,
typeDefinitions: Map[UniqidOf[TypeDefinition], TypeDefinition] = Map.empty,
imports: Imports = Imports.Empty,
loadedModules: LoadedModules = LoadedModules.Empty,
operators: OperatorsContext = OperatorsContext.Default,
currentModule: ModuleRef = DefaultModule
) {
def updateModule(module: ModuleRef): Context = copy(currentModule = module)

Expand Down
24 changes: 12 additions & 12 deletions tyck/src/main/scala/chester/tyck/ElaboraterBlock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,29 @@ trait ElaboraterBlock extends Elaborater {

// Case class for 'record' declarations
case class RecordDeclaration(
expr: RecordStmt,
uniqId: UniqidOf[RecordStmtTerm],
name: Name
expr: RecordStmt,
uniqId: UniqidOf[RecordStmtTerm],
name: Name
) extends DeclarationInfo

// New declarations for trait and interface
case class TraitDeclaration(
expr: TraitStmt,
uniqId: UniqidOf[TraitStmtTerm],
name: Name
expr: TraitStmt,
uniqId: UniqidOf[TraitStmtTerm],
name: Name
) extends DeclarationInfo

case class InterfaceDeclaration(
expr: InterfaceStmt,
uniqId: UniqidOf[InterfaceStmtTerm],
name: Name
expr: InterfaceStmt,
uniqId: UniqidOf[InterfaceStmtTerm],
name: Name
) extends DeclarationInfo

// Add case class for 'object' declarations
case class ObjectDeclaration(
expr: ObjectStmt,
uniqId: UniqidOf[ObjectStmtTerm],
name: Name
expr: ObjectStmt,
uniqId: UniqidOf[ObjectStmtTerm],
name: Name
) extends DeclarationInfo

def elabBlock(expr: Block, ty0: CellIdOr[Term], effects: CIdOf[EffectsCell])(using
Expand Down
8 changes: 4 additions & 4 deletions tyck/src/main/scala/chester/tyck/ElaboraterCommon.scala
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,10 @@ trait ElaboraterBase extends CommonPropagator[Tyck] {
}

def newLocalv(
name: Name,
ty: CellIdOr[Term],
id: UniqidOf[LocalV],
meta: Option[ExprMeta]
name: Name,
ty: CellIdOr[Term],
id: UniqidOf[LocalV],
meta: Option[ExprMeta]
)(using ck: Tyck, state: StateAbility[Tyck]): LocalV = {
val m = convertMeta(meta)
LocalV(name, toTerm(ty), id, m)
Expand Down
32 changes: 16 additions & 16 deletions tyck/src/main/scala/chester/tyck/api/SemanticCollector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ trait SemanticCollector {
def highlightLiteral(expr: Expr): Unit = ()

def newSymbol(
call: ReferenceCall,
id: UniqidOf[ReferenceCall],
definedOn: Expr,
localCtx: Context
call: ReferenceCall,
id: UniqidOf[ReferenceCall],
definedOn: Expr,
localCtx: Context
): SymbolCollector = NoopSymbolCollector

def metaFinished(replace: MetaTerm => Term): Unit = ()
Expand All @@ -35,10 +35,10 @@ private implicit inline def rwUniqIDOfVar[T]: ReadWriter[UniqidOf[ReferenceCall]

// TODO: handle when call's ty is MetaTerm
case class CollectedSymbol(
call: ReferenceCall,
id: UniqidOf[ReferenceCall],
definedOn: Expr,
referencedOn: Vector[Expr]
call: ReferenceCall,
id: UniqidOf[ReferenceCall],
definedOn: Expr,
referencedOn: Vector[Expr]
) derives ReadWriter {
def name: Name = call.name

Expand All @@ -51,10 +51,10 @@ class VectorSemanticCollector extends SemanticCollector {
private var builder: mutable.ArrayDeque[CollectedSymbol] =
new mutable.ArrayDeque[CollectedSymbol]()
override def newSymbol(
call: ReferenceCall,
id: UniqidOf[ReferenceCall],
definedOn: Expr,
localCtx: Context
call: ReferenceCall,
id: UniqidOf[ReferenceCall],
definedOn: Expr,
localCtx: Context
): SymbolCollector = {
val index = builder.length
builder.append(CollectedSymbol(call, id, definedOn, Vector()))
Expand All @@ -78,10 +78,10 @@ object NoopSemanticCollector extends SemanticCollector {}
class UnusedVariableWarningWrapper(x: SemanticCollector) extends SemanticCollector {
private var unusedVariables: Vector[CollectedSymbol] = Vector()
override def newSymbol(
call: ReferenceCall,
id: UniqidOf[ReferenceCall],
definedOn: Expr,
localCtx: Context
call: ReferenceCall,
id: UniqidOf[ReferenceCall],
definedOn: Expr,
localCtx: Context
): SymbolCollector = {
val symbolCollector = x.newSymbol(call, id, definedOn, localCtx)
val c = CollectedSymbol(call, id, definedOn, Vector())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import scala.jdk.CollectionConverters.*
trait ProvideMultithread extends ProvideImpl {

class HoldCell[+T <: Cell[?]](
val uniqId: UniqidOf[Impl[?]],
initialValue: T
val uniqId: UniqidOf[Impl[?]],
initialValue: T
) {
private val storeRef = new AtomicReference[Cell[?]](initialValue)
val readingPropagators = new ConcurrentLinkedQueue[PIdOf[Propagator[?]]]()
Expand Down Expand Up @@ -42,8 +42,8 @@ trait ProvideMultithread extends ProvideImpl {
x.asInstanceOf[CIdOf[Cell[?]]]

class HoldPropagator[+T <: Propagator[?]](
val uniqId: UniqidOf[Impl[?]],
initialValue: T
val uniqId: UniqidOf[Impl[?]],
initialValue: T
) {
private val storeRef = new AtomicReference[Propagator[?]](initialValue)
private val aliveRef = new AtomicBoolean(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ trait ProvideMutable extends ProvideImpl {
type CIdOf[+T <: Cell[?]] = HoldCell[T]

class HoldPropagator[+T <: Propagator[?]](
val uniqId: UniqidOf[Impl[?]],
value: T
val uniqId: UniqidOf[Impl[?]],
value: T
) {
var store: Propagator[?] = value
var alive: Boolean = true
Expand Down

0 comments on commit b2565a9

Please sign in to comment.