mirror of
https://github.com/iritmaximus/compilers.git
synced 2026-10-12 19:13:35 +03:00
initial server done in scala
This commit is contained in:
@@ -5,3 +5,5 @@ __pycache__
|
|||||||
build
|
build
|
||||||
.cache
|
.cache
|
||||||
target
|
target
|
||||||
|
.metals
|
||||||
|
.vscode
|
||||||
|
|||||||
@@ -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/
|
||||||
@@ -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).
|
||||||
@@ -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"
|
||||||
|
)
|
||||||
|
)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
sbt.version=1.12.1
|
||||||
@@ -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"))
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user