Skip to content

Instantly share code, notes, and snippets.

@icasdri
Last active November 5, 2017 00:48
Show Gist options
  • Save icasdri/94dac71eedde0bbababf20fe78ff8f2e to your computer and use it in GitHub Desktop.
Save icasdri/94dac71eedde0bbababf20fe78ff8f2e to your computer and use it in GitHub Desktop.
Write Java with only a C compiler
public class Hello {
public static void main(String args[]) {
System.out.println("Hello World!");
}
}
#ifndef JAVA_H
#define JAVA_H
int puts(char *);
struct out_struct {
int (*println)(char *);
};
struct system_struct {
struct out_struct out;
};
struct system_struct System = {
.out = { .println = &puts }
};
#define public
#define class int
#define Hello Hello()
#define static }
#define void int
#define String int argc, char *
#define println(s) println(s); {
#endif
CC = gcc
JAVAC = javac
SRCS = Hello.java
.PHONY: all run clean
all: hello Hello.class
hello: java.h $(SRCS)
$(CC) -x c -o $@ -include $^
Hello.class: $(SRCS)
$(JAVAC) $<
run: all
./hello
java Hello
clean:
rm -f hello Hello.class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment