Skip to content

Commit

Permalink
Fixed issues with TFileLexer
Browse files Browse the repository at this point in the history
TFileLexer can parse numerics now
TODO TEvent and TPriList combo broken
  • Loading branch information
AdventureT committed Apr 21, 2024
1 parent 863c041 commit 67a72eb
Show file tree
Hide file tree
Showing 15 changed files with 1,073 additions and 95 deletions.
32 changes: 32 additions & 0 deletions Tools/UnitTests/Source/TKernel/TDList_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ struct Test : TDList<Test>::TNode
Test() {}
};

struct Test2 : TPriList<Test>::TNode
{
Test2() : m_iTest(0) {}
Test2(int a_iTest) : m_iTest(a_iTest) {}

int m_iTest;
};


TEST_CASE("List", "[TDList]")
{
Expand All @@ -18,4 +26,28 @@ TEST_CASE("List", "[TDList]")
TDList<int>::TNode* node = list.Begin();
Test *t2 = list.Head();
REQUIRE(t2 == t);
}

TEST_CASE("Priority List", "[TDList]")
{
TPriList<Test2> list;
Test2 t;
list.InsertHead(&t);
REQUIRE(!list.IsEmpty());
REQUIRE(list.Head() == &t);
Test2 t2{1};
list.InsertHead(&t2);
REQUIRE(!list.IsEmpty());
REQUIRE(list.Head() == &t2);
Test2 t3{ 2 };
list.InsertTail(&t3);
REQUIRE(!list.IsEmpty());
REQUIRE(list.Tail() == &t3);

//int expected[2] = { 1, 0 };
//int i = 0;
//for (auto it = list.Begin(); it != list.End(); it++) {
// auto val = *it;
// REQUIRE(val.m_iTest == expected[i++]);
//}
}
46 changes: 40 additions & 6 deletions Tools/UnitTests/Source/TKernel/TFileLexer_Tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <catch2/catch_test_macros.hpp>
#include "TKernel/TFileLexer.h"
#include "TKernel/TFileLexerUTF8.h"

TOSHI_NAMESPACE_USING

Expand All @@ -10,7 +10,8 @@ TEST_CASE("Single Line Comment", "[TFileLexer]")
f->Write(data, strlen(data));
f->Destroy();
f = TFile::Create("singelLineComment.ini", TMODE_READONLY);
TFileLexer lexer = TFileLexer(f, 2);
TFileLexerUTF8 lexer = TFileLexerUTF8(f, 2);
lexer.SetOutputComments(TTRUE);
REQUIRE(lexer.Expect(TFileLexer::TOKEN_COMMENT));
f->Destroy();
}
Expand All @@ -22,10 +23,11 @@ TEST_CASE("Multi Line Comment", "[TFileLexer]")
f->Write(data, strlen(data));
f->Destroy();
f = TFile::Create("multiLineComment.ini", TMODE_READONLY);
TFileLexer lexer = TFileLexer(f, 2);
TFileLexerUTF8 lexer = TFileLexerUTF8(f, 2);
lexer.SetOutputComments(TTRUE);
REQUIRE(lexer.Expect(TFileLexer::TOKEN_COMMENT));
REQUIRE(lexer.Expect(TFileLexer::TOKEN_COMMENT));
TFileLexer::Token token = lexer.GetLastToken();
TFileLexerUTF8::Token token = lexer.GetLastToken();
REQUIRE(token.GetLine() == 3);
f->Destroy();
}
Expand All @@ -37,10 +39,42 @@ TEST_CASE("String", "[TFileLexer]")
f->Write(data, strlen(data));
f->Destroy();
f = TFile::Create("quotes.ini", TMODE_READONLY);
TFileLexer lexer = TFileLexer(f, 2);
TFileLexerUTF8 lexer = TFileLexerUTF8(f, 2);
REQUIRE(lexer.Expect(TFileLexer::TOKEN_STRING));
TFileLexer::Token token = lexer.GetLastToken();
TFileLexerUTF8::Token token = lexer.GetLastToken();
REQUIRE(token.GetString().GetCString() == "Test");
REQUIRE(token.GetLine() == 1);
f->Destroy();
}

