{"id":18320752,"url":"https://github.com/xtlsoft/pile","last_synced_at":"2025-04-09T14:29:21.994Z","repository":{"id":110415759,"uuid":"252938881","full_name":"xtlsoft/pile","owner":"xtlsoft","description":"[WIP] A standard library and scripting language for small devices.","archived":false,"fork":false,"pushed_at":"2020-10-19T16:18:20.000Z","size":9,"stargazers_count":0,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-15T08:30:49.770Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xtlsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-04T07:44:26.000Z","updated_at":"2021-02-12T14:09:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"2e9d65a8-fb67-4c03-a54d-88638fae7ed8","html_url":"https://github.com/xtlsoft/pile","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtlsoft%2Fpile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtlsoft%2Fpile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtlsoft%2Fpile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtlsoft%2Fpile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xtlsoft","download_url":"https://codeload.github.com/xtlsoft/pile/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248054873,"owners_count":21040079,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T18:17:24.002Z","updated_at":"2025-04-09T14:29:21.968Z","avatar_url":"https://github.com/xtlsoft.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pile\n\nPile aims to build an elegant library and scripting language for small devices.\n\nIt has several components including pile class, pile library, pile runtime and pile intepreter.\n\n## Pile Class\n\nPile uses C macro and struct to implement classes.\n\nHere's an example:\n\n```c\n// test_class.h\n#include \"pile_class.h\"\n#include \"pile_types.h\"\n#define PILE_CLASS_SHORTCUTS true\nclass(test_class)\n{\n    pile_int_t a;\n\n    pile_int_t def_fn(test_class, get_a);\n    def_destruct(test_class);\n};\n\n// test_class.c\npile_int_t impl_fn(test_class, get_a)\n{\n    return self-\u003ea;\n}\nimpl_construct(test_class, pile_int_t a)\n{\n    self-\u003ea = a;\n    _assgin(test_class, get_a);\n}\nimpl_default_destruct(test_class);\n\n// main.c\n#include \u003cstdio.h\u003e\n#include \"test_class.h\"\n\nint main() {\n    new(test_class, ins, 10);\n    printf(\"%d\\r\\n\", ins-\u003eget_a(ins));\n    ins-\u003ea = 1000;\n    printf(\"%d\\r\\n\", call(ins, get_a));\n    del(ins);\n    renew(test_class, ins, 200);\n    printf(\"%d\\r\\n\", ins-\u003eget_a(ins));\n    del(ins);\n    return 0;\n}\n```\n\n### Class Shortcuts\n\nThe macros have shortcuts when `PILE_CLASS_SHORTCUTS` is defined.\n\nYou can disable that if the shortcuts cause name conflicts.\n\n| Shortcut                | Origin                          |\n| ----------------------- | ------------------------------- |\n| `class`                 | `PILE_CLASS`                    |\n| `new`                   | `PILE_NEW`                      |\n| `del`                   | `PILE_DEL`                      |\n| `impl_fn`               | `PILE_CLASS_METHOD`             |\n| `impl_construct`        | `PILE_CLASS_CONSTRUCTOR`        |\n| `impl_destruct`         | `PILE_CLASS_DESTRUCTOR`         |\n| `impl_default_destruct` | `PILE_CLASS_DESTRUCTOR_DEFAULT` |\n| `_assign`               | `PILE_ASSIGN_METHOD`            |\n| `def_fn`                | `PILE_DEFINE_METHOD`            |\n| `def_destruct`          | `PILE_DEFINE_DESTRUCT`          |\n| `call`                  | `PILE_CALL`                     |\n\nIn the document, we will use shortcuts to tell.\n\n### Define a class\n\n```c\nclass(test_class)\n{\n    pile_int_t a;\n\n    void def_fn(test_class, set_a, pile_int_t a);\n\n    def_destruct(test_class);\n};\n```\n\nIn the code above, we define a class named `test_class`.\n\nTo define a class, we need to use macro `class` or `PILE_CLASS`. It receives an argument as the class name. Then a struct body need to be followed.\n\n\u003e Note: after the brackets the semicolon(`;`) cannot be missed.\n\nBecause of the limits of C language, pile doesn't support private atrributes. The properties can be defined in normal struct way, like: `pile_str_t str;`.\n\nWhen defining class methods, we use `def_fn` or `PILE_DEFINE_METHOD` macros. The macro receives its first parameter as the class name, and the second as method name. The return type should be defined before calling the macro, and the parameter list should start at the third macro parameter, separated by commas.\n\nNote the constructor should **not** be defined in the class definition. But the destructor must be defined using `def_destruct` or `PILE_DEFINE_DESTRUCT` macro. The macro receives its first parameter as the class name and no other parameters should be followed.\n\nThe following types will be defined as in the example:\n\n- struct test_class: The original struct type\n- test_class_t: Orignal type defined using typedef\n- test_class_ptr_t: Pointer to the original type (instance type)\n\n### Implement a method\n\n\u003e Note: constructors and destructors should be defined using another macro, which will be described in the next section.\n\nWhen implementing a method, macro `impl_fn` or `PILE_CLASS_METHOD` should be used.\n\n```c\nvoid impl_fn(test_class, set_a, pile_int_t a)\n{\n    self-\u003ea = a;\n}\n```\n\nThe return type should be defined before calling the macro.\n\nThe first parameter is the class name while the second is the method name.\n\nThe following parameters are argument list of the method.\n\nInside the function body we can use `self` to access the instance.\n\n### Implementing a destructor\n\nIn a destructor, a class can free the resources it uses.\n\nUse `impl_destruct` or `PILE_CLASS_DESTRUCTOR` to implement one.\n\nExample:\n\n```c\nimpl_destruct(test_class)\n{\n    PILE_FREE(self-\u003edata);\n}\n```\n\nWhen a class does nothing in a destructor, you can use `impl_default_destruct`.\n\nExample:\n\n```c\nimpl_default_destruct(test_class);\n```\n\n### Implement a constructor\n\nA constructor need to do two things:\n\n1. Init the properties\n2. Assign methods\n\nYou can use `impl_construct` or `PILE_CLASS_CONSTRUCT` macros to implement.\n\nThe first parameter is class name, and it can receive a parameter list.\n\n```c\nimpl_construct(test_class, pile_int_t a)\n{\n    _assign(test_class, set_a);\n\n    self-\u003eset_a(a);\n}\n```\n\nThe `_assign` or `PILE_ASSIGN` macro can automatically assign a method to a class.\n\nAll the methods need to be assigned in the constructor,\n\nThe macro receives two parameters, where the first one is the class name and the second one is the method name.\n\n### Creating a new instance\n\nYou can use `new` or `PILE_NEW` macros to create a new instance.\n\nExample:\n\n```c\nnew(test_class, ins, 123);\n```\n\nThe first parameter is the class name, while the second is the variable name. The following parameters will be passed to the constructor. Then you can use `ins` to access the instance.\n\nThe above code will create a new variable with type `test_class_ptr_t`.\n\nIf you have a variable typed that already available, you can use `renew` or `PILE_RENEW` macro to re-initialize it.\n\nExample:\n\n```c\ntest_class_ptr_t ins;\nrenew(test_class, ins, 123);\n```\n\n### Deleting a instance\n\nUse `del` or `PILE_DEL` macros.\n\nExample:\n\n```c\ndel(ins);\n```\n\nThe parameter is the variable name of the instance.\n\n### Calling a method\n\nYou can use `ins-\u003emethod_name(ins, 123);` to call a method.\n\nNote the first parameter should pass the variable again.\n\nIf you use `call` or `PILE_CALL` macro, then you don't need to pass itself again. It looks like:\n\n```c\ncall(test_cass, set_a, 100);\n```\n\nThe first parameter is the class name, while the second one is the method name.\n\n## Shortcuts\n\npcert = pile_container_t\n\npvcoer = PILE_VALUE_CONTAINER\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtlsoft%2Fpile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxtlsoft%2Fpile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtlsoft%2Fpile/lists"}