Chapter 16.2

This commit is contained in:
Tom Willemse 2021-08-02 18:35:10 -07:00
parent b131e343bb
commit 4143259eb3
Signed by: ryuslash
GPG key ID: 7D5C407B435025C1
3 changed files with 102 additions and 1 deletions

View file

@ -4,4 +4,20 @@
#include "compiler.h"
#include "scanner.h"
void compile(const char *source) { initScanner(source); }
void compile(const char *source) {
initScanner(source);
int line = -1;
for (;;) {
Token token = scanToken();
if (token.line != line) {
printf("%4d ", token.line);
line = token.line;
} else {
printf(" | ");
}
printf("%2d '%.*s'\n", token.type, token.length, token.start);
if (token.type == TOKEN_EOF)
break;
}
}

View file

@ -17,3 +17,32 @@ void initScanner(const char *source) {
scanner.current = source;
scanner.line = 1;
}
static bool isAtEnd() { return *scanner.current == '\0'; }
static Token makeToken(TokenType type) {
Token token;
token.type = type;
token.start = scanner.start;
token.length = (int)(scanner.current - scanner.start);
token.line = scanner.line;
return token;
}
static Token errorToken(const char *message) {
Token token;
token.type = TOKEN_ERROR;
token.start = message;
token.length = (int)strlen(message);
token.line = scanner.line;
return token;
}
Token scanToken() {
scanner.start = scanner.current;
if (isAtEnd())
return makeToken(TOKEN_EOF);
return errorToken("Unexpected character.");
}

View file

@ -1,6 +1,62 @@
#ifndef SCANNER_H
#define SCANNER_H
typedef enum {
/* Single-character tokens. */
TOKEN_LEFT_PAREN,
TOKEN_RIGHT_PAREN,
TOKEN_LEFT_BRACE,
TOKEN_RIGHT_BRACE,
TOKEN_COMMA,
TOKEN_DOT,
TOKEN_MINUS,
TOKEN_PLUS,
TOKEN_SEMICOLON,
TOKEN_SLASH,
TOKEN_STAR,
/* One || two character tokens. */
TOKEN_BANG,
TOKEN_BANG_EQUAL,
TOKEN_EQUAL,
TOKEN_EQUAL_EQUAL,
TOKEN_GREATER,
TOKEN_GREATER_EQUAL,
TOKEN_LESS,
TOKEN_LESS_EQUAL,
/* Literals. */
TOKEN_IDENTIFIER,
TOKEN_STRING,
TOKEN_NUMBER,
/* Keywords. */
TOKEN_AND,
TOKEN_CLASS,
TOKEN_ELSE,
TOKEN_FALSE,
TOKEN_FOR,
TOKEN_FUN,
TOKEN_IF,
TOKEN_NIL,
TOKEN_OR,
TOKEN_PRINT,
TOKEN_RETURN,
TOKEN_SUPER,
TOKEN_THIS,
TOKEN_TRUE,
TOKEN_VAR,
TOKEN_WHILE,
TOKEN_ERROR,
TOKEN_EOF
} TokenType;
typedef struct {
TokenType type;
const char *start;
int length;
int line;
} Token;
void initScanner(const char *source);
Token scanToken();
#endif