TEST_CASE("Numerics", "[TFileLexer]")
{
TFile* f = TFile::Create("numerics.ini", TMODE_CREATE);
TPCCHAR data = "1.0f\n5e+1\n20u\n0x20";
f->Write(data, strlen(data));
f->Destroy();
f = TFile::Create("numerics.ini", TMODE_READONLY);
TFileLexerUTF8 lexer = TFileLexerUTF8(f, 2);

REQUIRE(lexer.Expect(TFileLexer::TOKEN_FLOAT));
TFileLexerUTF8::Token token = lexer.GetLastToken();
REQUIRE(token.GetFloat() == 1.0f);
REQUIRE(token.GetLine() == 1);

REQUIRE(lexer.Expect(TFileLexer::TOKEN_FLOAT));
token = lexer.GetLastToken();
REQUIRE(token.GetFloat() == 50.0f);
REQUIRE(token.GetLine() == 2);

REQUIRE(lexer.Expect(TFileLexer::TOKEN_UINTEGER));
token = lexer.GetLastToken();
REQUIRE(token.GetUInteger() == 20);
REQUIRE(token.GetLine() == 3);

REQUIRE(lexer.Expect(TFileLexer::TOKEN_INTEGER));
token = lexer.GetLastToken();
REQUIRE(token.GetInteger() == 0x20);
REQUIRE(token.GetLine() == 4);

f->Destroy();
}
101 changes: 101 additions & 0 deletions Toshi/Include/TKernel/T2Iterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#pragma once
#include <type_traits>

#define T2_FOREACH(vecName, iteratorName) \
for (auto iteratorName = vecName.Begin(); iteratorName != vecName.End(); iteratorName++)

#define T2_FOREACH_BACK(vecName, iteratorName) \
for (auto iteratorName = vecName.Tail(); iteratorName != vecName.End(); iteratorName--)

#define T2_FOREACH_ARRAY(arrName, iteratorName) \
for (TINT iteratorName = 0; iteratorName < TARRAYSIZE(arrName); iteratorName++)

#define T2_FOREACH_ARRAY_BACK(arrName, iteratorName) \
for (TINT iteratorName = TARRAYSIZE(arrName) - 1; iteratorName >= 0; iteratorName--)

#define T2_DEFINE_ITERATOR_FRIEND() \
template<class T, class Node> friend class Toshi::T2Iterator

#define T2_DEFINE_ITERATOR(TYPE, NODE_TYPE) \
using Iterator_t = Toshi::T2Iterator<TYPE, NODE_TYPE>

namespace Toshi {

template <class T, class Node>
class T2Iterator
{
public:
//TSTATICASSERT(std::is_base_of<Node, T>::value);

T2Iterator()
{
m_pPtr = TNULL;
}

T2Iterator(Node* pPtr)
{
m_pPtr = static_cast<T*>(pPtr);
}

T2Iterator(T* pPtr)
{
m_pPtr = pPtr;
}

void operator=(const T2Iterator& other)
{
m_pPtr = other.m_pPtr;
}

void operator=(T* pPtr)
{
m_pPtr = pPtr;
}

T* operator->() const
{
TASSERT(m_pPtr != TNULL);
return m_pPtr;
}

operator T* () const
{
TASSERT(m_pPtr != TNULL);
return static_cast<T*>(m_pPtr);
}

T2Iterator operator++(TINT)
{
TASSERT(m_pPtr != TNULL);
T2Iterator old = m_pPtr;
m_pPtr = static_cast<T*>(m_pPtr->Next());
return old;
}

T2Iterator operator--(TINT)
{
TASSERT(m_pPtr != TNULL);
T2Iterator old = m_pPtr;
m_pPtr = static_cast<T*>(m_pPtr->Prev());
return old;
}

T2Iterator operator++()
{
TASSERT(m_pPtr != TNULL);
m_pPtr = static_cast<T*>(m_pPtr->Next());
return T2Iterator{ m_pPtr };
}

T2Iterator operator--()
{
TASSERT(m_pPtr != TNULL);
m_pPtr = static_cast<T*>(m_pPtr->Prev());
return T2Iterator{ m_pPtr };
}

private:
T* m_pPtr;
};

}
Loading

0 comments on commit 67a72eb

Please sign in to comment.