Skip to content

Commit

Permalink
Make sure to invalidate ugly fysh
Browse files Browse the repository at this point in the history
  • Loading branch information
cbebe committed Feb 7, 2024
1 parent b582dcd commit d8d29ae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
33 changes: 18 additions & 15 deletions src/Lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include "Lexer.h"
#include <iostream>

// debugging help!
// prints the line number of where the peek is called
// #define peek() peek(__LINE__)

bool isSpace(char c) noexcept {
switch (c) {
case ' ':
Expand Down Expand Up @@ -161,8 +165,8 @@ fysh::Fysh fysh::FyshLexer::fyshOutline() noexcept {
get();
return Fysh(Species::DIVIDE, start, current);
}
return Fysh(
Species::INVALID); // should be fixed since comments have slashes too
// should be fixed since comments have slashes too
return Fysh(Species::INVALID);
}

// swim right
Expand All @@ -184,15 +188,15 @@ fysh::Fysh fysh::FyshLexer::fyshOutline() noexcept {
return Fysh(Species::INVALID);
}
}

// swim left
else if (*start == '<') {
char c = peek();
if (isFyshEye(c) || isScale(c)) {
return scales(false); // false for negative
}
return Fysh(Species::INVALID);

// swim left
else if (*start == '<') {
char c = peek();
if (isFyshEye(c) || isScale(c)) {
return scales(false); // false for negative
}
return Fysh(Species::INVALID);
}
return Fysh(Species::END);
}

Expand All @@ -211,15 +215,14 @@ fysh::Fysh fysh::FyshLexer::random() noexcept {
return Fysh(Species::INVALID);
}


fysh::Fysh fysh::FyshLexer::scales(bool positive = true) noexcept {
auto c{get()};
uint32_t value{c == '{' || c == '}'};
while (isScale(peek())) {
auto c{get()};
value = (value << 1) | (c == '{' || c == '}');
}

/*
validate end of fysh
valid ends:
Expand All @@ -239,14 +242,15 @@ fysh::Fysh fysh::FyshLexer::scales(bool positive = true) noexcept {
// check if its the end of the token or not (2nd end character)
if (!isSpace(peek())) {
c = get();
if ((c != '>') && (c != '<' && !positive)) {
if ((positive && (c != '>' || c == '<')) ||
(!positive && (c != '<' || c == '>'))) {
gotoEndOfToken();
return Fysh{Species::INVALID};
}
}

// make sure the token ends
if (!isSpace(peek()) && peek() != '\0'){
if (!isSpace(peek()) && peek() != '\0') {
gotoEndOfToken();
return Fysh{Species::INVALID};
}
Expand All @@ -262,5 +266,4 @@ void fysh::FyshLexer::gotoEndOfToken() noexcept {
while (!isSpace(peek())) {
get();
}

}
9 changes: 8 additions & 1 deletion src/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* \file Lexer.h
*/
#include "Fysh.h"
#include <iostream>

#ifndef FYSH_LEXER_H_
#define FYSH_LEXER_H_
Expand All @@ -33,6 +34,7 @@ class FyshLexer {
FyshLexer(const char *streamStart) noexcept : current{streamStart} {}

Fysh nextFysh() noexcept;
const char *rest() const noexcept { return current; }

private:
Fysh identifier() noexcept;
Expand All @@ -49,7 +51,12 @@ class FyshLexer {
Fysh random() noexcept;
bool isFyshEye(char) noexcept;
bool isPositiveScale() noexcept;
char peek() const noexcept { return *current; }
char peek(int line = -1) const noexcept {
if (line > 0) {
std::cout << "Current (line " << line << "): " << *current << std::endl;
}
return *current;
}
char get() noexcept { return *current++; }
void gotoEndOfToken() noexcept;

Expand Down
10 changes: 5 additions & 5 deletions test/Lexer_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../src/Lexer.h"

#include "doctest.h"
#include <iostream>

using namespace fysh;

Expand Down Expand Up @@ -41,7 +42,6 @@ TEST_CASE("fysh open & wtf open") {
CHECK(lexer.nextFysh() == Species::WTF_OPEN);
}


TEST_CASE("random fysh") {
std::string_view input{"><##> ><###> ><####> <###><"};
// Assuming `input` is a std::string or std::string_view
Expand All @@ -52,7 +52,6 @@ TEST_CASE("random fysh") {
CHECK(lexer.nextFysh() == Species::INVALID);
}


TEST_CASE("negative fysh") {
std::string_view input{"><{{({(o> <3 <o})}>< <o})}><"};
// Assuming `input` is a std::string or std::string_view
Expand All @@ -75,12 +74,13 @@ TEST_CASE("weird fysh") {
}

TEST_CASE("Bad fysh") {
std::string_view input{"><{{({(o><DQHUD ><{{({(o>< ><{{((>< ><{{{< ><o{{}}><"};
std::string_view input{
"><{{({(o><DQHUD ><{{({(o>< ><{{((>< ><{{{< ><o{{}}><"};
// Assuming `input` is a std::string or std::string_view
FyshLexer lexer{input.data()};
CHECK(lexer.nextFysh() == Species::INVALID);
CHECK(lexer.nextFysh() == Species::INVALID);
CHECK(lexer.nextFysh() == Species::INVALID); // this keeps failing for some reason
CHECK(lexer.nextFysh() == Species::INVALID);
CHECK(lexer.nextFysh() == Species::INVALID);
}
CHECK(lexer.nextFysh() == Species::INVALID);
}

0 comments on commit d8d29ae

Please sign in to comment.