C-like mode

x
 
1
/* C demo code */
2
3
#include <zmq.h>
4
#include <pthread.h>
5
#include <semaphore.h>
6
#include <time.h>
7
#include <stdio.h>
8
#include <fcntl.h>
9
#include <malloc.h>
10
11
typedef struct {
12
  void* arg_socket;
13
  zmq_msg_t* arg_msg;
14
  char* arg_string;
15
  unsigned long arg_len;
16
  int arg_int, arg_command;
17
18
  int signal_fd;
19
  int pad;
20
  void* context;
21
  sem_t sem;
22
} acl_zmq_context;
23
24
#define p(X) (context->arg_##X)
25
26
void* zmq_thread(void* context_pointer) {
27
  acl_zmq_context* context = (acl_zmq_context*)context_pointer;
28
  char ok = 'K', err = 'X';
29
  int res;
30
31
  while (1) {
32
    while ((res = sem_wait(&context->sem)) == EINTR);
33
    if (res) {write(context->signal_fd, &err, 1); goto cleanup;}
34
    switch(p(command)) {
35
    case 0: goto cleanup;
36
    case 1: p(socket) = zmq_socket(context->context, p(int)); break;
37
    case 2: p(int) = zmq_close(p(socket)); break;
38
    case 3: p(int) = zmq_bind(p(socket), p(string)); break;
39
    case 4: p(int) = zmq_connect(p(socket), p(string)); break;
40
    case 5: p(int) = zmq_getsockopt(p(socket), p(int), (void*)p(string), &p(len)); break;
41
    case 6: p(int) = zmq_setsockopt(p(socket), p(int), (void*)p(string), p(len)); break;
42
    case 7: p(int) = zmq_send(p(socket), p(msg), p(int)); break;
43
    case 8: p(int) = zmq_recv(p(socket), p(msg), p(int)); break;
44
    case 9: p(int) = zmq_poll(p(socket), p(int), p(len)); break;
45
    }
46
    p(command) = errno;
47
    write(context->signal_fd, &ok, 1);
48
  }
49
 cleanup:
50
  close(context->signal_fd);
51
  free(context_pointer);
52
  return 0;
53
}
54
55
void* zmq_thread_init(void* zmq_context, int signal_fd) {
56
  acl_zmq_context* context = malloc(sizeof(acl_zmq_context));
57
  pthread_t thread;
58
59
  context->context = zmq_context;
60
  context->signal_fd = signal_fd;
61
  sem_init(&context->sem, 1, 0);
62
  pthread_create(&thread, 0, &zmq_thread, context);
63
  pthread_detach(thread);
64
  return context;
65
}
66

C++ example

39
 
1
#include <iostream>
2
#include "mystuff/util.h"
3
4
namespace {
5
enum Enum {
6
  VAL1, VAL2, VAL3
7
};
8
9
char32_t unicode_string = U"\U0010FFFF";
10
string raw_string = R"delim(anything
11
you
12
want)delim";
13
14
int Helper(const MyType& param) {
15
  return 0;
16
}
17
} // namespace
18
19
class ForwardDec;
20
21
template <class T, class V>
22
class Class : public BaseClass {
23
  const MyType<T, V> member_;
24
25
 public:
26
  const MyType<T, V>& Method() const {
27
    return member_;
28
  }
29
30
  void Method2(MyType<T, V>* value);
31
}
32
33
template <class T, class V>
34
void Class::Method2(MyType<T, V>* value) {
35
  std::out << 1 >> method();
36
  value->Method3(member_);
37
  member_ = value;
38
}
39

Objective-C example

16
 
1
/*
2
This is a longer comment
3
That spans two lines
4
*/
5
6
#import <Test/Test.h>
7
@implementation YourAppDelegate
8
9
// This is a one-line comment
10
11
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
12
  char myString[] = "This is a C character array";
13
  int test = 5;
14
  return YES;
15
}
16

Java example

28
 
1
import com.demo.util.MyType;
2
import com.demo.util.MyInterface;
3
4
public enum Enum {
5
  VAL1, VAL2, VAL3
6
}
7
8
public class Class<T, V> implements MyInterface {
9
  public static final MyType<T, V> member;
10
  
11
  private class InnerClass {
12
    public int zero() {
13
      return 0;
14
    }
15
  }
16
17
  @Override
18
  public MyType method() {
19
    return member;
20
  }
21
22
  public void method2(MyType<T, V> value) {
23
    method();
24
    value.method3();
25
    member = value;
26
  }
27
}
28

