Skip to content

Commit

Permalink
move files
Browse files Browse the repository at this point in the history
  • Loading branch information
mio-19 committed Oct 26, 2024
1 parent 17f9cde commit 4665c41
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 105 deletions.
92 changes: 92 additions & 0 deletions tyck-base/src/main/scala/chester/tyck/Context.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package chester.tyck

import chester.syntax.*
import chester.syntax.accociativity.OperatorsContext
import chester.syntax.core.*
import chester.tyck.api.SymbolCollector
import chester.uniqid.UniqidOf

case class TyAndVal(
ty: Term,
value: Term
) {}

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
)
object ContextItem {}
case class Imports()

object Imports {
val Empty: Imports = 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.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
) {
def updateModule(module: ModuleRef): Context = copy(currentModule = module)

def getKnown(x: ReferenceCall): Option[TyAndVal] =
knownMap.get(x.uniqId.asInstanceOf[UniqidOf[ReferenceCall]])

def get(id: Name): Option[ContextItem] =
map.get(id).flatMap(uniqId => contextItems.get(uniqId))

def knownAdd(id: UniqidOf[ReferenceCall], y: TyAndVal): Context =
knownAdd(Seq(id -> y))

def knownAdd(
seq: Seq[(UniqidOf[ReferenceCall], TyAndVal)]
): Context = {
val newKnownMap = seq.foldLeft(knownMap) { (acc, item) =>
assert(!acc.contains(item._1), s"Duplicate key ${item._1}")
acc + item
}
copy(knownMap = newKnownMap)
}

def add(item: ContextItem): Context = add(Seq(item))

def add(seq: Seq[ContextItem]): Context = {
val newMap = seq.foldLeft(map) { (acc, item) =>
acc + (item.name -> item.uniqId)
}
val newContextItems = seq.foldLeft(contextItems) { (acc, item) =>
require(!acc.contains(item.uniqId), s"Duplicate key ${item.uniqId}")
acc + (item.uniqId -> item)
}
copy(map = newMap, contextItems = newContextItems)
}
def addTypeDefinition(typeDef: TypeDefinition): Context = {
copy(
typeDefinitionNames = typeDefinitionNames + (typeDef.name -> typeDef.uniqId),
typeDefinitions = typeDefinitions + (typeDef.uniqId -> typeDef)
)
}

def getTypeDefinition(name: Name): Option[TypeDefinition] = {
typeDefinitionNames.get(name).flatMap(typeDefinitions.get)
}

def getTypeDefinitionById(id: UniqidOf[TypeDefinition]): Option[TypeDefinition] = {
typeDefinitions.get(id)
}
}

object Context {}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ trait SemanticCollector {
def highlightLiteral(expr: Expr): Unit = ()

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

def metaFinished(replace: MetaTerm => Term): Unit = ()
}
Expand All @@ -35,11 +35,11 @@ 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]
) derives ReadWriter {
call: ReferenceCall,
id: UniqidOf[ReferenceCall],
definedOn: Expr,
referencedOn: Vector[Expr]
) derives ReadWriter {
def name: Name = call.name

def metaFinished(replace: MetaTerm => Term): CollectedSymbol = {
Expand All @@ -51,11 +51,11 @@ 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
): SymbolCollector = {
call: ReferenceCall,
id: UniqidOf[ReferenceCall],
definedOn: Expr,
localCtx: Context
): SymbolCollector = {
val index = builder.length
builder.append(CollectedSymbol(call, id, definedOn, Vector()))
assert(builder.length == index + 1)
Expand All @@ -78,11 +78,11 @@ 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
): SymbolCollector = {
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())
unusedVariables = unusedVariables :+ c
Expand Down
85 changes: 0 additions & 85 deletions tyck/src/main/scala/chester/tyck/Context.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,88 +68,3 @@ trait ProvideCtx extends ProvideCellId with ElaboraterBase {
}

}

case class TyAndVal(
ty: Term,
value: Term
) {}

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
)
object ContextItem {}
case class Imports()

object Imports {
val Empty: Imports = 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.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
) {
def updateModule(module: ModuleRef): Context = copy(currentModule = module)

def getKnown(x: ReferenceCall): Option[TyAndVal] =
knownMap.get(x.uniqId.asInstanceOf[UniqidOf[ReferenceCall]])

def get(id: Name): Option[ContextItem] =
map.get(id).flatMap(uniqId => contextItems.get(uniqId))

def knownAdd(id: UniqidOf[ReferenceCall], y: TyAndVal): Context =
knownAdd(Seq(id -> y))

def knownAdd(
seq: Seq[(UniqidOf[ReferenceCall], TyAndVal)]
): Context = {
val newKnownMap = seq.foldLeft(knownMap) { (acc, item) =>
assert(!acc.contains(item._1), s"Duplicate key ${item._1}")
acc + item
}
copy(knownMap = newKnownMap)
}

def add(item: ContextItem): Context = add(Seq(item))

def add(seq: Seq[ContextItem]): Context = {
val newMap = seq.foldLeft(map) { (acc, item) =>
acc + (item.name -> item.uniqId)
}
val newContextItems = seq.foldLeft(contextItems) { (acc, item) =>
require(!acc.contains(item.uniqId), s"Duplicate key ${item.uniqId}")
acc + (item.uniqId -> item)
}
copy(map = newMap, contextItems = newContextItems)
}
def addTypeDefinition(typeDef: TypeDefinition): Context = {
copy(
typeDefinitionNames = typeDefinitionNames + (typeDef.name -> typeDef.uniqId),
typeDefinitions = typeDefinitions + (typeDef.uniqId -> typeDef)
)
}

def getTypeDefinition(name: Name): Option[TypeDefinition] = {
typeDefinitionNames.get(name).flatMap(typeDefinitions.get)
}

def getTypeDefinitionById(id: UniqidOf[TypeDefinition]): Option[TypeDefinition] = {
typeDefinitions.get(id)
}
}

object Context {}

0 comments on commit 4665c41

Please sign in to comment.