Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tools for code snippet execution

By "code snippet execution", I mean the ability to write a few lines of code, run and test it without having to fire up an IDE and create a dummy project.

It's incredibly useful for helping people with a small code sample without creating a project, compiling everything cleanly, sending them the code snippet and deleting the project.

I'm not asking about the best code snippets or a snippet editor or where to store snippets!

For C#, I use Snippet Compiler.

For Java, I use Eclipse Scrapbook.

For LINQ, I use LINQPad.

Any suggestions for other (better?) tools? e.g. is there one for Java that doesn't involve firing up Eclipse?

What about C?

like image 940
rbrayb Avatar asked Nov 05 '08 00:11

rbrayb


4 Answers

Try ideone. It is able to run your code on server side in more than 40 programming languages including C and Java (it is free for individual usage).

like image 87
kuszi Avatar answered Nov 06 '22 07:11

kuszi


For C, the in-browser http://codepad.org/ is truly excellent. Executes code and everything.

like image 38
Matt Campbell Avatar answered Nov 06 '22 07:11

Matt Campbell


For executing JavaScript snippets I use most Firebug and Google Chrome JavaScript console.

For F# I use the Interactive Console.

like image 1
Christian C. Salvadó Avatar answered Nov 06 '22 07:11

Christian C. Salvadó


I sometimes want to try something very short just to confirm semantics. Since creating a temporary file and putting in the boilerplate takes more than 30 seconds, I have this script:

#!/bin/sh

body="$1"
out=$(mktemp /tmp/ccrun-XXXXXX)
src=${out}.c
cat > ${src} <<EOF
#include <limits.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define UNUSED __attribute__((unused))

int main(int UNUSED argc,char UNUSED *argv[])
{
EOF
echo "$body" >> ${src}
echo -e "return 0;\n}" >> ${src}
cc -std=c99 -Wall -Wextra ${CCRUN_FLAGS} -o ${out} ${src} -lm
shift
echo ${out} "$@"
${out} "$@"
#rm ${out} ${src}

A sample invocation (this code statically initializes an array of function pointers) looks like:

$ ccrun 'int f(int a){return a+1;} int g(int a){return a+2;} int (*farr[2])(int) = {f,g}; for (int i=0; i<2; i++) printf("%d %d\n",i,farr[i](i));'
/tmp/ccrun-6nT4Wo
0 1
1 3

If I want to make little changes, I just edit the command line. If it becomes unwieldy on the command line, I'll edit the temporary file, in this case /tmp/ccrun-6nT4Wo.c. Command line arguments to the executable can be given after the program (first argument). The executable is left in place so it can be run without recompiling. You can do something similar for any language.

like image 1
Jed Avatar answered Nov 06 '22 06:11

Jed