2023-10-17 09:06:14 +00:00
# q
A simple programming language.
2023-10-17 13:10:35 +00:00
## Features
2023-10-21 15:46:20 +00:00
* Fast compilation
* Small binaries
2023-10-17 13:10:35 +00:00
2023-10-17 09:06:14 +00:00
## Installation
```shell
git clone https://git.akyoto.dev/cli/q
cd q
go build
```
2023-10-17 13:10:35 +00:00
## Usage
2023-10-17 09:06:14 +00:00
2024-06-19 10:19:32 +00:00
Build a Linux x86-64 ELF executable from `examples/hello` :
2023-10-21 15:46:20 +00:00
2023-10-17 13:10:35 +00:00
```shell
2023-10-17 18:29:36 +00:00
./q build examples/hello
2023-10-23 10:37:20 +00:00
./examples/hello/hello
2023-10-17 13:10:35 +00:00
```
2023-10-17 09:06:14 +00:00
2023-10-28 10:51:19 +00:00
## Documentation
2023-10-23 10:37:20 +00:00
2023-10-28 10:51:19 +00:00
### [main.go](main.go)
2023-10-28 11:02:26 +00:00
Entry point. It simply calls `cli.Main` which we can use for testing.
2023-10-28 10:51:19 +00:00
### [src/cli/Main.go](src/cli/Main.go)
The command line interface expects a command like `build` as the first argument.
Commands are implemented as functions in the [src/cli ](src/cli ) directory.
Each command has its own set of parameters.
### [src/cli/Build.go](src/cli/Build.go)
The build command creates a new `Build` instance with the given directory and calls the `Run` method.
If no directory is specified, it will use the current directory.
If the `--dry` flag is specified, it will perform all tasks except the final write to disk.
This flag should be used in most tests and benchmarks to avoid needless disk writes.
```shell
q build
q build examples/hello
q build examples/hello --dry
```
2024-06-30 09:41:59 +00:00
Adding the `-a` or `--assembler` flag shows the generated assembly instructions:
```shell
q build examples/hello -a
```
Adding the `-v` or `--verbose` flag shows verbose compiler information:
2024-06-19 10:19:32 +00:00
```shell
q build examples/hello -v
```
2023-10-28 10:51:19 +00:00
### [src/build/Build.go](src/build/Build.go)
The `Build` type defines all the information needed to start building an executable file.
The name of the executable will be equal to the name of the build directory.
2024-06-02 13:20:05 +00:00
`Run` starts the build which will scan all `.q` source files in the build directory.
Every source file is scanned in its own goroutine for performance reasons.
2024-06-19 10:19:32 +00:00
Parallelization here is possible because the order of files in a directory is not significant.
2024-06-02 13:20:05 +00:00
2024-06-19 10:19:32 +00:00
The main thread is meanwhile waiting for new function objects to arrive from the scanners.
Once a function has arrived, it will be stored for compilation later.
We need to wait with the compilation step until we have enough information about all identifiers from the scan.
Then all the functions that were scanned will be compiled in parallel.
We create a separate goroutine for each function compilation.
Each function will then be translated to generic assembler instructions.
2024-06-02 13:20:05 +00:00
All the functions that are required to run the program will be added to the final assembler.
2023-10-28 10:51:19 +00:00
The final assembler resolves label addresses, optimizes the performance and generates the specific x86-64 machine code from the generic instruction set.
2024-07-03 09:39:24 +00:00
### [src/build/core/Function.go](src/build/core/Function.go)
2024-06-19 10:19:32 +00:00
This is the "heart" of the compiler.
2024-07-03 09:39:24 +00:00
Each function runs `f.Compile` which organizes the source code into an abstract syntax tree that is then compiled via `f.CompileAST` .
You can think of AST nodes as the individual statements in your source code.
### [src/build/ast/Parse.go](src/build/ast/Parse.go)
This is what generates the AST from tokens.
### [src/build/expression/Parse.go](src/build/expression/Parse.go)
This is what generates expressions from tokens.
2023-10-23 10:37:20 +00:00
## Tests
2023-10-21 15:46:20 +00:00
```shell
2024-07-03 09:39:24 +00:00
go test ./... -v
2023-10-23 10:37:20 +00:00
```
## Benchmarks
```shell
2024-07-03 09:39:24 +00:00
go test ./tests -bench=. -benchmem
2023-10-21 15:46:20 +00:00
```
2023-10-17 09:06:14 +00:00
## License
Please see the [license documentation ](https://akyoto.dev/license ).
## Copyright
© 2023 Eduard Urbach