From 4665c410fad483072ad1fac0d2abf17b3ad35ad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BE=AA?= Date: Sat, 26 Oct 2024 20:40:23 +1300 Subject: [PATCH] move files --- .../src/main/scala/chester/tyck/Context.scala | 92 +++++++++++++++++++ .../chester/tyck/api/SemanticCollector.scala | 40 ++++---- .../src/main/scala/chester/tyck/Context.scala | 85 ----------------- 3 files changed, 112 insertions(+), 105 deletions(-) create mode 100644 tyck-base/src/main/scala/chester/tyck/Context.scala rename {tyck => tyck-base}/src/main/scala/chester/tyck/api/SemanticCollector.scala (71%) diff --git a/tyck-base/src/main/scala/chester/tyck/Context.scala b/tyck-base/src/main/scala/chester/tyck/Context.scala new file mode 100644 index 00000000..119e1467 --- /dev/null +++ b/tyck-base/src/main/scala/chester/tyck/Context.scala @@ -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 {} diff --git a/tyck/src/main/scala/chester/tyck/api/SemanticCollector.scala b/tyck-base/src/main/scala/chester/tyck/api/SemanticCollector.scala similarity index 71% rename from tyck/src/main/scala/chester/tyck/api/SemanticCollector.scala rename to tyck-base/src/main/scala/chester/tyck/api/SemanticCollector.scala index 2eed9f50..31dded5c 100644 --- a/tyck/src/main/scala/chester/tyck/api/SemanticCollector.scala +++ b/tyck-base/src/main/scala/chester/tyck/api/SemanticCollector.scala @@ -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 = () } @@ -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 = { @@ -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) @@ -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 diff --git a/tyck/src/main/scala/chester/tyck/Context.scala b/tyck/src/main/scala/chester/tyck/Context.scala index 043bef58..60ae4ca8 100644 --- a/tyck/src/main/scala/chester/tyck/Context.scala +++ b/tyck/src/main/scala/chester/tyck/Context.scala @@ -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 {}