How edubart/nelua-lang works
Project Type and Purpose
• Nelua is essentially a compiler and toolchain for a statically-typed, meta-programmable systems programming language.
• Its purpose is to offer a blend of Lua’s syntactic simplicity and C-like performance by generating readable C code, then using a standard C compiler to produce native binaries.
• Meta programming is key: compile-time manipulation of the source code via Lua, allowing for extensions and high-level language constructs without modifying the core specification.
Main Components
a. Compiler Infrastructure:
• Frontend/Parser: Responsible for parsing Nelua source files and generating the Abstract Syntax Tree (AST). This is reflected in files within the “lualib/nelua” directory (e.g., astdefs.lua, astnode.lua, syntaxdefs.lua).
• Preprocessor & Meta Programming Engine: The modules that allow compile-time Lua-based macros and AST manipulations (e.g., preprocessor.lua, plugins like nldecl.lua).
• Analyzer & Type Checker: Parts such as analyzer.lua, analyzercontext.lua, and various utilities for type checking are crucial.
• Code Generation: Components that emit C code from the Nelua AST (modules like cemitter.lua, cgenerator.lua, and luagenerator.lua) and handle emission of native code after C compilation.
b. Runtime and Standard Library:
• Core library files in “lib” include modules for fundamental operations (strings, math, memory, hashmaps, etc.) and various allocators (GC, manual, arena, etc.) that illustrate design choices for memory management.
• “lualib” holds the Nelua runtime, which includes compiler support scripts and built-in libraries.
• “src” contains low-level C files and integration with external components like the Lua core, srpmalloc, and lpeglabel – these reflect the language’s dependence on traditional C libraries and the adaptation of Lua components.
c. Testing and Examples:
• The “tests” directory is important for validating compiler components and libraries.
• The “examples” directory showcases sample Nelua programs (like a Snake game using SDL2) that demonstrate how users interact with the generated code and runtime.
d. Documentation & DevOps:
• The “docs” folder and GitHub workflows indicate the presence of automated testing, continuous integration, and documentation pipelines.
• The Makefile, Dockerfile, and CI configurations are part of the overall ecosystem used for building and deploying the language/toolchain.
Relationships & Interactions
• Data Flow: Begin with Nelua source code input → Frontend (parsing into AST) → Preprocessor & analyzer (for metaprogramming and type checking) → Code generation phase (emitting C code) → External C Compiler (GCC/Clang) that produces native binary.
• Interaction: The compiler interacts with the runtime libraries present in “lualib” and “lib” for built-in operations and memory management.
• Test and CI: Represent parallel paths where testing frameworks validate different components (referring to “tests” and the CI workflows).
Architectural Patterns and Design Principles
• Modular design: Each compiler phase (frontend, analysis, code generation) is isolated with its own source files, promoting separation of concerns.
• Dual memory management: Both garbage-collected and manual memory models exist – emphasize this choice in the runtime/memory subsystem.
• Interoperability with C: Highlight how the generated C code bridges into existing C-land, which underpins the portability and performance of Nelua.