Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework cli #374

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions msvcbuild.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@IF /I NOT "%~1" == "KEEP" IF EXIST bld RMDIR /S /Q bld
cmake -Bbld .

@ECHO.
@ECHO.
@ECHO.

@PUSHD bld
@FOR %%X IN (msbuild.exe) DO @IF NOT "%%~$PATH:X" == "" @CALL :msvc
@POPD

@GOTO:EOF



:msvc
@ECHO. ------------------------------------------------------------------------------
@ECHO. ------------------------------------------------------------------------------
@ECHO. -------- building Debug ------------------------------------------------------
@ECHO. ------------------------------------------------------------------------------
@ECHO. ------------------------------------------------------------------------------
@ECHO.
msbuild gravity.sln /p:Configuration=Debug

@ECHO.
@ECHO.
@ECHO.

@ECHO. ------------------------------------------------------------------------------
@ECHO. ------------------------------------------------------------------------------
@ECHO. -------- building Release ----------------------------------------------------
@ECHO. ------------------------------------------------------------------------------
@ECHO. ------------------------------------------------------------------------------
msbuild gravity.sln /p:Configuration=Release
18 changes: 15 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ set(GRAVITY_PRIVATE_COMPILE_OPTIONS "")
set(GRAVITY_INSTALL_RUNTIME_PATH "/usr/local/bin") # Gravity executable install path
set(GRAVITY_INSTALL_LIB_PATH "lib") # Gravity shared library install path
set(GRAVITY_INSTALL_LIB_STATIC_PATH "lib") # Gravity static library install path
set(GRAVITY_OUTDIR "${CMAKE_BINARY_DIR}/lib") # Gravity output directory (base)
set(GRAVITY_OUTDIR_DEBUG "${CMAKE_BINARY_DIR}/Debug/lib") # Gravity output directory (Debug)
set(GRAVITY_OUTDIR_RELEASE "${CMAKE_BINARY_DIR}/Release/lib") # Gravity output directory (Debug)

# ----------------------------------------------------------------
if(MSVC)
Expand Down Expand Up @@ -68,11 +71,13 @@ endif()
# ----------------------------------------------------------------
add_library(gravityapi SHARED ${SRC_FILES})
add_library(gravityapi_s STATIC ${SRC_FILES})
add_library(gravityapi_sx STATIC ${SRC_FILES})

target_compile_definitions(gravityapi PUBLIC BUILD_GRAVITY_API)
target_compile_definitions(gravityapi_sx PUBLIC BUILD_GRAVITY_API)

# ----------------------------------------------------------------
set(GRAVITY_TARGETS gravityapi gravityapi_s)
set(GRAVITY_TARGETS gravityapi gravityapi_s gravityapi_sx)

foreach(target ${GRAVITY_TARGETS})

Expand All @@ -81,8 +86,15 @@ foreach(target ${GRAVITY_TARGETS})
target_compile_options(${target} PRIVATE ${GRAVITY_PRIVATE_COMPILE_OPTIONS})
target_include_directories(${target} PUBLIC ${GRAVITY_INCLUDE_DIR})
set_target_properties(${target} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY "${GRAVITY_OUTDIR}"
ARCHIVE_OUTPUT_DIRECTORY "${GRAVITY_OUTDIR}"
LIBRARY_OUTPUT_DIRECTORY "${GRAVITY_OUTDIR}"
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${GRAVITY_OUTDIR_DEBUG}"
ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${GRAVITY_OUTDIR_DEBUG}"
LIBRARY_OUTPUT_DIRECTORY_DEBUG "${GRAVITY_OUTDIR_DEBUG}"
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${GRAVITY_OUTDIR_RELEASE}"
ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${GRAVITY_OUTDIR_RELEASE}"
LIBRARY_OUTPUT_DIRECTORY_RELEASE "${GRAVITY_OUTDIR_RELEASE}"
)

endforeach()
16 changes: 14 additions & 2 deletions src/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@
# Sources
SET(GRAVITY_SRC gravity.c)

set(GRAVITY_OUTDIR "${CMAKE_BINARY_DIR}/bin") # Gravity output directory (base)
set(GRAVITY_OUTDIR_DEBUG "${CMAKE_BINARY_DIR}/Debug/bin") # Gravity output directory (Debug)
set(GRAVITY_OUTDIR_RELEASE "${CMAKE_BINARY_DIR}/Release/bin") # Gravity output directory (Debug)

