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

Commit 1a9238d

Browse files
committed
Implement command line argument parsing
1 parent 5982925 commit 1a9238d

7 files changed

Lines changed: 118 additions & 14 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 4.0)
2-
project(lambda_discipline)
2+
project(lambda-discipline)
33

44
set(CMAKE_CXX_STANDARD 20)
55

@@ -18,11 +18,15 @@ add_library(fe STATIC
1818
add_library(intp STATIC
1919
${CMAKE_SOURCE_DIR}/src/intp/types.cpp
2020
${CMAKE_SOURCE_DIR}/src/intp/interpreter.cpp
21-
src/intp/buitlins.cpp
21+
${CMAKE_SOURCE_DIR}/src/intp/builtins.cpp
2222
)
2323

24-
add_executable(lambda_discipline ${CMAKE_SOURCE_DIR}/src/main.cpp)
25-
target_link_libraries(lambda_discipline PRIVATE fe intp)
24+
add_executable(lbd
25+
${CMAKE_SOURCE_DIR}/src/main.cpp
26+
${CMAKE_SOURCE_DIR}/src/cmd.cpp
27+
)
28+
29+
target_link_libraries(lbd PRIVATE fe intp)
2630

2731
# TODO: Add build tests
2832
# EXAMPLE: add_executable(lexer_test ../tests/lexer_test.cc)

‎README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ cmake ..
77
make
88
```
99

10+
## Usage
11+
12+
```bash
13+
usage: lbd [options]
14+
15+
options:
16+
-f, --file <filename> Specify input source file to run
17+
-h, --help Show this help message and exit
18+
-d, --debug Enable debug mode
19+
-r, --repl Run in interactive REPL node
20+
```
21+
1022
> TODO: Add build instructions for Windows as well.
1123
1224
> TODO: Add small script to print all TODOs and there Loc.

‎examples/test.lbd‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Define Pi as constant
2+
pi: Float = 3.14
3+
4+
-- Define function to calculate square
5+
square: Float -> Float = \x: Float. (mul x x)
6+
7+
-- Define function to calculate area of circle
8+
area_of_circle: Float -> Float = \r: Float.
9+
(mul pi (square r))
10+
11+
-- Define function to calculate volume of cylinder
12+
volume_of_cylinder: Float -> Float -> Float = \r: Float.
13+
\h: Float. (mul (area_of_circle r) h)
14+
15+
-- Calculate the volume of a cylinder
16+
(print "Volume of cylinder is: ")
17+
(print (volume_of_cylinder 5.0 10.0))

‎include/lbd/cmd.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <optional>
5+
#include <string>
6+
7+
namespace cmd {
8+
struct Options {
9+
std::optional<std::string> filepath;
10+
bool show_help = false;
11+
bool repl = false;
12+
bool debug = false;
13+
};
14+
15+
void print_help(std::ostream &os, const std::string &program_name);
16+
17+
Options parse_args(int argc, char **argv, const std::string &program_name);
18+
}

‎src/cmd.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <lbd/cmd.h>
2+
3+
namespace cmd {
4+
void print_help(std::ostream &os, const std::string &program_name) {
5+
os << "usage: " << program_name << " [options]\n\n"
6+
<< "options:\n"
7+
<< "-f, --file <filename> Specify input source file to run\n"
8+
<< "-h, --help Show this help message and exit\n"
9+
<< "-d, --debug Enable debug mode\n"
10+
<< "-r, --repl Run in interactive REPL node" << std::endl;
11+
}
12+
13+
Options parse_args(const int argc, char **argv, const std::string &program_name) {
14+
Options opts;
15+
for (int i = 1; i < argc; ++i) {
16+
if (std::string arg = argv[i]; arg == "-h" || arg == "--help") {
17+
opts.show_help = true;
18+
} else if (arg == "-f" || arg == "--file") {
19+
if (i + 1 < argc) {
20+
opts.filepath = argv[++i];
21+
} else {
22+
std::cerr << "error: missing filepath after " << arg << std::endl;
23+
print_help(std::cerr, program_name);
24+
std::exit(EXIT_FAILURE);
25+
}
26+
} else if (arg == "-d" || arg == "--debug") {
27+
opts.debug = true;
28+
} else if (arg == "-r" || arg == "--repl") {
29+
opts.repl = true;
30+
} else {
31+
std::cerr << "unknown option: " << arg << "\n";
32+
print_help(std::cerr, program_name);
33+
std::exit(EXIT_FAILURE);
34+
}
35+
}
36+
return opts;
37+
}
38+
}

‎src/main.cpp‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,37 @@
33
#include <lbd/fe/lexer.h>
44
#include <lbd/fe/parser.h>
55
#include <lbd/intp/interpreter.h>
6+
#include <lbd/cmd.h>
67
#include <string>
78
#include <vector>
89

9-
int main() {
10-
const std::string filepath = "../examples/math_demos.lbd";
11-
auto lexer_v = fe::lexer::Lexer(filepath);
10+
const std::string &program_name = "lbd";
1211

12+
int main(const int argc, char **argv) {
13+
const auto &[filepath, show_help, repl, debug] = cmd::parse_args(argc, argv, program_name);
14+
if (show_help) {
15+
cmd::print_help(std::cout, argv[0]);
16+
return 0;
17+
}
18+
if (repl) {
19+
std::cout << "entering REPL mode (not yet implemented)..." << std::endl;
20+
// TODO: implement REPL
21+
return 0;
22+
}
23+
// Lex
24+
auto lexer_v = fe::lexer::Lexer(*filepath);
1325
const std::vector<fe::token::Token> tokens = lexer_v.lex_all();
14-
for (const fe::token::Token &token: tokens) {
15-
std::cout << token << std::endl;
26+
if (debug) {
27+
for (const fe::token::Token &token: tokens) {
28+
std::cout << token << std::endl;
29+
}
1630
}
17-
31+
// Parse
1832
const auto parser = fe::parser::Parser(tokens);
19-
std::cout << parser.program << std::endl;
20-
33+
if (debug) {
34+
std::cout << parser.program << std::endl;
35+
}
36+
// Interpret
2137
auto result = intp::interp::interpret(parser.program);
22-
23-
return 0;
38+
return EXIT_SUCCESS;
2439
}

0 commit comments

Comments
 (0)