a toolkit for OOP programming in C language
Monk-C, is a toolkit for OOP programming use pure C (static library). the aim of Monk-C is to support OOP in pure C with some tiny C macros, functions and even a light preprocessor (optional). Monk-C is inspired by Apple Objective-C and gcc builtin "Constructing Calls". It is tiny and primitive but full of fun. I use it to play with my RaspberryPi and it really vary suitable for the ARM/Linux based embeded systems. It is open source under BSD license(3-clause license). I written it under the X86/Linux platform and X86/MacOS ARM/Linux is also fully tested and supportted both 32bit and 64bit.
[CPUArch/OS/Compiler]
IA-32/FreeBSD/clang On Working
IA-32/Linux/gcc&clang OK
IA-32/MacOS/gcc&clang No Test
IA-32/Windows7(MinGW-32bit)/mingw-gcc OK
X86-64/FreeBSD/clang OK
X86-64/Linux/gcc&clang OK
X86-64/MacOS/gcc&clang OK
X86-64/Windows7(MinGW-32bit)/mingw-gcc OK
ARM32/FreeBSD/clang On Working (RaspberryPi)
ARM32/Linux/gcc&clang OK (RaspberryPi/Debian)
ARM32/iOS/clang OK
ARM32/Android/clang OK (NDK build)
ARM64/Linux/gcc&clang On Working
ARM64/iOS/clang On Working
PowerPC64/FreeBSD/clang On Working (iMac G5)
1 wiki page on github
2 PDF doc (on writing)
the iOS version is latest 2.0:
other version will be ported later:
2 Android - Eclipse on Linux/Windows
Monk-C use "MC" as the prefix.
#include "monkc.h"
#include "BirdFather.h"
//avoid multi-defines
#ifndef Bird_
#define Bird_
//class define <child, super>
monkc(Bird, BirdFather);
char* name;
int type;
end(Bird, BirdFather);
//methods define
//Monk-C methods need at lease ONE argument
//use voida if you have no designed argument
method(Bird, Bird*, findBird);
method(Bird, void, bye, voida);
method(Bird, Bird*, initWithType, int type);
method(Bird, void, fly, voida);
method(Bird, int, fatherAge, voida);
#endif
####protocol file (Flyable.p)
binding(Flyable, void, duckFly);
binding(Flyable, void, chickenFly);
#include "Bird.h"
//called by runtime, initialize the class data
oninit(Bird)
{
obj->type = 3;
debug_logt("Bird", "[%p] init called\n", obj);
return obj;
}
//<special used method>
//[bye] is called when instance reference count became 0
//this is the callback for clean up
method(Bird, void, bye)
{
debug_logt(nameof(obj), "[%p] bye called\n", obj);
}
//<special used method>
//[findClassname] is an anchor mehtod for the child instance find super instance
//and directly access its data (see wiki for more info)
method(Bird, Bird*, findBird, xxx)
{
return obj;
}
//private C function
static void funcA(Bird* obj, int arg1)
{
debug_log("i am local function A\n");
}
//protocol you comply with
protocol(Flyable, void, duckFly, xxx)
{
debug_log("%s\n", "Bird:Duck GuaGuaGua fly");
}
protocol(Flyable, void, chickenFly, xxx)
{
debug_log("%s\n", "Bird:Chicken JiJiJi fly");
}
//public method implements
method(Bird, Bird*, initWithType, int type)
{
obj->type = type;
return obj;
}
method(Bird, int, fatherAge, xxx)
{
int fage = cast(BirdFather, obj->super)->age;
debug_logt(nameof(obj), "my father age is: %d\n", fage);
return fage;
}
method(Bird, void, fly, xxx)
{
debug_log("Bird[%p->%p]: default fly type %d\n", obj, obj->super, obj->type);
funcA(obj, 100);
}
//must have. binding methods at runtime.
loader(Bird)
{
debug_logt(nameofc(class), "load called\n");
//protocol itself is just a header file
#include "Flyable.p"
//DO NOT WRITE THEM BY HAND!
//YOU CAN COPY ALL THE METHODS DECLEARED IN HEADER FILE
//AND CHANGE "method->binding"
binding(Bird, Bird*, initWithType, int type);
binding(Bird, void, bye, xxx);
binding(Bird, void, fly, xxx);
binding(Bird, int, fatherAge, xxx);
return claz;
}
####Dynamically calling method
it just like the Objective-C. sending message instead of function call.
Bird* bird = new(Bird);
ff(bird, fly, 0);
####Statically calling method
C style: Bird_fly(0, bird, 0);
int main(int argc, char const *argv[])
{
LOG_LEVEL = MC_VERBOSE;
//your code here
return 0;
}
global log level:
you can set the global variable LOG_LEVEL to:
MC_SILENT //no log outputed
MC_ERROR_ONLY //error log only
MC_DEBUG //error log and debug log
MC_VERBOSE //error log and debug log and runtime log
you can use:
error_log()
debug_log()
runtime_log()
to output logs. parameter is same as printf(char* fmt, ...)
####Macros and runtime functions often used
- monkc
- end
- initer
- loader
- method
- protocol
- binding
- new
- call
- ff
- retain
- release
- recycle
- obj
- claz
- xxx
Total only 16 words.1
Sublime Text
Atom
Vim/Emacs (with plugins)
(you can use any editor. but some one have auto-complete function will help a lot)
gmake - this is needed on FreeBSD
i am trying to write a generic makefile for both
make and GNU make. but for now. please alias your
make to gmake.
clang - I strongly recommand use this C compiler.
because i found it can report more detailed error infomations
flex - this is needed to build the 'mcpp' preprocessor for monkc
0. default is compile by 'clang'. make sure you have one installed.
it also need 'flex'. you can install flex by 'sudo apt-get install flex' on Ubuntu
or install flex use macport on Mac OS X
1. cd ./src
2. sudo make install
clang is recommand. cause it can output better error infomations
if you want change it to gcc
change [ CC = clang -> CC = gcc ] in:
1. /src/monkc_runtime/Makefile -> line10
2. /src/monkc_buildtool/mcbuild -> line12
3. /src/lemontea/Makefile -> line9
4. /src/tests/Makefile -> line7
to use gcc as the compiler
the command above will build the <libmonkc.a> <liblemontea.a>
and automatically copy them to /usr/local/lib/
and copy the <.h> header files to /usr/local/include/
and copy the <mcpp> <mcbuild> tool to /usr/local/bin/
1. mkdir <your project dir>
2. cd <your project dir> && mcbuild -create
3. write code in the <your project dir/src> folder
(you can use any folder structure to organize your code)
4. cd <your project dir> && mcbuild -sync
5. cd <your project dir>/build && make
the output binary will be 'exec' in the build folder
(see the examples folder for more details)
##For more infomation please goto wiki page on github
##TODO list:
1. add type check/convert to preprocessor mcpp
2. add auto binding to preprocessor mcpp
3. add memory leak detector to preprocessor mcpp
4. lemontea_WEB
5. lemontea_GUI
6. lemontea_3D
Footnotes
-
the syntex is improving, maybe more/less keywords in the future. โฉ