# Executable
add_executable(gravity ${GRAVITY_SRC})
target_link_libraries(gravity gravityapi_s)
target_link_libraries(gravity gravityapi_sx)
target_include_directories(gravity PUBLIC ${GRAVITY_INCLUDE_DIR})
set_target_properties(gravity PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
RUNTIME_OUTPUT_DIRECTORY "${GRAVITY_OUTDIR}"
ARCHIVE_OUTPUT_DIRECTORY "${GRAVITY_OUTDIR}/lib"
LIBRARY_OUTPUT_DIRECTORY "${GRAVITY_OUTDIR}/lib"
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${GRAVITY_OUTDIR_DEBUG}"
ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${GRAVITY_OUTDIR_DEBUG}/lib"
LIBRARY_OUTPUT_DIRECTORY_DEBUG "${GRAVITY_OUTDIR_DEBUG}/lib"
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${GRAVITY_OUTDIR_RELEASE}"
ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${GRAVITY_OUTDIR_RELEASE}/lib"
LIBRARY_OUTPUT_DIRECTORY_RELEASE "${GRAVITY_OUTDIR_RELEASE}/lib"
)

# Install
Expand Down
2 changes: 1 addition & 1 deletion src/cli/gravity.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ int main (int argc, const char* argv[]) {
gravity_vm *vm = gravity_vm_new(&delegate);

// pass argc and argv to the ENV class
gravity_env_register_args(vm, argc, argv);
gravity_vm_register_args(vm, argc, argv);

// check if input file is source code that needs to be compiled
if ((type == OP_COMPILE) || (type == OP_COMPILE_RUN) || (type == OP_INLINE_RUN)) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/gravity_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void gravity_compiler_free (gravity_compiler_t *compiler) {
mem_free(compiler);
}

gnode_t *gravity_compiler_ast (gravity_compiler_t *compiler) {
static gnode_t *gravity_compiler_ast (gravity_compiler_t *compiler) {
return compiler->ast;
}

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/gravity_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef __GRAVITY_COMPILER__
#define __GRAVITY_COMPILER__

#include "gravity_common.h"

#include "gravity_delegate.h"
#include "debug_macros.h"
#include "gravity_utils.h"
Expand All @@ -25,7 +27,6 @@ typedef struct gravity_compiler_t gravity_compiler_t;
GRAVITY_API gravity_compiler_t *gravity_compiler_create (gravity_delegate_t *delegate);
GRAVITY_API gravity_closure_t *gravity_compiler_run (gravity_compiler_t *compiler, const char *source, size_t len, uint32_t fileid, bool is_static, bool add_debug);

GRAVITY_API gnode_t *gravity_compiler_ast (gravity_compiler_t *compiler);
GRAVITY_API void gravity_compiler_free (gravity_compiler_t *compiler);
GRAVITY_API json_t *gravity_compiler_serialize (gravity_compiler_t *compiler, gravity_closure_t *closure);
GRAVITY_API bool gravity_compiler_serialize_infile (gravity_compiler_t *compiler, gravity_closure_t *closure, const char *path);
Expand Down
22 changes: 11 additions & 11 deletions src/compiler/gravity_ircode.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static inst_t *inst_new (opcode_t op, uint32_t p1, uint32_t p2, uint32_t p3, opt
if (tag == LABEL_TAG) {
DEBUG_OPCODE("LABEL %d", p1);
} else if (tag != PRAGMA_MOVE_OPTIMIZATION){
const char *op_name = opcode_name(op);
const char *op_name = gravity_opcode_name(op);

if (op == LOADI) {
if (tag == DOUBLE_TAG)
Expand All @@ -118,7 +118,7 @@ static inst_t *inst_new (opcode_t op, uint32_t p1, uint32_t p2, uint32_t p3, opt
} else if (nop == 2) {
DEBUG_OPCODE("%s %d %d", op_name, p1, p2);
} else if (nop == 3) {
DEBUG_OPCODE("%s %d %d %d", opcode_name(op), p1, p2, p3);
DEBUG_OPCODE("%s %d %d %d", gravity_opcode_name(op), p1, p2, p3);
}
}
}
Expand Down Expand Up @@ -289,31 +289,31 @@ void ircode_dump (void *_code) {

switch (n) {
case 0: {
printf("%05d\t%s\n", line, opcode_name(op));
printf("%05d\t%s\n", line, gravity_opcode_name(op));
}

case 1: {
printf("%05d\t%s %d\n", line, opcode_name(op), p1);
printf("%05d\t%s %d\n", line, gravity_opcode_name(op), p1);
} break;

case 2: {
if (op == LOADI) {
if (inst->tag == DOUBLE_TAG) printf("%05d\t%s %d %.2f\n", line, opcode_name(op), p1, inst->d);
if (inst->tag == DOUBLE_TAG) printf("%05d\t%s %d %.2f\n", line, gravity_opcode_name(op), p1, inst->d);
#if defined(_WIN32)
else printf("%05d\t%s %d %I64d\n", line, opcode_name(op), p1, inst->n);
else printf("%05d\t%s %d %I64d\n", line, gravity_opcode_name(op), p1, inst->n);
#else
else printf("%05d\t%s %d %"PRId64"\n", line, opcode_name(op), p1, inst->n);
else printf("%05d\t%s %d %"PRId64"\n", line, gravity_opcode_name(op), p1, inst->n);
#endif
} else if (op == LOADK) {
if (p2 < CPOOL_INDEX_MAX) printf("%05d\t%s %d %d\n", line, opcode_name(op), p1, p2);
else printf("%05d\t%s %d %s\n", line, opcode_name(op), p1, opcode_constname(p2));
if (p2 < CPOOL_INDEX_MAX) printf("%05d\t%s %d %d\n", line, gravity_opcode_name(op), p1, p2);
else printf("%05d\t%s %d %s\n", line, gravity_opcode_name(op), p1, gravity_opcode_constname(p2));
} else {
printf("%05d\t%s %d %d\n", line, opcode_name(op), p1, p2);
printf("%05d\t%s %d %d\n", line, gravity_opcode_name(op), p1, p2);
}
} break;

case 3: {
printf("%05d\t%s %d %d %d\n", line, opcode_name(op), p1, p2, p3);
printf("%05d\t%s %d %d %d\n", line, gravity_opcode_name(op), p1, p2, p3);
} break;

default: assert(0);
Expand Down
14 changes: 8 additions & 6 deletions src/runtime/gravity_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef __GRAVITY_CORE__
#define __GRAVITY_CORE__

#include "gravity_common.h"

#include "gravity_vm.h"

#ifdef __cplusplus
Expand All @@ -24,14 +26,14 @@ GRAVITY_API void gravity_core_register (gravity_vm *vm);
GRAVITY_API bool gravity_iscore_class (gravity_class_t *c);

// conversion functions
gravity_value_t convert_value2bool (gravity_vm *vm, gravity_value_t v);
gravity_value_t convert_value2float (gravity_vm *vm, gravity_value_t v);
gravity_value_t convert_value2int (gravity_vm *vm, gravity_value_t v);
gravity_value_t convert_value2string (gravity_vm *vm, gravity_value_t v);
GRAVITY_API gravity_value_t convert_value2bool (gravity_vm *vm, gravity_value_t v);
GRAVITY_API gravity_value_t convert_value2float (gravity_vm *vm, gravity_value_t v);
GRAVITY_API gravity_value_t convert_value2int (gravity_vm *vm, gravity_value_t v);
GRAVITY_API gravity_value_t convert_value2string (gravity_vm *vm, gravity_value_t v);

// internal functions
gravity_closure_t *computed_property_create (gravity_vm *vm, gravity_function_t *getter_func, gravity_function_t *setter_func);
void computed_property_free (gravity_class_t *c, const char *name, bool remove_flag);
GRAVITY_API gravity_closure_t *computed_property_create (gravity_vm *vm, gravity_function_t *getter_func, gravity_function_t *setter_func);
GRAVITY_API void computed_property_free (gravity_class_t *c, const char *name, bool remove_flag);

#ifdef __cplusplus
}
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/gravity_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,10 @@ gravity_vm *gravity_vm_newmini (void) {
return vm;
}

void gravity_vm_register_args (gravity_vm *vm, uint32_t _argc, const char **_argv) {
gravity_env_register_args(vm, _argc, _argv);
}

void gravity_vm_free (gravity_vm *vm) {
if (!vm) return;

Expand Down Expand Up @@ -1642,7 +1646,7 @@ static void gravity_vm_stats (gravity_vm *vm) {
double d = vm->tstat[i];
double m = d / (double)n;
double p = (d * 100) / total;
printf("%12s %*d %*.4f %*.4f (%.2f%%)\n", opcode_name((opcode_t)i), 10, n, 11, m, 10, d, p);
printf("%12s %*d %*.4f %*.4f (%.2f%%)\n", gravity_opcode_name((opcode_t)i), 10, n, 11, m, 10, d, p);
}
}
printf("=======================================================\n");
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/gravity_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef __GRAVITY_VM__
#define __GRAVITY_VM__

#include "gravity_common.h"

#include "gravity_delegate.h"
#include "gravity_value.h"

Expand Down Expand Up @@ -40,6 +42,7 @@ GRAVITY_API void gravity_vm_loadclosure (gravity_vm *vm, gravity_
GRAVITY_API gravity_value_t gravity_vm_lookup (gravity_vm *vm, gravity_value_t key);
GRAVITY_API gravity_vm *gravity_vm_new (gravity_delegate_t *delegate);
GRAVITY_API gravity_vm *gravity_vm_newmini (void);
GRAVITY_API void gravity_vm_register_args (gravity_vm *vm, uint32_t _argc, const char **_argv);
GRAVITY_API void gravity_vm_reset (gravity_vm *vm);
GRAVITY_API gravity_value_t gravity_vm_result (gravity_vm *vm);
GRAVITY_API bool gravity_vm_runclosure (gravity_vm *vm, gravity_closure_t *closure, gravity_value_t sender, gravity_value_t params[], uint16_t nparams);
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/gravity_vmmacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@
#define CHECK_FAST_BINBOOL_BIT(r1,v2,v3,OP) if (VALUE_ISA_BOOL(v2) && VALUE_ISA_BOOL(v3)) FMATH_BIN_BOOL(r1, v2.n, v3.n, OP)

#define DECODE_BINARY_OPERATION(r1,r2,r3) OPCODE_GET_TWO8bit_ONE10bit(inst, const uint32_t r1, const uint32_t r2, const uint32_t r3); \
DEBUG_VM("%s %d %d %d", opcode_name(op), r1, r2, r3)
DEBUG_VM("%s %d %d %d", gravity_opcode_name(op), r1, r2, r3)

#define PREPARE_FUNC_CALLN(_c,_i,_w,_N) gravity_closure_t *_c = (gravity_closure_t *)gravity_class_lookup_closure(gravity_value_getclass(v2), cache[_i]); \
if (!_c || !_c->f) RUNTIME_ERROR("Unable to perform operator %s on object", opcode_name(op)); \
if (!_c || !_c->f) RUNTIME_ERROR("Unable to perform operator %s on object", gravity_opcode_name(op)); \
uint32_t _w = FN_COUNTREG(func, frame->nargs); \
uint32_t _rneed = FN_COUNTREG(_c->f, _N); \
uint32_t stacktopdelta = (uint32_t)MAXNUM(stackstart + _w + _rneed - fiber->stacktop, 0); \
Expand Down
11 changes: 11 additions & 0 deletions src/shared/gravity_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef __GRAVITY_COMMON__
#define __GRAVITY_COMMON__

//DLL export/import support for Windows
#if !defined(GRAVITY_API) && defined(_WIN32) && defined(BUILD_GRAVITY_API)
#define GRAVITY_API __declspec(dllexport)
#else
#define GRAVITY_API
#endif

#endif
3 changes: 3 additions & 0 deletions src/shared/gravity_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include "gravity_common.h"

#include "gravity_value.h"

#define GRAVITYHASH_ENABLE_STATS 1 // if 0 then stats are not enabled
Expand Down
9 changes: 2 additions & 7 deletions src/shared/gravity_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef __GRAVITY_VALUES__
#define __GRAVITY_VALUES__

#include "gravity_common.h"

#include "gravity_memory.h"
#include "gravity_utils.h"
#include "gravity_array.h"
Expand Down Expand Up @@ -149,13 +151,6 @@ extern "C" {
#define GRAVITY_BRIDGE_INDEX UINT16_MAX
#define GRAVITY_COMPUTED_INDEX UINT16_MAX-1

//DLL export/import support for Windows
#if !defined(GRAVITY_API) && defined(_WIN32) && defined(BUILD_GRAVITY_API)
#define GRAVITY_API __declspec(dllexport)
#else
#define GRAVITY_API
#endif

// MARK: - STRUCT -

// FLOAT_MAX_DECIMALS FROM https://stackoverflow.com/questions/13542944/how-many-significant-digits-have-floats-and-doubles-in-java
Expand Down