From d6a39355886ae53031ff0e80aa0d4ca4ada9eb93 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 9 Feb 2022 19:53:57 +0000 Subject: [PATCH] Setup a skeleton for the scanner. --- lox.c | 2 +- scanner.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ scanner.h | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 scanner.c create mode 100644 scanner.h diff --git a/lox.c b/lox.c index 071dbeb..47fe950 100644 --- a/lox.c +++ b/lox.c @@ -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) { diff --git a/scanner.c b/scanner.c new file mode 100644 index 0000000..8677b59 --- /dev/null +++ b/scanner.c @@ -0,0 +1,55 @@ +#include "scanner.h" +#include + +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++]; +} \ No newline at end of file diff --git a/scanner.h b/scanner.h new file mode 100644 index 0000000..7dd08c9 --- /dev/null +++ b/scanner.h @@ -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 \ No newline at end of file