Skip to content

Instantly share code, notes, and snippets.

@antonijn
Last active August 29, 2015 14:05
Show Gist options
  • Save antonijn/3debc66fd174737bcfe3 to your computer and use it in GitHub Desktop.
Save antonijn/3debc66fd174737bcfe3 to your computer and use it in GitHub Desktop.
The ACE superset
#ifndef __ACE__
#error ACE is required to compile this unit
#endif
// Line comments are allowed!
// Defines symbols to make code more readable
#include <ace.h>
#include <string.h>
#include <stdio.h>
// 'class' expands to '_Class'
class object {
// 'virtual' expands to '_Virtual'
// the this argument is left out
virtual void tostring(FILE *f);
};
// implement object.tostring
// 'member' expands to '_Member'
// _Member is used to avoid name collisions
member void tostring(class object *const this, FILE *f)
{
f.printf("class object");
}
// 'extends' expands to '_Extends'
class derived extends object {
// 'override' expands to '_Override'
// it requires a seperate tostring for class derived
// than the one that's already defined for class object
// the this argument is left out
override void tostring(FILE *f);
// regular structure fields
// every field is public
// methods can be made private by making them static,
// and only declaring them within the class implementation unit
int a, b;
};
member void tostring(class derived *const this, FILE *f)
{
f.printf("{ a: %d, b: %d }", this->a, this->b);
}
// constructor implementation
member void init(class derived *const this, int a, int b)
{
this->a = a;
this->b = a;
}
int main(void)
{
class derived d;
d.init(10, 20);
class object *o = &d;
o->tostring(stdout); // prints '{ a: 10, b: 20 }'
stdout.printf("\n"); // printf _Member function
d.tostring(stdout); // prints '{ a: 10, b: 20 }'
stdout.fprintf("\n"); // fprintf non-_Member function
// equivalent to fprintf(stdout, "\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment