aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2016-08-25 19:21:16 +0200
committerCamil Staps2016-08-25 19:21:16 +0200
commitd43473edfd99630d9d4702e6187188a34be880cc (patch)
tree27f26f61e3a62f2dc696f4b4f996cfac9c494000
parentFixed overwriting errors (diff)
Use perror
-rw-r--r--compiler/Makefile6
-rw-r--r--compiler/error.c16
-rw-r--r--compiler/error.h9
-rw-r--r--compiler/fuspelc.c23
-rw-r--r--compiler/mem.c8
5 files changed, 21 insertions, 41 deletions
diff --git a/compiler/Makefile b/compiler/Makefile
index a4485c4..ef54249 100644
--- a/compiler/Makefile
+++ b/compiler/Makefile
@@ -1,7 +1,7 @@
-CFLAGS=-Wall
+CFLAGS=-Wall -s
-DEPS=lex.h syntax.h error.h print.h parse.h log.h eval.h mem.h
-OBJ=fuspelc.o lex.o syntax.o error.o print.o parse.o log.o eval.o mem.o
+DEPS=lex.h syntax.h print.h parse.h log.h eval.h mem.h
+OBJ=fuspelc.o lex.o syntax.o print.o parse.o log.o eval.o mem.o
EXE=fuspelc
all: $(EXE)
diff --git a/compiler/error.c b/compiler/error.c
deleted file mode 100644
index 7d70f60..0000000
--- a/compiler/error.c
+++ /dev/null
@@ -1,16 +0,0 @@
-#include "error.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-
-void error(int code, char* message) {
- if (message) {
- fprintf(stderr, "%s\n", message);
- }
-
- exit(code);
-}
-
-void error_no_mem(void) {
- error(ERROR_NO_MEMORY, "No memory");
-}
diff --git a/compiler/error.h b/compiler/error.h
deleted file mode 100644
index 872afd7..0000000
--- a/compiler/error.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#ifndef _H_ERROR
-#define _H_ERROR
-
-void error(int code, char* message);
-void error_no_mem(void);
-
-#define ERROR_NO_MEMORY 10
-
-#endif
diff --git a/compiler/fuspelc.c b/compiler/fuspelc.c
index e0e34b7..5a02126 100644
--- a/compiler/fuspelc.c
+++ b/compiler/fuspelc.c
@@ -2,10 +2,10 @@
#include <stdio.h>
#include <string.h>
-#include "error.h"
+#include "eval.h"
#include "lex.h"
+#include "mem.h"
#include "parse.h"
-#include "eval.h"
#include "print.h"
int main(void) {
@@ -16,20 +16,25 @@ int main(void) {
if (!fgets(program, 79, stdin)) {
if (feof(stdin))
break;
- error(11, "Couldn't read input.");
+ fprintf(stderr, "Couldn't read input.");
+ exit(EXIT_FAILURE);
}
tokens = lex(tokens, program);
- if (!tokens)
- error(12, "Couldn't lex program.");
+ if (!tokens) {
+ fprintf(stderr, "Couldn't lex program.");
+ exit(EXIT_FAILURE);
+ }
}
fuspel* pgm = parse(tokens);
free_token_list(tokens);
free(tokens);
- if (!pgm)
- error(13, "Couldn't parse program.");
+ if (!pgm) {
+ fprintf(stderr, "Couldn't parse program.");
+ exit(EXIT_FAILURE);
+ }
printf("\nParsed program:\n");
print_fuspel(pgm);
@@ -38,9 +43,7 @@ int main(void) {
expression to_eval, *evaled;
to_eval.kind = EXPR_NAME;
- to_eval.var1 = malloc(5);
- if (!to_eval.var1)
- error_no_mem();
+ to_eval.var1 = my_calloc(1, 5);
strcpy(to_eval.var1, "main");
evaled = eval(pgm, &to_eval);
diff --git a/compiler/mem.c b/compiler/mem.c
index 01435ff..7affd71 100644
--- a/compiler/mem.c
+++ b/compiler/mem.c
@@ -1,11 +1,13 @@
#include "mem.h"
-#include "error.h"
+#include <stdio.h>
void* my_calloc(size_t num, size_t size) {
void* ptr = calloc(num, size);
- if (!ptr)
- error_no_mem();
+ if (!ptr) {
+ perror(NULL);
+ exit(EXIT_FAILURE);
+ }
return ptr;
}