-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from adamhutchings/token
Add unlexing capability
- Loading branch information
Showing
2 changed files
with
36 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "lex.h" | ||
|
||
#include <string.h> // memcpy | ||
|
||
int lex(Lexer *l, Token *t) { | ||
// If there are any tokens waiting in the putback buffer, read from there | ||
// instead. | ||
if (l->unlexed_count > 0) { | ||
l->unlexed_count--; | ||
memcpy(t, &l->unlexed[l->unlexed_count], sizeof(Token)); | ||
return 0; | ||
} | ||
// Now, do the actual lexing: TODO! | ||
return 0; | ||
} | ||
|
||
int unlex(Lexer *l, Token *t) { | ||
// First, make sure we can actually fit it in the buffer. | ||
if (l->unlexed_count >= TOKEN_PUTBACKS) { | ||
// TODO: error reporting | ||
return -1; // Error return code | ||
} | ||
memcpy(&l->unlexed[l->unlexed_count], t, sizeof(Token)); | ||
l->unlexed_count++; | ||
return 0; | ||
} |
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