Compare commits

...

2 Commits

Author SHA1 Message Date
8a9655cdd5 Added tests for the parser
All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
2025-10-03 22:22:38 +03:00
b6c095caf1 Changed List value to deque for easier testing and use, 2025-10-03 22:22:25 +03:00
2 changed files with 33 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
#pragma once
#include <concepts>
#include <vector>
#include <deque>
#include <cstdint>
#include <string>
#include <variant>
@@ -17,7 +17,7 @@ struct Nil {};
using LispValue = std::variant<Integer, Double, String, Symbol, List>;
struct List {std::vector<LispValue> list;};
struct List {std::deque<LispValue> list;};
// during compilation, we don't really care for cyclical lists etc.
// during compilation we'll mostly be dealing with regular, flat lists
// that form function calls.

View File

@@ -1,7 +1,16 @@
#include "value.hpp"
#include <catch2/catch_test_macros.hpp>
#include <lex.hpp>
#include <parse.hpp>
using namespace std;
template <typename T>
T pop_and_front(deque<T> &dq) {
T t = dq.front();
dq.pop_front();
return t;
}
TEST_CASE("Lexer lexes doubles correctly", "[Lexer]") {
SECTION("double and negative syntax") {
@@ -19,3 +28,25 @@ TEST_CASE("Lexer lexes doubles correctly", "[Lexer]") {
}
}
TEST_CASE("Parser parses correctly", "[Parser]") {
SECTION("hello world") {
Parser p (Lexer("(print \"hello world\")"));
auto dq = get<List>(*p.next()).list;
REQUIRE(get<Symbol>(pop_and_front(dq)).value == "print");
REQUIRE(get<String>(pop_and_front(dq)).value == "hello world");
}
SECTION("doubles") {
Parser p (Lexer("(1.0 0.1 -.1 -1. . - -. .-)"));
auto dq = get<List>(*p.next()).list;
REQUIRE(get<Double>(pop_and_front(dq)).value == 1.0);
REQUIRE(get<Double>(pop_and_front(dq)).value == 0.1);
REQUIRE(get<Double>(pop_and_front(dq)).value == -0.1);
REQUIRE(get<Double>(pop_and_front(dq)).value == -1.0);
REQUIRE(get<Symbol>(pop_and_front(dq)).value == ".");
REQUIRE(get<Symbol>(pop_and_front(dq)).value == "-");
REQUIRE(get<Symbol>(pop_and_front(dq)).value == "-.");
REQUIRE(get<Symbol>(pop_and_front(dq)).value == ".-");
}
}