rbrayb
rbrayb

Reputation: 46753

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?

Upvotes: 11

Views: 2420

Answers (6)

Jed
Jed

Reputation: 1701

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.

Upvotes: 1

kuszi
kuszi

Reputation: 2079

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).

Upvotes: 4

clemesha
clemesha

Reputation: 1898

For Python and Sage, try out http://live.codenode.org. It is also an open source project, licensed under the BSD, so it can be downloaded and ran from your own computer, more info is here: http://codenode.org

Upvotes: 0

Jim Burger
Jim Burger

Reputation: 4547

In Ruby you can use the Interactive Ruby Shell.

It also looks like the guru's at the mono project have gone and made a C# interactive. YAY

Upvotes: 0

Matt Campbell
Matt Campbell

Reputation: 244

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

Upvotes: 3

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827684

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

For F# I use the Interactive Console.

Upvotes: 1

Related Questions