add js compiler

This commit is contained in:
MeiK 2020-10-28 18:56:19 +08:00
parent 09890bd293
commit 6d7438a7d8
7 changed files with 50 additions and 3 deletions

1
plugins/js/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

5
plugins/js/README.md Normal file
View File

@ -0,0 +1,5 @@
```bash
cd plugins/js
./validate.js success.js
./validate.js failure.js
```

6
plugins/js/failure.js Normal file
View File

@ -0,0 +1,6 @@
return 42; // should be inside a function
function f() {
'use strict';
var x = 042;
with (z) { }
}

11
plugins/js/package-lock.json generated Normal file
View File

@ -0,0 +1,11 @@
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"esprima": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A=="
}
}
}

1
plugins/js/success.js Normal file
View File

@ -0,0 +1 @@
console.log("Hello World!");

23
plugins/js/validate.js Executable file
View File

@ -0,0 +1,23 @@
#!/usr/local/bin/node
const fs = require('fs');
const esprima = require('esprima');
const file = process.argv[2];
const code = fs.readFileSync(file).toString();
const res = esprima.parseScript(code, { tolerant: true });
if (res.errors.length != 0) {
const split = code.split('\n');
for (const error of res.errors) {
console.error(split[error.lineNumber - 1]);
for (let i = 1; i < error.column; i++) {
process.stderr.write(' ');
}
console.error('^');
console.error(`${error.toString()}
at (${file}:${error.lineNumber}:${error.column})`);
console.error('--------------------------------------------------------------------------');
}
process.exit(1);
}
process.exit(0);

View File

@ -35,6 +35,7 @@ pub async fn judger(
path: &Path,
) -> Result<JudgeResponse> {
let mut resp = JudgeResponse::new();
// TODO: 使用配置文件
let cmd = match Language::from_i32(request.language) {
Some(Language::C) => "./a.out",
Some(Language::Cpp) => "./a.out",
@ -120,9 +121,8 @@ pub async fn compile(
Some(Language::Cpp) => "/usr/bin/g++ main.cpp -O2 -Wall --static -o a.out --std=gnu++17",
Some(Language::Python) => "/usr/bin/python3 -m compileall main.py",
Some(Language::Rust) => "/usr/bin/rustc main.rs -o a.out -C opt-level=2",
// TODO: eslint......
Some(Language::Node) => "/bin/echo hello",
Some(Language::TypeScript) => "/usr/bin/tsc",
Some(Language::Node) => "validate.js main.js",
Some(Language::TypeScript) => "/usr/bin/tsc main.ts",
Some(Language::Go) => "/usr/bin/go build -ldflags \"-s -w\" main.go",
None => return Err(Error::LanguageNotFound(request.language)),
};