From 9b7ad74a7caefa57d9246861fe96a982d7a2585b Mon Sep 17 00:00:00 2001 From: Thakee Nathees Date: Thu, 7 Apr 2022 06:13:29 +0530 Subject: [PATCH] pk_var changed to pk_value and some minor changes --- cli/common.h | 17 +++++++++-------- cli/repl.c | 4 ++-- docs/wasm/main.c | 2 +- src/include/pocketlang.h | 8 ++++---- src/pk_buffers.h | 6 +++--- src/pk_common.h | 17 +++++++++-------- src/pk_compiler.c | 9 +++------ src/pk_compiler.h | 8 ++++---- src/pk_core.c | 5 ++++- src/pk_core.h | 8 ++++---- src/pk_debug.c | 2 +- src/pk_debug.h | 8 ++++---- src/pk_internal.h | 22 ++++++++++++++++++++++ src/pk_utils.h | 6 +++--- src/{pk_var.c => pk_value.c} | 4 ++-- src/{pk_var.h => pk_value.h} | 18 +++--------------- src/pk_vm.h | 8 ++++---- tests/check.py | 2 +- tests/tests.py | 1 + 19 files changed, 84 insertions(+), 71 deletions(-) rename src/{pk_var.c => pk_value.c} (99%) rename src/{pk_var.h => pk_value.h} (98%) diff --git a/cli/common.h b/cli/common.h index 0ac1614..7cd37b2 100644 --- a/cli/common.h +++ b/cli/common.h @@ -15,14 +15,6 @@ #define __has_builtin(x) 0 #endif -#if defined(__GNUC__) - #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" - #pragma GCC diagnostic ignored "-Wunused-parameter" -#elif defined(__clang__) - #pragma clang diagnostic ignored "-Wint-to-pointer-cast" - #pragma clang diagnostic ignored "-Wunused-parameter" -#endif - #include //< Only needed here for ASSERT() macro and for release mode //< TODO; macro use this to print a crash report. @@ -106,6 +98,15 @@ #define forceinline __attribute__((always_inline)) #endif +// To use dynamic variably-sized struct with a tail array add an array at the +// end of the struct with size DYNAMIC_TAIL_ARRAY. This method was a legacy +// standard called "struct hack". +#if defined(_MSC_VER) || __STDC_VERSION__ >= 199901L // stdc >= c99 + #define DYNAMIC_TAIL_ARRAY +#else + #define DYNAMIC_TAIL_ARRAY 0 +#endif + // Using __ASSERT() for make it crash in release binary too. #define TODO __ASSERT(false, "TODO: It hasn't implemented yet.") #define OOPS "Oops a bug!! report please." diff --git a/cli/repl.c b/cli/repl.c index 09bdc60..3019ca5 100644 --- a/cli/repl.c +++ b/cli/repl.c @@ -64,7 +64,7 @@ int repl(PKVM* vm, const PkCompileOptions* options) { user_data->repl_mode = true; // The main module that'll be used to compile and execute the input source. - PkHandle* module = pkNewModule(vm, "$(REPL)"); + PkHandle* module = pkNewModule(vm, "@(REPL)"); // A buffer to store lines read from stdin. ByteBuffer lines; @@ -124,7 +124,7 @@ int repl(PKVM* vm, const PkCompileOptions* options) { if (result != PK_RESULT_SUCCESS) continue; // Compiled source would be the "main" function of the module. Run it. - PkHandle* _main = pkGetFunction(vm, module, PK_IMPLICIT_MAIN_NAME); + PkHandle* _main = pkGetMainFunction(vm, module); PkHandle* fiber = pkNewFiber(vm, _main); result = pkRunFiber(vm, fiber, 0, NULL); pkReleaseHandle(vm, _main); diff --git a/docs/wasm/main.c b/docs/wasm/main.c index 158ab36..430a037 100644 --- a/docs/wasm/main.c +++ b/docs/wasm/main.c @@ -34,7 +34,7 @@ int runSource(const char* source) { PKVM* vm = pkNewVM(&config); PkStringPtr src = { source, NULL, NULL }; - PkStringPtr module = { "$(TRY)", NULL, NULL }; + PkStringPtr module = { "@(TRY)", NULL, NULL }; PkResult result = pkInterpretSource(vm, src, module, NULL); pkFreeVM(vm); diff --git a/src/include/pocketlang.h b/src/include/pocketlang.h index e4f0010..070c0d9 100644 --- a/src/include/pocketlang.h +++ b/src/include/pocketlang.h @@ -55,10 +55,6 @@ extern "C" { #define PK_PUBLIC #endif -// Name of the implicit function for a module. When a module is parsed all of -// it's statements are wrapped around an implicit function with this name. -#define PK_IMPLICIT_MAIN_NAME "$(SourceBody)" - /*****************************************************************************/ /* POCKETLANG TYPES */ /*****************************************************************************/ @@ -257,6 +253,10 @@ PK_PUBLIC void pkModuleAddFunction(PKVM* vm, PkHandle* module, PK_PUBLIC PkHandle* pkGetFunction(PKVM* vm, PkHandle* module, const char* name); +// Returns the main function of the [module]. When a module is compiled all of +// it's statements are wrapped around an implicit main function. +PK_PUBLIC PkHandle* pkGetMainFunction(PKVM* vm, PkHandle* module); + // Compile the [module] with the provided [source]. Set the compiler options // with the the [options] argument or set to NULL for default options. PK_PUBLIC PkResult pkCompileModule(PKVM* vm, PkHandle* module, diff --git a/src/pk_buffers.h b/src/pk_buffers.h index 3967268..48904a9 100644 --- a/src/pk_buffers.h +++ b/src/pk_buffers.h @@ -4,8 +4,8 @@ * Distributed Under The MIT License */ -#ifndef BUFFERS_TEMPLATE_H -#define BUFFERS_TEMPLATE_H +#ifndef PK_BUFFERS_TEMPLATE_H +#define PK_BUFFERS_TEMPLATE_H #include "pk_internal.h" @@ -100,4 +100,4 @@ self->count += other->count; \ } -#endif // BUFFERS_TEMPLATE_H +#endif // PK_BUFFERS_TEMPLATE_H diff --git a/src/pk_common.h b/src/pk_common.h index 0ac1614..7cd37b2 100644 --- a/src/pk_common.h +++ b/src/pk_common.h @@ -15,14 +15,6 @@ #define __has_builtin(x) 0 #endif -#if defined(__GNUC__) - #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" - #pragma GCC diagnostic ignored "-Wunused-parameter" -#elif defined(__clang__) - #pragma clang diagnostic ignored "-Wint-to-pointer-cast" - #pragma clang diagnostic ignored "-Wunused-parameter" -#endif - #include //< Only needed here for ASSERT() macro and for release mode //< TODO; macro use this to print a crash report. @@ -106,6 +98,15 @@ #define forceinline __attribute__((always_inline)) #endif +// To use dynamic variably-sized struct with a tail array add an array at the +// end of the struct with size DYNAMIC_TAIL_ARRAY. This method was a legacy +// standard called "struct hack". +#if defined(_MSC_VER) || __STDC_VERSION__ >= 199901L // stdc >= c99 + #define DYNAMIC_TAIL_ARRAY +#else + #define DYNAMIC_TAIL_ARRAY 0 +#endif + // Using __ASSERT() for make it crash in release binary too. #define TODO __ASSERT(false, "TODO: It hasn't implemented yet.") #define OOPS "Oops a bug!! report please." diff --git a/src/pk_compiler.c b/src/pk_compiler.c index 99e9150..5be37fb 100644 --- a/src/pk_compiler.c +++ b/src/pk_compiler.c @@ -50,9 +50,6 @@ // Max number of break statement in a loop statement to patch. #define MAX_BREAK_PATCH 256 -// The name of a literal function. -#define LITERAL_FN_NAME "$(LiteralFn)" - /*****************************************************************************/ /* TOKENS */ /*****************************************************************************/ @@ -2482,9 +2479,9 @@ static int compilerImportName(Compiler* compiler, int line, static void compilerImportSingleEntry(Compiler* compiler, const char* name, uint32_t length) { - // Special names are begins with '$' like function body (only for now). - // Skip them. - if (name[0] == '$') return; + // Special names are begins with '@' (implicit main function, literal + // functions etc) skip them. + if (name[0] == SPECIAL_NAME_CHAR) return; // Line number of the variables which will be bind to the imported symbol. int line = compiler->previous.line; diff --git a/src/pk_compiler.h b/src/pk_compiler.h index f64cfa2..e01e65b 100644 --- a/src/pk_compiler.h +++ b/src/pk_compiler.h @@ -4,11 +4,11 @@ * Distributed Under The MIT License */ -#ifndef COMPILER_H -#define COMPILER_H +#ifndef PK_COMPILER_H +#define PK_COMPILER_H #include "pk_internal.h" -#include "pk_var.h" +#include "pk_value.h" typedef enum { #define OPCODE(name, _, __) OP_##name, @@ -38,4 +38,4 @@ PkResult compile(PKVM* vm, Script* script, const char* source, // called at the marking phase of vmCollectGarbage(). void compilerMarkObjects(PKVM* vm, Compiler* compiler); -#endif // COMPILER_H +#endif // PK_COMPILER_H diff --git a/src/pk_core.c b/src/pk_core.c index d98b74c..ea823cb 100644 --- a/src/pk_core.c +++ b/src/pk_core.c @@ -13,7 +13,6 @@ #include "pk_debug.h" #include "pk_utils.h" -#include "pk_var.h" #include "pk_vm.h" // M_PI is non standard. The macro _USE_MATH_DEFINES defining before importing @@ -94,6 +93,10 @@ PkHandle* pkGetFunction(PKVM* vm, PkHandle* module, return NULL; } +PkHandle* pkGetMainFunction(PKVM* vm, PkHandle* module) { + return pkGetFunction(vm, module, IMPLICIT_MAIN_NAME); +} + // A convenient macro to get the nth (1 based) argument of the current // function. #define ARG(n) (vm->fiber->ret[n]) diff --git a/src/pk_core.h b/src/pk_core.h index b70dead..e0f0382 100644 --- a/src/pk_core.h +++ b/src/pk_core.h @@ -4,11 +4,11 @@ * Distributed Under The MIT License */ -#ifndef CORE_H -#define CORE_H +#ifndef PK_CORE_H +#define PK_CORE_H #include "pk_internal.h" -#include "pk_var.h" +#include "pk_value.h" // Initialize core language, builtin function and core libs. void initializeCore(PKVM* vm); @@ -63,4 +63,4 @@ Var varGetSubscript(PKVM* vm, Var on, Var key); // Set subscript [value] with the [key] (ie. on[key] = value). void varsetSubscript(PKVM* vm, Var on, Var key, Var value); -#endif // CORE_H +#endif // PK_CORE_H diff --git a/src/pk_debug.c b/src/pk_debug.c index 6397ff7..526d7f6 100644 --- a/src/pk_debug.c +++ b/src/pk_debug.c @@ -8,7 +8,7 @@ #include #include "pk_core.h" -#include "pk_var.h" +#include "pk_value.h" #include "pk_vm.h" // Opcode names array. diff --git a/src/pk_debug.h b/src/pk_debug.h index a7abf48..5d3e615 100644 --- a/src/pk_debug.h +++ b/src/pk_debug.h @@ -4,11 +4,11 @@ * Distributed Under The MIT License */ -#ifndef DEBUG_H -#define DEBUG_H +#ifndef PK_DEBUG_H +#define PK_DEBUG_H #include "pk_internal.h" -#include "pk_var.h" +#include "pk_value.h" // Dump the value of the [value] without a new line at the end to the buffer // [buff]. Note that this will not write a null byte at of the buffer @@ -24,4 +24,4 @@ void dumpGlobalValues(PKVM* vm); // Dump the current (top most) stack call frame. void dumpStackFrame(PKVM* vm); -#endif // DEBUG_H +#endif // PK_DEBUG_H diff --git a/src/pk_internal.h b/src/pk_internal.h index 0bd7ca2..1b8ebf3 100644 --- a/src/pk_internal.h +++ b/src/pk_internal.h @@ -30,6 +30,14 @@ #define __STDC_LIMIT_MACROS #include +#if defined(__GNUC__) + #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" + #pragma GCC diagnostic ignored "-Wunused-parameter" +#elif defined(__clang__) + #pragma clang diagnostic ignored "-Wint-to-pointer-cast" + #pragma clang diagnostic ignored "-Wunused-parameter" +#endif + /*****************************************************************************/ /* INTERNAL CONFIGURATIONS */ /*****************************************************************************/ @@ -58,6 +66,20 @@ // The size of the error message buffer, used ar vsnprintf (since c99) buffer. #define ERROR_MESSAGE_SIZE 512 +// Functions, methods, classes and other names which are intrenal / special to +// pocketlang are starts with the following character (ex: @main, @literalFn). +// When importing all (*) from a module, if the name of an entry starts with +// this character it'll be skipped. +#define SPECIAL_NAME_CHAR '@' + +// Name of the implicit function for a module. When a module is parsed all of +// it's statements are wrapped around an implicit function with this name. +#define IMPLICIT_MAIN_NAME "@main" + +// Name of a literal function. All literal function will have the same name but +// they're uniquely identified by their index in the script's function buffer. +#define LITERAL_FN_NAME "@literalFn" + /*****************************************************************************/ /* ALLOCATION MACROS */ /*****************************************************************************/ diff --git a/src/pk_utils.h b/src/pk_utils.h index 0c2d73b..c6970f8 100644 --- a/src/pk_utils.h +++ b/src/pk_utils.h @@ -4,8 +4,8 @@ * Distributed Under The MIT License */ -#ifndef UTILS_H -#define UTILS_H +#ifndef PK_UTILS_H +#define PK_UTILS_H #include "pk_internal.h" @@ -34,7 +34,7 @@ uint32_t utilHashNumber(double num); // Generate a has code for [string]. uint32_t utilHashString(const char* string); -#endif // UTILS_H +#endif // PK_UTILS_H /**************************************************************************** * UTF8 * diff --git a/src/pk_var.c b/src/pk_value.c similarity index 99% rename from src/pk_var.c rename to src/pk_value.c index 634fdad..c2a3b28 100644 --- a/src/pk_var.c +++ b/src/pk_value.c @@ -4,7 +4,7 @@ * Distributed Under The MIT License */ -#include "pk_var.h" +#include "pk_value.h" #include #include @@ -1157,7 +1157,7 @@ uint32_t scriptAddGlobal(PKVM* vm, Script* script, void scriptAddMain(PKVM* vm, Script* script) { ASSERT(script->body == NULL, OOPS); - const char* fn_name = PK_IMPLICIT_MAIN_NAME; + const char* fn_name = IMPLICIT_MAIN_NAME; script->body = newFunction(vm, fn_name, (int)strlen(fn_name), script, false, NULL/*TODO*/); script->body->arity = 0; diff --git a/src/pk_var.h b/src/pk_value.h similarity index 98% rename from src/pk_var.h rename to src/pk_value.h index 0556cdf..42d05e3 100644 --- a/src/pk_var.h +++ b/src/pk_value.h @@ -4,8 +4,8 @@ * Distributed Under The MIT License */ -#ifndef VAR_H -#define VAR_H +#ifndef PK_VALUE_H +#define PK_VALUE_H #include "pk_buffers.h" #include "pk_internal.h" @@ -24,18 +24,6 @@ * programme inefficient for small types such null, bool, int and float. */ -// To use dynamic variably-sized struct with a tail array add an array at the -// end of the struct with size DYNAMIC_TAIL_ARRAY. This method was a legacy -// standard called "struct hack". -#if defined(_MSC_VER) || __STDC_VERSION__ >= 199901L // std >= c99 - #define DYNAMIC_TAIL_ARRAY -#else - #define DYNAMIC_TAIL_ARRAY 0 -#endif - -// Number of maximum import statements in a script. -#define MAX_IMPORT_SCRIPTS 16 - // There are 2 main implemenation of Var's internal representation. First one // is NaN-tagging, and the second one is union-tagging. (read below for more). #if VAR_NAN_TAGGING @@ -656,4 +644,4 @@ String * toRepr(PKVM * vm, const Var value); // Returns the truthy value of the var. bool toBool(Var v); -#endif // VAR_H +#endif // PK_VAR_H diff --git a/src/pk_vm.h b/src/pk_vm.h index e002f70..f272dfe 100644 --- a/src/pk_vm.h +++ b/src/pk_vm.h @@ -4,12 +4,12 @@ * Distributed Under The MIT License */ -#ifndef VM_H -#define VM_H +#ifndef PK_VM_H +#define PK_VM_H #include "pk_compiler.h" #include "pk_internal.h" -#include "pk_var.h" +#include "pk_value.h" // The maximum number of temporary object reference to protect them from being // garbage collected. @@ -213,4 +213,4 @@ bool vmSwitchFiber(PKVM* vm, Fiber* fiber, Var* value); // yield value. void vmYieldFiber(PKVM* vm, Var* value); -#endif // VM_H +#endif // PK_VM_H diff --git a/tests/check.py b/tests/check.py index 9b294d6..9b86ccc 100644 --- a/tests/check.py +++ b/tests/check.py @@ -28,7 +28,7 @@ def to_rel_path(path): ## corresponding cstring in the CASE_ATTRIB(name, hash) macro calls. HASH_CHECK_LIST = [ "../src/pk_core.c", - "../src/pk_var.c", + "../src/pk_value.c", ] ## A list of extension to perform static checks, of all the files in the diff --git a/tests/tests.py b/tests/tests.py index b0eaa89..bb339e4 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -27,6 +27,7 @@ TEST_SUITE = { "Random Scripts" : ( "random/lisp_eval.pk", + "random/string_algo.pk", ), "Examples": (