Fēng Programming Language
The Fēng language is a statically typed, object-oriented programming language that prioritizes memory safety:
- Memory safety as a top priority, with mandatory type and bounds checking.
- Class and interface designs simplified from Java, supporting inheritance and abstraction.
- No forms of abstraction other than classes and interfaces.
- Automatic reference counting (ARC).
- Resource classes with destructors.
structandunionlike in C, supporting arbitrary conversion between them.- Phantom reference mechanism, similar to C++ references or Rust borrowing.
- Simple compile-time generics.
- References and methods support an immutable (
read-only) marker, enabling write protection of data without encapsulation. - Non-null references and non-null checks help reduce null pointer issues and improve code quality.
- Simple modular code organization.
- Supports partial operator overloading.
For detailed syntax design, please refer to the reference manual.
Currently under development. Although simple projects can be compiled, the lack of system call libraries and utility libraries still prevents normal usage. Contributions from interested friends are welcome!
The parser is generated using ANTLR4. The language specification can be found in grammar. The parse results are traversed and the AST is built using SourceParseVisitor.
During build, parser classes are automatically generated via Maven plugins, so simply building with mvn allows
debugging in IDEA.
The main analysis class is SemanticAnalysis.
Completed semantic analyses include:
- Symbol checking: Check whether types and functions are defined, and whether variables are declared.
- Constant folding: Evaluate constants directly.
- Type checking: Check variable assignments, return value types, function prototype comparisons, and convertible type checks.
- Class inheritance and interface implementation checks.
- Check for missing return paths.
- Variable lifetime checking.
- Check for anonymous objects in expressions.
- Required checks for reference and function type variables.
- Unmodifiable checks for references.
- Statement context checking.
- Generic type parameter checking.
- Checking of symbols exported from other modules.
Only C++ code generation is currently implemented, with the tool class CppGenerator.
Completed code features:
- Derived class definitions: Classes, interfaces, struct types, function types completed; properties [Incomplete]
- Expressions: Power operation [Incomplete]
- Statements: Exceptions [Incomplete]
- Variables: Completed
- Types: Completed
- Polymorphic calls for classes: Completed
- Runtime type checking: Completed
- Variable cleanup and reference instance management: Completed
- Literals and initialization: Completed
- Generics: Completed
- String formatting: [Incomplete]
- Modules: One cpp file generated per module.
The current build tool supports compiling a single source file, a single module, or multi-module joint builds.
The tool is developed in Java, requiring JDK and Maven to be installed first. For details, consult deepseek. The project dependencies include only antlr4-runtime, jcommander, and 3 Maven plugins, which are automatically downloaded during build. Recommended build command:
mvn clean package -Dmaven.test.skip=trueThe packaged JAR will be in the target directory: feng-${version}.jar
For example, with the current version "0.0.1-dev", the built package is feng-0.0.1-dev.jar.
Tool usage:
java -jar feng-0.0.1-dev.jar -t [type] -i [source] -o [output directory]Parameter descriptions:
- -t Source type: f/file - single file, m/module - single module, p/project - simple project with multi-module organization
- -i Source path: For a single file, points to the full file path; for a module or project, points to the corresponding directory.
- -o Output directory: For a single file, outputs one C++ file; for a module or project, each module corresponds to one C++ file. If not specified, defaults to the source directory.
- -p Current package name: Defaults to the filename or directory name.
- -L Add dependency packages: Multiple packages can be specified as key-value pairs (package name = path), for example:
-Lfoo=D:\dev\libs\foo
Compile a single source file:
java -jar feng-0.0.1-dev.jar -t f -i jjj.feng -o jjj.cppThe generated C++ requires a C/C++ compilation environment, and the C++20 standard must be specified during compilation:
c++ --std=c++20 -c jjj.cpp -o jjj.oIf the Fēng code contains a main function, a main function will be created in the corresponding C++ file, allowing
it to be compiled into an executable:
c++ --std=c++20 jjj.cpp -o jjj.o