Merge pull request #183 from ThakeeNathees/minor-refactor

pk_var changed to pk_value and some minor changes
This commit is contained in:
Thakee Nathees 2022-04-07 06:17:53 +05:30 committed by GitHub
commit 5a0690aa4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 84 additions and 71 deletions

View File

@ -15,14 +15,6 @@
#define __has_builtin(x) 0 #define __has_builtin(x) 0
#endif #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 <stdio.h> //< Only needed here for ASSERT() macro and for release mode #include <stdio.h> //< Only needed here for ASSERT() macro and for release mode
//< TODO; macro use this to print a crash report. //< TODO; macro use this to print a crash report.
@ -106,6 +98,15 @@
#define forceinline __attribute__((always_inline)) #define forceinline __attribute__((always_inline))
#endif #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. // Using __ASSERT() for make it crash in release binary too.
#define TODO __ASSERT(false, "TODO: It hasn't implemented yet.") #define TODO __ASSERT(false, "TODO: It hasn't implemented yet.")
#define OOPS "Oops a bug!! report please." #define OOPS "Oops a bug!! report please."

View File

@ -64,7 +64,7 @@ int repl(PKVM* vm, const PkCompileOptions* options) {
user_data->repl_mode = true; user_data->repl_mode = true;
// The main module that'll be used to compile and execute the input source. // 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. // A buffer to store lines read from stdin.
ByteBuffer lines; ByteBuffer lines;
@ -124,7 +124,7 @@ int repl(PKVM* vm, const PkCompileOptions* options) {
if (result != PK_RESULT_SUCCESS) continue; if (result != PK_RESULT_SUCCESS) continue;
// Compiled source would be the "main" function of the module. Run it. // 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); PkHandle* fiber = pkNewFiber(vm, _main);
result = pkRunFiber(vm, fiber, 0, NULL); result = pkRunFiber(vm, fiber, 0, NULL);
pkReleaseHandle(vm, _main); pkReleaseHandle(vm, _main);

View File

@ -34,7 +34,7 @@ int runSource(const char* source) {
PKVM* vm = pkNewVM(&config); PKVM* vm = pkNewVM(&config);
PkStringPtr src = { source, NULL, NULL }; PkStringPtr src = { source, NULL, NULL };
PkStringPtr module = { "$(TRY)", NULL, NULL }; PkStringPtr module = { "@(TRY)", NULL, NULL };
PkResult result = pkInterpretSource(vm, src, module, NULL); PkResult result = pkInterpretSource(vm, src, module, NULL);
pkFreeVM(vm); pkFreeVM(vm);

View File

@ -55,10 +55,6 @@ extern "C" {
#define PK_PUBLIC #define PK_PUBLIC
#endif #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 */ /* POCKETLANG TYPES */
/*****************************************************************************/ /*****************************************************************************/
@ -257,6 +253,10 @@ PK_PUBLIC void pkModuleAddFunction(PKVM* vm, PkHandle* module,
PK_PUBLIC PkHandle* pkGetFunction(PKVM* vm, PkHandle* module, PK_PUBLIC PkHandle* pkGetFunction(PKVM* vm, PkHandle* module,
const char* name); 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 // Compile the [module] with the provided [source]. Set the compiler options
// with the the [options] argument or set to NULL for default options. // with the the [options] argument or set to NULL for default options.
PK_PUBLIC PkResult pkCompileModule(PKVM* vm, PkHandle* module, PK_PUBLIC PkResult pkCompileModule(PKVM* vm, PkHandle* module,

View File

@ -4,8 +4,8 @@
* Distributed Under The MIT License * Distributed Under The MIT License
*/ */
#ifndef BUFFERS_TEMPLATE_H #ifndef PK_BUFFERS_TEMPLATE_H
#define BUFFERS_TEMPLATE_H #define PK_BUFFERS_TEMPLATE_H
#include "pk_internal.h" #include "pk_internal.h"
@ -100,4 +100,4 @@
self->count += other->count; \ self->count += other->count; \
} }
#endif // BUFFERS_TEMPLATE_H #endif // PK_BUFFERS_TEMPLATE_H

View File

@ -15,14 +15,6 @@
#define __has_builtin(x) 0 #define __has_builtin(x) 0
#endif #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 <stdio.h> //< Only needed here for ASSERT() macro and for release mode #include <stdio.h> //< Only needed here for ASSERT() macro and for release mode
//< TODO; macro use this to print a crash report. //< TODO; macro use this to print a crash report.
@ -106,6 +98,15 @@
#define forceinline __attribute__((always_inline)) #define forceinline __attribute__((always_inline))
#endif #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. // Using __ASSERT() for make it crash in release binary too.
#define TODO __ASSERT(false, "TODO: It hasn't implemented yet.") #define TODO __ASSERT(false, "TODO: It hasn't implemented yet.")
#define OOPS "Oops a bug!! report please." #define OOPS "Oops a bug!! report please."

View File

@ -50,9 +50,6 @@
// Max number of break statement in a loop statement to patch. // Max number of break statement in a loop statement to patch.
#define MAX_BREAK_PATCH 256 #define MAX_BREAK_PATCH 256
// The name of a literal function.
#define LITERAL_FN_NAME "$(LiteralFn)"
/*****************************************************************************/ /*****************************************************************************/
/* TOKENS */ /* TOKENS */
/*****************************************************************************/ /*****************************************************************************/
@ -2482,9 +2479,9 @@ static int compilerImportName(Compiler* compiler, int line,
static void compilerImportSingleEntry(Compiler* compiler, static void compilerImportSingleEntry(Compiler* compiler,
const char* name, uint32_t length) { const char* name, uint32_t length) {
// Special names are begins with '$' like function body (only for now). // Special names are begins with '@' (implicit main function, literal
// Skip them. // functions etc) skip them.
if (name[0] == '$') return; if (name[0] == SPECIAL_NAME_CHAR) return;
// Line number of the variables which will be bind to the imported symbol. // Line number of the variables which will be bind to the imported symbol.
int line = compiler->previous.line; int line = compiler->previous.line;

View File

@ -4,11 +4,11 @@
* Distributed Under The MIT License * Distributed Under The MIT License
*/ */
#ifndef COMPILER_H #ifndef PK_COMPILER_H
#define COMPILER_H #define PK_COMPILER_H
#include "pk_internal.h" #include "pk_internal.h"
#include "pk_var.h" #include "pk_value.h"
typedef enum { typedef enum {
#define OPCODE(name, _, __) OP_##name, #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(). // called at the marking phase of vmCollectGarbage().
void compilerMarkObjects(PKVM* vm, Compiler* compiler); void compilerMarkObjects(PKVM* vm, Compiler* compiler);
#endif // COMPILER_H #endif // PK_COMPILER_H

View File

@ -13,7 +13,6 @@
#include "pk_debug.h" #include "pk_debug.h"
#include "pk_utils.h" #include "pk_utils.h"
#include "pk_var.h"
#include "pk_vm.h" #include "pk_vm.h"
// M_PI is non standard. The macro _USE_MATH_DEFINES defining before importing // 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; 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 // A convenient macro to get the nth (1 based) argument of the current
// function. // function.
#define ARG(n) (vm->fiber->ret[n]) #define ARG(n) (vm->fiber->ret[n])

View File

@ -4,11 +4,11 @@
* Distributed Under The MIT License * Distributed Under The MIT License
*/ */
#ifndef CORE_H #ifndef PK_CORE_H
#define CORE_H #define PK_CORE_H
#include "pk_internal.h" #include "pk_internal.h"
#include "pk_var.h" #include "pk_value.h"
// Initialize core language, builtin function and core libs. // Initialize core language, builtin function and core libs.
void initializeCore(PKVM* vm); 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). // Set subscript [value] with the [key] (ie. on[key] = value).
void varsetSubscript(PKVM* vm, Var on, Var key, Var value); void varsetSubscript(PKVM* vm, Var on, Var key, Var value);
#endif // CORE_H #endif // PK_CORE_H

View File

@ -8,7 +8,7 @@
#include <stdio.h> #include <stdio.h>
#include "pk_core.h" #include "pk_core.h"
#include "pk_var.h" #include "pk_value.h"
#include "pk_vm.h" #include "pk_vm.h"
// Opcode names array. // Opcode names array.

View File

@ -4,11 +4,11 @@
* Distributed Under The MIT License * Distributed Under The MIT License
*/ */
#ifndef DEBUG_H #ifndef PK_DEBUG_H
#define DEBUG_H #define PK_DEBUG_H
#include "pk_internal.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 // 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 // [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. // Dump the current (top most) stack call frame.
void dumpStackFrame(PKVM* vm); void dumpStackFrame(PKVM* vm);
#endif // DEBUG_H #endif // PK_DEBUG_H

View File

@ -30,6 +30,14 @@
#define __STDC_LIMIT_MACROS #define __STDC_LIMIT_MACROS
#include <stdint.h> #include <stdint.h>
#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 */ /* INTERNAL CONFIGURATIONS */
/*****************************************************************************/ /*****************************************************************************/
@ -58,6 +66,20 @@
// The size of the error message buffer, used ar vsnprintf (since c99) buffer. // The size of the error message buffer, used ar vsnprintf (since c99) buffer.
#define ERROR_MESSAGE_SIZE 512 #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 */ /* ALLOCATION MACROS */
/*****************************************************************************/ /*****************************************************************************/

View File

@ -4,8 +4,8 @@
* Distributed Under The MIT License * Distributed Under The MIT License
*/ */
#ifndef UTILS_H #ifndef PK_UTILS_H
#define UTILS_H #define PK_UTILS_H
#include "pk_internal.h" #include "pk_internal.h"
@ -34,7 +34,7 @@ uint32_t utilHashNumber(double num);
// Generate a has code for [string]. // Generate a has code for [string].
uint32_t utilHashString(const char* string); uint32_t utilHashString(const char* string);
#endif // UTILS_H #endif // PK_UTILS_H
/**************************************************************************** /****************************************************************************
* UTF8 * * UTF8 *

View File

@ -4,7 +4,7 @@
* Distributed Under The MIT License * Distributed Under The MIT License
*/ */
#include "pk_var.h" #include "pk_value.h"
#include <math.h> #include <math.h>
#include <ctype.h> #include <ctype.h>
@ -1157,7 +1157,7 @@ uint32_t scriptAddGlobal(PKVM* vm, Script* script,
void scriptAddMain(PKVM* vm, Script* script) { void scriptAddMain(PKVM* vm, Script* script) {
ASSERT(script->body == NULL, OOPS); 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->body = newFunction(vm, fn_name, (int)strlen(fn_name),
script, false, NULL/*TODO*/); script, false, NULL/*TODO*/);
script->body->arity = 0; script->body->arity = 0;

View File

@ -4,8 +4,8 @@
* Distributed Under The MIT License * Distributed Under The MIT License
*/ */
#ifndef VAR_H #ifndef PK_VALUE_H
#define VAR_H #define PK_VALUE_H
#include "pk_buffers.h" #include "pk_buffers.h"
#include "pk_internal.h" #include "pk_internal.h"
@ -24,18 +24,6 @@
* programme inefficient for small types such null, bool, int and float. * 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 // 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). // is NaN-tagging, and the second one is union-tagging. (read below for more).
#if VAR_NAN_TAGGING #if VAR_NAN_TAGGING
@ -656,4 +644,4 @@ String * toRepr(PKVM * vm, const Var value);
// Returns the truthy value of the var. // Returns the truthy value of the var.
bool toBool(Var v); bool toBool(Var v);
#endif // VAR_H #endif // PK_VAR_H

View File

@ -4,12 +4,12 @@
* Distributed Under The MIT License * Distributed Under The MIT License
*/ */
#ifndef VM_H #ifndef PK_VM_H
#define VM_H #define PK_VM_H
#include "pk_compiler.h" #include "pk_compiler.h"
#include "pk_internal.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 // The maximum number of temporary object reference to protect them from being
// garbage collected. // garbage collected.
@ -213,4 +213,4 @@ bool vmSwitchFiber(PKVM* vm, Fiber* fiber, Var* value);
// yield value. // yield value.
void vmYieldFiber(PKVM* vm, Var* value); void vmYieldFiber(PKVM* vm, Var* value);
#endif // VM_H #endif // PK_VM_H

View File

@ -28,7 +28,7 @@ def to_rel_path(path):
## corresponding cstring in the CASE_ATTRIB(name, hash) macro calls. ## corresponding cstring in the CASE_ATTRIB(name, hash) macro calls.
HASH_CHECK_LIST = [ HASH_CHECK_LIST = [
"../src/pk_core.c", "../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 ## A list of extension to perform static checks, of all the files in the

View File

@ -27,6 +27,7 @@ TEST_SUITE = {
"Random Scripts" : ( "Random Scripts" : (
"random/lisp_eval.pk", "random/lisp_eval.pk",
"random/string_algo.pk",
), ),
"Examples": ( "Examples": (