From 1d554f3161e7bcf76a180c9280a53cd0f5927591 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 5 Aug 2015 15:46:40 -0700 Subject: [PATCH] input output --- CMakeLists.txt | 7 ++++++- src/main.c | 43 ++++++++++++++++++++++++++++++++++++++++++- test/add.h | 1 + test/add.zig | 3 +++ test/hello.zig | 6 ++++++ 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 test/add.h create mode 100644 test/add.zig create mode 100644 test/hello.zig diff --git a/CMakeLists.txt b/CMakeLists.txt index f044e0bd4c..4ec8834460 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_BUILD_TYPE "Debug" CACHE STRING project(zig C) set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) -set(ZIG_VERSION_MAJOR 1) +set(ZIG_VERSION_MAJOR 0) set(ZIG_VERSION_MINOR 0) set(ZIG_VERSION_PATCH 0) set(ZIG_VERSION "${ZIG_VERSION_MAJOR}.${ZIG_VERSION_MINOR}.${ZIG_VERSION_PATCH}") @@ -15,6 +15,11 @@ message("Configuring zig version ${ZIG_VERSION}") find_package(llvm) include_directories(${LLVM_INCLUDE_DIR}) +include_directories( + ${CMAKE_SOURCE_DIR} + ${CMAKE_BINARY_DIR} +) + set(ZIG_SOURCES "${CMAKE_SOURCE_DIR}/src/main.c" ) diff --git a/src/main.c b/src/main.c index e6173ef043..b54205ce3f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,46 @@ +#include "config.h" #include +#include +#include + +static int usage(char *arg0) { + fprintf(stderr, "Usage: %s --output outfile code.zig\n" + "Other options:\n" + "--version print version number and exit\n" + , arg0); + return EXIT_FAILURE; +} int main(int argc, char **argv) { - return 0; + char *arg0 = argv[0]; + char *in_file = NULL; + char *out_file = NULL; + for (int i = 1; i < argc; i += 1) { + char *arg = argv[i]; + if (arg[0] == '-' && arg[1] == '-') { + if (strcmp(arg, "--version") == 0) { + printf("%s\n", ZIG_VERSION_STRING); + return EXIT_SUCCESS; + } else if (i + 1 >= argc) { + return usage(arg0); + } else { + i += 1; + if (strcmp(arg, "--output") == 0) { + out_file = argv[i]; + } else { + return usage(arg0); + } + } + } else if (!in_file) { + in_file = arg; + } else { + return usage(arg0); + } + } + + if (!in_file || !out_file) + return usage(arg0); + + fprintf(stderr, "in: %s out: %s\n", in_file, out_file); + return EXIT_SUCCESS; } diff --git a/test/add.h b/test/add.h new file mode 100644 index 0000000000..6295ab95cd --- /dev/null +++ b/test/add.h @@ -0,0 +1 @@ +int add(int a, int b); diff --git a/test/add.zig b/test/add.zig new file mode 100644 index 0000000000..59aab49836 --- /dev/null +++ b/test/add.zig @@ -0,0 +1,3 @@ +int add(int a, int b) { + return a + b; +} diff --git a/test/hello.zig b/test/hello.zig new file mode 100644 index 0000000000..f18ba2558b --- /dev/null +++ b/test/hello.zig @@ -0,0 +1,6 @@ +#include +#include "add.h" + +int main(int argc, char **argv) { + fprintf(stderr, "hello: %d", add(1, 2)); +}