Added a Makefile, removed the build script and had to modify the function names for either the scanner or parser so they didn't collide. Refactoring should be the next step here.
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
clox
|
clox
|
||||||
*.lox
|
*.lox
|
||||||
|
obj/
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
CC = gcc
|
||||||
|
SOURCEFILES := $(wildcard *.c)
|
||||||
|
|
||||||
|
SOURCE := $(wildcard *.c)
|
||||||
|
|
||||||
|
$(SOURCE:.c=.o): $(SOURCE)
|
||||||
|
@mkdir -p obj
|
||||||
|
$(CC) $^ -o obj/clox
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -r obj
|
||||||
@@ -35,13 +35,13 @@ int current; //points to the character currently being considered.
|
|||||||
int length;
|
int length;
|
||||||
int line = 1;
|
int line = 1;
|
||||||
|
|
||||||
const char* Advance(void);
|
const char* SAdvance(void);
|
||||||
int IsAtEnd(void);
|
int SIsAtEnd(void);
|
||||||
void ScanToken(TokenList*);
|
void ScanToken(TokenList*);
|
||||||
TokenList* CreateList(void);
|
TokenList* CreateList(void);
|
||||||
int AddTokenToList(TokenType, const char*, int, TokenList*);
|
int AddTokenToList(TokenType, const char*, int, TokenList*);
|
||||||
int Match(char);
|
int SMatch(char);
|
||||||
char Peek(void);
|
char SPeek(void);
|
||||||
char PeekNext(void);
|
char PeekNext(void);
|
||||||
void ParseString(TokenList *);
|
void ParseString(TokenList *);
|
||||||
void ParseNumber(TokenList *);
|
void ParseNumber(TokenList *);
|
||||||
@@ -59,7 +59,7 @@ TokenList* ScanTokens(const char* source) {
|
|||||||
|
|
||||||
TokenList* tokens = CreateList();
|
TokenList* tokens = CreateList();
|
||||||
|
|
||||||
while(!IsAtEnd()) {
|
while(!SIsAtEnd()) {
|
||||||
start = current;
|
start = current;
|
||||||
ScanToken(tokens);
|
ScanToken(tokens);
|
||||||
}
|
}
|
||||||
@@ -137,7 +137,7 @@ int AddTokenToList(TokenType type, const char* lexeme, int length, TokenList* to
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ScanToken(TokenList* tokens) {
|
void ScanToken(TokenList* tokens) {
|
||||||
const char* c = Advance();
|
const char* c = SAdvance();
|
||||||
|
|
||||||
switch (*c) {
|
switch (*c) {
|
||||||
case '(':
|
case '(':
|
||||||
@@ -171,24 +171,24 @@ void ScanToken(TokenList* tokens) {
|
|||||||
AddTokenToList(Star, c, 1, tokens);
|
AddTokenToList(Star, c, 1, tokens);
|
||||||
break;
|
break;
|
||||||
case '!':
|
case '!':
|
||||||
if (Match('=')) AddTokenToList(Bang_Equal, "!=", 2, tokens);
|
if (SMatch('=')) AddTokenToList(Bang_Equal, "!=", 2, tokens);
|
||||||
else AddTokenToList(Bang, c, 1, tokens);
|
else AddTokenToList(Bang, c, 1, tokens);
|
||||||
break;
|
break;
|
||||||
case '=':
|
case '=':
|
||||||
if (Match('=')) AddTokenToList(Equal_Equal, "==", 2, tokens);
|
if (SMatch('=')) AddTokenToList(Equal_Equal, "==", 2, tokens);
|
||||||
else AddTokenToList(Equal, c, 1, tokens);
|
else AddTokenToList(Equal, c, 1, tokens);
|
||||||
break;
|
break;
|
||||||
case '<':
|
case '<':
|
||||||
if (Match('=')) AddTokenToList(Less_Equal, "<=", 2, tokens);
|
if (SMatch('=')) AddTokenToList(Less_Equal, "<=", 2, tokens);
|
||||||
else AddTokenToList(Less, c, 1, tokens);
|
else AddTokenToList(Less, c, 1, tokens);
|
||||||
break;
|
break;
|
||||||
case '>':
|
case '>':
|
||||||
if (Match('=')) AddTokenToList(Greater_Equal, ">=", 2, tokens);
|
if (SMatch('=')) AddTokenToList(Greater_Equal, ">=", 2, tokens);
|
||||||
else AddTokenToList(Greater, c, 1, tokens);
|
else AddTokenToList(Greater, c, 1, tokens);
|
||||||
break;
|
break;
|
||||||
case '/':
|
case '/':
|
||||||
if (Match('/')) {
|
if (SMatch('/')) {
|
||||||
while(Peek() != '\n' && !IsAtEnd()) Advance();
|
while(SPeek() != '\n' && !SIsAtEnd()) SAdvance();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
AddTokenToList(Slash, c, 1, tokens);
|
AddTokenToList(Slash, c, 1, tokens);
|
||||||
@@ -218,16 +218,16 @@ void ScanToken(TokenList* tokens) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int IsAtEnd() {
|
int SIsAtEnd() {
|
||||||
return current >= length;
|
return current >= length;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* Advance() {
|
const char* SAdvance() {
|
||||||
return &source_code[current++];
|
return &source_code[current++];
|
||||||
}
|
}
|
||||||
|
|
||||||
int Match(char expected) {
|
int SMatch(char expected) {
|
||||||
if (IsAtEnd()) return 0;
|
if (SIsAtEnd()) return 0;
|
||||||
if (source_code[current] != expected) return 0;
|
if (source_code[current] != expected) return 0;
|
||||||
|
|
||||||
current++;
|
current++;
|
||||||
@@ -235,8 +235,8 @@ int Match(char expected) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char Peek() {
|
char SPeek() {
|
||||||
if (IsAtEnd()) return '\0';
|
if (SIsAtEnd()) return '\0';
|
||||||
|
|
||||||
return source_code[current];
|
return source_code[current];
|
||||||
}
|
}
|
||||||
@@ -244,28 +244,28 @@ char Peek() {
|
|||||||
void ParseString(TokenList *list) {
|
void ParseString(TokenList *list) {
|
||||||
start = current; //The start is currently pointing to the first double quote so we need to move it
|
start = current; //The start is currently pointing to the first double quote so we need to move it
|
||||||
//to the next (first character) of the string literal.
|
//to the next (first character) of the string literal.
|
||||||
while(Peek() != '"' && !IsAtEnd()) {
|
while(SPeek() != '"' && !SIsAtEnd()) {
|
||||||
if (Peek() == '\n') line++;
|
if (SPeek() == '\n') line++;
|
||||||
Advance();
|
SAdvance();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsAtEnd()) {
|
if (SIsAtEnd()) {
|
||||||
fprintf(stderr, "Unterminated string.\n");
|
fprintf(stderr, "Unterminated string.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
AddTokenToList(String, &source_code[start], current - start, list);
|
AddTokenToList(String, &source_code[start], current - start, list);
|
||||||
|
|
||||||
Advance(); // The closing ".
|
SAdvance(); // The closing ".
|
||||||
}
|
}
|
||||||
|
|
||||||
void ParseNumber(TokenList* list) {
|
void ParseNumber(TokenList* list) {
|
||||||
while(isdigit(Peek())) Advance();
|
while(isdigit(SPeek())) SAdvance();
|
||||||
|
|
||||||
if (Peek() == '.' && isdigit(PeekNext())) {
|
if (SPeek() == '.' && isdigit(PeekNext())) {
|
||||||
Advance();
|
SAdvance();
|
||||||
|
|
||||||
while(isdigit(Peek())) Advance();
|
while(isdigit(SPeek())) SAdvance();
|
||||||
}
|
}
|
||||||
|
|
||||||
AddTokenToList(Number, &source_code[start], current - start, list);
|
AddTokenToList(Number, &source_code[start], current - start, list);
|
||||||
@@ -278,7 +278,7 @@ char PeekNext() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ParseIdentifier(TokenList * list) {
|
void ParseIdentifier(TokenList * list) {
|
||||||
while(IsAlpha(Peek())) Advance();
|
while(IsAlpha(SPeek())) SAdvance();
|
||||||
|
|
||||||
char* lexeme = calloc(sizeof(char*), (current - start + 1));
|
char* lexeme = calloc(sizeof(char*), (current - start + 1));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user