authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-08-05 16:22:21-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-08-05 16:22:21-07:00
log899c9fe94e04f50c6176644b61e2d89cb74a8886
tree5ffba668fd70c6be47a93eedb9033bb12e1feab6
parente66c34980cfd74a2643600822c4dbde93a86e585

read a file


6 files changed, 300 insertions(+), 50 deletions(-)

CMakeLists.txt+8-4
...@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 2.8)...@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 2.8)
3set(CMAKE_BUILD_TYPE "Debug" CACHE STRING3set(CMAKE_BUILD_TYPE "Debug" CACHE STRING
4 "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)4 "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
55
6project(zig C)6project(zig C CXX)
7set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})7set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
88
9set(ZIG_VERSION_MAJOR 0)9set(ZIG_VERSION_MAJOR 0)
...@@ -21,7 +21,8 @@ include_directories(...@@ -21,7 +21,8 @@ include_directories(
21)21)
2222
23set(ZIG_SOURCES23set(ZIG_SOURCES
24 "${CMAKE_SOURCE_DIR}/src/main.c"24 "${CMAKE_SOURCE_DIR}/src/main.cpp"
25 "${CMAKE_SOURCE_DIR}/src/util.cpp"
25)26)
2627
27set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")28set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")
...@@ -30,10 +31,13 @@ configure_file (...@@ -30,10 +31,13 @@ configure_file (
30 ${CONFIGURE_OUT_FILE}31 ${CONFIGURE_OUT_FILE}
31)32)
3233
34# GTFO, -lstdc++ !!
35set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
36set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "")
37
33set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Wno-unused-variable -Wno-unused-but-set-variable")38set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Wno-unused-variable -Wno-unused-but-set-variable")
3439
35set(EXE_CFLAGS "-std=c11 -Werror -Wall -Werror=strict-prototypes -Werror=old-style-definition -Werror=missing-prototypes")40set(EXE_CFLAGS "-std=c++11 -Werror -Wall -Werror=strict-prototypes -Werror=old-style-definition -Werror=missing-prototypes")
36set(TEST_CFLAGS "-std=c99 -Werror -Wall")
3741
3842
39add_executable(zig ${ZIG_SOURCES})43add_executable(zig ${ZIG_SOURCES})
src/list.hpp created+89
...@@ -0,0 +1,89 @@
1/*
2 * Copyright (c) 2015 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef GROOVE_LIST_HPP
9#define GROOVE_LIST_HPP
10
11#include "util.hpp"
12
13#include <assert.h>
14
15template<typename T>
16struct GrooveList {
17 void deinit() {
18 deallocate(items);
19 }
20 void append(T item) {
21 int err = ensure_capacity(length + 1);
22 if (err)
23 return err;
24 items[length++] = item;
25 return 0;
26 }
27 // remember that the pointer to this item is invalid after you
28 // modify the length of the list
29 const T & at(int index) const {
30 assert(index >= 0);
31 assert(index < length);
32 return items[index];
33 }
34 T & at(int index) {
35 assert(index >= 0);
36 assert(index < length);
37 return items[index];
38 }
39 T pop() {
40 assert(length >= 1);
41 return items[--length];
42 }
43
44 void add_one() {
45 return resize(length + 1);
46 }
47
48 const T & last() const {
49 assert(length >= 1);
50 return items[length - 1];
51 }
52
53 T & last() {
54 assert(length >= 1);
55 return items[length - 1];
56 }
57
58 void resize(int new_length) {
59 assert(new_length >= 0);
60 int err = ensure_capacity(new_length);
61 if (err)
62 return err;
63 length = new_length;
64 return 0;
65 }
66
67 void clear() {
68 length = 0;
69 }
70
71 void ensure_capacity(int new_capacity) {
72 int better_capacity = max(capacity, 16);
73 while (better_capacity < new_capacity)
74 better_capacity = better_capacity * 2;
75 if (better_capacity != capacity) {
76 items = reallocate_nonzero(items, better_capacity);
77 capacity = better_capacity;
78 }
79 return 0;
80 }
81
82 T * items;
83 int length;
84 int capacity;
85};
86
87#endif
88
89
src/main.c deleted-46
...@@ -1,46 +0,0 @@
1#include "config.h"
2#include <stdio.h>
3#include <string.h>
4#include <stdlib.h>
5
6static int usage(char *arg0) {
7 fprintf(stderr, "Usage: %s --output outfile code.zig\n"
8 "Other options:\n"
9 "--version print version number and exit\n"
10 , arg0);
11 return EXIT_FAILURE;
12}
13
14int main(int argc, char **argv) {
15 char *arg0 = argv[0];
16 char *in_file = NULL;
17 char *out_file = NULL;
18 for (int i = 1; i < argc; i += 1) {
19 char *arg = argv[i];
20 if (arg[0] == '-' && arg[1] == '-') {
21 if (strcmp(arg, "--version") == 0) {
22 printf("%s\n", ZIG_VERSION_STRING);
23 return EXIT_SUCCESS;
24 } else if (i + 1 >= argc) {
25 return usage(arg0);
26 } else {
27 i += 1;
28 if (strcmp(arg, "--output") == 0) {
29 out_file = argv[i];
30 } else {
31 return usage(arg0);
32 }
33 }
34 } else if (!in_file) {
35 in_file = arg;
36 } else {
37 return usage(arg0);
38 }
39 }
40
41 if (!in_file || !out_file)
42 return usage(arg0);
43
44 fprintf(stderr, "in: %s out: %s\n", in_file, out_file);
45 return EXIT_SUCCESS;
46}
src/main.cpp created+106
...@@ -0,0 +1,106 @@
1/*
2 * Copyright (c) 2015 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#include "config.h"
9#include "util.hpp"
10#include <stdio.h>
11#include <string.h>
12#include <stdlib.h>
13#include <stdarg.h>
14#include <limits.h>
15#include <stdint.h>
16#include <errno.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <unistd.h>
20
21struct Buf {
22 int len;
23 char ptr[0];
24};
25
26static Buf *alloc_buf(int size) {
27 Buf *buf = (Buf *)allocate_nonzero<char>(sizeof(Buf) + size + 1);
28 buf->len = size;
29 buf->ptr[buf->len] = 0;
30 return buf;
31}
32
33static int usage(char *arg0) {
34 fprintf(stderr, "Usage: %s --output outfile code.zig\n"
35 "Other options:\n"
36 "--version print version number and exit\n"
37 , arg0);
38 return EXIT_FAILURE;
39}
40
41static struct Buf *fetch_file(FILE *f) {
42 int fd = fileno(f);
43 struct stat st;
44 if (fstat(fd, &st))
45 zig_panic("unable to stat file: %s", strerror(errno));
46 off_t big_size = st.st_size;
47 if (big_size > INT_MAX)
48 zig_panic("file too big");
49 int size = (int)big_size;
50
51 Buf *buf = alloc_buf(size);
52 size_t amt_read = fread(buf->ptr, 1, buf->len, f);
53 if (amt_read != (size_t)buf->len)
54 zig_panic("error reading: %s", strerror(errno));
55
56 return buf;
57}
58
59int main(int argc, char **argv) {
60 char *arg0 = argv[0];
61 char *in_file = NULL;
62 char *out_file = NULL;
63 for (int i = 1; i < argc; i += 1) {
64 char *arg = argv[i];
65 if (arg[0] == '-' && arg[1] == '-') {
66 if (strcmp(arg, "--version") == 0) {
67 printf("%s\n", ZIG_VERSION_STRING);
68 return EXIT_SUCCESS;
69 } else if (i + 1 >= argc) {
70 return usage(arg0);
71 } else {
72 i += 1;
73 if (strcmp(arg, "--output") == 0) {
74 out_file = argv[i];
75 } else {
76 return usage(arg0);
77 }
78 }
79 } else if (!in_file) {
80 in_file = arg;
81 } else {
82 return usage(arg0);
83 }
84 }
85
86 if (!in_file || !out_file)
87 return usage(arg0);
88
89 FILE *in_f;
90 if (strcmp(in_file, "-") == 0) {
91 in_f = stdin;
92 } else {
93 in_f = fopen(in_file, "rb");
94 if (!in_f)
95 zig_panic("unable to open %s for reading: %s\n", in_file, strerror(errno));
96 }
97
98 struct Buf *in_data = fetch_file(in_f);
99
100 fprintf(stderr, "%s\n", in_data->ptr);
101
102 //tokenize(in_data);
103
104
105 return EXIT_SUCCESS;
106}
src/util.cpp created+46
...@@ -0,0 +1,46 @@
1/*
2 * Copyright (c) 2015 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <stdarg.h>
11
12#include "util.hpp"
13
14void zig_panic(const char *format, ...) {
15 va_list ap;
16 va_start(ap, format);
17 vfprintf(stderr, format, ap);
18 fprintf(stderr, "\n");
19 va_end(ap);
20 abort();
21}
22
23char *zig_alloc_sprintf(int *len, const char *format, ...) {
24 va_list ap, ap2;
25 va_start(ap, format);
26 va_copy(ap2, ap);
27
28 int len1 = vsnprintf(nullptr, 0, format, ap);
29 assert(len1 >= 0);
30
31 size_t required_size = len1 + 1;
32 char *mem = allocate<char>(required_size);
33 if (!mem)
34 return nullptr;
35
36 int len2 = vsnprintf(mem, required_size, format, ap2);
37 assert(len2 == len1);
38
39 va_end(ap2);
40 va_end(ap);
41
42 if (len)
43 *len = len1;
44 return mem;
45}
46
src/util.hpp created+51
...@@ -0,0 +1,51 @@
1/*
2 * Copyright (c) 2015 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_UTIL_HPP
9#define ZIG_UTIL_HPP
10
11#include <stdlib.h>
12#include <string.h>
13#include <assert.h>
14
15void zig_panic(const char *format, ...)
16 __attribute__((cold))
17 __attribute__ ((noreturn))
18 __attribute__ ((format (printf, 1, 2)));
19
20template<typename T>
21__attribute__((malloc)) static inline T *allocate_nonzero(size_t count) {
22 T *ptr = reinterpret_cast<T*>(malloc(count * sizeof(T)));
23 if (!ptr)
24 zig_panic("allocation failed");
25 return ptr;
26}
27
28template<typename T>
29__attribute__((malloc)) static inline T *allocate(size_t count) {
30 T *ptr = reinterpret_cast<T*>(calloc(count, sizeof(T)));
31 if (!ptr)
32 zig_panic("allocation failed");
33 return ptr;
34}
35
36template<typename T>
37static inline T *reallocate_nonzero(T * old, size_t new_count) {
38 T *ptr = reinterpret_cast<T*>(realloc(old, new_count * sizeof(T)));
39 if (!ptr)
40 zig_panic("allocation failed");
41 return ptr;
42}
43
44char *zig_alloc_sprintf(int *len, const char *format, ...)
45 __attribute__ ((format (printf, 2, 3)));
46
47template <typename T, long n>
48constexpr long array_length(const T (&)[n]) {
49 return n;
50}
51#endif