https://github.com/azhenley/knox
A toy programming language written in Go that compiles to C.
https://github.com/azhenley/knox
compiler programming-language
Last synced: 3 days ago
JSON representation
A toy programming language written in Go that compiles to C.
- Host: GitHub
- URL: https://github.com/azhenley/knox
- Owner: AZHenley
- License: mit
- Created: 2019-01-12T20:59:53.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-06-20T23:30:40.000Z (over 3 years ago)
- Last Synced: 2024-06-21T17:51:48.180Z (over 1 year ago)
- Topics: compiler, programming-language
- Language: Go
- Homepage:
- Size: 172 KB
- Stars: 90
- Watchers: 9
- Forks: 7
- Open Issues: 65
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# The Knox Programming Language
Knox is an experimental language meant to help me learn Go and explore compiler design (NOTE: I don't work on this anymore.). It acts as a systems language with high-level constructs for convenience. The compiler is written in Go and generates C. It is very early in development.
The principles behind the design of Knox are:
- Explicitness. Explicit and unambiguous code is a priority, even over brevity. No surprises.
- Pass by reference. All complex types should be pass by reference and pointers should be hidden, like Java and C#.
- Small language. Simple and consistent syntax with few constructs, as an alternative to Zig or C.
- Convenient data structures. Strings, lists, and maps are first-class data structures that should be as easy as Python.
- Operability. Use any C library and produce C libraries.
- Easy to setup and use. No massive installation like C# or Java and no annoying configuration like Go's gopath.
- Fast enough. Compiling time, execution time, and memory usage should be comparable to directly using C.
- Low overhead. All runtime dependencies are optional for systems programming.
- Well-behaved. Contracts, error handling, and unit tests are first-class constructs.
```
func main() void {
fizzbuzz(300);
}
func fizzbuzz(n : int) void {
for i : int in stl.range(1,10,1) {
if i%15 == 0 {
stl.print("FizzBuzz");
} else if i%3 == 0 {
stl.print("Fizz");
} else if i%5 == 0 {
stl.print("Buzz");
} else {
stl.print(i);
}
}
}
```
## Comparison to Go:
- Classes instead of structs
- Objects are pass-by-reference
- Ada-style type constraints
- No garbage collector
- No type inference
- No short form of variable declarations
- No variable declaration blocks
- No implicit casting
- Variables must be initialized
- Semicolons required
- Different syntax for variable and function declarations
- Python-style While and For loops
- Allows whitespace between if and elseif blocks
- Enum support
- Sum types
- Constructors
- Classes must explicitly implement interfaces
- No pointers
- All return values must be used or explicitly thrown away
- No goto
- Multiple assignment is only for multiple return values