Skip to content

Commit 45b085a

Browse files
authored
feat(lang): support java and golang (#11)
1 parent bb19118 commit 45b085a

File tree

11 files changed

+190
-14
lines changed

11 files changed

+190
-14
lines changed

.changes/lang.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eval-stack": patch:feat
3+
---
4+
5+
Support for Java and Golang.

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"covector",
77
"fmax",
88
"getuid",
9+
"javac",
910
"libc",
1011
"NEWNS",
1112
"prctl",

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Online Judge core runtime for ACMers",
55
"main": "index.js",
66
"scripts": {
7-
"covector": "covector"
7+
"covector": "covector",
8+
"test": "cargo test"
89
},
910
"keywords": [
1011
"oj",

src/case.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ where
3434
let exec_path = match &language {
3535
Language::Python => which("python")?,
3636
Language::NodeJs => which("deno")?,
37+
Language::Java => which("java")?,
3738
_ => workspace.join("out"),
3839
};
3940

@@ -65,9 +66,11 @@ where
6566
"--deny-ffi=*",
6667
source_file_path.as_str(),
6768
];
69+
let java_args = vec!["Main"];
6870
let args = match language {
6971
Language::Python => Some(&py_args),
7072
Language::NodeJs => Some(&deno_args),
73+
Language::Java => Some(&java_args),
7174
_ => None,
7275
}
7376
.map(|v| &**v);

src/compile.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,30 @@ use std::{
55
};
66

77
use anyhow::Result;
8-
use tokio::process::Command;
8+
use tokio::{fs::File, io, process::Command};
99

10-
#[derive(Clone, Copy)]
10+
#[derive(Default, Debug, Clone, Copy)]
1111
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1212
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
1313
pub enum Language {
14+
#[default]
15+
Rust,
1416
C,
1517
CPP,
16-
Rust,
1718
Python,
1819
NodeJs,
20+
Golang,
21+
Java,
1922
}
2023

21-
pub async fn compile<B: Into<PathBuf>, E: AsRef<str>, O: AsRef<str>>(
24+
pub async fn compile<B: Into<PathBuf>, S: Into<PathBuf>, O: AsRef<str>>(
2225
language: Language,
2326
base: B,
24-
source_file: E,
27+
source_file_path: S,
2528
output_file: O,
2629
) -> Result<()> {
2730
let base_path = Into::<PathBuf>::into(base);
28-
let source_path = base_path.join(source_file.as_ref());
31+
let source_path = Into::<PathBuf>::into(source_file_path);
2932
let source_path_str = source_path.to_string_lossy();
3033
let output_path = base_path.join(output_file.as_ref());
3134
let output_path_str = output_path.to_string_lossy();
@@ -84,6 +87,27 @@ pub async fn compile<B: Into<PathBuf>, E: AsRef<str>, O: AsRef<str>>(
8487
Some(command)
8588
}
8689
Language::NodeJs => None,
90+
Language::Golang => {
91+
let mut command = Command::new("go");
92+
command.args([
93+
"build",
94+
"-o",
95+
output_path_str.as_ref(),
96+
source_path_str.as_ref(),
97+
]);
98+
Some(command)
99+
}
100+
Language::Java => {
101+
let java_path = base_path.join("Main.java");
102+
let mut command = Command::new("javac");
103+
io::copy(
104+
&mut File::open(source_path_str.as_ref()).await?,
105+
&mut File::create(&java_path).await?,
106+
)
107+
.await?;
108+
command.arg(java_path.to_string_lossy().as_ref());
109+
Some(command)
110+
}
87111
};
88112

89113
if let Some(mut command) = command {

tests/test.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
3+
typedef long long int i64;
4+
5+
int main()
6+
{
7+
i64 a, b;
8+
scanf("%lld %lld", &a, &b);
9+
printf("%lld\n", a + b);
10+
printf("%lld\n", a + b);
11+
return 0;
12+
}

tests/test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
var num1, num2 int32
9+
10+
fmt.Scan(&num1)
11+
12+
fmt.Scan(&num2)
13+
14+
sum := num1 + num2
15+
fmt.Println(sum)
16+
fmt.Println(sum)
17+
}

tests/test.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.Scanner;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Scanner scanner = new Scanner(System.in);
6+
7+
int num1 = scanner.nextInt();
8+
9+
int num2 = scanner.nextInt();
10+
11+
int sum = num1 + num2;
12+
System.out.printf("%d\n", sum);
13+
System.out.printf("%d", sum);
14+
15+
scanner.close();
16+
}
17+
}

tests/test_fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use eval_stack::{case::run_test_cases, compile::Language, config::JudgeOptions};
66
#[tokio::test]
77
async fn test_fs() -> Result<()> {
88
let current_dir = std::env::current_dir()?;
9-
let workspace_path = current_dir.join("workspace");
9+
let workspace_path = current_dir.join("fs_workspace");
1010
let tests_path = current_dir.join("tests");
1111

1212
let results = run_test_cases(

0 commit comments

Comments
 (0)