Latest 2.0.1
Homepage https://github.com/sunpaq/monkc
License BSD
Platforms ios 9.0
Authors

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

Monk-C is based on C99 standard
No stable version released now. but the syntax is nearly ready to be fixed.
I am focusing on X86_64 and ARM64 CPUs.

Next version

Documents:

1 wiki page on github
2 Infos at this page

Use Monk-C on iOS

Library via Cocoapods

Demo App

The BohdiEngine (3D) written use Monk-C:

Mac version

iOS version via Cocoapods

Android version

The BohdiAR written use Monk-C:

https://github.com/sunpaq/BohdiAR-pod

The Demo Google Cardboard VR App using BohdiEngine:

https://github.com/sunpaq/BohdiEngineDemoSwift

Monk-C on Windows (64bit):

https://github.com/sunpaq/monkc4win64

Monk-C on UNIX(Mac & Linux) via command line tools (this repo):

requires

clang/gcc
git
ruby

install build tool

sudo gem install mcbuild

build & run

./build.rb all
./build.rb run 

install snippets for sublime text

1.[SublimeText Menu -> Preferences -> Browse Packages...] open the plugin folder
2.copy the 'mcsublime-snippets' folder into 'User' 

The MCBuild tool:

MCBuild is a script library written use Ruby. Helping C/Monk-C developr generate Makefile.
https://github.com/sunpaq/mcbuild

Syntax

Monk-C use "MC" as the prefix.

Header file

#include "monkc.h"

class(MCSort, MCObject,
      MCGeneric* array;
      size_t length);

method(MCSort, void, bye, voida);
method(MCSort, MCSort*, initWithArray, MCGeneric* array, size_t length);
method(MCSort, void, insertionSort, voida);
method(MCSort, void, quickSort, voida);
method(MCSort, void, printArray, voida);

C file

#include "MCSort.h"

oninit(MCSort)
{
    if (init(MCObject)) {
        var(array) = null;
        var(length) = 0;
        return obj;
    }else{
        return null;
    }
}

method(MCSort, void, bye, voida)
{
    if (obj->array && obj->length > 0) {
        free(obj->array);
    }
}

method(MCSort, MCSort*, initWithArray, MCGeneric* array, size_t length)
{
    var(array) = (MCGeneric*)malloc(sizeof(MCGeneric) * length);
    for (size_t i=0; i<length; i++) {
        obj->array[i] = array[i];
    }
    var(length) = length;
    //debug
    //ff(obj, printArray, 0);
    return obj;
}

function(void, swap, size_t a, size_t b)
{
    as(MCSort);
    if (a < b) {
        MCGeneric t = obj->array[a];
        obj->array[a] = obj->array[b];
        obj->array[b] = t;
    }
}

method(MCSort, void, insertionSort, voida)
{

}

function(void, quicksort, const size_t l, const size_t r)
{
    as(MCSort);
    if (l >= r || l > obj->length || r > obj->length) {
        //debug_log("quicksort exit l=%ld r=%ldn", l, r);
        return;
    }
    MCGeneric pivot = obj->array[l];
    size_t cur=l;
    for (size_t idx=l+1; idx<=r; idx++) {
        if (MCGenericCompare(obj->array[idx], pivot) < 0)
            swap(obj, ++cur, idx);
    }

    swap(obj, l, cur);
    quicksort(obj, l, cur-1);
    quicksort(obj, cur+1, r);
}

method(MCSort, void, quickSort, voida)
{
    quicksort(obj, 0, var(length)-1);
}

method(MCSort, void, printArray, voida)
{
    for (size_t i=0; i<obj->length; i++) {
        printf("element of array[%ld]=%.2fn", i, obj->array[i].mcfloat);
    }
}

onload(MCSort)
{
    if (load(MCObject)) {
        binding(MCSort, void, bye, voida);
        binding(MCSort, MCSort*, initWithArray, MCGeneric* array, size_t length);
        binding(MCSort, void, insertionSort, voida);
        binding(MCSort, void, quickSort, voida);
        binding(MCSort, void, printArray, voida);
        return cla;
    }else{
        return null;
    }
}

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(bird, 0);

Macros and runtime functions often used


  1. class
  2. method (binding)
  3. function (mixing)
  4. compute (computing)
  5. utility
  6. var
  7. new
  8. ff
  9. oninit (init)
  10. onload (load)
  11. retain
  12. release
  13. recycle
  14. obj
  15. cla
  16. voida & null

Total only 16 words.1


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

Latest podspec

{
    "name": "monkc",
    "version": "2.0.1",
    "summary": "a toolkit for OOP programming in C language",
    "description": "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).",
    "homepage": "https://github.com/sunpaq/monkc",
    "license": {
        "type": "BSD",
        "file": "LICENSE"
    },
    "authors": {
        "Sun YuLi": "[email protected]"
    },
    "source": {
        "git": "https://github.com/sunpaq/monkc-pod.git",
        "tag": "2.0.1"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source_files": "monkc/Classes/**/*",
    "public_header_files": "monkc/Classes/**/*.h"
}

Pin It on Pinterest

Share This