Git Product home page Git Product logo

monkc's Introduction

Monk-C

a toolkit for OOP programming in C language

Mou icon

Overview

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.

Monk-C is based on C99 standard
No stable version released now, developing commit: 0.1.140223

Supported platforms:

[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)

Documents:

1 wiki page on github
2 PDF doc (on writing)
3 Infos at this page

Play with Monk-C use IDEs (template project):

the iOS version is latest 2.0:

1 iOS - Xcode on MacOS

other version will be ported later:

2 Android - Eclipse on Linux/Windows

3 Linux - Eclipse on Linux

4 Mac - Xcode on MacOS

5 Win32 - Eclipse on Windows

Syntax

Monk-C use "MC" as the prefix.

declear interface - write in .h file

#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);

implement methods - write in .c file

#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);

main entry

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


  1. monkc
  2. end
  3. initer
  4. loader
  5. method
  6. protocol
  7. binding
  8. new
  9. call
  10. ff
  11. retain
  12. release
  13. recycle
  14. obj
  15. claz
  16. xxx

Total only 16 words.1

Hack the MonkC runtime on UNIX-like system use command line tools

recomand code editor:

Sublime Text
Atom
Vim/Emacs (with plugins)
(you can use any editor. but some one have auto-complete function will help a lot)

need these tools:

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

how to compile and install (command line):

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/

how to create and build a monkc project:

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

  1. the syntex is improving, maybe more/less keywords in the future. โ†ฉ

monkc's People

Contributors

sunpaq avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.