-
Notifications
You must be signed in to change notification settings - Fork 0
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
Implement translation phase 2 #7
Open
newcomb-luke
wants to merge
2
commits into
develop
Choose a base branch
from
feature/translation-phase-2
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod phase2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::{ | ||
diagnostic::session::Session, | ||
lexer::{PToken, PTokenKind}, | ||
}; | ||
|
||
/// Phase 1 according to the C specification is replacing trigraph sequences. Because of the nature | ||
/// of preprocessing tokens, and a distaste of looping through every character before it gets to | ||
/// the lexer, that phase will be postponed as it correctly can be. Therefore phase 2 will come | ||
/// first. | ||
/// | ||
/// According to the C specification, phase 2 consists of: | ||
/// | ||
/// Each instance of a backslash character ( \) immediately followed by a new-line | ||
/// character is deleted, splicing physical source lines to form logical source lines. | ||
/// Only the last backslash on any physical source line shall be eligible for being part | ||
/// of such a splice. A source file that is not empty shall end in a new-line character, | ||
/// which shall not be immediately preceded by a backslash character before any such | ||
/// splicing takes place. | ||
/// | ||
/// Therefore this function removes all newlines following a backslash. Because comments also | ||
/// have no effect on the code generated from C, they are also stripped here. | ||
/// | ||
pub fn phase2(tokens: Vec<PToken>, session: &Session) -> Result<Vec<PToken>, ()> { | ||
let mut new_tokens = Vec::with_capacity(tokens.capacity()); | ||
|
||
let mut backslash: Option<PToken> = None; | ||
let mut has_error = false; | ||
|
||
for token in tokens { | ||
if backslash.is_some() { | ||
if token.kind == PTokenKind::Newline { | ||
backslash = None; | ||
} else if token.kind == PTokenKind::Whitespace { | ||
session | ||
.struct_span_warn(token.into(), "whitespace before newline after `\\`") | ||
.emit(); | ||
} else { | ||
// At this point we don't have to worry about other files being included in the | ||
// token stream | ||
let s = session.span_to_string(&token.into()).unwrap(); | ||
|
||
session | ||
.struct_error(format!("found unexpected token `{}`", s)) | ||
.span_label(token.into(), "expected newline after `\\`, found this") | ||
.emit(); | ||
|
||
// We can continue to try, just in case they make the same mistake again? | ||
has_error = true; | ||
backslash = None; | ||
} | ||
} else if token.kind != PTokenKind::CommentSingle { | ||
if token.kind == PTokenKind::Backslash { | ||
backslash = Some(token); | ||
} else { | ||
new_tokens.push(token); | ||
} | ||
} | ||
} | ||
|
||
if let Some(backslash) = backslash { | ||
session | ||
.struct_error("unexpected end of file") | ||
.span_label(backslash.into(), "after backslash") | ||
.emit(); | ||
has_error = true; | ||
} | ||
|
||
if has_error { | ||
Err(()) | ||
} else { | ||
Ok(new_tokens) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,12 @@ | |
int main() { | ||
printf("Hello world"); | ||
|
||
return 0; | ||
// Single-line comment | ||
|
||
/* | ||
* $$$ test $$$ | ||
*/ | ||
|
||
return \ | ||
0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: long sentence