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

Feat: Add ability to compile from stdin #1327

Merged
merged 6 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,9 @@ class cmdline_processor
[](auto& a, auto& b){ return a.group < b.group || (a.group == b.group && a.name < b.name); }
);

print("\nUsage: cppfront [options] file ...\n\nOptions:\n");
print("\nUsage: cppfront [options] file ...\n");
print("\nfile - The source file(s) to compile (can be 'stdin' to read text directly) \n");
print("\nOptions: \n");
int last_group = -1;
for (auto& flag : flags) {
// Skip hidden flags
Expand Down
7 changes: 6 additions & 1 deletion source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ auto main(
}

// Load + lex + parse + sema
cppfront c(arg.text);
cppfront c = [&]() -> cppfront {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be less change to handle this split in the load() function instead? https://github.com/hsutter/cppfront/blob/main/source/io.h#L885

It would need a little change where it currently checks for the file extension, but the rest may just work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yea you're right. I was struggling a bit to figure out where to split this but I think that would make a lot more sense. Ill go ahead and give that a try. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that suggestion, that greatly simplified the code!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome.

if (arg.text == "stdin")
return cppfront(std::cin);
else
return cppfront(arg.text);
}();

// Generate Cpp1 (this may catch additional late errors)
auto count = c.lower_to_cpp1();
Expand Down
14 changes: 14 additions & 0 deletions source/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,20 @@ class source
return false;
}

return load(in);
}

//-----------------------------------------------------------------------
// load: Read a line-by-line view of a source file, preserving line breaks
//
// in the loaded source file
// source program textual representation
//
auto load(
std::istream& in
)
-> bool
{
auto in_comment = false;
auto in_string_literal = false;
auto in_raw_string_literal = false;
Expand Down
93 changes: 67 additions & 26 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -1202,37 +1202,78 @@ class cppfront

else
{
// Tokenize
//
tokens.lex(source.get_lines());

// Parse
//
try
{
for (auto const& [line, entry] : tokens.get_map()) {
if (!parser.parse(entry, tokens.get_generated())) {
errors.emplace_back(
source_position(line, 0),
"parse failed for section starting here",
false,
true // a noisy fallback error message
);
}
}
process_cpp2();
}
}

// Sema
parser.visit(sema);
if (!sema.apply_local_rules()) {
violates_initialization_safety = true;
}
}
catch (std::runtime_error& e) {
//-----------------------------------------------------------------------
// Constructor
//
// source_stream the contents of a source file to be processed
//
cppfront(std::istream& source_stream)
: sourcefile{ "stdin.cpp2" }
, source { errors }
, tokens { errors }
, parser { errors, includes }
, sema { errors }
{
// Load the program file into memory
//
if (!source.load(source_stream))
{
if (errors.empty()) {
errors.emplace_back(
source_position(-1, -1),
e.what()
"error reading source content from stdin"
);
}
source_loaded = false;
}

else
{
process_cpp2();
}
}

//-----------------------------------------------------------------------
// process_cpp2
//
// Runs the lexer and parser on the loaded source code
//
auto process_cpp2() -> void
{
// Tokenize
//
tokens.lex(source.get_lines());

// Parse
//
try
{
for (auto const& [line, entry] : tokens.get_map()) {
if (!parser.parse(entry, tokens.get_generated())) {
errors.emplace_back(
source_position(line, 0),
"parse failed for section starting here",
false,
true // a noisy fallback error message
);
}
}

// Sema
parser.visit(sema);
if (!sema.apply_local_rules()) {
violates_initialization_safety = true;
}
}
catch (std::runtime_error& e) {
errors.emplace_back(
source_position(-1, -1),
e.what()
);
}
}

Expand Down