diff --git a/.gitignore b/.gitignore index eb84848..decd285 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ clox *.lox +obj/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c3a1252 --- /dev/null +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/build.sh b/build.sh deleted file mode 100755 index 1a1f8a8..0000000 --- a/build.sh +++ /dev/null @@ -1 +0,0 @@ -gcc -o clox lox.c scanner.c parser.c diff --git a/scanner.c b/scanner.c index 846b66d..a557371 100644 --- a/scanner.c +++ b/scanner.c @@ -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));