From 6422bcca8a5ae13888dbaf92d1c0a525bd3439e4 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 21 Dec 2018 00:03:47 +0100 Subject: [PATCH] moving as/error.c code to lib to have the functions reusable. --- Makefile | 9 +++++---- as/asm_error.c | 35 +++++++++++++++++++++++++++++++++++ as/asm_error.h | 29 +++++++++++++++++++++++++++++ as/lexer.c | 2 +- as/parser.c | 2 +- {as => lib}/error.c | 28 +++++++++++++++------------- {as => lib/include}/error.h | 10 +++++----- 7 files changed, 91 insertions(+), 24 deletions(-) create mode 100644 as/asm_error.c create mode 100644 as/asm_error.h rename {as => lib}/error.c (71%) rename {as => lib/include}/error.h (83%) diff --git a/Makefile b/Makefile index 481f174..281dfef 100644 --- a/Makefile +++ b/Makefile @@ -24,15 +24,16 @@ PROGRAMS = m16vm m16as all: $(PROGRAMS) -m16vm : vm/vm.o vm/cpu.o vm/mm.o vm/instr_decode.o vm/syscall.o vm/program.o +m16vm : vm/vm.o vm/cpu.o vm/mm.o vm/instr_decode.o \ + vm/syscall.o vm/program.o lib/libm16.a $(LD) $(LDFLAGS) -o $@ $^ m16as : as/as.o as/parser.o as/lexer.o \ - as/codegen.o as/error.o as/symtab.o \ - as/ast.o lib/libm16.a + as/codegen.o as/symtab.o \ + as/ast.o as/asm_error.o lib/libm16.a $(LD) $(LDFLAGS) -o $@ $^ -lib/libm16.a : lib/vector.o +lib/libm16.a : lib/vector.o lib/error.o $(AR) rcs $@ $^ clean : diff --git a/as/asm_error.c b/as/asm_error.c new file mode 100644 index 0000000..b3c0145 --- /dev/null +++ b/as/asm_error.c @@ -0,0 +1,35 @@ +/* asm_error.c + * + * Copyright (C) 2018 Henrik Hautakoski + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +#include +#include +#include "error.h" +#include "asm_error.h" + +void asm_warn(int lineno, const char *message, ...) { + + fprintf(stderr, "Line %i: ", lineno); + warn(message); +} + +int asm_error(int lineno, const char *message, ...) { + + fprintf(stderr, "Line %i: ", lineno); + return error(message); +} diff --git a/as/asm_error.h b/as/asm_error.h new file mode 100644 index 0000000..10444d9 --- /dev/null +++ b/as/asm_error.h @@ -0,0 +1,29 @@ +/* asm_error.h + * + * Copyright (C) 2018 Henrik Hautakoski + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +#ifndef ASM_ERROR_H +#define ASM_ERROR_H + +#include + +void asm_warn(int lineno, const char *message, ...); + +int asm_error(int lineno, const char *message, ...); + +#endif /* ASM_ERROR_H */ diff --git a/as/lexer.c b/as/lexer.c index 53bf8b9..fb9db99 100644 --- a/as/lexer.c +++ b/as/lexer.c @@ -1,7 +1,7 @@ #include #include -#include "error.h" +#include "asm_error.h" #include "lexer.h" /** diff --git a/as/parser.c b/as/parser.c index 1b4eb6e..3d0c822 100644 --- a/as/parser.c +++ b/as/parser.c @@ -20,7 +20,7 @@ #include #include #include -#include "error.h" +#include "asm_error.h" #include "codegen.h" #include "ast.h" #include "lexer.h" diff --git a/as/error.c b/lib/error.c similarity index 71% rename from as/error.c rename to lib/error.c index 704658b..3b73682 100644 --- a/as/error.c +++ b/lib/error.c @@ -21,26 +21,28 @@ #include #include "error.h" -void asm_warn(int lineno, const char *fmt, ...) { +static inline void _write(FILE *fd, const char *prefix, const char *fmt, va_list list) { - va_list vl; - - fprintf(stderr, "Line %i: Warning: ", lineno); - va_start(vl, fmt); - vfprintf(stderr, fmt, vl); - va_end(vl); - fprintf(stderr, "\n"); + char buf[4096]; + vsnprintf(buf, sizeof(buf), fmt, list); + fprintf(stderr, "%s: %s\n", prefix, buf); } -int asm_error(int lineno, const char *fmt, ...) { +void warn(const char *message, ...) { va_list vl; - fprintf(stderr, "Line %i: Error: ", lineno); - va_start(vl, fmt); - vfprintf(stderr, fmt, vl); + va_start(vl, message); + _write(stderr, "warning", message, vl); va_end(vl); - fprintf(stderr, "\n"); +} +int error(const char *message, ...) { + + va_list vl; + + va_start(vl, message); + _write(stderr, "error", message, vl); + va_end(vl); return -1; } diff --git a/as/error.h b/lib/include/error.h similarity index 83% rename from as/error.h rename to lib/include/error.h index 70ebb71..1f8754d 100644 --- a/as/error.h +++ b/lib/include/error.h @@ -17,11 +17,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ -#ifndef ASM_ERROR_H -#define ASM_ERROR_H +#ifndef M16_ERROR_H +#define M16_ERROR_H -void asm_warn(int lineno, const char *fmt, ...); +void warn(const char *message, ...); -int asm_error(int lineno, const char *fmt, ...); +int error(const char *message, ...); -#endif /* ASM_ERROR_H */ +#endif /* M16_ERROR_H */