minor adjustments to the docs

This commit is contained in:
Francesco Cozzuto
2023-01-25 01:34:13 +01:00
parent 9f7d1f6a1e
commit 401aeb130f
4 changed files with 85 additions and 64 deletions
+63 -30
View File
@@ -18,37 +18,56 @@ Noja is a very high level and dynamic language. It operates in the same range of
This project aims at being an interpreter design reference, therefore it optimizes for code quality and readability. That's not to mean that it won't be feature-complete. The end goal is to have a language you can do arbitrarily complex things in.
## Show me the code!
Here's an example of a noja program that orders the items in list using a bubble sort
Here's an example of a noja program taken from the example HTTP server in `examples/http_server/main.noja`
```
L = [3, 2, 1];
router->plug("/login", {
GET: 'pages/login.html',
fun POST(req: Request) {
fun swap(a: int, b: int)
return b, a;
username, _, error = getUsername(req);
if error != none:
return respond(400, error);
if username != none:
return respond(400, "You're already logged in");
fun order(L: List) {
do {
swapped = false;
i = 0;
while i < count(L)-1 and not swapped: {
swapped = L[i+1] < L[i];
if swapped:
L[i], L[i+1] = swap(L[i], L[i+1]);
i = i+1;
fun areValid(params) {
expect = {username: String, password: String};
return istypeof(expect, params)
and count(params.username) > 0
and count(params.password) > 0;
}
} while swapped;
}
print(L, '\n'); # [3, 2, 1]
order(L);
print(L, '\n'); # [1, 2, 3]
params = urlencoded.parse(req.body);
if not areValid(params):
return respond(400, "Invalid parameters");
username = params.username;
password = params.password;
info = user_table[username];
if info == none:
return respond(200, "No such user");
if info != password:
return respond(200, "Wrong password");
# Log-in succeded
# Create a new session
session_id = random.generate(0, 1000);
session_table[session_id] = username;
message = string.cat("Welcome, ", username, "!");
headers = {
"Location" : "/home",
"Set-Cookie": string.cat("SESSID=", toString(session_id))
};
return respond(303, message, headers);
}
});
```
## Implementation Overview
The architecture is pretty much the same as CPython. The source code is executed by compilig it to bytecode. The bytecode is much more high level than what the CPU understands, it's more like a serialized version of the AST. For example, some bytecode instructions refer to variables by names, which means the compiler does very little static analisys. Memory is managed by a garbage collector that moves and compacts allocations.
The architecture is pretty much the same as CPython. The source code is executed by compilig it to bytecode. The bytecode is much more high level than what the CPU understands, it's more like a serialized version of the AST. For example, some bytecode instructions refer to variables by names, which means the compiler does very little static analisys. Memory is managed by a garbage collector that moves and compacts allocations.
(More detailed explanations are provided alongside the code.)
@@ -56,7 +75,7 @@ The architecture is pretty much the same as CPython. The source code is executed
I wrote it on a linux machine, but there should be very few places where a linux host is assumed. It should be very easy to port.
## Development state
The interpreter is fully functional, but lots of built-in functions that one would expect still need to be implemented. Unfortunately, I feel like, at the moment, this requires much more work than what it's worth. At this time the priority is writing documentation and tests so that more people can try it, give feedback and move forward without breaking the world.
The interpreter is fully functional, but many built-in functions that one would expect still need to be implemented. At this time the priority is writing documentation and tests so that more people can try it, give feedback and move forward without breaking the world!
## Build
To build the interpreter, run:
@@ -65,20 +84,32 @@ $ make
```
The `noja` executable will be generated, which is a command-line interface that runs Noja code.
## Usage
You can run files by doing:
By default, the compilation is in release mode. The mode can be specified through the `BUILD_MODE` variable
```sh
location/of/noja <filename>
$ make -B BUILD_MODE=DEBUG
```
the `-B` option forces the makefile to rebuild all of the code, ignoring previous compilation artifacts. This is probably what you want when changing the build mode.
Also, a mode for analyzing code coverage is available if your interested in that kind of stuff
```sh
$ make -B BUILD_MODE=COVERAGE
```
or you can run strings by doing:
## Usage
You can run files by providing a path to the command line utility, or you can simply provide the string to be executed
```sh
location/of/noja -i <string>
$ noja <filename>
$ noja -i <string>
```
More usage information can be accessed using the `-h` option
```sh
$ noja -h
```
## Testing
Running `make` will also generate the `test` executable, which is a program that lets you run the testcases in the `tests/` folder. A testcase is a text file with extension `.noja-test`.
Running `make` will also generate the `test` executable, a program necessary to run the testcases in the `tests/` folder. A testcase is a text file with extension `.noja-test`.
Tu run all tests, you can do:
```sh
@@ -88,3 +119,5 @@ or you can execute specific suites of tests like this:
```sh
$ ./test tests/compiler/expr tests/runtime/push
```
By running `report_test_suite_coverage.sh`, the test suite will be executed and a report of the code coverage will be stored in the `/report` folder. Note that this script will compile the CLI in coverage mode, so you'll need to rebuild it again for generic use cases.