Skip to content

Commit 60343b8

Browse files
committed
feat: add command documentation comments and implement sum command
1 parent aa01554 commit 60343b8

12 files changed

Lines changed: 138 additions & 5 deletions

File tree

examples/nestedFor.script

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ var count 99
77

88
for i 0 10 inc
99
for j 10 0 dec
10-
echo .
11-
# sum neg_j_minus_one -1 $j
12-
# sum low 10 $neg_j_minus_one
10+
sum neg_j_minus_one -1 $j
11+
sum low 10 $neg_j_minus_one
1312
# mul high $i 10
1413
# sum value $high $low
1514
# if_lte $value $count

src/commands/command

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@
8787
#define REMOVE_METADATA(name) ScriptInstance::removeMetadata(name)
8888

8989
// Writes a standard command error message with the command name and line number.
90-
#define ERROR(msg) std::cerr << std::endl << "ERROR: '" << COMMAND << "' " << msg << " : " << SOURCE; std::exit(1);
90+
#define ERROR(msg) {std::cerr << std::endl << "ERROR: '" << COMMAND << "' " << msg << " : " << SOURCE; std::exit(1);}

src/commands/comments.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
#include <command>
22
#include <no_op.h>
33

4+
/*
5+
NAME
6+
comments - register comment tokens as no-op commands
7+
8+
SYNOPSIS
9+
# <text>
10+
// <text>
11+
/// <text>
12+
13+
DESCRIPTION
14+
Treats comment-prefixed lines as valid commands that do nothing when
15+
executed.
16+
*/
417

518
namespace
619
{

src/commands/echo.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
#include <command>
22

3+
/*
4+
NAME
5+
echo - write text to standard output
6+
7+
SYNOPSIS
8+
echo <text>
9+
10+
DESCRIPTION
11+
Expands variables in the provided text and writes the result without
12+
appending a trailing newline.
13+
*/
314
REGISTER_COMMAND
415
{
516
auto output = args;

src/commands/end_for.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
#include <command>
22

3+
/*
4+
NAME
5+
end_for - jump back to the matching for loop
6+
7+
SYNOPSIS
8+
end_for
9+
10+
DESCRIPTION
11+
Uses loop metadata recorded by for to continue iteration or fall through
12+
when the loop is complete.
13+
*/
314
REGISTER_COMMAND
415
{
516
try

src/commands/endl.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
#include <command>
22

3+
/*
4+
NAME
5+
endl - write a newline to standard output
6+
7+
SYNOPSIS
8+
endl
9+
10+
DESCRIPTION
11+
Flushes a single line ending to standard output.
12+
*/
313
REGISTER_COMMAND
414
{
515
std::cout << std::endl;
616
std::cout.flush();
7-
};
17+
};

src/commands/for.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
#include <command>
22
#include <findMatch.h>
33

4+
/*
5+
NAME
6+
for - execute a counted loop
7+
8+
SYNOPSIS
9+
for <loop_var> <start> <end> <unary_func>
10+
11+
DESCRIPTION
12+
Iterates from <start> toward the exclusive <end> bound using unary_func,
13+
which must currently be inc or dec. The current value is stored in
14+
<loop_var> for the body of the loop.
15+
*/
416
REGISTER_COMMAND
517
{
618
auto splitArgs = utils::split(args);

src/commands/ret.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
#include <format>
33
#include <split.h>
44

5+
/*
6+
NAME
7+
ret - return from the current script frame
8+
9+
SYNOPSIS
10+
ret <value>
11+
12+
DESCRIPTION
13+
Returns control to the previous stack frame with the given integer status.
14+
If there is no previous frame, the process exits with that status code.
15+
*/
516
REGISTER_COMMAND
617
{
718
try

src/commands/scope.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
#include <command>
22
#include <no_op.h>
33

4+
/*
5+
NAME
6+
scope - register brace tokens as no-op commands
7+
8+
SYNOPSIS
9+
{
10+
}
11+
12+
DESCRIPTION
13+
Treats brace-only lines as valid commands that do nothing when executed.
14+
*/
415

516
namespace
617
{

src/commands/sum.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <command>
2+
3+
/*
4+
NAME
5+
sum - add two integers and store the result
6+
7+
SYNOPSIS
8+
sum <output> <a> <b>
9+
10+
DESCRIPTION
11+
Expands variables in the numeric inputs, adds the two integer values, and
12+
stores the result in <output>.
13+
*/
14+
REGISTER_COMMAND
15+
{
16+
auto vars = args;
17+
DEREFERENCE(vars);
18+
const auto& splitVars = utils::split(vars);
19+
20+
if (splitVars.size() != 3)
21+
ERROR("requires 3 arguments <output> <a> <b>");
22+
23+
try
24+
{
25+
const int a = std::stoi(splitVars[1].c_str());
26+
const int b = std::stoi(splitVars[2].c_str());
27+
SET_VARIABLE(splitVars[0], std::to_string(a + b));
28+
}
29+
catch (std::exception& e)
30+
{
31+
ERROR("Input values must be integers");
32+
}
33+
};

0 commit comments

Comments
 (0)