diff --git a/Makefile b/Makefile index 5870feb..882396f 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ LDFLAGS = -lm TARGET_EXEC = pocket BUILD_DIR = ./build/ -SRC_DIRS = ./src/ ./cli/ +SRC_DIRS = ./cli/ ./src/core/ ./src/libs/ INC_DIRS = ./src/include/ BIN_DIR = bin/ diff --git a/README.md b/README.md index b2684b9..10408bb 100644 --- a/README.md +++ b/README.md @@ -68,12 +68,12 @@ except for a c99 compatible compiler. It can be compiled with the following comm #### GCC / MinGw / Clang (alias with gcc) ``` -gcc -o pocket cli/*.c src/*.c -Isrc/include -lm +gcc -o pocket cli/*.c src/core/*.c src/libs/*.c -Isrc/include -lm ``` #### MSVC ``` -cl /Fepocket cli/*.c src/*.c /Isrc/include && rm *.obj +cl /Fepocket cli/*.c src/core/*.c src/libs/*.c /Isrc/include && rm *.obj ``` #### Makefile diff --git a/cli/all.c b/cli/all.c deleted file mode 100644 index 99e07e2..0000000 --- a/cli/all.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2020-2022 Thakee Nathees - * Copyright (c) 2021-2022 Pocketlang Contributors - * Distributed Under The MIT License - */ - -// This file will include all source files from the modules, modules/thirdparty -// sub directories here into this single file. That'll make it easy to compile -// with the command `gcc cli/*.c ...` instead of having to add every single -// sub directory to the list of source directory. - -/*****************************************************************************/ -/* STD MODULE SOURCES */ -/*****************************************************************************/ - -#include "modules/std_dummy.c" -#include "modules/std_io.c" -#include "modules/std_path.c" -#include "modules/std_math.c" - -/*****************************************************************************/ -/* THIRDPARTY SOURCES */ -/*****************************************************************************/ - -// Library : cwalk -// License : MIT -// Source : https://github.com/likle/cwalk/ -// Doc : https://likle.github.io/cwalk/ -// About : Path library for C/C++. Cross-Platform for Windows, MacOS and -// Linux. Supports UNIX and Windows path styles on those platforms. -#include "modules/thirdparty/cwalk/cwalk.c" - -// Library : argparse -// License : MIT -// Source : https://github.com/cofyc/argparse/ -// About : Command-line arguments parsing library. -#include "thirdparty/argparse/argparse.c" diff --git a/cli/thirdparty/argparse/argparse.c b/cli/argparse.h similarity index 70% rename from cli/thirdparty/argparse/argparse.c rename to cli/argparse.h index 71ba018..8fea11f 100644 --- a/cli/thirdparty/argparse/argparse.c +++ b/cli/argparse.h @@ -1,3 +1,158 @@ +/* +* The MIT License (MIT) +* +* Copyright (c) 2012-2013 Yecheng Fu +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + + +/** + * Copyright (C) 2012-2015 Yecheng Fu + * All rights reserved. + * + * Use of this source code is governed by a MIT-style license that can be found + * in the LICENSE file. + */ +#ifndef ARGPARSE_H +#define ARGPARSE_H + +/* For c++ compatibility */ +#ifdef __cplusplus +extern "C" { +#endif + +#include + +struct argparse; +struct argparse_option; + +typedef int argparse_callback (struct argparse *self, + const struct argparse_option *option); + +enum argparse_flag { + ARGPARSE_STOP_AT_NON_OPTION = 1, +}; + +enum argparse_option_type { + /* special */ + ARGPARSE_OPT_END, + ARGPARSE_OPT_GROUP, + /* options with no arguments */ + ARGPARSE_OPT_BOOLEAN, + ARGPARSE_OPT_BIT, + /* options with arguments (optional or required) */ + ARGPARSE_OPT_INTEGER, + ARGPARSE_OPT_FLOAT, + ARGPARSE_OPT_STRING, +}; + +enum argparse_option_flags { + OPT_NONEG = 1, /* disable negation */ +}; + +/** + * argparse option + * + * `type`: + * holds the type of the option, you must have an ARGPARSE_OPT_END last in your + * array. + * + * `short_name`: + * the character to use as a short option name, '\0' if none. + * + * `long_name`: + * the long option name, without the leading dash, NULL if none. + * + * `value`: + * stores pointer to the value to be filled. + * + * `help`: + * the short help message associated to what the option does. + * Must never be NULL (except for ARGPARSE_OPT_END). + * + * `callback`: + * function is called when corresponding argument is parsed. + * + * `data`: + * associated data. Callbacks can use it like they want. + * + * `flags`: + * option flags. + */ +struct argparse_option { + enum argparse_option_type type; + const char short_name; + const char *long_name; + void *value; + const char *help; + argparse_callback *callback; + intptr_t data; + int flags; +}; + +/** + * argpparse + */ +struct argparse { + // user supplied + const struct argparse_option *options; + const char *const *usages; + int flags; + const char *description; // a description after usage + const char *epilog; // a description at the end + // internal context + int argc; + const char **argv; + const char **out; + int cpidx; + const char *optvalue; // current option value +}; + +// built-in callbacks +int argparse_help_cb(struct argparse *self, + const struct argparse_option *option); + +// built-in option macros +#define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 } +#define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ } +#define OPT_BIT(...) { ARGPARSE_OPT_BIT, __VA_ARGS__ } +#define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ } +#define OPT_FLOAT(...) { ARGPARSE_OPT_FLOAT, __VA_ARGS__ } +#define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ } +#define OPT_GROUP(h) { ARGPARSE_OPT_GROUP, 0, NULL, NULL, h, NULL, 0, 0 } +#define OPT_HELP() OPT_BOOLEAN('h', "help", NULL, \ + "show this help message and exit", \ + argparse_help_cb, 0, OPT_NONEG) + +int argparse_init(struct argparse *self, struct argparse_option *options, + const char *const *usages, int flags); +void argparse_describe(struct argparse *self, const char *description, + const char *epilog); +int argparse_parse(struct argparse *self, int argc, const char **argv); +void argparse_usage(struct argparse *self); + +#ifdef __cplusplus +} +#endif + +#if defined(_ARGPARSE_IMPL) /** * Copyright (C) 2012-2015 Yecheng Fu * All rights reserved. @@ -397,4 +552,7 @@ argparse_help_cb(struct argparse *self, const struct argparse_option *option) (void)option; argparse_usage(self); exit(EXIT_SUCCESS); -} \ No newline at end of file +} +#endif // _ARGPARSE_IMPL + +#endif \ No newline at end of file diff --git a/cli/main.c b/cli/main.c index b982c6e..ceda01d 100644 --- a/cli/main.c +++ b/cli/main.c @@ -19,8 +19,9 @@ #pragma warning(disable:26812) #endif -#include "modules/modules.h" -#include "thirdparty/argparse/argparse.h" +#define _ARGPARSE_IMPL +#include "argparse.h" +#undef _ARGPARSE_IMPL // FIXME: // Included for isatty(). This should be moved to somewhere. and I'm not sure @@ -44,7 +45,6 @@ static PKVM* intializePocketVM() { PkConfiguration config = pkNewConfiguration(); - config.resolve_path_fn = pathResolveImport; // FIXME: // Refactor and make it portable. Maybe custom is_tty() function?. @@ -59,7 +59,9 @@ static PKVM* intializePocketVM() { config.use_ansi_color = true; } - return pkNewVM(&config); + PKVM* vm = pkNewVM(&config); + pkRegisterLibs(vm); + return vm; } int main(int argc, const char** argv) { @@ -112,8 +114,6 @@ int main(int argc, const char** argv) { // Create and initialize pocket VM. PKVM* vm = intializePocketVM(); - REGISTER_ALL_MODULES(vm); - if (cmd != NULL) { // pocket -c "print('foo')" PkResult result = pkRunString(vm, cmd); exitcode = (int) result; diff --git a/cli/modules/modules.h b/cli/modules/modules.h deleted file mode 100644 index 37d74a3..0000000 --- a/cli/modules/modules.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2020-2022 Thakee Nathees - * Copyright (c) 2021-2022 Pocketlang Contributors - * Distributed Under The MIT License - */ - -#ifndef MODULES_H -#define MODULES_H - -#include - -#include "common.h" -#include -#include -#include - -void registerModuleDummy(PKVM* vm); -void registerModuleIO(PKVM* vm); -void registerModulePath(PKVM* vm); -void registerModuleMath(PKVM* vm); - -// Registers all the cli modules. -#define REGISTER_ALL_MODULES(vm) \ - do { \ - registerModuleDummy(vm); \ - registerModuleIO(vm); \ - registerModulePath(vm); \ - registerModuleMath(vm); \ - } while (false) - -/*****************************************************************************/ -/* MODULES INTERNAL */ -/*****************************************************************************/ - -// Allocate a new module object of type [Ty]. -#define NEW_OBJ(Ty) (Ty*)malloc(sizeof(Ty)) - -// Dellocate module object, allocated by NEW_OBJ(). Called by the freeObj -// callback. -#define FREE_OBJ(ptr) free(ptr) - -// Returns the docstring of the function, which is a static const char* defined -// just above the function by the DEF() macro below. -#define DOCSTRING(fn) __doc_##fn - -// A macro to declare a function, with docstring, which is defined as -// ___doc_ = docstring; That'll used to generate function help text. -#define DEF(fn, docstring) \ - static const char* DOCSTRING(fn) = docstring; \ - static void fn(PKVM* vm) - -/*****************************************************************************/ -/* SHARED FUNCTIONS */ -/*****************************************************************************/ - -// These are "public" module functions that can be shared. Since some modules -// can be used for cli's internals we're defining such functions here and they -// will be imported in the cli. - -// The pocketlang's import statement path resolving function. This -// implementation is required by pockelang from it's hosting application -// inorder to use the import statements. -char* pathResolveImport(PKVM * vm, const char* from, const char* path); - -#endif // MODULES_H diff --git a/cli/modules/thirdparty/cwalk/LICENSE.md b/cli/modules/thirdparty/cwalk/LICENSE.md deleted file mode 100644 index 4d4d4ad..0000000 --- a/cli/modules/thirdparty/cwalk/LICENSE.md +++ /dev/null @@ -1,9 +0,0 @@ -MIT License - -Copyright (c) 2020 Leonard Iklé - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/cli/modules/thirdparty/cwalk/cwalk.h b/cli/modules/thirdparty/cwalk/cwalk.h deleted file mode 100644 index 8dd7808..0000000 --- a/cli/modules/thirdparty/cwalk/cwalk.h +++ /dev/null @@ -1,497 +0,0 @@ -#pragma once - -#ifndef CWK_LIBRARY_H -#define CWK_LIBRARY_H - -#include -#include - -#if defined(_WIN32) || defined(__CYGWIN__) -#define CWK_EXPORT __declspec(dllexport) -#define CWK_IMPORT __declspec(dllimport) -#elif __GNUC__ >= 4 -#define CWK_EXPORT __attribute__((visibility("default"))) -#define CWK_IMPORT __attribute__((visibility("default"))) -#else -#define CWK_EXPORT -#define CWK_IMPORT -#endif - -#if defined(CWK_SHARED) -#if defined(CWK_EXPORTS) -#define CWK_PUBLIC CWK_EXPORT -#else -#define CWK_PUBLIC CWK_IMPORT -#endif -#else -#define CWK_PUBLIC -#endif - -#ifdef __cplusplus -extern "C" -{ -#endif - - /** - * A segment represents a single component of a path. For instance, on linux a - * path might look like this "/var/log/", which consists of two segments "var" - * and "log". - */ - struct cwk_segment - { - const char* path; - const char* segments; - const char* begin; - const char* end; - size_t size; - }; - - /** - * The segment type can be used to identify whether a segment is a special - * segment or not. - * - * CWK_NORMAL - normal folder or file segment - * CWK_CURRENT - "./" current folder segment - * CWK_BACK - "../" relative back navigation segment - */ - enum cwk_segment_type - { - CWK_NORMAL, - CWK_CURRENT, - CWK_BACK - }; - - /** - * @brief Determines the style which is used for the path parsing and - * generation. - */ - enum cwk_path_style - { - CWK_STYLE_WINDOWS, - CWK_STYLE_UNIX - }; - - /** - * @brief Generates an absolute path based on a base. - * - * This function generates an absolute path based on a base path and another - * path. It is guaranteed to return an absolute path. If the second submitted - * path is absolute, it will override the base path. The result will be - * written to a buffer, which might be truncated if the buffer is not large - * enough to hold the full path. However, the truncated result will always be - * null-terminated. The returned value is the amount of characters which the - * resulting path would take if it was not truncated (excluding the - * null-terminating character). - * - * @param base The absolute base path on which the relative path will be - * applied. - * @param path The relative path which will be applied on the base path. - * @param buffer The buffer where the result will be written to. - * @param buffer_size The size of the result buffer. - * @return Returns the total amount of characters of the new absolute path. - */ - CWK_PUBLIC size_t cwk_path_get_absolute(const char* base, const char* path, - char* buffer, size_t buffer_size); - - /** - * @brief Generates a relative path based on a base. - * - * This function generates a relative path based on a base path and another - * path. It determines how to get to the submitted path, starting from the - * base directory. The result will be written to a buffer, which might be - * truncated if the buffer is not large enough to hold the full path. However, - * the truncated result will always be null-terminated. The returned value is - * the amount of characters which the resulting path would take if it was not - * truncated (excluding the null-terminating character). - * - * @param base_directory The base path from which the relative path will - * start. - * @param path The target path where the relative path will point to. - * @param buffer The buffer where the result will be written to. - * @param buffer_size The size of the result buffer. - * @return Returns the total amount of characters of the full path. - */ - CWK_PUBLIC size_t cwk_path_get_relative(const char* base_directory, - const char* path, char* buffer, size_t buffer_size); - - /** - * @brief Joins two paths together. - * - * This function generates a new path by combining the two submitted paths. It - * will remove double separators, and unlike cwk_path_get_absolute it permits - * the use of two relative paths to combine. The result will be written to a - * buffer, which might be truncated if the buffer is not large enough to hold - * the full path. However, the truncated result will always be - * null-terminated. The returned value is the amount of characters which the - * resulting path would take if it was not truncated (excluding the - * null-terminating character). - * - * @param path_a The first path which comes first. - * @param path_b The second path which comes after the first. - * @param buffer The buffer where the result will be written to. - * @param buffer_size The size of the result buffer. - * @return Returns the total amount of characters of the full, combined path. - */ - CWK_PUBLIC size_t cwk_path_join(const char* path_a, const char* path_b, - char* buffer, size_t buffer_size); - - /** - * @brief Joins multiple paths together. - * - * This function generates a new path by joining multiple paths together. It - * will remove double separators, and unlike cwk_path_get_absolute it permits - * the use of multiple relative paths to combine. The last path of the - * submitted string array must be set to NULL. The result will be written to a - * buffer, which might be truncated if the buffer is not large enough to hold - * the full path. However, the truncated result will always be - * null-terminated. The returned value is the amount of characters which the - * resulting path would take if it was not truncated (excluding the - * null-terminating character). - * - * @param paths An array of paths which will be joined. - * @param buffer The buffer where the result will be written to. - * @param buffer_size The size of the result buffer. - * @return Returns the total amount of characters of the full, combined path. - */ - CWK_PUBLIC size_t cwk_path_join_multiple(const char** paths, char* buffer, - size_t buffer_size); - - /** - * @brief Determines the root of a path. - * - * This function determines the root of a path by finding it's length. The - * root always starts at the submitted path. If the path has no root, the - * length will be set to zero. - * - * @param path The path which will be inspected. - * @param length The output of the root length. - */ - CWK_PUBLIC void cwk_path_get_root(const char* path, size_t* length); - - /** - * @brief Changes the root of a path. - * - * This function changes the root of a path. It does not normalize the result. - * The result will be written to a buffer, which might be truncated if the - * buffer is not large enough to hold the full path. However, the truncated - * result will always be null-terminated. The returned value is the amount of - * characters which the resulting path would take if it was not truncated - * (excluding the null-terminating character). - * - * @param path The original path which will get a new root. - * @param new_root The new root which will be placed in the path. - * @param buffer The output buffer where the result is written to. - * @param buffer_size The size of the output buffer where the result is - * written to. - * @return Returns the total amount of characters of the new path. - */ - CWK_PUBLIC size_t cwk_path_change_root(const char* path, const char* new_root, - char* buffer, size_t buffer_size); - - /** - * @brief Determine whether the path is absolute or not. - * - * This function checks whether the path is an absolute path or not. A path is - * considered to be absolute if the root ends with a separator. - * - * @param path The path which will be checked. - * @return Returns true if the path is absolute or false otherwise. - */ - CWK_PUBLIC bool cwk_path_is_absolute(const char* path); - - /** - * @brief Determine whether the path is relative or not. - * - * This function checks whether the path is a relative path or not. A path is - * considered to be relative if the root does not end with a separator. - * - * @param path The path which will be checked. - * @return Returns true if the path is relative or false otherwise. - */ - CWK_PUBLIC bool cwk_path_is_relative(const char* path); - - /** - * @brief Gets the basename of a file path. - * - * This function gets the basename of a file path. A pointer to the beginning - * of the basename will be returned through the basename parameter. This - * pointer will be positioned on the first letter after the separator. The - * length of the file path will be returned through the length parameter. The - * length will be set to zero and the basename to NULL if there is no basename - * available. - * - * @param path The path which will be inspected. - * @param basename The output of the basename pointer. - * @param length The output of the length of the basename. - */ - CWK_PUBLIC void cwk_path_get_basename(const char* path, const char** basename, - size_t* length); - - /** - * @brief Changes the basename of a file path. - * - * This function changes the basename of a file path. This function will not - * write out more than the specified buffer can contain. However, the - * generated string is always null-terminated - even if not the whole path is - * written out. The function returns the total number of characters the - * complete buffer would have, even if it was not written out completely. The - * path may be the same memory address as the buffer. - * - * @param path The original path which will be used for the modified path. - * @param new_basename The new basename which will replace the old one. - * @param buffer The buffer where the changed path will be written to. - * @param buffer_size The size of the result buffer where the changed path is - * written to. - * @return Returns the size which the complete new path would have if it was - * not truncated. - */ - CWK_PUBLIC size_t cwk_path_change_basename(const char* path, - const char* new_basename, char* buffer, size_t buffer_size); - - /** - * @brief Gets the dirname of a file path. - * - * This function determines the dirname of a file path and returns the length - * up to which character is considered to be part of it. If no dirname is - * found, the length will be set to zero. The beginning of the dirname is - * always equal to the submitted path pointer. - * - * @param path The path which will be inspected. - * @param length The length of the dirname. - */ - CWK_PUBLIC void cwk_path_get_dirname(const char* path, size_t* length); - - /** - * @brief Gets the extension of a file path. - * - * This function extracts the extension portion of a file path. A pointer to - * the beginning of the extension will be returned through the extension - * parameter if an extension is found and true is returned. This pointer will - * be positioned on the dot. The length of the extension name will be returned - * through the length parameter. If no extension is found both parameters - * won't be touched and false will be returned. - * - * @param path The path which will be inspected. - * @param extension The output of the extension pointer. - * @param length The output of the length of the extension. - * @return Returns true if an extension is found or false otherwise. - */ - CWK_PUBLIC bool cwk_path_get_extension(const char* path, const char** extension, - size_t* length); - - /** - * @brief Determines whether the file path has an extension. - * - * This function determines whether the submitted file path has an extension. - * This will evaluate to true if the last segment of the path contains a dot. - * - * @param path The path which will be inspected. - * @return Returns true if the path has an extension or false otherwise. - */ - CWK_PUBLIC bool cwk_path_has_extension(const char* path); - - /** - * @brief Changes the extension of a file path. - * - * This function changes the extension of a file name. The function will - * append an extension if the basename does not have an extension, or use the - * extension as a basename if the path does not have a basename. This function - * will not write out more than the specified buffer can contain. However, the - * generated string is always null-terminated - even if not the whole path is - * written out. The function returns the total number of characters the - * complete buffer would have, even if it was not written out completely. The - * path may be the same memory address as the buffer. - * - * @param path The path which will be used to make the change. - * @param new_extension The extension which will be placed within the new - * path. - * @param buffer The output buffer where the result will be written to. - * @param buffer_size The size of the output buffer where the result will be - * written to. - * @return Returns the total size which the output would have if it was not - * truncated. - */ - CWK_PUBLIC size_t cwk_path_change_extension(const char* path, - const char* new_extension, char* buffer, size_t buffer_size); - - /** - * @brief Creates a normalized version of the path. - * - * This function creates a normalized version of the path within the specified - * buffer. This function will not write out more than the specified buffer can - * contain. However, the generated string is always null-terminated - even if - * not the whole path is written out. The function returns the total number of - * characters the complete buffer would have, even if it was not written out - * completely. The path may be the same memory address as the buffer. - * - * The following will be true for the normalized path: - * 1) "../" will be resolved. - * 2) "./" will be removed. - * 3) double separators will be fixed with a single separator. - * 4) separator suffixes will be removed. - * - * @param path The path which will be normalized. - * @param buffer The buffer where the new path is written to. - * @param buffer_size The size of the buffer. - * @return The size which the complete normalized path has if it was not - * truncated. - */ - CWK_PUBLIC size_t cwk_path_normalize(const char* path, char* buffer, - size_t buffer_size); - - /** - * @brief Finds common portions in two paths. - * - * This function finds common portions in two paths and returns the number - * characters from the beginning of the base path which are equal to the other - * path. - * - * @param path_base The base path which will be compared with the other path. - * @param path_other The other path which will compared with the base path. - * @return Returns the number of characters which are common in the base path. - */ - CWK_PUBLIC size_t cwk_path_get_intersection(const char* path_base, - const char* path_other); - - /** - * @brief Gets the first segment of a path. - * - * This function finds the first segment of a path. The position of the - * segment is set to the first character after the separator, and the length - * counts all characters until the next separator (excluding the separator). - * - * @param path The path which will be inspected. - * @param segment The segment which will be extracted. - * @return Returns true if there is a segment or false if there is none. - */ - CWK_PUBLIC bool cwk_path_get_first_segment(const char* path, - struct cwk_segment* segment); - - /** - * @brief Gets the last segment of the path. - * - * This function gets the last segment of a path. This function may return - * false if the path doesn't contain any segments, in which case the submitted - * segment parameter is not modified. The position of the segment is set to - * the first character after the separator, and the length counts all - * characters until the end of the path (excluding the separator). - * - * @param path The path which will be inspected. - * @param segment The segment which will be extracted. - * @return Returns true if there is a segment or false if there is none. - */ - CWK_PUBLIC bool cwk_path_get_last_segment(const char* path, - struct cwk_segment* segment); - - /** - * @brief Advances to the next segment. - * - * This function advances the current segment to the next segment. If there - * are no more segments left, the submitted segment structure will stay - * unchanged and false is returned. - * - * @param segment The current segment which will be advanced to the next one. - * @return Returns true if another segment was found or false otherwise. - */ - CWK_PUBLIC bool cwk_path_get_next_segment(struct cwk_segment* segment); - - /** - * @brief Moves to the previous segment. - * - * This function moves the current segment to the previous segment. If the - * current segment is the first one, the submitted segment structure will stay - * unchanged and false is returned. - * - * @param segment The current segment which will be moved to the previous one. - * @return Returns true if there is a segment before this one or false - * otherwise. - */ - CWK_PUBLIC bool cwk_path_get_previous_segment(struct cwk_segment* segment); - - /** - * @brief Gets the type of the submitted path segment. - * - * This function inspects the contents of the segment and determines the type - * of it. Currently, there are three types CWK_NORMAL, CWK_CURRENT and - * CWK_BACK. A CWK_NORMAL segment is a normal folder or file entry. A - * CWK_CURRENT is a "./" and a CWK_BACK a "../" segment. - * - * @param segment The segment which will be inspected. - * @return Returns the type of the segment. - */ - CWK_PUBLIC enum cwk_segment_type cwk_path_get_segment_type( - const struct cwk_segment* segment); - - /** - * @brief Changes the content of a segment. - * - * This function overrides the content of a segment to the submitted value and - * outputs the whole new path to the submitted buffer. The result might - * require less or more space than before if the new value length differs from - * the original length. The output is truncated if the new path is larger than - * the submitted buffer size, but it is always null-terminated. The source of - * the segment and the submitted buffer may be the same. - * - * @param segment The segment which will be modifier. - * @param value The new content of the segment. - * @param buffer The buffer where the modified path will be written to. - * @param buffer_size The size of the output buffer. - * @return Returns the total size which would have been written if the output - * was not truncated. - */ - CWK_PUBLIC size_t cwk_path_change_segment(struct cwk_segment* segment, - const char* value, char* buffer, size_t buffer_size); - - /** - * @brief Checks whether the submitted pointer points to a separator. - * - * This function simply checks whether the submitted pointer points to a - * separator, which has to be null-terminated (but not necessarily after the - * separator). The function will return true if it is a separator, or false - * otherwise. - * - * @param symbol A pointer to a string. - * @return Returns true if it is a separator, or false otherwise. - */ - CWK_PUBLIC bool cwk_path_is_separator(const char* str); - - /** - * @brief Guesses the path style. - * - * This function guesses the path style based on a submitted path-string. The - * guessing will look at the root and the type of slashes contained in the - * path and return the style which is more likely used in the path. - * - * @param path The path which will be inspected. - * @return Returns the style which is most likely used for the path. - */ - CWK_PUBLIC enum cwk_path_style cwk_path_guess_style(const char* path); - - /** - * @brief Configures which path style is used. - * - * This function configures which path style is used. The following styles are - * currently supported. - * - * CWK_STYLE_WINDOWS: Use backslashes as a separator and volume for the root. - * CWK_STYLE_UNIX: Use slashes as a separator and a slash for the root. - * - * @param style The style which will be used from now on. - */ - CWK_PUBLIC void cwk_path_set_style(enum cwk_path_style style); - - /** - * @brief Gets the path style configuration. - * - * This function gets the style configuration which is currently used for the - * paths. This configuration determines how paths are parsed and generated. - * - * @return Returns the current path style configuration. - */ - CWK_PUBLIC enum cwk_path_style cwk_path_get_style(void); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif \ No newline at end of file diff --git a/cli/modules/thirdparty/dirent/LICENSE b/cli/modules/thirdparty/dirent/LICENSE deleted file mode 100644 index 209c35e..0000000 --- a/cli/modules/thirdparty/dirent/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 1998-2019 Toni Ronkko - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file diff --git a/cli/thirdparty/argparse/LICENSE b/cli/thirdparty/argparse/LICENSE deleted file mode 100644 index 6c6cffd..0000000 --- a/cli/thirdparty/argparse/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2012-2013 Yecheng Fu - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. \ No newline at end of file diff --git a/cli/thirdparty/argparse/argparse.h b/cli/thirdparty/argparse/argparse.h deleted file mode 100644 index 4933329..0000000 --- a/cli/thirdparty/argparse/argparse.h +++ /dev/null @@ -1,130 +0,0 @@ -/** - * Copyright (C) 2012-2015 Yecheng Fu - * All rights reserved. - * - * Use of this source code is governed by a MIT-style license that can be found - * in the LICENSE file. - */ -#ifndef ARGPARSE_H -#define ARGPARSE_H - -/* For c++ compatibility */ -#ifdef __cplusplus -extern "C" { -#endif - -#include - -struct argparse; -struct argparse_option; - -typedef int argparse_callback (struct argparse *self, - const struct argparse_option *option); - -enum argparse_flag { - ARGPARSE_STOP_AT_NON_OPTION = 1, -}; - -enum argparse_option_type { - /* special */ - ARGPARSE_OPT_END, - ARGPARSE_OPT_GROUP, - /* options with no arguments */ - ARGPARSE_OPT_BOOLEAN, - ARGPARSE_OPT_BIT, - /* options with arguments (optional or required) */ - ARGPARSE_OPT_INTEGER, - ARGPARSE_OPT_FLOAT, - ARGPARSE_OPT_STRING, -}; - -enum argparse_option_flags { - OPT_NONEG = 1, /* disable negation */ -}; - -/** - * argparse option - * - * `type`: - * holds the type of the option, you must have an ARGPARSE_OPT_END last in your - * array. - * - * `short_name`: - * the character to use as a short option name, '\0' if none. - * - * `long_name`: - * the long option name, without the leading dash, NULL if none. - * - * `value`: - * stores pointer to the value to be filled. - * - * `help`: - * the short help message associated to what the option does. - * Must never be NULL (except for ARGPARSE_OPT_END). - * - * `callback`: - * function is called when corresponding argument is parsed. - * - * `data`: - * associated data. Callbacks can use it like they want. - * - * `flags`: - * option flags. - */ -struct argparse_option { - enum argparse_option_type type; - const char short_name; - const char *long_name; - void *value; - const char *help; - argparse_callback *callback; - intptr_t data; - int flags; -}; - -/** - * argpparse - */ -struct argparse { - // user supplied - const struct argparse_option *options; - const char *const *usages; - int flags; - const char *description; // a description after usage - const char *epilog; // a description at the end - // internal context - int argc; - const char **argv; - const char **out; - int cpidx; - const char *optvalue; // current option value -}; - -// built-in callbacks -int argparse_help_cb(struct argparse *self, - const struct argparse_option *option); - -// built-in option macros -#define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 } -#define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ } -#define OPT_BIT(...) { ARGPARSE_OPT_BIT, __VA_ARGS__ } -#define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ } -#define OPT_FLOAT(...) { ARGPARSE_OPT_FLOAT, __VA_ARGS__ } -#define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ } -#define OPT_GROUP(h) { ARGPARSE_OPT_GROUP, 0, NULL, NULL, h, NULL, 0, 0 } -#define OPT_HELP() OPT_BOOLEAN('h', "help", NULL, \ - "show this help message and exit", \ - argparse_help_cb, 0, OPT_NONEG) - -int argparse_init(struct argparse *self, struct argparse_option *options, - const char *const *usages, int flags); -void argparse_describe(struct argparse *self, const char *description, - const char *epilog); -int argparse_parse(struct argparse *self, int argc, const char **argv); -void argparse_usage(struct argparse *self); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/scripts/amalgamate.py b/scripts/amalgamate.py new file mode 100644 index 0000000..b06b7d9 --- /dev/null +++ b/scripts/amalgamate.py @@ -0,0 +1,109 @@ +## +## Copyright (c) 2020-2022 Thakee Nathees +## Copyright (c) 2021-2022 Pocketlang Contributors +## Distributed Under The MIT License +## + +## +## USAGE: python amalgamate.py > pocketlang.h +## + +import os, re, sys +from os.path import * + +## Pocket lang root directory. All the listed paths bellow are relative to +## the root path. +ROOT_PATH = abspath(join(dirname(__file__), "..")) + +## Filter all the files in the [path] with extension and return them. +def files(path, ext, filter_= lambda file : True): + return [ + join(ROOT_PATH, path, file) for file in os.listdir(join(ROOT_PATH, path)) + if file.endswith(ext) and filter_(file) + ] + +SOURCES = [ + *files('src/core/', '.c'), + *files('src/libs/', '.c'), +] + +PUBLIC_HEADER = join(ROOT_PATH, 'src/include/pocketlang.h') + +HEADERS = [ + *[ ## The order of the headers are important. + join(ROOT_PATH, header) + for header in [ + 'src/core/common.h', + 'src/core/utils.h', + 'src/core/internal.h', + 'src/core/buffers.h', + 'src/core/value.h', + 'src/core/compiler.h', + 'src/core/core.h', + 'src/core/debug.h', + 'src/core/vm.h', + 'src/libs/libs.h', + ] + ] +] + +## return include path from a linclude "statement" line. +def _get_include_path(line): + assert '#include ' in line + index = line.find('#include ') + start = line.find('"', index) + end = line.find('"', start + 1) + return line[start+1:end] + +## Since the stdout is redirected to a file, stderr can be +## used for logging. +def log(*msg): + message = ' '.join(map(lambda x: str(x), msg)) + sys.stderr.write(message + '\n') + +def parse(path): + text = "" + with open(path, 'r') as fp: + for line in fp.readlines(): + if "//<< AMALG_INLINE >>" in line: + path = join(dirname(path), _get_include_path(line)) + path = path.replace('\\', '/') ## Aaah windows path. + text += parse(path) + '\n' + else: text += line + + ## Note that this comment stripping regex is dubious, and wont + ## work on all cases ex: + ## + ## const char* s = "//";\n + ## + ## The above '//' considered as comment but it's inside a string. + text = re.sub('/\*.*?\*/', '', text, flags=re.S) + text = re.sub('//.*?\n', '\n', text, flags=re.S) + text = re.sub('[^\n\S]*\n', '\n', text, flags=re.S) + text = re.sub('\n\n+', '\n\n', text, flags=re.S) + return text + +def generate(): + + gen = '' + with open(PUBLIC_HEADER, 'r') as fp: + gen = fp.read() + + gen += '\n#define PK_AMALGAMATED\n' + for header in HEADERS: + gen += '// Amalgamated file: ' +\ + relpath(header, ROOT_PATH).replace("\\", "/") +\ + '\n' + gen += parse(header) + '\n' + + gen += '#ifdef PK_IMPL\n\n' + for source in SOURCES: + gen += parse(source) + gen += '#endif // PK_IMPL\n' + + return gen + +if __name__ == '__main__': + print(generate()) + + diff --git a/scripts/build.bat b/scripts/build.bat index 8c1fbda..99d5dba 100644 --- a/scripts/build.bat +++ b/scripts/build.bat @@ -128,7 +128,7 @@ if not exist %target_dir%obj\cli\ mkdir %target_dir%obj\cli\ cd %target_dir%obj\pocket -cl /nologo /c %addnl_cdefines% %addnl_cflags% %pocket_root%src\*.c +cl /nologo /c %addnl_cdefines% %addnl_cflags% /I%pocket_root%src\include\ %pocket_root%src\core\*.c %pocket_root%src\libs\*.c if errorlevel 1 goto :FAIL :: If compiling shared lib, jump pass the lib/cli binaries. diff --git a/scripts/premake5.lua b/scripts/premake5.lua index dc19c37..dd9c154 100644 --- a/scripts/premake5.lua +++ b/scripts/premake5.lua @@ -7,8 +7,10 @@ -- Requires premake5.0.0-alpha-12 or greater. local build_dir = "../build/" -local src_dir = "../src/" local cli_dir = "../cli/" +local include_dir = "../src/include/" +local core_dir = "../src/core/" +local libs_dir = "../src/libs/" workspace "pocketlang" location (build_dir) @@ -47,16 +49,26 @@ project "pocket_static" kind "StaticLib" targetname "pocket" proj_commons("lib/") - files { src_dir .. "*.c", - src_dir .. "**.h"} + includedirs { include_dir } + files { core_dir .. "*.c", + core_dir .. "*.h", + libs_dir .. "*.c", + libs_dir .. "*.h", + include_dir .. "*.h", + } -- pocketlang sources as shared library. project "pocket_shared" kind "SharedLib" targetname "pocket" proj_commons("bin/") - files { src_dir .. "*.c", - src_dir .. "**.h"} + includedirs { include_dir } + files { core_dir .. "*.c", + core_dir .. "*.h", + libs_dir .. "*.c", + libs_dir .. "*.h", + include_dir .. "*.h", + } defines { "PK_DLL", "PK_COMPILE" } -- command line executable. @@ -65,6 +77,6 @@ project "pocket_cli" targetname "pocket" proj_commons("bin/") files { cli_dir .. "*.c", - cli_dir .. "**.h" } - includedirs ({ src_dir .. "include/" }) + cli_dir .. "*.h" } + includedirs { include_dir } links { "pocket_static" } diff --git a/scripts/static_check.py b/scripts/static_check.py index d4f3f84..4038538 100644 --- a/scripts/static_check.py +++ b/scripts/static_check.py @@ -8,7 +8,7 @@ import os, sys, re from os import listdir -from os.path import join, abspath, dirname, relpath, normpath +from os.path import * ## Pocket lang root directory. All the listed paths bellow are relative to ## the root path. @@ -17,8 +17,8 @@ ROOT_PATH = abspath(join(dirname(__file__), "..")) ## A list of source files, to check if the fnv1a hash values match it's ## corresponding cstring in the CASE_ATTRIB(name, hash) macro calls. HASH_CHECK_LIST = [ - "src/pk_core.c", - "src/pk_value.c", + "src/core/core.c", + "src/core/value.c", ] ## A list of extension to perform static checks, of all the files in the @@ -29,18 +29,21 @@ CHECK_EXTENTIONS = ('.c', '.h', '.py', '.pk', '.js') ## 79 characters, It's not "the correct way" but it works. ALLOW_LONG_LINES = ('http://', 'https://', '