Scala example

11
 
1
object FilterTest extends App {
2
  def filter(xs: List[Int], threshold: Int) = {
3
    def process(ys: List[Int]): List[Int] =
4
      if (ys.isEmpty) ys
5
      else if (ys.head < threshold) ys.head :: process(ys.tail)
6
      else process(ys.tail)
7
    process(xs)
8
  }
9
  println(filter(List(1, 9, 2, 8, 3, 7, 4), 5))
10
}
11

Kotlin mode

48
 
1
package org.wasabi.http
2
3
import java.util.concurrent.Executors
4
import java.net.InetSocketAddress
5
import org.wasabi.app.AppConfiguration
6
import io.netty.bootstrap.ServerBootstrap
7
import io.netty.channel.nio.NioEventLoopGroup
8
import io.netty.channel.socket.nio.NioServerSocketChannel
9
import org.wasabi.app.AppServer
10
11
public class HttpServer(private val appServer: AppServer) {
12
13
    val bootstrap: ServerBootstrap
14
    val primaryGroup: NioEventLoopGroup
15
    val workerGroup:  NioEventLoopGroup
16
17
    init {
18
        // Define worker groups
19
        primaryGroup = NioEventLoopGroup()
20
        workerGroup = NioEventLoopGroup()
21
22
        // Initialize bootstrap of server
23
        bootstrap = ServerBootstrap()
24
25
        bootstrap.group(primaryGroup, workerGroup)
26
        bootstrap.channel(javaClass<NioServerSocketChannel>())
27
        bootstrap.childHandler(NettyPipelineInitializer(appServer))
28
    }
29
30
    public fun start(wait: Boolean = true) {
31
        val channel = bootstrap.bind(appServer.configuration.port)?.sync()?.channel()
32
33
        if (wait) {
34
            channel?.closeFuture()?.sync()
35
        }
36
    }
37
38
    public fun stop() {
39
        // Shutdown all event loops
40
        primaryGroup.shutdownGracefully()
41
        workerGroup.shutdownGracefully()
42
43
        // Wait till all threads are terminated
44
        primaryGroup.terminationFuture().sync()
45
        workerGroup.terminationFuture().sync()
46
    }
47
}
48

Ceylon mode

41
 
1
"Produces the [[stream|Iterable]] that results from repeated
2
 application of the given [[function|next]] to the given
3
 [[first]] element of the stream, until the function first
4
 returns [[finished]]. If the given function never returns 
5
 `finished`, the resulting stream is infinite.
6
7
 For example:
8
9
     loop(0)(2.plus).takeWhile(10.largerThan)
10
11
 produces the stream `{ 0, 2, 4, 6, 8 }`."
12
tagged("Streams")
13
shared {Element+} loop<Element>(
14
        "The first element of the resulting stream."
15
        Element first)(
16
        "The function that produces the next element of the
17
         stream, given the current element. The function may
18
         return [[finished]] to indicate the end of the 
19
         stream."
20
        Element|Finished next(Element element))
21
    => let (start = first)
22
    object satisfies {Element+} {
23
        first => start;
24
        empty => false;
25
        function nextElement(Element element)
26
                => next(element);
27
        iterator()
28
                => object satisfies Iterator<Element> {
29
            variable Element|Finished current = start;
30
            shared actual Element|Finished next() {
31
                if (!is Finished result = current) {
32
                    current = nextElement(result);
33
                    return result;
34
                }
35
                else {
36
                    return finished;
37
                }
38
            }
39
        };
40
    };
41

Simple mode that tries to handle C-like languages as well as it can. Takes two configuration parameters: keywords, an object whose property names are the keywords in the language, and useCPP, which determines whether C preprocessor directives are recognized.

MIME types defined: text/x-csrc (C), text/x-c++src (C++), text/x-java (Java), text/x-csharp (C#), text/x-objectivec (Objective-C), text/x-scala (Scala), text/x-vertex x-shader/x-fragment (shader programs), text/x-squirrel (Squirrel) and text/x-ceylon (Ceylon)