Skip to content

Commit

Permalink
fix: comment lexing & empty file crash
Browse files Browse the repository at this point in the history
  • Loading branch information
xensik committed Dec 7, 2024
1 parent 9ff3b87 commit fbc03bb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
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

0 comments on commit fbc03bb

Please sign in to comment.