Giter Club home page Giter Club 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).

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

in 'platforms/windows' folder of this repo, there have a Visual Studio Solution 'monkc.sln' you can build 'monkc.lib' using it. the binary and header will copy into 'output' folder

here is a demo using the 'monkc.lib' in other Visual Studio Solution the binary and header placed into its 'libs' folder 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 colored
sudo gem install mcbuild

build & run

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

please use "build-linux.rb" on linux

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=%ld\n", 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]=%.2f\n", 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

Footnotes

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

monkc's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

monkc's Issues

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.