Skip to content

Instantly share code, notes, and snippets.

@thomaslee
Created December 18, 2011 05:33
Show Gist options
  • Save thomaslee/1492471 to your computer and use it in GitHub Desktop.
Save thomaslee/1492471 to your computer and use it in GitHub Desktop.
"Hello World" in cue -- a (throwaway) experiment in compiler geekery
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <memory.h>
typedef struct _Module {
const char *name;
} Module;
void
_sys__printf(const char * arg0, ...)
{
va_list args;
va_start(args, arg0);
vprintf(arg0, args);
va_end(args);
}
void
_sys__println(const char * arg0)
{
printf("%s\n", arg0);
}
typedef struct _sys_Module {
Module common;
void (*printf)(const char *, ...);
void (*println)(const char *);
} sys_Module;
static const sys_Module sys_impl = {
{ "sys" },
_sys__printf,
_sys__println
};
static const sys_Module *sys = &sys_impl;
// enter package main
int
_main__main(void){
(sys)->printf("Hello, World\n");
return (0);
}
// leave package main
int
main(int argc, char **argv)
{
_main__main();
return 0;
}
use sys;
int32
main(array[string] args)
{
sys.printf("Hello, World\n");
return 0;
}
tom@laptop ~/Source/cue $ ./cue examples/HelloWorld.cue
Module(package=[Name(id="main")], [Use(name=[Name(id="sys")]), FunctionDecl(name="main", body=[Expr(value=Call(target=GetAttr(target=Name(id="sys"), attr="printf"), args=[Str(value=""Hello, World\n"")])), Return(value=Int(value="0"))])])
Allocated 1331 bytes
tom@laptop ~/Source/cue $ cat aout.c
# <... snip ...>
tom@laptop ~/Source/cue $ gcc -o aout aout.c
tom@laptop ~/Source/cue $ ./aout
Hello, World
tom@laptop ~/Source/cue $ # aww ye, muffins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment