Setup a skeleton for the scanner.
This commit is contained in:
@@ -22,7 +22,7 @@ int main(int argc, char** argv) {
|
||||
void RunFile(const char* path) {
|
||||
printf("Running '%s'\n", path);
|
||||
char* contents = GetFileContents(path);
|
||||
printf("%s", contents);
|
||||
//Tokenize(contents);
|
||||
}
|
||||
|
||||
char* GetFileContents(const char* path) {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
#include "scanner.h"
|
||||
#include <string.h>
|
||||
|
||||
const char* source_code;
|
||||
//Start and Current hold the offsets that index into the string source_code.
|
||||
int start; //Points to the first character in the lexeme being scanned.
|
||||
int current; //points to the character currently being considered.
|
||||
int length;
|
||||
int line = 1;
|
||||
|
||||
char Advance(void);
|
||||
int IsAtend(void);
|
||||
void ScanToken(void);
|
||||
|
||||
void ScanTokens(const char* source) {
|
||||
if (!source) return;
|
||||
|
||||
source_code = source;
|
||||
length = strlen(source);
|
||||
|
||||
if (length == 0) return;
|
||||
|
||||
while(!IsAtend()) {
|
||||
start = current;
|
||||
ScanToken();
|
||||
}
|
||||
|
||||
//Add EOF token and return list once that's set up.
|
||||
}
|
||||
|
||||
void ScanToken() {
|
||||
char c = Advance();
|
||||
|
||||
switch (c) {
|
||||
case '(':
|
||||
case ')':
|
||||
case '{':
|
||||
case '}':
|
||||
case ',':
|
||||
case '.':
|
||||
case '-':
|
||||
case '+':
|
||||
case ';':
|
||||
case '*':
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int IsAtEnd() {
|
||||
return current >= length;
|
||||
}
|
||||
|
||||
char Advance() {
|
||||
return source_code[current++];
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef SCANNER_H
|
||||
#define SCANNER_H
|
||||
|
||||
typedef enum {
|
||||
//Single-character tokens
|
||||
LParen, RParen,
|
||||
LBrace, RBrace,
|
||||
Comma,
|
||||
Dot,
|
||||
Minus, Plus,
|
||||
Semicolon,
|
||||
Slash, Star,
|
||||
//One or two character tokens
|
||||
Bang, Bang_Equal,
|
||||
Equal, Equal_Equal,
|
||||
Greater, Greater_Equal,
|
||||
Less, Less_Equal,
|
||||
//Literals
|
||||
Identifier,
|
||||
String,
|
||||
Number,
|
||||
//Keywords
|
||||
AND, CLASS, ELSE, FALSE, FUN,
|
||||
FOR, IF, NIL, OR, PRINT, RETURN,
|
||||
SUPER, THIS, TRUE, VAR, WHILE,
|
||||
|
||||
EndOF
|
||||
} TokenType;
|
||||
|
||||
void ScanTokens(const char*);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user