Skip to content

Commit

Permalink
Add support for C++ programs
Browse files Browse the repository at this point in the history
  • Loading branch information
navzam committed Sep 21, 2022
1 parent ba7ee33 commit b8ca7c7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
20 changes: 17 additions & 3 deletions express.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,21 @@ if (config.server.dependencies.libkipr_c && config.server.dependencies.emsdk_env
});
}

if (!('language' in req.body)) {
return res.status(400).json({
error: "Expected language key in body"
});
}

if (typeof req.body.language !== 'string' || !['c', 'cpp'].includes(req.body.language)) {
return res.status(400).json({
error: "Expected language key in body to be a supported language string"
});
}

const id = uuid.v4();
const path = `/tmp/${id}.c`;
const fileExtension = req.body.language;
const path = `/tmp/${id}.${fileExtension}`;
fs.writeFile(path, req.body.code, err => {
if (err) {
return res.status(500).json({
Expand All @@ -60,8 +73,9 @@ if (config.server.dependencies.libkipr_c && config.server.dependencies.emsdk_env
env['PATH'] = `${config.server.dependencies.emsdk_env.PATH}:${process.env.PATH}`;
env['EMSDK'] = config.server.dependencies.emsdk_env.EMSDK;
env['EM_CONFIG'] = config.server.dependencies.emsdk_env.EM_CONFIG;

exec(`emcc -s WASM=1 -s SINGLE_FILE=1 -s INVOKE_RUN=0 -s EXIT_RUNTIME=1 -s "EXPORTED_FUNCTIONS=['_main']" -I${config.server.dependencies.libkipr_c}/include -L${config.server.dependencies.libkipr_c}/lib -lkipr -o ${path}.js ${path}`, {

const compiler = req.body.language === 'c' ? 'emcc' : 'em++';
exec(`${compiler} -s WASM=1 -s SINGLE_FILE=1 -s INVOKE_RUN=0 -s EXIT_RUNTIME=1 -s "EXPORTED_FUNCTIONS=['_main']" -I${config.server.dependencies.libkipr_c}/include -L${config.server.dependencies.libkipr_c}/lib -lkipr -o ${path}.js ${path}`, {
env
}, (err, stdout, stderr) => {
if (err) {
Expand Down
4 changes: 3 additions & 1 deletion src/ProgrammingLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
type ProgrammingLanguage = 'c' | 'python';
type ProgrammingLanguage = 'c' | 'cpp' | 'python';

namespace ProgrammingLanguage {
export const fileExtension = (language: ProgrammingLanguage) => {
switch (language) {
case 'c':
return 'c';
case 'cpp':
return 'cpp';
case 'python':
return 'py';
}
Expand Down
7 changes: 5 additions & 2 deletions src/compile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default (code: string): Promise<CompileResult> => {
import ProgrammingLanguage from "./ProgrammingLanguage";

export default (code: string, language: ProgrammingLanguage): Promise<CompileResult> => {

return new Promise<CompileResult>((resolve, reject) => {
const req = new XMLHttpRequest();
Expand All @@ -19,7 +21,8 @@ export default (code: string): Promise<CompileResult> => {
req.setRequestHeader('Content-Type', 'application/json');

req.send(JSON.stringify({
code
code,
language,
}));
});

Expand Down
3 changes: 3 additions & 0 deletions src/components/Editor/LanguageSelectCharm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const Container = styled('div', {
const OPTIONS: ComboBox.Option[] = [{
text: 'C',
data: 'c'
}, {
text: 'C++',
data: 'cpp'
}, {
text: 'Python',
data: 'python'
Expand Down
8 changes: 5 additions & 3 deletions src/components/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export class Root extends React.Component<Props, State> {
activeLanguage: 'c',
code: {
'c': window.localStorage.getItem('code-c') || '#include <stdio.h>\n#include <kipr/wombat.h>\n\nint main()\n{\n printf("Hello, World!\\n");\n return 0;\n}\n',
'cpp': window.localStorage.getItem('code-cpp') || '#include <iostream>\n#include <kipr/wombat.hpp>\n\nint main()\n{\n std::cout << "Hello, World!" << std::endl;\n return 0;\n}\n',
'python': window.localStorage.getItem('code-python') || 'from kipr import *\n\nprint(\'Hello, World!\')',
},
modal: Modal.NONE,
Expand Down Expand Up @@ -288,7 +289,8 @@ export class Root extends React.Component<Props, State> {
const activeCode = code[activeLanguage];

switch (activeLanguage) {
case 'c': {
case 'c':
case 'cpp': {
let nextConsole: StyledText = StyledText.extend(console, StyledText.text({
text: `Compiling...\n`,
style: STDOUT_STYLE(this.state.theme)
Expand All @@ -298,7 +300,7 @@ export class Root extends React.Component<Props, State> {
simulatorState: SimulatorState.COMPILING,
console: nextConsole
}, () => {
compile(activeCode)
compile(activeCode, activeLanguage)
.then(compileResult => {
nextConsole = this.state.console;
const messages = sort(parseMessages(compileResult.stderr));
Expand All @@ -322,7 +324,7 @@ export class Root extends React.Component<Props, State> {
}));

WorkerInstance.start({
language: 'c',
language: activeLanguage,
code: compileResult.result
});
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ const startPython = async (message: Protocol.Worker.StartRequest) => {

const start = async (message: Protocol.Worker.StartRequest) => {
switch (message.language) {
case 'c': {
case 'c':
case 'cpp': {
startC(message);
break;
}
Expand Down

0 comments on commit b8ca7c7

Please sign in to comment.