Skip to content
This repository was archived by the owner on Oct 12, 2026. It is now read-only.

Commit 317ea4d

Browse files
committed
feat: add cli flag to generate langauge reference document
1 parent 941a9ee commit 317ea4d

10 files changed

Lines changed: 73 additions & 45 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ lbd [options]
112112
-f, --file <filepath> Specify input source filepath to run
113113
-h, --help Show this help message and exit
114114
-d, --debug Enable debug mode
115-
-r, --repl Run in interactive REPL mode
115+
-r, --repl Run in interactive REPL node
116+
--docs Generate language-reference-document
116117
```
117118

119+
> Generate the language-reference-document by using the `--docs` flag, and use it as the source of truth for
120+
> native-function-signatures.
121+
118122
## Editor Plugins
119123

120124
1. [GNU Emacs](./editor-plugins/emacs)
@@ -126,4 +130,4 @@ lbd [options]
126130
- TODO: Fix tests output in README.
127131
- TODO: Step debug kind of functionality in REPL. Inside REPL, while loading a file, load one expression at a time and
128132
provide user with some feedback and actions to perform.
129-
- TODO: Make a pre-commit-hook which does not allow to push to master, if the tests are failing.
133+
- TODO: Make a pre-commit-hook which does not allow to push to master, if the tests are failing. Additionally, test whether the `docs/SIGNATURE.txt` is up-to-date or not.

‎docs/SIGNATURE.md‎

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎docs/SIGNATURE.txt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
add: Float -> Float -> Float
2+
sub: Float -> Float -> Float
3+
mul: Float -> Float -> Float
4+
cmp: Float -> Float -> Float
5+
null: Float -> Any -> Any -> Any
6+
parseFloat: String -> Float
7+
list: Any -> []
8+
listSize: [] -> Float
9+
listGet: [] -> Float -> Any
10+
listRemove: [] -> Float -> Any
11+
listAppend: [] -> Any -> Void
12+
map: (Any -> Any) -> [] -> []
13+
transpose: [] -> []
14+
sort: [] -> []
15+
zip: [] -> []
16+
foldRight: (Any -> Any -> Any) -> Any -> [] -> Any
17+
print: Any -> Void
18+
slurpFile: String -> String
19+
lines: String -> []
20+
split: String -> String -> []

‎include/lbd/cmd.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ namespace cmd {
1010
bool showHelp = false;
1111
bool repl = false;
1212
bool debug = false;
13+
bool generateDocs = false;
1314
};
1415

15-
void printHelp(std::ostream &os, const std::string &programName);
16+
void printHelp(std::ostream &outputStream, const std::string &programName);
1617

1718
Options parseArguments(int argc, char **argv, const std::string &programName);
1819
}

‎include/lbd/docs.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
5+
namespace docs {
6+
void dumpDocs(std::ostream &outputStream);
7+
}

‎include/lbd/logs.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22

33
#include <optional>
4+
#include <lbd/exceptions.h>
45
#include <lbd/frontend/location.h>
56
#include <lbd/utils/terminal.h>
6-
#include <lbd/exceptions.h>
77

88
namespace logs {
99
struct Logger {

‎src/cmd.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#include <lbd/cmd.h>
22

33
namespace cmd {
4-
void printHelp(std::ostream &os, const std::string &programName) {
5-
os << "usage: " << programName << " [options]\n\n"
4+
void printHelp(std::ostream &outputStream, const std::string &programName) {
5+
outputStream << "usage: " << programName << " [options]\n\n"
66
<< "options:\n"
77
<< " -f, --file <filepath> Specify input source filepath to run\n"
88
<< " -h, --help Show this help message and exit\n"
99
<< " -d, --debug Enable debug mode\n"
10-
<< " -r, --repl Run in interactive REPL node" << std::endl;
10+
<< " -r, --repl Run in interactive REPL node\n"
11+
<< " --docs Generate language-reference-document" << std::endl;
1112
}
1213

1314
Options parseArguments(const int argc, char **argv, const std::string &programName) {
@@ -27,6 +28,8 @@ namespace cmd {
2728
opts.debug = true;
2829
} else if (arg == "-r" || arg == "--repl") {
2930
opts.repl = true;
31+
} else if (arg == "--docs") {
32+
opts.generateDocs = true;
3033
} else {
3134
std::cerr << "unknown option: " << arg << "\n";
3235
printHelp(std::cerr, programName);

‎src/docs.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <lbd/docs.h>
2+
#include <lbd/runtime/builtins.h>
3+
#include <lbd/runtime/interpreter.h>
4+
5+
namespace docs {
6+
void dumpDocs(std::ostream &outputStream) {
7+
// TODO: Use colors if in tty, show module, description, usages/examples.
8+
// Maybe add a flag to rather generate an html-document.
9+
for (runtime::NativeFunction nativeFunction: runtime::builtins::getBuiltins()) {
10+
std::string signature = nativeFunction.getSignature()->toString();
11+
outputStream << nativeFunction.getName() << ": " << signature << std::endl;
12+
}
13+
}
14+
}

‎src/main.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
#include <lbd/cmd.h>
5+
#include <lbd/docs.h>
6+
#include <lbd/repl.h>
27
#include <lbd/frontend/lexer.h>
38
#include <lbd/frontend/parser.h>
49
#include <lbd/runtime/interpreter.h>
5-
#include <lbd/cmd.h>
6-
#include <lbd/repl.h>
7-
#include <string>
8-
#include <vector>
910

1011
const std::string &programName = "lbd";
1112

1213
int main(const int argc, char **argv) {
13-
const auto &[filepath, show_help, repl, debug] = cmd::parseArguments(argc, argv, programName);
14+
const auto &[filepath, show_help, repl, debug, generateDocs] = cmd::parseArguments(argc, argv, programName);
1415
if (show_help) {
1516
cmd::printHelp(std::cout, argv[0]);
1617
return EXIT_SUCCESS;
1718
}
19+
if (generateDocs) {
20+
docs::dumpDocs(std::cout);
21+
return EXIT_SUCCESS;
22+
}
1823
if (repl) {
1924
repl::loop(debug);
2025
} else {

‎src/runtime/type.cpp‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,14 @@ namespace runtime::type {
177177
outputStringStream << argumentType->toString() << " -> ";
178178
}
179179
}
180-
if (returnType->isCallable()) {
181-
outputStringStream << "(" << returnType->toString() << ")";
180+
if (returnType) {
181+
if (returnType->isCallable()) {
182+
outputStringStream << "(" << returnType->toString() << ")";
183+
} else {
184+
outputStringStream << returnType->toString();
185+
}
182186
} else {
183-
outputStringStream << returnType->toString();
187+
outputStringStream << "Void";
184188
}
185189
return outputStringStream.str();
186190
}

0 commit comments

Comments
 (0)