some error handling

This commit is contained in:
Shautvast 2024-10-11 18:08:05 +02:00
parent 9009fcb9ef
commit 912064cb22
8 changed files with 67 additions and 24 deletions

8
.idea/.gitignore generated vendored Normal file
View file

@ -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

9
.idea/clox.iml generated Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="24" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/clox.iml" filepath="$PROJECT_DIR$/.idea/clox.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View file

@ -11,7 +11,7 @@ $(TARGET)/lox: $(TARGET)/lox.c.o $(TARGET)/tokens.c.o
$(TARGET)/tokens.c.o: $(SRC)/tokens.c $(TARGET)/tokens.c.o: $(SRC)/tokens.c
$(CC) $< -o $@ $(CC) $< -o $@
$(TARGET)/lox.c.o: $(SRC)/lox.c $(TARGET)/lox.c.o: $(SRC)/lox.c $(TARGET)
$(CC) $< -o $@ $(CC) $< -o $@
$(TARGET): $(TARGET):
@ -19,4 +19,3 @@ $(TARGET):
clean: clean:
rm -rf $(TARGET)/* rm -rf $(TARGET)/*

View file

@ -77,13 +77,13 @@ void run_prompt(void){
break; break;
} }
run(line); int len =(int)strlen(line);
run(substring(line, 1, len-1));
had_error = false; had_error = false;
} }
} }
void run(char* source){ void run(char* source){
printf("%s", source);
scan_tokens(source); scan_tokens(source);
} }
@ -94,8 +94,9 @@ void scan_tokens(char* source){
TokenList token_list; TokenList token_list;
tokenlist_init(&token_list); tokenlist_init(&token_list);
int len = (int)strlen(source);
while (current < (int)strlen(source)) { while (current < len) {
start = current; start = current;
scan_token(source, line, start, &current, &token_list); scan_token(source, line, start, &current, &token_list);
} }
@ -112,9 +113,15 @@ void add_token(char* source, int line, TokenList* token_list, enum TokenType typ
tokenlist_add(token_list, token); 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){ void scan_token(char* source, int line, int start, int* current_pos, TokenList* token_list){
char c = source[*current_pos]; char c = source[*current_pos];
(*current_pos) += 1;
advance(current_pos);
switch (c){ switch (c){
case '(': add_token(source, line, token_list, LEFT_PAREN, start, *current_pos); break; 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; 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, COMMA, start, *current_pos); break;
case '.': add_token(source, line, token_list, DOT, 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, PLUS, start, *current_pos); break;
case ';': add_token(source, line, token_list, SEMICOLON, start, *current_pos); break; case '-': add_token(source, line, token_list, MINUS, start, *current_pos); break;
case '/': add_token(source, line, token_list, SLASH, 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){ 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; had_error = true;
} }
@ -145,7 +153,7 @@ char* substring(char* string, int position, int length){
} }
int c; int c;
for (c=0; c< length; c++){ for (c=0; c < length; c+=1){
*(ptr+c) = *(string+position-1); *(ptr+c) = *(string+position-1);
string += sizeof(char); string += sizeof(char);
} }
@ -153,4 +161,3 @@ char* substring(char* string, int position, int length){
return ptr; return ptr;
} }

View file

@ -74,10 +74,10 @@ Token* tokenlist_get(TokenList* list, int index){
void tokenlist_print(TokenList* tokenlist){ void tokenlist_print(TokenList* tokenlist){
for (int i=0; i< tokenlist->size; i++){ for (int i=0; i< tokenlist->size; i++){
printf("%s, ", token_name(tokenlist_get(tokenlist, i))); printf("%s, ", token_name(tokenlist_get(tokenlist, i)));
printf("\n");
} }
} }
void tokenlist_free(TokenList* list){ void tokenlist_free(TokenList* list){
free(list->tokens); free(list->tokens);
} }