From dbf8dc4bbd0e2921e70f94234d5be8f5c68f3640 Mon Sep 17 00:00:00 2001 From: Aaro Koivukangas Date: Thu, 29 Jan 2026 17:42:05 +0200 Subject: [PATCH] initial server done in scala --- .gitignore | 2 + compiler/.gitignore | 32 ++++++++++++++++ compiler/README.md | 8 ++++ compiler/build.sbt | 14 +++++++ compiler/project/build.properties | 1 + compiler/src/main/scala/Main.scala | 54 +++++++++++++++++++++++++++ compiler/src/test/scala/MySuite.scala | 9 +++++ 7 files changed, 120 insertions(+) create mode 100644 compiler/.gitignore create mode 100644 compiler/README.md create mode 100644 compiler/build.sbt create mode 100644 compiler/project/build.properties create mode 100644 compiler/src/main/scala/Main.scala create mode 100644 compiler/src/test/scala/MySuite.scala diff --git a/.gitignore b/.gitignore index 4349de8..28f2453 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ __pycache__ build .cache target +.metals +.vscode diff --git a/compiler/.gitignore b/compiler/.gitignore new file mode 100644 index 0000000..9e79245 --- /dev/null +++ b/compiler/.gitignore @@ -0,0 +1,32 @@ +# macOS +.DS_Store + +# sbt specific +dist/* +target/ +lib_managed/ +src_managed/ +project/boot/ +project/plugins/project/ +project/local-plugins.sbt +.history +.ensime +.ensime_cache/ +.sbt-scripted/ +local.sbt + +# Bloop +.bsp + +# VS Code +.vscode/ + +# Metals +.bloop/ +.metals/ +metals.sbt + +# IDEA +.idea +.idea_modules +/.worksheet/ diff --git a/compiler/README.md b/compiler/README.md new file mode 100644 index 0000000..102c5ca --- /dev/null +++ b/compiler/README.md @@ -0,0 +1,8 @@ +## sbt project compiled with Scala 3 + +### Usage + +This is a normal sbt project. You can compile code with `sbt compile`, run it with `sbt run`, and `sbt console` will start a Scala 3 REPL. + +For more information on the sbt-dotty plugin, see the +[scala3-example-project](https://github.com/scala/scala3-example-project/blob/main/README.md). diff --git a/compiler/build.sbt b/compiler/build.sbt new file mode 100644 index 0000000..6c1640b --- /dev/null +++ b/compiler/build.sbt @@ -0,0 +1,14 @@ +val scala3Version = "3.8.1" + +lazy val root = project + .in(file(".")) + .settings( + name := "compiler", + version := "0.1.0-SNAPSHOT", + + scalaVersion := scala3Version, + libraryDependencies ++= Seq( + "org.scalameta" %% "munit" % "1.0.0" % Test, + "org.scala-lang" %% "toolkit" % "0.7.0" + ) +) diff --git a/compiler/project/build.properties b/compiler/project/build.properties new file mode 100644 index 0000000..bcdf773 --- /dev/null +++ b/compiler/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.12.1 diff --git a/compiler/src/main/scala/Main.scala b/compiler/src/main/scala/Main.scala new file mode 100644 index 0000000..61e5a85 --- /dev/null +++ b/compiler/src/main/scala/Main.scala @@ -0,0 +1,54 @@ +import java.io.{BufferedReader, InputStreamReader, PrintWriter} +import java.net.{ServerSocket, Socket}; +import java.util.stream.Stream + + + +@main def hello(): Unit = + println("Hello world!") + println(msg) + run_server() + +def msg = "I was compiled by Scala 3. :)" + +def run_server(): Unit = + println("Starting server...") + val serverSocket: ServerSocket = new ServerSocket(3000); + println("Waiting for connections...") + + while true do + // Accept incoming client connection + val clientSocket: Socket = serverSocket.accept(); + println("Client connected!"); + + // Setup input and output streams for communication with the client + val in: BufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + val out: PrintWriter = new PrintWriter(clientSocket.getOutputStream(), true); + + // Read message from client + val message: String = in.readLine(); + println("Client says: " + message); + + val json: ujson.Value = ujson.read(message) + val command = json("command").str + + val response = command match { + case "ping" => "{}" + case "compile" => s"{\"program\": \"${compile(json("code").str)}\"}" + case _ => s"{\"error\": \"Incorrect json request body\"}" + } + + // Send response to the client + out.println(response); + +def compile(source: String): String = + return tokenizer(source).toString() + + +class TokenLocation(column: Int, line: Int) +class TokenLocationDebug() + +class Token(value: String, location: TokenLocation | TokenLocationDebug = TokenLocationDebug()) + +def tokenizer(source: String): List[Token] = + return List(Token("test")) diff --git a/compiler/src/test/scala/MySuite.scala b/compiler/src/test/scala/MySuite.scala new file mode 100644 index 0000000..621784d --- /dev/null +++ b/compiler/src/test/scala/MySuite.scala @@ -0,0 +1,9 @@ +// For more information on writing tests, see +// https://scalameta.org/munit/docs/getting-started.html +class MySuite extends munit.FunSuite { + test("example test that succeeds") { + val obtained = 42 + val expected = 42 + assertEquals(obtained, expected) + } +}