Skip to content

Commit

Permalink
Change to expected behavior for ttype functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeRoggenbuck committed Jun 25, 2024
1 parent 74f6a0d commit 2971779
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
32 changes: 18 additions & 14 deletions src/lexer/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ int skip_to_token(Lexer *l) {
}

// This is a function for parsing single char tokens
TokenType _ttype_one_char(char c) {
// Now handles all cases of single char tokens
TokenType ttype_one_char(char c) {
switch (c) {
case '(':
return TT_OPAREN; // (
Expand Down Expand Up @@ -331,13 +332,16 @@ TokenType _ttype_one_char(char c) {
case '?':
return TT_QMARK;
default:
PRINT_ERROR("Token type for token '%c' not recognized", c);
return TT_NO_TOKEN;
if (isdigit(c)) {
return TT_LITERAL;
} else {
return TT_IDENTIFIER;
}
}
}

// This is a function for parsing exclusively tokens with more than one char
TokenType _ttype_many_chars(const char *contents) {
TokenType ttype_many_chars(const char *contents) {
if (STREQ(contents, "auto")) {
return TT_AUTO;
} else if (STREQ(contents, "break")) {
Expand Down Expand Up @@ -514,7 +518,7 @@ TokenType ttype_from_string(const char *contents) {

// Single character contents
if (len == 1) {
TokenType token = _ttype_one_char(contents[0]);
TokenType token = ttype_one_char(contents[0]);

if (token != TT_NO_TOKEN) {
return token;
Expand Down Expand Up @@ -618,9 +622,9 @@ const char *ttype_name(TokenType tt) { return ttype_names[tt]; }
int test_ttype_many_chars() {
testing_func_setup();

tassert(_ttype_many_chars("foo") == TT_IDENTIFIER);
tassert(_ttype_many_chars("struct") == TT_STRUCT);
tassert(_ttype_many_chars("while") == TT_WHILE);
tassert(ttype_many_chars("foo") == TT_IDENTIFIER);
tassert(ttype_many_chars("struct") == TT_STRUCT);
tassert(ttype_many_chars("while") == TT_WHILE);

return 0;
}
Expand All @@ -629,13 +633,13 @@ int test_ttype_one_char() {
testing_func_setup();

// Use ttype_from_string
tassert(_ttype_one_char('a') == TT_NO_TOKEN);
tassert(_ttype_one_char('1') == TT_NO_TOKEN);
tassert(ttype_one_char('a') == TT_IDENTIFIER);
tassert(ttype_one_char('1') == TT_LITERAL);

tassert(_ttype_one_char('+') == TT_PLUS);
tassert(_ttype_one_char('-') == TT_MINUS);
tassert(_ttype_one_char('>') == TT_GREATER);
tassert(_ttype_one_char('~') == TT_BNOT);
tassert(ttype_one_char('+') == TT_PLUS);
tassert(ttype_one_char('-') == TT_MINUS);
tassert(ttype_one_char('>') == TT_GREATER);
tassert(ttype_one_char('~') == TT_BNOT);

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lexer/lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ int lex(Lexer *l, Token *token);
int unlex(Lexer *l, Token *token);

// Find token type for single char
TokenType _ttype_one_char(char c);
TokenType ttype_one_char(char c);

// Find token type for a char* with multiple characters
TokenType _ttype_many_chars(const char *contents);
TokenType ttype_many_chars(const char *contents);

// Discern the token type from the given contents of unknown length
TokenType ttype_from_string(const char *contents);
Expand Down

0 comments on commit 2971779

Please sign in to comment.