ANIL
ANIL(A Nice Intermediate Language) Python & C++ inspired programming language that transpiles to C and can be embedded within C source files.
Install / Use
/learn @AnilBK/ANILREADME
A Nice Intermediate Language(ANIL)
ANIL is a statically typed programming language, inspired by Python and C++, that can be embedded within C source files.
Fig: ANIL functions written in C and ANIL respectively. The first function written in C is the general implementation for the __contains__ method whereas the second function written in ANIL is template specialization for String class.
ANIL generates appropriate C code, which is then compiled using a C compiler.
Fig: The generated C code from the second __contains__ method in the first screenshot.
Requirements:
- C compiler.
- Python.
- clang-format(Recommended).
Usage
- Open preprocess_2.py.
- Uncomment one of the
source_file = "FILE_NAME*.c"line. - Run
python preprocess_2.py
Alternatively, you can specify the filename directly by running:
python preprocess_2.py --filename examples\FILE_NAME*.c
- This will generate a FILE_NAME*_generated.c file. Compile the generated c file using a C compiler and execute the generated executable.
Features:
- Classes: Example 1 Example 2
- Templates & Function Overloading: Example
- Macros: Example
- Compile-Time Reflection: Example 1
- Annotations: Example 1 WebSever Example @static Annotation for static functions
- Standard Library: See Lib/. Shows mixing of C code and ANIL.
- Windows UI FrameWork: See Todo App Example
- Windows UI App using JSX like syntax: See Todo App JSX like Syntax Example
- Windows Web Server: See Todo App Web Server Example
- Function Pointers: See Example
- Threads & Channel: See Example
- Dunder methods:
__init__: To implement constructors.__del__: To implement destructors. For automatic destructor call for a struct member(if it has a destructor), use the ~ operator. Lookup_call_destructor_for_element()in Vector__str__: To print a given object with print(). It should return str(char*).__getitem__: To get an item at a specified index (e.g.obj[index]).__setitem__: To set an item at a specified index (e.g.obj[index] = value).__reassign__: To implement reassignment (e.g.obj = value).__add__: To implement addition.__contains__: To implementinoperator (e.gvalue in list).len: To get total number of items. It is used along with__getitem__to implement iterators.
The examples in examples/ and Lib/ folder shows all different features of ANIL. Right now, these examples serve as documentation for the language.
Project Structure
Directories
-
Bootstrap: Implementation of the compiler in ANIL itself. Files in this directory uses all (advanced) features available in the language.
-
examples: Demonstrations of various language features and usage of the standard library.
-
Lib: Standard Library.
Individual Files
-
preprocess_2.py: The actual compiler and code generator.
-
batch_compile.py: Compiles all the ANIL files one by one. Used to verify if the changes made to the compiler doesn't break existing functionality.
[!NOTE]
This is my first ever compiler project.
I created this compiler for the following reasons:
- To learn about compiler development.
- To build a self-compiling compiler.
- To implement features I find interesting and cool.
