Skip to content

A NodeJS mediator for compile and execute code from other languages

License

Notifications You must be signed in to change notification settings

gabrielgouv/code-runner-js

Repository files navigation

Compiler Mediator for NodeJS

This is a WIP project. Tested only in linux (Ubuntu 18.04).

To run all tests successfully you must have installed in your machine:

  • Java
  • PHP CLI
  • Python 2
  • Python 3
  • Bash

These above requirements will be removed soon.

Set up a new compiler (pre-release v0.1.0) (outdated)

To setup a new compiler is pretty simple. You have three ways to add a new compiler:

1) Via langs file

  1. Go to src/common/langs.ts file. It look like this:
export const lang = {
    java: { 
        compileCommand: 'javac {fileName}',
        runCommand: 'java {compiledFileName}',
        filePath: './__tests__/files/java'
    },
    python: { 
        runCommand: 'python{version} {fileName}',
        filePath: './__tests__/files/python'
    }
}
  1. You can add a new object that sets up your new compiler. Here, we add a C compiler that uses GCC:
c: { 
    compileCommand: 'gcc MyFile.c -o MyFile',
    runCommand: './MyFile',
    filePath: 'path/to/c/file'
}
  1. Now you can use your compiler. In the constructor you need only pass a lang option that references your compiler definitions in langs file:
let myCompiler: Compiler = new Compiler({
    lang: lang.c
})
myCompiler.execute().subscribe((result) => {
    console.log(result.output)
})

2) Via Compiler contructor

  1. In a Compiler constructor you can pass compiler options (CompilerOptions interface), same names as you passed in langs file:
let myCompiler: Compiler = new Compiler({
    compileCommand: 'gcc MyFile.c -o MyFile',
    runCommand: './MyFile',
    filePath: 'path/to/c/file'
})
  1. And than, execute it!
myCompiler.execute().subscribe((result) => {
    console.log(result.output)
})

3) Mixing the last two ways

If you noticed, we set a fixed file name in the compileCommand and runCommand option. But if we need a dynamic file name? Let's make some changes to our compiler definition in langs file.

  1. We can pass dynamic variables in our commands:
c: { 
    compileCommand: 'gcc {fileName} -o {compiledFileName}',
    runCommand: './{compiledFileName}',
    filePath: 'path/to/c/file'
}

Variables are always used inside two braces ({variableName}). By default there are four types of variables. These variables will be replaced by the compiler options that have the same name:

Variable name Description
version Replaced by version compiler option
fileName Replaced by fileName compiler option
compiledFileName Replaced by compiledFileName option
filePath Replaced by compiledFileName option
  1. Now you can define a "template" of your commands in the langs file and in the constructor options you can define the variables values:
let myCompiler: Compiler = new Compiler({
    lang: lang.c,
    fileName: 'MyCFile.c',
    compiledFileName: 'MyCFile'
})
  1. Finally you can normaly execute your compiler:
myCompiler.execute().subscribe((result) => {
    console.log(result.output)
})

Note that {fileName} and {compiledFileName} will be replaced by MyCFile.c and MyCFile respectively.

BE CAREFUL: Compiler options defined in the constructor overrides lang file defined options.

NOTE: The C and Java language, other than Python, for example, needs to be compiled before it can be run. For these cases add the option compileCommand and runCommand. If the language can be executed directly, only the runCommand is needed.

About

A NodeJS mediator for compile and execute code from other languages

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages