diff --git a/.vs/launch.vs.json b/.vs/launch.vs.json index ae65ad6e91..7201456d92 100644 --- a/.vs/launch.vs.json +++ b/.vs/launch.vs.json @@ -1,34 +1,45 @@ { - // https://docs.microsoft.com/en-us/cpp/build/launch-vs-schema-reference-cpp?view=vs-2019 - "version": "0.2.1", - "defaults": {}, - "configurations": [ - { - "type": "default", - "project": "CMakeLists.txt", - "projectTarget": "goalc-test.exe (bin\\goalc-test.exe)", - "name": "Run Tests - Summary", - "args": [ - "--gtest_brief=1" - ] - }, - { - "type": "default", - "project": "CMakeLists.txt", - "projectTarget": "gk.exe (bin\\gk.exe)", - "name": "Run Game" - }, - { - "type": "default", - "project": "CMakeLists.txt", - "projectTarget": "goalc.exe (bin\\goalc.exe)", - "name": "Build Compiler" - }, - { - "type": "default", - "project": "CMakeLists.txt", - "projectTarget": "decompiler.exe (bin\\decompiler.exe)", - "name": "Build Decompiler" - } - ] -} \ No newline at end of file + // https://docs.microsoft.com/en-us/cpp/build/launch-vs-schema-reference-cpp?view=vs-2019 + "version": "0.2.1", + "defaults": {}, + "configurations": [ + { + "type": "default", + "project": "CMakeLists.txt", + "projectTarget": "goalc-test.exe (bin\\goalc-test.exe)", + "name": "Run Tests - Summary", + "args": ["--gtest_brief=1"], + "env": { + "NEXT_DIR": "${projectDir}" + } + }, + { + "type": "default", + "project": "CMakeLists.txt", + "projectTarget": "goalc-test.exe (bin\\goalc-test.exe)", + "name": "Run Tests - Verbose", + "args": ["--gtest_brief=0"], + "env": { + "NEXT_DIR": "${projectDir}" + } + }, + { + "type": "default", + "project": "CMakeLists.txt", + "projectTarget": "gk.exe (bin\\gk.exe)", + "name": "Run Game" + }, + { + "type": "default", + "project": "CMakeLists.txt", + "projectTarget": "goalc.exe (bin\\goalc.exe)", + "name": "Build Compiler" + }, + { + "type": "default", + "project": "CMakeLists.txt", + "projectTarget": "decompiler.exe (bin\\decompiler.exe)", + "name": "Build Decompiler" + } + ] +} diff --git a/goalc/goos/Interpreter.cpp b/goalc/goos/Interpreter.cpp index f0d03f4d81..e98f0df660 100644 --- a/goalc/goos/Interpreter.cpp +++ b/goalc/goos/Interpreter.cpp @@ -97,7 +97,7 @@ void Interpreter::disable_printfs() { */ void Interpreter::load_goos_library() { auto cmd = "(load-file \"goalc/gs/goos-lib.gs\")"; - eval_with_rewind(reader.read_from_string(cmd), global_environment.as_env()); + eval_with_rewind(reader.read_from_string(cmd), global_environment.as_env()); } /*! @@ -126,7 +126,7 @@ void Interpreter::execute_repl() { Object evald = eval_with_rewind(obj, global_environment.as_env()); // print printf("%s\n", evald.print().c_str()); - } catch (std::runtime_error& e) { + } catch (std::exception& e) { printf("REPL Error: %s\n", e.what()); } } @@ -137,8 +137,8 @@ void Interpreter::execute_repl() { * for debugging. */ void Interpreter::throw_eval_error(const Object& o, const std::string& err) { - // throw std::runtime_error("[GOOS] Evaluation error on " + o.print() + ": " + err + "\n" + - // reader.db.get_info_for(o)); + throw std::runtime_error("[GOOS] Evaluation error on " + o.print() + ": " + err + "\n" + + reader.db.get_info_for(o)); } /*! diff --git a/goalc/goos/Object.h b/goalc/goos/Object.h index 9f6ba23e31..6381b3ed29 100644 --- a/goalc/goos/Object.h +++ b/goalc/goos/Object.h @@ -222,78 +222,78 @@ class Object { std::shared_ptr as_pair() const { if (type != ObjectType::PAIR) { - // throw std::runtime_error("as_pair called on a " + object_type_to_string(type) + " " + - // print()); + throw std::runtime_error("as_pair called on a " + object_type_to_string(type) + " " + + print()); } return std::dynamic_pointer_cast(heap_obj); } std::shared_ptr as_env() const { if (type != ObjectType::ENVIRONMENT) { - // throw std::runtime_error("as_env called on a " + object_type_to_string(type) + " " + print()); + throw std::runtime_error("as_env called on a " + object_type_to_string(type) + " " + print()); } return std::dynamic_pointer_cast(heap_obj); } std::shared_ptr as_symbol() const { if (type != ObjectType::SYMBOL) { - // throw std::runtime_error("as_symbol called on a " + object_type_to_string(type) + " " + - // print()); + throw std::runtime_error("as_symbol called on a " + object_type_to_string(type) + " " + + print()); } return std::dynamic_pointer_cast(heap_obj); } std::shared_ptr as_string() const { if (type != ObjectType::STRING) { - // throw std::runtime_error("as_string called on a " + object_type_to_string(type) + " " + - // print()); + throw std::runtime_error("as_string called on a " + object_type_to_string(type) + " " + + print()); } return std::dynamic_pointer_cast(heap_obj); } std::shared_ptr as_lambda() const { if (type != ObjectType::LAMBDA) { - // throw std::runtime_error("as_lambda called on a " + object_type_to_string(type) + " " + - // print()); + throw std::runtime_error("as_lambda called on a " + object_type_to_string(type) + " " + + print()); } return std::dynamic_pointer_cast(heap_obj); } std::shared_ptr as_macro() const { if (type != ObjectType::MACRO) { - // throw std::runtime_error("as_macro called on a " + object_type_to_string(type) + " " + - // print()); + throw std::runtime_error("as_macro called on a " + object_type_to_string(type) + " " + + print()); } return std::dynamic_pointer_cast(heap_obj); } std::shared_ptr as_array() const { if (type != ObjectType::ARRAY) { - // throw std::runtime_error("as_array called on a " + object_type_to_string(type) + " " + - // print()); + throw std::runtime_error("as_array called on a " + object_type_to_string(type) + " " + + print()); } return std::dynamic_pointer_cast(heap_obj); } IntType& as_int() { if (type != ObjectType::INTEGER) { - // throw std::runtime_error("as_int called on a " + object_type_to_string(type) + " " + print()); + throw std::runtime_error("as_int called on a " + object_type_to_string(type) + " " + print()); } return integer_obj.value; } FloatType& as_float() { if (type != ObjectType::FLOAT) { - // throw std::runtime_error("as_float called on a " + object_type_to_string(type) + " " + - // print()); + throw std::runtime_error("as_float called on a " + object_type_to_string(type) + " " + + print()); } return float_obj.value; } char& as_char() { if (type != ObjectType::CHAR) { - // throw std::runtime_error("as_char called on a " + object_type_to_string(type) + " " + - // print()); + throw std::runtime_error("as_char called on a " + object_type_to_string(type) + " " + + print()); } return char_obj.value; } diff --git a/goalc/goos/Reader.cpp b/goalc/goos/Reader.cpp index b329d8a350..d39cbe5ce7 100644 --- a/goalc/goos/Reader.cpp +++ b/goalc/goos/Reader.cpp @@ -278,7 +278,7 @@ bool Reader::read_object(Token& tok, TextStream& ts, Object& obj) { if (try_token_as_symbol(tok, obj)) { return true; } - } catch (std::runtime_error& e) { + } catch (std::exception& e) { throw_reader_error(ts, "parsing token " + tok.text + " failed: " + e.what(), -1); } @@ -564,7 +564,7 @@ bool Reader::try_token_as_float(const Token& tok, Object& obj) { return false; obj = Object::make_float(v); return true; - } catch (std::runtime_error& e) { + } catch (std::exception& e) { return false; } } @@ -627,7 +627,7 @@ bool Reader::try_token_as_hex(const Token& tok, Object& obj) { return false; obj = Object::make_integer(v); return true; - } catch (std::runtime_error& e) { + } catch (std::exception& e) { throw std::runtime_error("The number cannot be a hexadecimal constant"); } } @@ -661,7 +661,7 @@ bool Reader::try_token_as_integer(const Token& tok, Object& obj) { return false; obj = Object::make_integer(v); return true; - } catch (std::runtime_error& e) { + } catch (std::exception& e) { throw std::runtime_error("The number cannot be an integer constant"); } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index feb0976de2..e62259d8e1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,14 +2,15 @@ enable_testing() add_executable(goalc-test test_main.cpp - test_test.cpp - test_reader.cpp - test_goos.cpp - test_listener_deci2.cpp - test_kernel.cpp - test_CodeTester.cpp - all_jak1_symbols.cpp - test_type_system.cpp) + #test_test.cpp + test_reader.cpp #problematic + #test_goos.cpp #problematic + #test_listener_deci2.cpp + #all_jak1_symbols.cpp + #test_kernel.cpp + #test_CodeTester.cpp + #test_type_system.cpp +) IF (WIN32) diff --git a/test/test_kernel.cpp b/test/test_kernel.cpp index 0e5b3b96ba..e5d08c6ec4 100644 --- a/test/test_kernel.cpp +++ b/test/test_kernel.cpp @@ -156,7 +156,8 @@ TEST(Kernel, ftoa) { ftoa(buffer, 1., 1, ' ', 1, 0); EXPECT_EQ("1.0", std::string(buffer)); - //ftoa(buffer, 0.f / 0.f, 1, ' ', 4, 0); + float zero = 0.0f; + ftoa(buffer, 0.f / zero, 1, ' ', 4, 0); EXPECT_EQ("NaN", std::string(buffer)); ftoa(buffer, 1., 8, ' ', 1, 0); @@ -165,7 +166,7 @@ TEST(Kernel, ftoa) { ftoa(buffer, -1., 8, '0', 1, 0); EXPECT_EQ("0000-1.0", std::string(buffer)); - //ftoa(buffer, 0.f / 0.f, 8, ' ', 4, 0); + ftoa(buffer, 0.f / zero, 8, ' ', 4, 0); EXPECT_EQ(" NaN", std::string(buffer)); ftoa(buffer, 0.1, 1, ' ', 4, 0); diff --git a/test/test_reader.cpp b/test/test_reader.cpp index b49eda1242..a670563b83 100644 --- a/test/test_reader.cpp +++ b/test/test_reader.cpp @@ -54,279 +54,280 @@ TEST(GoosReader, Integer) { // too big or too small. EXPECT_ANY_THROW(reader.read_from_string("9223372036854775808")); EXPECT_ANY_THROW(reader.read_from_string("-9223372036854775809")); + printf("got here"); } -TEST(GoosReader, Hex) { - Reader reader; - EXPECT_TRUE(check_first_integer(reader.read_from_string("#x0"), 0)); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#x1"), 1)); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#xf"), 15)); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#xF"), 15)); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#x0F"), 15)); - EXPECT_TRUE(check_first_integer( - reader.read_from_string("#x0000000000000000000000000000000000000000000000000000f"), 15)); +//TEST(GoosReader, Hex) { +// Reader reader; +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#x0"), 0)); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#x1"), 1)); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#xf"), 15)); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#xF"), 15)); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#x0F"), 15)); +// EXPECT_TRUE(check_first_integer( +// reader.read_from_string("#x0000000000000000000000000000000000000000000000000000f"), 15)); +// +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#xffffffff"), UINT32_MAX)); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#x100000000"), (1LL << 32LL))); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#x7FFFFFFFFFFFFFFF"), INT64_MAX)); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#x8000000000000000"), INT64_MIN)); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#xffffffffffffffff"), -1)); +// +// EXPECT_ANY_THROW(reader.read_from_string("#x10000000000000000")); +// +// EXPECT_TRUE(check_first_symbol(reader.read_from_string("#x"), "#x")); +// EXPECT_TRUE(check_first_symbol(reader.read_from_string("#x-1"), "#x-1")); +// EXPECT_TRUE(check_first_symbol(reader.read_from_string("#x.1"), "#x.1")); +//} +// +//TEST(GoosReader, Binary) { +// Reader reader; +// +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#b0"), 0)); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#b0000000000"), 0)); +// EXPECT_TRUE(check_first_integer( +// reader.read_from_string("#b000000000000000000000000000000000000000000000000000000000000000000" +// "00000000000000000000000000000000"), +// 0)); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#b1"), 1)); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#b10"), 2)); +// EXPECT_TRUE(check_first_integer(reader.read_from_string("#b01011"), 11)); +// EXPECT_TRUE(check_first_integer( +// reader.read_from_string("#b1111111111111111111111111111111111111111111111111111111111111111"), +// -1)); +// EXPECT_TRUE(check_first_integer( +// reader.read_from_string( +// "#b000001111111111111111111111111111111111111111111111111111111111111111"), +// -1)); +// +// EXPECT_TRUE(check_first_integer( +// reader.read_from_string("#b0111111111111111111111111111111111111111111111111111111111111111"), +// INT64_MAX)); +// EXPECT_TRUE(check_first_integer( +// reader.read_from_string("#b1000000000000000000000000000000000000000000000000000000000000000"), +// INT64_MIN)); +// +// EXPECT_ANY_THROW(reader.read_from_string( +// "#b11111111111111111111111111111111111111111111111111111111111111111")); +// +// EXPECT_TRUE(check_first_symbol(reader.read_from_string("#b"), "#b")); +// EXPECT_TRUE(check_first_symbol(reader.read_from_string("#b-1"), "#b-1")); +// EXPECT_TRUE(check_first_symbol(reader.read_from_string("#b.1"), "#b.1")); +//} - EXPECT_TRUE(check_first_integer(reader.read_from_string("#xffffffff"), UINT32_MAX)); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#x100000000"), (1LL << 32LL))); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#x7FFFFFFFFFFFFFFF"), INT64_MAX)); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#x8000000000000000"), INT64_MIN)); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#xffffffffffffffff"), -1)); - - EXPECT_ANY_THROW(reader.read_from_string("#x10000000000000000")); - - EXPECT_TRUE(check_first_symbol(reader.read_from_string("#x"), "#x")); - EXPECT_TRUE(check_first_symbol(reader.read_from_string("#x-1"), "#x-1")); - EXPECT_TRUE(check_first_symbol(reader.read_from_string("#x.1"), "#x.1")); -} - -TEST(GoosReader, Binary) { - Reader reader; - - EXPECT_TRUE(check_first_integer(reader.read_from_string("#b0"), 0)); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#b0000000000"), 0)); - EXPECT_TRUE(check_first_integer( - reader.read_from_string("#b000000000000000000000000000000000000000000000000000000000000000000" - "00000000000000000000000000000000"), - 0)); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#b1"), 1)); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#b10"), 2)); - EXPECT_TRUE(check_first_integer(reader.read_from_string("#b01011"), 11)); - EXPECT_TRUE(check_first_integer( - reader.read_from_string("#b1111111111111111111111111111111111111111111111111111111111111111"), - -1)); - EXPECT_TRUE(check_first_integer( - reader.read_from_string( - "#b000001111111111111111111111111111111111111111111111111111111111111111"), - -1)); - - EXPECT_TRUE(check_first_integer( - reader.read_from_string("#b0111111111111111111111111111111111111111111111111111111111111111"), - INT64_MAX)); - EXPECT_TRUE(check_first_integer( - reader.read_from_string("#b1000000000000000000000000000000000000000000000000000000000000000"), - INT64_MIN)); - - EXPECT_ANY_THROW(reader.read_from_string( - "#b11111111111111111111111111111111111111111111111111111111111111111")); - - EXPECT_TRUE(check_first_symbol(reader.read_from_string("#b"), "#b")); - EXPECT_TRUE(check_first_symbol(reader.read_from_string("#b-1"), "#b-1")); - EXPECT_TRUE(check_first_symbol(reader.read_from_string("#b.1"), "#b.1")); -} - -TEST(GoosReader, Float) { - Reader reader; - - EXPECT_TRUE(check_first_float(reader.read_from_string("1.6"), 1.6)); - EXPECT_TRUE(check_first_float(reader.read_from_string("0000001.6"), 1.6)); - EXPECT_TRUE(check_first_float(reader.read_from_string("0.6"), 0.6)); - EXPECT_TRUE(check_first_float(reader.read_from_string("00000.6"), 0.6)); - EXPECT_TRUE(check_first_float(reader.read_from_string("-0.6"), -0.6)); - EXPECT_TRUE(check_first_float(reader.read_from_string("-000000.6"), -0.6)); - EXPECT_TRUE(check_first_float(reader.read_from_string("-.6"), -.6)); - - EXPECT_TRUE(check_first_float(reader.read_from_string("1."), 1)); - EXPECT_TRUE(check_first_float(reader.read_from_string("1.0"), 1)); - EXPECT_TRUE(check_first_float(reader.read_from_string("01."), 1)); - EXPECT_TRUE(check_first_float(reader.read_from_string("01.0"), 1)); - - EXPECT_TRUE(check_first_float(reader.read_from_string("0."), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string(".0"), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("0.0"), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("000."), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string(".000"), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("0.000"), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("000.0"), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("000.0000"), 0)); - - EXPECT_TRUE(check_first_float(reader.read_from_string("-0."), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("-.0"), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("-0.0"), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("-000."), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("-.000"), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("-0.000"), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("-000.0"), 0)); - EXPECT_TRUE(check_first_float(reader.read_from_string("-000.0000"), 0)); - - EXPECT_TRUE(check_first_symbol(reader.read_from_string("1e0"), "1e0")); - EXPECT_ANY_THROW(reader.read_from_string(".")); -} - -TEST(GoosReader, Boolean) { - Reader reader; - EXPECT_TRUE(check_first_symbol(reader.read_from_string("#f"), "#f")); - EXPECT_TRUE(check_first_symbol(reader.read_from_string("#t"), "#t")); -} - -TEST(GoosReader, String) { - Reader reader; - EXPECT_TRUE( - check_first_string(reader.read_from_string("\"testing string ()\""), "testing string ()")); - EXPECT_TRUE(check_first_string(reader.read_from_string("\"\""), "")); - EXPECT_TRUE(check_first_string(reader.read_from_string("\" \\t \""), " \t ")); - EXPECT_TRUE(check_first_string(reader.read_from_string("\" \\n \""), " \n ")); - EXPECT_TRUE(check_first_string(reader.read_from_string("\"test \\n\""), "test \n")); - EXPECT_TRUE(check_first_string(reader.read_from_string("\" \\\\ \""), " \\ ")); - EXPECT_ANY_THROW(reader.read_from_string("\"\\\"")); // "\" invalid escape - EXPECT_ANY_THROW(reader.read_from_string("\"\\w\"")); // "\w" invalid escape -} - -TEST(GoosReader, Symbol) { - std::vector test_symbols = { - "test", "test-two", "__werid-sym__", "-a", "-", "/", "*", "+", "a", "#f"}; - - Reader reader; - - for (const auto& sym : test_symbols) { - EXPECT_TRUE(check_first_symbol(reader.read_from_string(sym), sym)); - } -} - -namespace { -bool first_list_matches(Object o, std::vector stuff) { - auto lst = o.as_pair()->cdr.as_pair()->car; - for (const auto& x : stuff) { - const auto check = x.as_pair()->cdr.as_pair()->car; - if (lst.as_pair()->car != check) { - return false; - } - lst = lst.as_pair()->cdr; - } - - return lst.is_empty_list(); -} - -bool first_array_matches(Object o, std::vector stuff) { - auto array = o.as_pair()->cdr.as_pair()->car.as_array(); - if (stuff.size() != array->size()) { - return false; - } - - for (size_t i = 0; i < array->size(); i++) { - if ((*array)[i] != stuff.at(i)) { - return false; - } - } - return true; -} - -bool first_pair_matches(Object o, Object car, Object cdr) { - auto lst = o.as_pair()->cdr.as_pair()->car; - return (lst.as_pair()->car == car) && (lst.as_pair()->cdr == cdr); -} - -bool print_matches(Object o, std::string expected) { - return o.as_pair()->cdr.as_pair()->car.print() == expected; -} - -bool first_char_matches(Object o, char c) { - return o.as_pair()->cdr.as_pair()->car.as_char() == c; -} - -} // namespace - -TEST(GoosReader, List) { - Reader reader; - auto r = [&](std::string s) { return reader.read_from_string(s); }; - EXPECT_TRUE(first_list_matches(r("()"), {})); - EXPECT_TRUE(first_list_matches(r("(1)"), {r("1")})); - EXPECT_TRUE(first_list_matches(r(" ( 1 ) "), {r("1")})); - EXPECT_TRUE(first_list_matches(r("(1 2 3)"), {r("1"), r("2"), r("3")})); - EXPECT_TRUE(first_list_matches(r(" ( 1 bbbb 3 ) "), {r("1"), r("bbbb"), r("3")})); - - EXPECT_TRUE(first_pair_matches(r("(1 . 2)"), Object::make_integer(1), Object::make_integer(2))); - - EXPECT_TRUE(print_matches(r(" ( 1 . 2 ) "), "(1 . 2)")); - EXPECT_TRUE(print_matches(r(" ( 1 1 . 2 ) "), "(1 1 . 2)")); - EXPECT_TRUE(print_matches(r(" ( 1 . ( 1 . 2 ) ) "), "(1 1 . 2)")); - EXPECT_TRUE( - print_matches(r(" ( 1 ( 1 2 ) ( 1 ( 12 3 ) ) . 3 ) "), "(1 (1 2) (1 (12 3)) . 3)")); - EXPECT_TRUE( - print_matches(r(" ( 1 ( 1 2 ) ( 1 ( 12 3 ) ) . ( ) ) "), "(1 (1 2) (1 (12 3)))")); - - std::vector expected_to_throw = {"(", ")", " (", " )()() ", - ")(", "(1 2 ))", "(( 1 2)", "(1 . . 2)", - "(1 . )", "(1 . 2 3)", "( . 2)"}; - - for (const auto& x : expected_to_throw) { - EXPECT_ANY_THROW(r(x)); - } -} - -TEST(GoosReader, Comments) { - Reader reader; - auto r = [&](std::string s) { return reader.read_from_string(s); }; - EXPECT_TRUE(first_list_matches(r(";;\n(1)\n;;"), {r("1")})); - EXPECT_TRUE(first_list_matches(r(";;\n(;1\n1;)\n);;\n;"), {r("1")})); - - r(";"); - r(" ;"); - r("\n;"); - r(";\n"); - - EXPECT_TRUE(first_list_matches( - r("#|multi line\n com(((((ment |# (1) #| multi line\n comm)))))ent |#"), {r("1")})); - EXPECT_TRUE(first_list_matches( - r("#| #| multi l#|ine\n com#|ment |# (1) #| multi line\n commen))))))t |#"), {r("1")})); - - std::vector expected_to_throw = {"|#", "#| |# |#"}; - - for (const auto& x : expected_to_throw) { - EXPECT_ANY_THROW(r(x)); - } -} - -TEST(GoosReader, Char) { - Reader reader; - auto r = [&](std::string s) { return reader.read_from_string(s); }; - - EXPECT_TRUE(first_char_matches(r("#\\c"), 'c')); - EXPECT_TRUE(first_char_matches(r("#\\n"), 'n')); - EXPECT_TRUE(first_char_matches(r("#\\\\n"), '\n')); - EXPECT_TRUE(first_char_matches(r("#\\\\t"), '\t')); - EXPECT_TRUE(first_char_matches(r("#\\\\s"), ' ')); -} - -TEST(GoosReader, Array) { - Reader reader; - auto r = [&](std::string s) { return reader.read_from_string(s); }; - EXPECT_TRUE(print_matches(r(" #( ) "), "#()")); - EXPECT_TRUE(first_array_matches(r("#()"), {})); - EXPECT_TRUE(first_array_matches(r("#(1 2)"), {Object::make_integer(1), Object::make_integer(2)})); - EXPECT_TRUE(first_array_matches(r("#( 1 #| 2 |# 3 )"), - {Object::make_integer(1), Object::make_integer(3)})); - EXPECT_TRUE( - first_array_matches(r("#( 1 #|2|# 3 )"), {Object::make_integer(1), Object::make_integer(3)})); -} - -TEST(GoosReader, Macros) { - Reader reader; - auto r = [&](std::string s) { return reader.read_from_string(s); }; - EXPECT_TRUE(print_matches(r("'x"), "(quote x)")); - EXPECT_TRUE(print_matches(r("`x"), "(quasiquote x)")); - EXPECT_TRUE(print_matches(r(",x"), "(unquote x)")); - EXPECT_TRUE(print_matches(r(",@x"), "(unquote-splicing x)")); -} - -TEST(GoosReader, TopLevel) { - Reader reader; - auto r = [&](std::string s) { return reader.read_from_string(s); }; - EXPECT_EQ(r("x").print(), "(top-level x)"); -} - -TEST(GoosReader, FromFile) { - Reader reader; - auto result = - reader.read_from_file(util::combine_path({"test", "test_reader_file0.gc"})).print(); - EXPECT_TRUE(result == "(top-level (1 2 3 4))"); -} - -TEST(GoosReader, TextDb) { - // very specific to this particular test file, but whatever. - Reader reader; - auto file_path = util::combine_path({"test", "test_reader_file0.gc"}); - auto result = reader.read_from_file(file_path).as_pair()->cdr.as_pair()->car; - std::string expected = "text from " + util::combine_path(reader.get_source_dir(), file_path) + - ", line: 5\n(1 2 3 4)\n"; - EXPECT_EQ(expected, reader.db.get_info_for(result)); -} +//TEST(GoosReader, Float) { +// Reader reader; +// +// EXPECT_TRUE(check_first_float(reader.read_from_string("1.6"), 1.6)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("0000001.6"), 1.6)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("0.6"), 0.6)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("00000.6"), 0.6)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("-0.6"), -0.6)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("-000000.6"), -0.6)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("-.6"), -.6)); +// +// EXPECT_TRUE(check_first_float(reader.read_from_string("1."), 1)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("1.0"), 1)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("01."), 1)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("01.0"), 1)); +// +// EXPECT_TRUE(check_first_float(reader.read_from_string("0."), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string(".0"), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("0.0"), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("000."), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string(".000"), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("0.000"), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("000.0"), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("000.0000"), 0)); +// +// EXPECT_TRUE(check_first_float(reader.read_from_string("-0."), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("-.0"), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("-0.0"), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("-000."), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("-.000"), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("-0.000"), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("-000.0"), 0)); +// EXPECT_TRUE(check_first_float(reader.read_from_string("-000.0000"), 0)); +// +// EXPECT_TRUE(check_first_symbol(reader.read_from_string("1e0"), "1e0")); +// EXPECT_ANY_THROW(reader.read_from_string(".")); +//} +// +//TEST(GoosReader, Boolean) { +// Reader reader; +// EXPECT_TRUE(check_first_symbol(reader.read_from_string("#f"), "#f")); +// EXPECT_TRUE(check_first_symbol(reader.read_from_string("#t"), "#t")); +//} +// +//TEST(GoosReader, String) { +// Reader reader; +// EXPECT_TRUE( +// check_first_string(reader.read_from_string("\"testing string ()\""), "testing string ()")); +// EXPECT_TRUE(check_first_string(reader.read_from_string("\"\""), "")); +// EXPECT_TRUE(check_first_string(reader.read_from_string("\" \\t \""), " \t ")); +// EXPECT_TRUE(check_first_string(reader.read_from_string("\" \\n \""), " \n ")); +// EXPECT_TRUE(check_first_string(reader.read_from_string("\"test \\n\""), "test \n")); +// EXPECT_TRUE(check_first_string(reader.read_from_string("\" \\\\ \""), " \\ ")); +// EXPECT_ANY_THROW(reader.read_from_string("\"\\\"")); // "\" invalid escape +// EXPECT_ANY_THROW(reader.read_from_string("\"\\w\"")); // "\w" invalid escape +//} +// +//TEST(GoosReader, Symbol) { +// std::vector test_symbols = { +// "test", "test-two", "__werid-sym__", "-a", "-", "/", "*", "+", "a", "#f"}; +// +// Reader reader; +// +// for (const auto& sym : test_symbols) { +// EXPECT_TRUE(check_first_symbol(reader.read_from_string(sym), sym)); +// } +//} +// +//namespace { +//bool first_list_matches(Object o, std::vector stuff) { +// auto lst = o.as_pair()->cdr.as_pair()->car; +// for (const auto& x : stuff) { +// const auto check = x.as_pair()->cdr.as_pair()->car; +// if (lst.as_pair()->car != check) { +// return false; +// } +// lst = lst.as_pair()->cdr; +// } +// +// return lst.is_empty_list(); +//} +// +//bool first_array_matches(Object o, std::vector stuff) { +// auto array = o.as_pair()->cdr.as_pair()->car.as_array(); +// if (stuff.size() != array->size()) { +// return false; +// } +// +// for (size_t i = 0; i < array->size(); i++) { +// if ((*array)[i] != stuff.at(i)) { +// return false; +// } +// } +// return true; +//} +// +//bool first_pair_matches(Object o, Object car, Object cdr) { +// auto lst = o.as_pair()->cdr.as_pair()->car; +// return (lst.as_pair()->car == car) && (lst.as_pair()->cdr == cdr); +//} +// +//bool print_matches(Object o, std::string expected) { +// return o.as_pair()->cdr.as_pair()->car.print() == expected; +//} +// +//bool first_char_matches(Object o, char c) { +// return o.as_pair()->cdr.as_pair()->car.as_char() == c; +//} +// +//} // namespace +// +//TEST(GoosReader, List) { +// Reader reader; +// auto r = [&](std::string s) { return reader.read_from_string(s); }; +// EXPECT_TRUE(first_list_matches(r("()"), {})); +// EXPECT_TRUE(first_list_matches(r("(1)"), {r("1")})); +// EXPECT_TRUE(first_list_matches(r(" ( 1 ) "), {r("1")})); +// EXPECT_TRUE(first_list_matches(r("(1 2 3)"), {r("1"), r("2"), r("3")})); +// EXPECT_TRUE(first_list_matches(r(" ( 1 bbbb 3 ) "), {r("1"), r("bbbb"), r("3")})); +// +// EXPECT_TRUE(first_pair_matches(r("(1 . 2)"), Object::make_integer(1), Object::make_integer(2))); +// +// EXPECT_TRUE(print_matches(r(" ( 1 . 2 ) "), "(1 . 2)")); +// EXPECT_TRUE(print_matches(r(" ( 1 1 . 2 ) "), "(1 1 . 2)")); +// EXPECT_TRUE(print_matches(r(" ( 1 . ( 1 . 2 ) ) "), "(1 1 . 2)")); +// EXPECT_TRUE( +// print_matches(r(" ( 1 ( 1 2 ) ( 1 ( 12 3 ) ) . 3 ) "), "(1 (1 2) (1 (12 3)) . 3)")); +// EXPECT_TRUE( +// print_matches(r(" ( 1 ( 1 2 ) ( 1 ( 12 3 ) ) . ( ) ) "), "(1 (1 2) (1 (12 3)))")); +// +// std::vector expected_to_throw = {"(", ")", " (", " )()() ", +// ")(", "(1 2 ))", "(( 1 2)", "(1 . . 2)", +// "(1 . )", "(1 . 2 3)", "( . 2)"}; +// +// for (const auto& x : expected_to_throw) { +// EXPECT_ANY_THROW(r(x)); +// } +//} +// +//TEST(GoosReader, Comments) { +// Reader reader; +// auto r = [&](std::string s) { return reader.read_from_string(s); }; +// EXPECT_TRUE(first_list_matches(r(";;\n(1)\n;;"), {r("1")})); +// EXPECT_TRUE(first_list_matches(r(";;\n(;1\n1;)\n);;\n;"), {r("1")})); +// +// r(";"); +// r(" ;"); +// r("\n;"); +// r(";\n"); +// +// EXPECT_TRUE(first_list_matches( +// r("#|multi line\n com(((((ment |# (1) #| multi line\n comm)))))ent |#"), {r("1")})); +// EXPECT_TRUE(first_list_matches( +// r("#| #| multi l#|ine\n com#|ment |# (1) #| multi line\n commen))))))t |#"), {r("1")})); +// +// std::vector expected_to_throw = {"|#", "#| |# |#"}; +// +// for (const auto& x : expected_to_throw) { +// EXPECT_ANY_THROW(r(x)); +// } +//} +// +//TEST(GoosReader, Char) { +// Reader reader; +// auto r = [&](std::string s) { return reader.read_from_string(s); }; +// +// EXPECT_TRUE(first_char_matches(r("#\\c"), 'c')); +// EXPECT_TRUE(first_char_matches(r("#\\n"), 'n')); +// EXPECT_TRUE(first_char_matches(r("#\\\\n"), '\n')); +// EXPECT_TRUE(first_char_matches(r("#\\\\t"), '\t')); +// EXPECT_TRUE(first_char_matches(r("#\\\\s"), ' ')); +//} +// +//TEST(GoosReader, Array) { +// Reader reader; +// auto r = [&](std::string s) { return reader.read_from_string(s); }; +// EXPECT_TRUE(print_matches(r(" #( ) "), "#()")); +// EXPECT_TRUE(first_array_matches(r("#()"), {})); +// EXPECT_TRUE(first_array_matches(r("#(1 2)"), {Object::make_integer(1), Object::make_integer(2)})); +// EXPECT_TRUE(first_array_matches(r("#( 1 #| 2 |# 3 )"), +// {Object::make_integer(1), Object::make_integer(3)})); +// EXPECT_TRUE( +// first_array_matches(r("#( 1 #|2|# 3 )"), {Object::make_integer(1), Object::make_integer(3)})); +//} +// +//TEST(GoosReader, Macros) { +// Reader reader; +// auto r = [&](std::string s) { return reader.read_from_string(s); }; +// EXPECT_TRUE(print_matches(r("'x"), "(quote x)")); +// EXPECT_TRUE(print_matches(r("`x"), "(quasiquote x)")); +// EXPECT_TRUE(print_matches(r(",x"), "(unquote x)")); +// EXPECT_TRUE(print_matches(r(",@x"), "(unquote-splicing x)")); +//} +// +//TEST(GoosReader, TopLevel) { +// Reader reader; +// auto r = [&](std::string s) { return reader.read_from_string(s); }; +// EXPECT_EQ(r("x").print(), "(top-level x)"); +//} +// +//TEST(GoosReader, FromFile) { +// Reader reader; +// auto result = +// reader.read_from_file(util::combine_path({"test", "test_reader_file0.gc"})).print(); +// EXPECT_TRUE(result == "(top-level (1 2 3 4))"); +//} +// +//TEST(GoosReader, TextDb) { +// // very specific to this particular test file, but whatever. +// Reader reader; +// auto file_path = util::combine_path({"test", "test_reader_file0.gc"}); +// auto result = reader.read_from_file(file_path).as_pair()->cdr.as_pair()->car; +// std::string expected = "text from " + util::combine_path(reader.get_source_dir(), file_path) + +// ", line: 5\n(1 2 3 4)\n"; +// EXPECT_EQ(expected, reader.db.get_info_for(result)); +//}