compilr
v1.2.16
Published
Compile and execute code
Readme
Compilr
Compilr is a powerful NPM package designed to compile code in multiple programming languages and return the output using a TD's APIs. With a simple API, it allows you to send code to be compiled and executed, and retrieve the results in a straightforward manner.
Features
- Supports Multiple Languages: Compilr supports a wide range of programming languages.
- Easy to Use: Simple methods to compile and execute code.
- Handles Compilation and Execution: Manages the entire process from code submission to result retrieval.
- Error Handling: Provides clear error messages for missing or incorrect inputs.
- Base64 Encoding and Decoding: Handles base64 encoding for code and input data to ensure compatibility with the API.
- Support for Large Codebases: Can handle large codebases by encoding them efficiently in base64 format.
Installation
Install using npm:
npm install compilrInstall using pnpm:
pnpm install compilrInstall using Yarn:
yarn add compilrUsage
Require the library:
const { CompilerClient, CompilerRequestDto } = require('compilr');Create a Compiler Client Instance
const compilerClient = new CompilerClient();Prepare the Code for Compilation
const code = `
for i in range(1, 11):
print(i)
`;
const requestDto = new CompilerRequestDto(code, 'python');Compile and Execute the Code
compilerClient
.compileAndExecute(requestDto)
.then((response) => {
console.log('Output:', response.stdout);
console.log('Compilation Time:', response.time);
console.log('Memory Usage:', response.memory);
})
.catch((error) => {
console.error('Error:', error.message);
});API Reference
CompilerClient
Compiles and executes the given code.
Parameters:
data: An instance ofCompilerRequestDtocontaining the code, language, and optional input.
Returns:
- A
Promisethat resolves to an instance ofCompilerResponseDtocontaining the output, compilation time, memory usage, and other details.
- A
CompilerRequestDto
A DTO (Data Transfer Object) representing the request data for compilation.
- Properties:
code(string): The source code to be compiled.language(string): The programming language of the source code.stdin(string, optional): The standard input for the program.
CompilerResponseDto
A DTO representing the response data from the compilation and execution.
- Properties:
stdout(string | null): The standard output of the program.time(string): The time taken for compilation.memory(number): The memory used during execution.stderr(string | null): The standard error output of the program.token(string): The unique token for the compilation request.compile_output(string | null): The output of the compilation process.message(string): Any additional messages from the server.status(object): The status of the compilation request, withidanddescription.
Error Handling
Compilr provides clear error messages for common issues such as missing required fields or unsupported languages.
Example Error Handling
try {
await compilerClient.compileAndExecute(requestDto);
} catch (error) {
console.error('Error:', error.message);
}Available Programming Languages with Keywords
Compilr supports a wide range of languages, including but not limited to:
- Assembly -
assembly - Bash -
bash - C -
c - C# -
csharp - C++ -
cpp - Erlang -
erlang - Go -
go - Java -
java - JavaScript -
javascript - PHP -
php - Python -
python - Ruby -
ruby - Rust -
rust - TypeScript -
typescript - Kotlin -
kotlin - Objective-C -
objectiveC - R -
r - Scala -
scala - SQLite -
sqlite - Swift -
swift - Perl -
perl - Clojure -
clojure
Example Code Snippet
const { CompilerClient, CompilerRequestDto } = require('compilr');
const compilerClient = new CompilerClient();
var data = {
language: 'python',
code: 'print("Hello")',
input: '',
};
compilerClient
.compileAndExecute(
new CompilerRequestDto(data.code, data.language, data.input),
)
.then((result) => {
console.log('Output:', result.stdout);
console.log('Compilation Time:', result.time);
console.log('Memory Usage:', result.memory);
})
.catch((error) => {
console.error('Error:', error.message);
});Response Example
Successful Response
{
"stdout": "Hello\n",
"time": "0.049",
"memory": 3264,
"stderr": null,
"token": "b3e5c26d-44dd-4bd0-9619-45b071464243",
"compile_output": null,
"message": null,
"status": {
"id": 3,
"description": "Accepted"
}
}Compile Time Error Response
{
stdout: '',
time: null,
memory: null,
stderr: '',
token: '5a9ceb8b-ebc3-461c-9668-a8f2191a9cd7',
compile_output: 'main.cpp:2:9: error: #include expects "FILENAME" or <FILENAME>\n' +
' 2 | #include\n' +
' | ^\n',
message: '',
status: { id: 6, description: 'Compilation Error' }
}Run Time Error Response
{
stdout: '',
time: '0.027',
memory: 3268,
stderr: ' File "script.py", line 1\n' +
' print("Hello)\n' +
' ^\n' +
'SyntaxError: EOL while scanning string literal\n',
token: 'e31129d5-35fb-42e6-9d7d-26a17104dd2a',
compile_output: '',
message: 'Exited with error status 1',
status: { id: 11, description: 'Runtime Error (NZEC)' }
}About Me
Karan Dattani
With Compilr, compiling and executing code has never been easier. Get started today and streamline your coding workflow!
