some error handling
This commit is contained in:
parent
9009fcb9ef
commit
912064cb22
8 changed files with 67 additions and 24 deletions
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal 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
9
.idea/clox.iml
generated
Normal 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
6
.idea/misc.xml
generated
Normal 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
8
.idea/modules.xml
generated
Normal 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
6
.idea/vcs.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
7
makefile
7
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)/*
|
||||
|
||||
|
|
|
|||
45
src/lox.c
45
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue