Skip to content

Commit

Permalink
replaced instances of sbstrcpy with sbstr2str
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandMarchand committed Jul 16, 2022
1 parent f6ff15e commit 284c305
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
11 changes: 8 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
* SUCH DAMAGE.
*/

#include "vm.h"
#include "debug/debug.h"
#include "vm/vm.h"
#include "vm/debug/debug.h"
#include "scanner/scanner.h"
#include "macros.h"

#include <stdio.h>

int main(int argc, char **argv)
{

struct scan * s = scan_init(argv[1]);
for (int i = 0; i < s->tokens->count; i++) {
char lexeme[SUBSTRING_LENGTH(s->tokens->array[i].lexeme)];
sbstrcpy(&s->tokens->array[i].lexeme, lexeme);
printf("%s\n", lexeme);
}
}
25 changes: 16 additions & 9 deletions src/scanner/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,23 @@ static struct token digit()

static struct token identifier()
{
while (IS_ALPHA(scanner.current[0]) || IS_DIGIT(scanner.current[0])) advance();

int max_id_size = 1024;
while (IS_ALPHA(scanner.current[0]) || IS_DIGIT(scanner.current[0])) {
max_id_size--;
advance();
}

/* max identifier size of 1024 */
if (max_id_size <= 0) {
fprintf(stderr, "Identifier at line %d is too large,\
max size of 1024 characters.", scanner.line);
return GET_TOKEN(TOKEN_INVALID);
}

/* substring to string */
struct substring *sbstr = &(struct substring){.start=scanner.start, .end=scanner.current};
char str[SUBSTRING_LENGTH(*sbstr)];
sbstrcpy(sbstr, str);
char *string = sbstr2str(&(struct substring){.start=scanner.start, .end=scanner.current});

enum token_type t = get_keyword_type(str);
enum token_type t = get_keyword_type(string);
return GET_TOKEN(t);
}

Expand Down Expand Up @@ -370,9 +379,7 @@ static enum token_type keywordcmp(int offset, const char* str, enum token_type t
.start = scanner.start,
.end = scanner.current
};
char s_sbstr[SUBSTRING_LENGTH(*sbstr)];
sbstrcpy(sbstr, s_sbstr);

if (strcmp(s_sbstr + offset, str) == 0) return t;
if (strcmp(sbstr2str(sbstr) + offset, str) == 0) return t;
return TOKEN_IDENTIFIER;
}
4 changes: 2 additions & 2 deletions src/scanner/substring.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ void sbstrcpy(const struct substring *from, char *to)

char *sbstr2str(const struct substring *sbstr)
{
if (SUBSTRING_LENGTH(*sbstr) > 1024) {
fprintf(stderr, "Max substring size of 1024 allowed.\
if (SUBSTRING_LENGTH(*sbstr) > 1025) {
fprintf(stderr, "Max substring size of 1024 + null allowed.\
Use sbstrcpy() instead.\n");
return NULL;
}
Expand Down

0 comments on commit 284c305

Please sign in to comment.