Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(t6): comment lexing & empty file crash #233

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/arc/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ auto lexer::lex() -> token
}
else
{
auto first = true;

while (true)
{
if (reader_.ended())
Expand All @@ -94,18 +96,21 @@ auto lexer::lex() -> token
loc_.lines();
loc_.step();
}
else if (last == '#' && curr == '/')
else if (last == '#' && curr == '/' && !first)
{
advance();
break;
}

advance();
first = false;
}
}
}
else if (last == '@')
{
auto first = true;

while (true)
{
if (reader_.ended())
Expand All @@ -116,17 +121,20 @@ auto lexer::lex() -> token
loc_.lines();
loc_.step();
}
else if (last == '@' && curr == '/')
else if (last == '@' && curr == '/' && !first)
{
advance();
break;
}

advance();
first = false;
}
}
else if (last == '*')
{
auto first = true;

while (true)
{
if (reader_.ended())
Expand All @@ -137,13 +145,14 @@ auto lexer::lex() -> token
loc_.lines();
loc_.step();
}
else if (last == '*' && curr == '/')
else if (last == '*' && curr == '/' && !first)
{
advance();
break;
}

advance();
first = false;
}
}
else if (last == '/')
Expand Down
15 changes: 12 additions & 3 deletions src/gsc/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ auto lexer::lex() -> token
}
else
{
auto first = true;

while (true)
{
if (reader_.ended())
Expand All @@ -94,18 +96,21 @@ auto lexer::lex() -> token
loc_.lines();
loc_.step();
}
else if (last == '#' && curr == '/')
else if (last == '#' && curr == '/' && !first)
{
advance();
break;
}

advance();
first = false;
}
}
}
else if (last == '@')
{
auto first = true;

while (true)
{
if (reader_.ended())
Expand All @@ -116,17 +121,20 @@ auto lexer::lex() -> token
loc_.lines();
loc_.step();
}
else if (last == '@' && curr == '/')
else if (last == '@' && curr == '/' && !first)
{
advance();
break;
}

advance();
first = false;
}
}
else if (last == '*')
{
auto first = true;

while (true)
{
if (reader_.ended())
Expand All @@ -137,13 +145,14 @@ auto lexer::lex() -> token
loc_.lines();
loc_.step();
}
else if (last == '*' && curr == '/')
else if (last == '*' && curr == '/' && !first)
{
advance();
break;
}

advance();
first = false;
}
}
else if (last == '/')
Expand Down
11 changes: 9 additions & 2 deletions src/tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,13 @@ auto assemble_file(game game, mach mach, fs::path const& file, fs::path rel) ->
rel = fs::path{ games_rev.at(game) } / rel / file.filename().replace_extension((file.extension() == ".gscasm" ? ".gsc" : ".csc"));

auto data = utils::file::read(file);

if (data.size() >= 4 && !std::memcmp(&data[0], "\x80GSC", 4))
{
std::cerr << std::format("{} at {}\n", "already assembled", file.generic_string());
return result::success;
}

auto outasm = contexts[game][mach]->source().parse_assembly(data);
auto outbin = contexts[game][mach]->assembler().assemble(*outasm);

Expand Down Expand Up @@ -834,7 +841,7 @@ auto compile_file(game game, mach mach, fs::path const& file, fs::path rel) -> r

auto data = utils::file::read(file);

if (!std::memcmp(&data[0], "\x80GSC", 4))
if (data.size() >= 4 && !std::memcmp(&data[0], "\x80GSC", 4))
{
std::cerr << std::format("{} at {}\n", "already compiled", file.generic_string());
return result::success;
Expand Down Expand Up @@ -904,7 +911,7 @@ auto parse_file(game game, mach mach, fs::path file, fs::path rel) -> result

auto data = utils::file::read(file);

if (!std::memcmp(&data[0], "\x80GSC", 4))
if (data.size() >= 4 && !std::memcmp(&data[0], "\x80GSC", 4))
{
std::cerr << std::format("{} at {}\n", "already compiled", file.generic_string());
return result::success;
Expand Down