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:
2022-03-01 18:03:57 +00:00
parent 41a04094e2
commit 1b33468343
4 changed files with 42 additions and 29 deletions
+1
View File
@@ -1,2 +1,3 @@
clox
*.lox
obj/
+13
View File
@@ -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
-1
View File
@@ -1 +0,0 @@
gcc -o clox lox.c scanner.c parser.c
+28 -28
View File
@@ -35,13 +35,13 @@ int current; //points to the character currently being considered.
int length;
int line = 1;
const char* Advance(void);
int IsAtEnd(void);
const char* SAdvance(void);
int SIsAtEnd(void);
void ScanToken(TokenList*);
TokenList* CreateList(void);
int AddTokenToList(TokenType, const char*, int, TokenList*);
int Match(char);
char Peek(void);
int SMatch(char);
char SPeek(void);
char PeekNext(void);
void ParseString(TokenList *);
void ParseNumber(TokenList *);
@@ -59,7 +59,7 @@ TokenList* ScanTokens(const char* source) {
TokenList* tokens = CreateList();
while(!IsAtEnd()) {
while(!SIsAtEnd()) {
start = current;
ScanToken(tokens);
}
@@ -137,7 +137,7 @@ int AddTokenToList(TokenType type, const char* lexeme, int length, TokenList* to
}
void ScanToken(TokenList* tokens) {
const char* c = Advance();
const char* c = SAdvance();
switch (*c) {
case '(':
@@ -171,24 +171,24 @@ void ScanToken(TokenList* tokens) {
AddTokenToList(Star, c, 1, tokens);
break;
case '!':
if (Match('=')) AddTokenToList(Bang_Equal, "!=", 2, tokens);
if (SMatch('=')) AddTokenToList(Bang_Equal, "!=", 2, tokens);
else AddTokenToList(Bang, c, 1, tokens);
break;
case '=':
if (Match('=')) AddTokenToList(Equal_Equal, "==", 2, tokens);
if (SMatch('=')) AddTokenToList(Equal_Equal, "==", 2, tokens);
else AddTokenToList(Equal, c, 1, tokens);
break;
case '<':
if (Match('=')) AddTokenToList(Less_Equal, "<=", 2, tokens);
if (SMatch('=')) AddTokenToList(Less_Equal, "<=", 2, tokens);
else AddTokenToList(Less, c, 1, tokens);
break;
case '>':
if (Match('=')) AddTokenToList(Greater_Equal, ">=", 2, tokens);
if (SMatch('=')) AddTokenToList(Greater_Equal, ">=", 2, tokens);
else AddTokenToList(Greater, c, 1, tokens);
break;
case '/':
if (Match('/')) {
while(Peek() != '\n' && !IsAtEnd()) Advance();
if (SMatch('/')) {
while(SPeek() != '\n' && !SIsAtEnd()) SAdvance();
}
else {
AddTokenToList(Slash, c, 1, tokens);
@@ -218,16 +218,16 @@ void ScanToken(TokenList* tokens) {
}
}
int IsAtEnd() {
int SIsAtEnd() {
return current >= length;
}
const char* Advance() {
const char* SAdvance() {
return &source_code[current++];
}
int Match(char expected) {
if (IsAtEnd()) return 0;
int SMatch(char expected) {
if (SIsAtEnd()) return 0;
if (source_code[current] != expected) return 0;
current++;
@@ -235,8 +235,8 @@ int Match(char expected) {
return 1;
}
char Peek() {
if (IsAtEnd()) return '\0';
char SPeek() {
if (SIsAtEnd()) return '\0';
return source_code[current];
}
@@ -244,28 +244,28 @@ char Peek() {
void ParseString(TokenList *list) {
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.
while(Peek() != '"' && !IsAtEnd()) {
if (Peek() == '\n') line++;
Advance();
while(SPeek() != '"' && !SIsAtEnd()) {
if (SPeek() == '\n') line++;
SAdvance();
}
if (IsAtEnd()) {
if (SIsAtEnd()) {
fprintf(stderr, "Unterminated string.\n");
return;
}
AddTokenToList(String, &source_code[start], current - start, list);
Advance(); // The closing ".
SAdvance(); // The closing ".
}
void ParseNumber(TokenList* list) {
while(isdigit(Peek())) Advance();
while(isdigit(SPeek())) SAdvance();
if (Peek() == '.' && isdigit(PeekNext())) {
Advance();
if (SPeek() == '.' && isdigit(PeekNext())) {
SAdvance();
while(isdigit(Peek())) Advance();
while(isdigit(SPeek())) SAdvance();
}
AddTokenToList(Number, &source_code[start], current - start, list);
@@ -278,7 +278,7 @@ char PeekNext() {
}
void ParseIdentifier(TokenList * list) {
while(IsAlpha(Peek())) Advance();
while(IsAlpha(SPeek())) SAdvance();
char* lexeme = calloc(sizeof(char*), (current - start + 1));