aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/lex.h1
-rw-r--r--src/lex.c26
-rw-r--r--src/main.c3
3 files changed, 28 insertions, 2 deletions
diff --git a/include/lex.h b/include/lex.h
index 942df9b..8050c4e 100644
--- a/include/lex.h
+++ b/include/lex.h
@@ -83,6 +83,7 @@ enum lexical_token {
T_LAST_OPERATOR,
// Tokens with additional information
+ T_COMMENT,
T_NAME,
T_NUMBER,
T_STRING,
diff --git a/src/lex.c b/src/lex.c
index f6d7595..0fdf6af 100644
--- a/src/lex.c
+++ b/src/lex.c
@@ -215,6 +215,25 @@ lex_string(struct lexer *lexer, struct token *out, uint32_t delim)
return out->token;
}
+static enum lexical_token
+lex_comment(struct lexer *lexer, struct token *out)
+{
+ uint32_t c;
+ if ((c = next(lexer)) != C_EOF && c != ' ') {
+ unget(lexer);
+ }
+
+ while ((c = next(lexer)) != C_EOF && c != '\n') {
+ push(lexer, c);
+ }
+
+ out->info.str = strdup(lexer->buf);
+
+ clear(lexer);
+ out->token = T_COMMENT;
+ return out->token;
+}
+
enum lexical_token
lex(struct lexer *lexer, struct token *out)
{
@@ -307,7 +326,12 @@ lex(struct lexer *lexer, struct token *out)
out->token = T_COMMA;
break;
case '/':
- out->token = T_DIV;
+ if (wgetc(lexer) == '/') {
+ return lex_comment(lexer, out);
+ } else {
+ unget(lexer);
+ out->token = T_DIV;
+ }
break;
case '.':
out->token = T_DOT;
diff --git a/src/main.c b/src/main.c
index e809190..ddde9a9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -43,7 +43,8 @@ main(int argc, char *argv[])
struct token token;
while (lex(&lexer, &token) != T_EOF) {
printf("%d", token.token);
- if (token.token == T_NAME || token.token == T_STRING) {
+ if (token.token == T_COMMENT || token.token == T_NAME
+ || token.token == T_STRING) {
printf(" %s", token.info.str);
} else if (token.token == T_NUMBER
&& token.info.num.isfloat) {