Skip to content

Commit 8729f9a

Browse files
committed
Standalone programs.
Demo stack machine. Update README.md.
1 parent 08ab835 commit 8729f9a

File tree

9 files changed

+213
-11
lines changed

9 files changed

+213
-11
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ IndentWidth: 8
33
PointerAlignment: Left
44
SpaceBeforeParens: ControlStatements
55
BreakBeforeBraces: Allman
6+
SortIncludes: false

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(CMAKE_C_COMPILER gcc)
66
set(CMAKE_C_STANDARD 99)
77

88
add_subdirectory("include")
9+
add_subdirectory("programs")
910

1011
if(NOT DEFINED ENV{NO_BENCHMARKS})
1112
add_subdirectory("benchmarks")

README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,39 @@
33
[![Documentation Status](https://readthedocs.org/projects/leet/badge/?version=latest)](https://leet.readthedocs.io/en/latest/?badge=latest)
44

55
# leet
6-
This repo contains a well documented and tested header library for data structures and algorithms, along with my solutions to leetcode problems. The solutions use the header library through icanc[^5].
6+
This repo contains a well documented and tested header library for data structures and algorithms, assorted standalone programs, and solutions to leetcode problems. The solutions use the header library through icanc[^5].
77

88
## Getting started
9-
1. Check out the [documentation for the header library](https://leet.readthedocs.io/en/latest/lib/index.html);
9+
1. Check out the [documentation](https://leet.readthedocs.io/en/latest/lib/index.html);
1010
2. Browse the library code at [`include/`](https://github.com/voxelstack/leet/tree/main/include);
11-
3. See the library being used at [`tests/`](https://github.com/voxelstack/leet/tree/main/tests);
12-
4. *If you have solved a problem* and would like to compare your solution to mine or look for alternate solutions, see [my commentary for the solutions](https://leet.readthedocs.io/en/latest/problems/index.html#) (which includes links to the source code), or browse the source code at [`problems/`](https://github.com/voxelstack/leet/tree/main/problems/).
11+
3. Browse the programs at [`programs/`](https://github.com/voxelstack/leet/tree/main/programs);
12+
3. See the tests at [`tests/`](https://github.com/voxelstack/leet/tree/main/tests);
13+
4. See the benchmarks at [`benchmarks/`](https://github.com/voxelstack/leet/tree/main/benchmarks);
14+
5. *If you have solved a problem* and would like to compare your solution to mine or look for alternate solutions, see [my commentary for the solutions](https://leet.readthedocs.io/en/latest/problems/index.html#) (which includes links to the source code), or browse the source code at [`problems/`](https://github.com/voxelstack/leet/tree/main/problems/).
1315

1416
## Features
1517
### Header library
1618
On the `include/` directory you will find a header library with my implementations of data structures and algorithms.
1719

20+
### Solutions
21+
The `problems/` directory has my solutions to leetcode problems, organized by judge and problem, along with testcases. On the documentation website, you can also see [my commentary on each solution](https://leet.readthedocs.io/en/latest/problems/index.html).
22+
23+
All solutions are tested with icanc[^5] using the testcases from the repo. When you see passing tests, it means that all solutions were accepted for the given testcases. I use the same [definition of accepted](https://www.udebug.com/faq#accepted-section) as udebug[^6]. I also test my solutions with udebug inputs using the icanc udebug integration. Those tests are not committed to the repo.
24+
25+
### Programs
26+
On the `programs/` directory you can find the sources for standalone programs. Whenever I want to study, showcase, or document something that is not a leetcode problem, it goes here.
27+
These are varied programs that don't need their own repo, built only for learning or showcasing skills.
28+
1829
### Documentation
19-
All library code is documented with Doxygen[^1], which is then turned into a [Read the Docs website](https://leet.readthedocs.io/en/latest/?badge=latest) using sphinx[^2] and breathe[^3].
30+
All library code is documented with Doxygen[^1], which is then turned into a [Read the Docs website](https://leet.readthedocs.io/en/latest/?badge=latest) using sphinx[^2] and breathe[^3]. The programs are documented as well.
2031

21-
The setup is based on this [excellent guide](https://devblogs.microsoft.com/cppblog/clear-functional-c-documentation-with-sphinx-breathe-doxygen-cmake/) and adapted it to my needs.
32+
The setup is based on this [excellent guide](https://devblogs.microsoft.com/cppblog/clear-functional-c-documentation-with-sphinx-breathe-doxygen-cmake/) and adapted it to my needs. If you want to browse the documentation sources, they can be found in the `docs/` directory.
2233

2334
### Tests
24-
All library code is tested with ctest[^4]. The tests are a good way to see the library in use, and you can find them in the `tests/` directory.
35+
All library code is tested with ctest[^4]. Non-trivial programs may be tested as well. You can find the test code in the `tests/` directory.
2536

26-
### Solutions
27-
The `problems/` directory has my solutions to leetcode problems, organized by judge and problem, along with testcases. On the documentation website, you can also see [my commentary on each solution](https://leet.readthedocs.io/en/latest/problems/index.html).
28-
29-
All solutions are tested with icanc[^5] using the testcases from the repo. When you see passing tests, it means that all solutions were accepted for the given testcases. I use the same [definition of accepted](https://www.udebug.com/faq#accepted-section) as udebug[^6]. I also test my solutions with udebug inputs using the icanc udebug integration. Those tests are not committed to the repo.
37+
### Benchmarks
38+
The performance charts on the documentation come from benchmarks on the `benchmarks/` directory.
3039

3140
## License
3241
The header library is released under the MIT license and meant to be used as a learning resource. You are free to use it on your own leetcode solutions but I'd be happier (and you'd learn more) if you made your own.

cmake/Programs.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_custom_target(programs)
2+
3+
function(leet_program TARGET SOURCE)
4+
add_executable(${TARGET} ${SOURCE})
5+
target_link_libraries(${TARGET} PRIVATE leet)
6+
7+
add_dependencies(programs ${TARGET})
8+
endfunction()

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Algorithms, data structures, and leetcode.
1212

1313
lib/index
1414
problems/index
15+
programs/index
1516
todo
1617

1718
Indices and tables

docs/programs/calc.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
calc
2+
====
3+
4+
A stack machine capable of adding, subtracting, multiplying, and dividing.
5+
6+
Operations
7+
----------
8+
A :code:`|` indicates the program is waiting for an operation. Operations are defined by a single character, possibly with one integer parameter separated by a space.
9+
10+
help
11+
.. code-block::
12+
:caption: Print a help message.
13+
14+
| h
15+
16+
exit
17+
.. code-block::
18+
:caption: Exit the program.
19+
20+
| .
21+
22+
push
23+
.. code-block::
24+
:caption: Push a value onto the stack.
25+
26+
| < 123
27+
28+
pop
29+
.. code-block::
30+
:caption: Print the value at the top of the stack and pop it.
31+
32+
| >
33+
34+
op
35+
Pop the two values on top of the stack, execute an operation, and push the result back onto the stack.
36+
37+
.. code-block::
38+
:caption: 3 * 1 + 4
39+
40+
| < 1
41+
| < 4
42+
| +
43+
| < 3
44+
| *
45+
| >
46+
47+
Outputs
48+
-------
49+
Values printed without a :code:`|` are outputs.
50+
51+
number
52+
The result of a pop.
53+
nil
54+
There is nothing on the stack.
55+
err
56+
Error executing the last command. The stack was not changed.

docs/programs/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Programs
2+
========
3+
4+
.. toctree::
5+
:maxdepth: 2
6+
7+
calc

programs/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include(Programs)
2+
3+
leet_program(calc calc/main.c)

programs/calc/main.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include <leet.h>
2+
#include <ds/arrstack.h>
3+
4+
void
5+
help()
6+
{
7+
printf("help: h\n");
8+
printf("exit: .\n");
9+
printf("push: < 123\n");
10+
printf("pop : >\n");
11+
printf("op : +,-,*,/\n");
12+
}
13+
14+
void
15+
bin_op(struct slice* s, char op)
16+
{
17+
if (s->_ptr < s->_el_size * 2)
18+
{
19+
printf("err\n");
20+
return;
21+
}
22+
23+
int r = (int)s->_data[s->_ptr - s->_el_size];
24+
int l = (int)s->_data[s->_ptr - s->_el_size * 2];
25+
int res;
26+
27+
switch (op)
28+
{
29+
case '+':
30+
res = r + l;
31+
break;
32+
case '-':
33+
res = r - l;
34+
break;
35+
case '*':
36+
res = r * l;
37+
break;
38+
case '/':
39+
res = r / l;
40+
break;
41+
}
42+
43+
s->_ptr -= s->_el_size * 2;
44+
arrstack_push(s, &res);
45+
}
46+
47+
int
48+
main()
49+
{
50+
char in[32];
51+
char op;
52+
int val;
53+
54+
struct slice s = { 0 };
55+
arrstack_make(&s, sizeof(int), 128);
56+
57+
printf("| h\n");
58+
help();
59+
60+
do
61+
{
62+
printf("| ");
63+
64+
fgets(in, sizeof(in), stdin);
65+
if (sscanf(in, "%c %d\n", &op, &val) == 1)
66+
{
67+
switch (op)
68+
{
69+
case '.':
70+
printf("end\n");
71+
return 0;
72+
case 'h':
73+
help();
74+
break;
75+
case '>':
76+
if (arrstack_empty(&s))
77+
{
78+
printf("nil\n");
79+
continue;
80+
}
81+
int* p = arrstack_peek(&s);
82+
printf("%d\n", *p);
83+
arrstack_rwd(&s);
84+
break;
85+
case '+':
86+
case '-':
87+
case '*':
88+
case '/':
89+
bin_op(&s, op);
90+
break;
91+
default:
92+
printf("err\n");
93+
continue;
94+
}
95+
}
96+
else
97+
{
98+
switch (op)
99+
{
100+
case '<':
101+
if (s._ptr >= s._capacity)
102+
{
103+
printf("full\n");
104+
continue;
105+
}
106+
arrstack_push(&s, &val);
107+
break;
108+
default:
109+
printf("err\n");
110+
continue;
111+
}
112+
}
113+
} while (true);
114+
115+
return 0;
116+
}

0 commit comments

Comments
 (0)