aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lexer.c')
-rw-r--r--src/lexer.c117
1 files changed, 71 insertions, 46 deletions
diff --git a/src/lexer.c b/src/lexer.c
index e23b078..77de3e4 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -27,9 +27,10 @@ void lexer_next(lexer_t* lexer) {
}
void lexer_pass(lexer_t* lexer) {
- while (lexer->c == ' ' ||
- lexer->c == '\t' ||
- lexer->c == '\n') {
+ while (
+ lexer->c == ' ' ||
+ lexer->c == '\t' ||
+ lexer->c == '\n') {
lexer_next(lexer);
}
}
@@ -43,62 +44,39 @@ token_t* lexer_get_next_token(lexer_t* lexer) {
}
if (isalnum(lexer->c))
- return lexer_get_id(lexer);
+ return lexer_get_keyword(lexer);
switch (lexer->c) {
- case '"': return lexer_get_string(lexer); break;
- case '=': return lexer_next_token(
+ case '"':
+ return lexer_get_string(lexer); break;
+ case '[':
+ return lexer_get_comment(lexer); break;
+ case '=':
+ return lexer_get_def_const(lexer); break;
+ case '-':
+ return lexer_get_def_mut(lexer); break;
+ case ';': return lexer_next_token(
lexer,
token_init(
- TOKEN_EQ,
+ TOKEN_END,
lexer_get_c_as_string(lexer)
)
); break;
- case '{': return lexer_next_token(
+ case '(': return lexer_next_token(
lexer,
token_init(
TOKEN_LORD,
lexer_get_c_as_string(lexer)
)
); break;
- case '}': return lexer_next_token(
+ case ')': return lexer_next_token(
lexer,
token_init(
TOKEN_RORD,
lexer_get_c_as_string(lexer)
)
); break;
- case '&': return lexer_next_token(
- lexer,
- token_init(
- TOKEN_AMP,
- lexer_get_c_as_string(lexer)
- )
- ); break;
- case '[':
- return lexer_get_comment(lexer);
- break;
- case '#': return lexer_next_token(
- lexer,
- token_init(
- TOKEN_POUND,
- lexer_get_c_as_string(lexer)
- )
- ); break;
- case '~': return lexer_next_token(
- lexer,
- token_init(
- TOKEN_TILDE,
- lexer_get_c_as_string(lexer)
- )
- ); break;
- case ';': return lexer_next_token(
- lexer,
- token_init(
- TOKEN_SEMI,
- lexer_get_c_as_string(lexer)
- )
- ); break;
+ case '#':
default:
exit(1);
}
@@ -114,9 +92,12 @@ token_t* lexer_get_string(lexer_t* lexer) {
str_so_far[0] = '\0';
while (lexer->c != '"') {
- // until reaching the closing ", add each character to str_so_far and adjust size to match.
char* current = lexer_get_c_as_string(lexer);
- str_so_far = realloc(str_so_far, (strlen(str_so_far) + strlen(current) * sizeof(char)));
+ str_so_far = realloc(
+ str_so_far,
+ (strlen(str_so_far) + strlen(current) * sizeof(char))
+ );
+
strcat(str_so_far, current);
lexer_next(lexer);
@@ -124,12 +105,14 @@ token_t* lexer_get_string(lexer_t* lexer) {
lexer_next(lexer); // skip over closing "
- return token_init(TOKEN_QUOTE, str_so_far);
+ return token_init(TOKEN_STR, str_so_far);
}
token_t* lexer_get_comment(lexer_t* lexer) {
+ lexer_next(lexer);
while (lexer->c != ']') {
- lexer_next(lexer);
+ lexer_next(lexer); // don't need to keep track of comments
+ // for now. might change this later.
}
lexer_next(lexer); // skip over closing ]
@@ -137,7 +120,48 @@ token_t* lexer_get_comment(lexer_t* lexer) {
return token_init(TOKEN_COMM, lexer_get_c_as_string(lexer));
}
-token_t* lexer_get_id(lexer_t* lexer) {
+token_t* lexer_get_def_const(lexer_t* lexer) {
+ lexer_pass(lexer);
+
+ if (lexer_next(lexer), lexer->c == '>') {
+ lexer_next(lexer);
+ return token_init(TOKEN_DEFINE_CONST, "=>");
+ } else {
+ exit(1);
+ }
+}
+
+token_t* lexer_get_def_mut(lexer_t* lexer) {
+ lexer_pass(lexer);
+
+ if (lexer_next(lexer), lexer->c == '>') {
+ lexer_next(lexer);
+ return token_init(TOKEN_DEFINE_MUT, "->");
+ } else {
+ exit(1);
+ }
+}
+
+token_t* lexer_get_directive(lexer_t* lexer) {
+ lexer_next(lexer);
+
+ char* directive_so_far = calloc(1, sizeof(char));
+ directive_so_far[0] = '\0';
+
+ while (lexer->c != ';') {
+ char* current = lexer_get_c_as_string(lexer);
+ directive_so_far = realloc(directive_so_far, (strlen(directive_so_far) + strlen(current) * sizeof(char)));
+ strcat(directive_so_far, current);
+
+ lexer_next(lexer);
+ }
+
+ lexer_next(lexer);
+
+ return token_init(TOKEN_DIRECTIVE, directive_so_far);
+}
+
+token_t* lexer_get_keyword(lexer_t* lexer) {
char* str_so_far = calloc(1, sizeof(char));
str_so_far[0] = '\0';
@@ -149,7 +173,7 @@ token_t* lexer_get_id(lexer_t* lexer) {
lexer_next(lexer);
}
- return token_init(TOKEN_ID, str_so_far);
+ return token_init(TOKEN_KEYWORD, str_so_far);
}
token_t* lexer_next_token(lexer_t* lexer, token_t* token) {
@@ -162,6 +186,7 @@ char* lexer_get_c_as_string(lexer_t* lexer) {
char* str = calloc(2, 1 * sizeof(char));
str[0] = lexer->c;
str[1] = '\0';
+
return str;
}