From 8c2115b13314c25fde436402260e65a744d27c39 Mon Sep 17 00:00:00 2001 From: Kota Mizushima Date: Fri, 25 Aug 2023 00:34:52 +0900 Subject: [PATCH] Refactor ImportItem class --- .../scala/onion/compiler/ImportItem.scala | 7 ++-- src/main/scala/onion/compiler/Typing.scala | 32 +++++++++---------- .../onion/compiler/tools/ImportSpec.scala | 28 ++++++++++++++++ 3 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 src/test/scala/onion/compiler/tools/ImportSpec.scala diff --git a/src/main/scala/onion/compiler/ImportItem.scala b/src/main/scala/onion/compiler/ImportItem.scala index 79e7ab5..4996f74 100644 --- a/src/main/scala/onion/compiler/ImportItem.scala +++ b/src/main/scala/onion/compiler/ImportItem.scala @@ -11,7 +11,7 @@ package onion.compiler * @author Kota Mizushima * */ -case class ImportItem(simpleName : String, fqcn: String) { +case class ImportItem(simpleName : String, fqcn: Seq[String]) { val isOnDemand: Boolean = simpleName == "*" /** @@ -21,9 +21,10 @@ case class ImportItem(simpleName : String, fqcn: String) { */ def matches(simpleName: String): Option[String] = { if (isOnDemand) { - Some(fqcn.replaceAll("\\*", simpleName)) + if(fqcn.length == 0) None + else Some(fqcn.take(fqcn.length - 1).appended(simpleName).mkString(".")) } else if (this.simpleName == simpleName) { - Some(fqcn) + Some(fqcn.mkString(("."))) } else { None } diff --git a/src/main/scala/onion/compiler/Typing.scala b/src/main/scala/onion/compiler/Typing.scala index 16f9bbf..bc93d6c 100644 --- a/src/main/scala/onion/compiler/Typing.scala +++ b/src/main/scala/onion/compiler/Typing.scala @@ -81,25 +81,25 @@ class Typing(config: CompilerConfig) extends AnyRef with ProcessingUnit[Seq[AST. val module = unit.module val moduleName = if (module != null) module.name else null val imports = Buffer[ImportItem]( - ImportItem("*", "java.lang.*"), - ImportItem("*", "java.io.*"), - ImportItem("*", "java.util.*"), - ImportItem("*", "javax.swing.*"), - ImportItem("*", "java.awt.event.*"), - ImportItem("JByte", "java.lang.Byte"), - ImportItem("JShort", "java.lang.Short"), - ImportItem("JCharacter", "java.lang.Character"), - ImportItem("JInteger", "java.lang.Integer"), - ImportItem("JLong", "java.lang.Long"), - ImportItem("JFloat", "java.lang.Float"), - ImportItem("JDouble", "java.lang.Double"), - ImportItem("JBoolean", "java.lang.Boolean"), - ImportItem("*", "onion.*"), - ImportItem("*", if (moduleName != null) moduleName + ".*" else "*") + ImportItem("*", Seq("java", "lang", "*")), + ImportItem("*", Seq("java", "io", "*")), + ImportItem("*", Seq("java", "util", "*")), + ImportItem("*", Seq("javax", "swing", "*")), + ImportItem("*", Seq("java", "awt", "event", "*")), + ImportItem("JByte", Seq("java", "lang", "Byte")), + ImportItem("JShort", Seq("java", "lang", "Short")), + ImportItem("JCharacter", Seq("java", "lang", "Character")), + ImportItem("JInteger", Seq("java", "lang", "Integer")), + ImportItem("JLong", Seq("java", "lang", "Long")), + ImportItem("JFloat", Seq("java", "lang", "Float")), + ImportItem("JDouble", Seq("java", "lang", "Double")), + ImportItem("JBoolean", Seq("java", "lang", "Boolean")), + ImportItem("*", Seq("onion", "*")), + ImportItem("*", if (moduleName != null) moduleName.split("\\.").appended("*")else Seq("*")) ) if(unit.imports != null) { for((key, value) <- unit.imports.mapping) { - imports.append(ImportItem(key, value)) + imports.append(ImportItem(key, value.split("\\."))) } } val staticList = new StaticImportList diff --git a/src/test/scala/onion/compiler/tools/ImportSpec.scala b/src/test/scala/onion/compiler/tools/ImportSpec.scala new file mode 100644 index 0000000..2f35700 --- /dev/null +++ b/src/test/scala/onion/compiler/tools/ImportSpec.scala @@ -0,0 +1,28 @@ +package onion.compiler.tools + +import onion.tools.Shell + +class ImportSpec extends AbstractShellSpec { + describe("Import a class") { + it("import java.util.*") { + val result = shell.run( + """ + | import { + | java.util.*; + | } + | class Increment { + | public: + | static def main(args: String[]): Int { + | xs = new ArrayList(); + | xs.add(new Integer(2)); + | return xs.get(0)$Integer.intValue(); + | } + | } + """.stripMargin, + "None", + Array() + ) + assert(Shell.Success(2) == result) + } + } +} \ No newline at end of file