diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/clox.iml b/.idea/clox.iml
new file mode 100644
index 0000000..d6ebd48
--- /dev/null
+++ b/.idea/clox.iml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..a16d8e7
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..a28bd99
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/makefile b/makefile
index 85ef53a..0523659 100644
--- a/makefile
+++ b/makefile
@@ -1,6 +1,6 @@
TARGET := ./target
SRC := ./src
-CC := clang -c -std=c17 -Wall -Wextra -pedantic -Werror
+CC := clang -c -std=c17 -Wall -Wextra -pedantic -Werror
SRCS := $(shell find $(SRC) -name '*.c')
OBJS := $(SRCS:%=$(TARGET)/%.o)
@@ -11,12 +11,11 @@ $(TARGET)/lox: $(TARGET)/lox.c.o $(TARGET)/tokens.c.o
$(TARGET)/tokens.c.o: $(SRC)/tokens.c
$(CC) $< -o $@
-$(TARGET)/lox.c.o: $(SRC)/lox.c
+$(TARGET)/lox.c.o: $(SRC)/lox.c $(TARGET)
$(CC) $< -o $@
$(TARGET):
mkdir -p $(TARGET)
-
+
clean:
rm -rf $(TARGET)/*
-
diff --git a/src/lox.c b/src/lox.c
index 57c78bf..8772832 100644
--- a/src/lox.c
+++ b/src/lox.c
@@ -35,30 +35,30 @@ int run_file(char* filename){
return EXIT_FAILURE;
}
char line[255];
-
+
char* content = malloc(1);
if (content == NULL){
puts("Out of memory");
return EXIT_FAILURE;
}
content[0] = '\0';
-
-
+
+
while (fgets(line, sizeof(line), file)){
content = realloc(content, strlen(content) + strlen(line) + 1 );
if (content == NULL){
return EXIT_FAILURE;
}
strcat(content, line);
- }
-
+ }
+
fclose(file);
-
+
run(content);
// FREE UP
free(content);
-
+
if (had_error){
return 65;
}
@@ -72,18 +72,18 @@ void run_prompt(void){
for (;;){
printf(">");
char* r = fgets(line, 255, stdin);
-
+
if (r == NULL){
break;
- }
+ }
- run(line);
+ int len =(int)strlen(line);
+ run(substring(line, 1, len-1));
had_error = false;
}
}
void run(char* source){
- printf("%s", source);
scan_tokens(source);
}
@@ -94,8 +94,9 @@ void scan_tokens(char* source){
TokenList token_list;
tokenlist_init(&token_list);
+ int len = (int)strlen(source);
- while (current < (int)strlen(source)) {
+ while (current < len) {
start = current;
scan_token(source, line, start, ¤t, &token_list);
}
@@ -108,13 +109,19 @@ void add_token(char* source, int line, TokenList* token_list, enum TokenType typ
token.lexeme = substring(source, start, current_pos);
token.literal = NULL;
token.line = line;
-
+
tokenlist_add(token_list, token);
}
+void advance(int* pos){
+ (*pos) +=1;
+}
+
void scan_token(char* source, int line, int start, int* current_pos, TokenList* token_list){
char c = source[*current_pos];
- (*current_pos) += 1;
+
+ advance(current_pos);
+
switch (c){
case '(': add_token(source, line, token_list, LEFT_PAREN, start, *current_pos); break;
case ')': add_token(source, line, token_list, RIGHT_PAREN, start, *current_pos); break;
@@ -123,8 +130,9 @@ void scan_token(char* source, int line, int start, int* current_pos, TokenList*
case ',': add_token(source, line, token_list, COMMA, start, *current_pos); break;
case '.': add_token(source, line, token_list, DOT, start, *current_pos); break;
case '+': add_token(source, line, token_list, PLUS, start, *current_pos); break;
- case ';': add_token(source, line, token_list, SEMICOLON, start, *current_pos); break;
- case '/': add_token(source, line, token_list, SLASH, start, *current_pos); break;
+ case '-': add_token(source, line, token_list, MINUS, start, *current_pos); break;
+
+ default: error(line, "Unexpected character."); break;
}
}
@@ -133,7 +141,7 @@ void error(int line, char* message){
}
void report(int line, char* where, char* message){
- printf("[Line %i] Error %s : %s\n", line, where, message);
+ printf("*[Line %i] Error %s : %s\n", line, where, message);
had_error = true;
}
@@ -145,7 +153,7 @@ char* substring(char* string, int position, int length){
}
int c;
- for (c=0; c< length; c++){
+ for (c=0; c < length; c+=1){
*(ptr+c) = *(string+position-1);
string += sizeof(char);
}
@@ -153,4 +161,3 @@ char* substring(char* string, int position, int length){
return ptr;
}
-
diff --git a/src/tokens.c b/src/tokens.c
index b6793d1..755e681 100644
--- a/src/tokens.c
+++ b/src/tokens.c
@@ -74,10 +74,10 @@ Token* tokenlist_get(TokenList* list, int index){
void tokenlist_print(TokenList* tokenlist){
for (int i=0; i< tokenlist->size; i++){
printf("%s, ", token_name(tokenlist_get(tokenlist, i)));
+ printf("\n");
}
}
void tokenlist_free(TokenList* list){
free(list->tokens);
}
-