From bb9708a7e31b9101dc6a9f8dd48b57a37e1ba3d7 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 21 Jan 2015 10:21:04 +0100 Subject: [PATCH 01/13] Resolving (non-standard) zero-sized array usage warning. --- src/wren_value.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wren_value.h b/src/wren_value.h index 359975d1..4dae64f1 100644 --- a/src/wren_value.h +++ b/src/wren_value.h @@ -112,7 +112,7 @@ typedef struct Obj obj; // Does not include the null terminator. int length; - char value[]; + char value[0]; } ObjString; // The dynamically allocated data structure for a variable that has been used @@ -260,7 +260,7 @@ typedef struct ObjFn* fn; // The upvalues this function has closed over. - Upvalue* upvalues[]; + Upvalue* upvalues[0]; } ObjClosure; typedef enum @@ -323,7 +323,7 @@ struct sObjClass typedef struct { Obj obj; - Value fields[]; + Value fields[0]; } ObjInstance; typedef struct From a45ec69f9f0be5f91ca0cd9789ccdd9ab584f486 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 21 Jan 2015 10:22:18 +0100 Subject: [PATCH 02/13] In MSVC the "inline" modifier is not available when compiling in plain-C. --- src/wren_common.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wren_common.h b/src/wren_common.h index 5fd1d1fe..a7c91e3c 100644 --- a/src/wren_common.h +++ b/src/wren_common.h @@ -127,4 +127,10 @@ #endif +// The Microsoft compiler does not sport the "inline" modifier when +// compiling in plain-C +#if defined( _MSC_VER ) && !defined(__cplusplus) + #define inline _inline +#endif + #endif From bc74c2890417b5e50798440926d9a2f8df63d30b Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 21 Jan 2015 10:22:56 +0100 Subject: [PATCH 03/13] Using a "char" could cause odd sign extension results. --- src/wren_compiler.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wren_compiler.c b/src/wren_compiler.c index d57b5476..7141709b 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -627,7 +627,7 @@ static void readUnicodeEscape(Parser* parser) break; } - char digit = readHexDigit(parser); + int digit = readHexDigit(parser); if (digit == -1) { lexError(parser, "Invalid Unicode escape sequence."); From 67da0c42f042797314a9fd662c4edfa1581bee24 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 21 Jan 2015 10:23:46 +0100 Subject: [PATCH 04/13] Fixed returning a "void" function return value (!). --- src/wren_compiler.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/wren_compiler.c b/src/wren_compiler.c index 7141709b..7417b228 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -2576,7 +2576,10 @@ void statement(Compiler* compiler) return; } - if (match(compiler, TOKEN_FOR)) return forStatement(compiler); + if (match(compiler, TOKEN_FOR)) { + forStatement(compiler); + return; + } if (match(compiler, TOKEN_IF)) { @@ -2629,7 +2632,10 @@ void statement(Compiler* compiler) return; } - if (match(compiler, TOKEN_WHILE)) return whileStatement(compiler); + if (match(compiler, TOKEN_WHILE)) { + whileStatement(compiler); + return; + } // Expression statement. expression(compiler); @@ -2798,8 +2804,14 @@ static void variableDefinition(Compiler* compiler) // like the non-curly body of an if or while. void definition(Compiler* compiler) { - if (match(compiler, TOKEN_CLASS)) return classDefinition(compiler); - if (match(compiler, TOKEN_VAR)) return variableDefinition(compiler); + if (match(compiler, TOKEN_CLASS)) { + classDefinition(compiler); + return; + } + if (match(compiler, TOKEN_VAR)) { + variableDefinition(compiler); + return; + } block(compiler); } From 991ada8919674d0771bc7eb83b8cfe5c9944dfb3 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 21 Jan 2015 10:27:27 +0100 Subject: [PATCH 05/13] Microsoft compiler seems not to support the computed-goto approach. --- src/wren_common.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/wren_common.h b/src/wren_common.h index a7c91e3c..2f876f68 100644 --- a/src/wren_common.h +++ b/src/wren_common.h @@ -29,7 +29,12 @@ // // Defaults to on. #ifndef WREN_COMPUTED_GOTO -#define WREN_COMPUTED_GOTO 1 + #ifdef _MSC_VER + // No computed gotos in Visual Studio. + #define WREN_COMPUTED_GOTO 0 + #else + #define WREN_COMPUTED_GOTO 1 + #endif #endif // If true, loads the "IO" class in the standard library. From 73ce2b0b7e793bbf7e766b084097c3d886f31b22 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 21 Jan 2015 10:30:37 +0100 Subject: [PATCH 06/13] Revised headers include order. The "wren_common.h" should be included first, being a sort of "configuration" header. Then the "wren-XYZ.h" header should follow for "XYZ.c" source file. Then the other "wren_*.h" headers (in lexicographical order if possible). Al last, the standard include files. --- src/main.c | 2 ++ src/wren_common.h | 4 ++++ src/wren_compiler.c | 15 +++++++-------- src/wren_core.c | 7 ++++--- src/wren_debug.c | 5 +++-- src/wren_io.c | 1 + src/wren_utils.c | 5 +++-- src/wren_value.c | 9 +++++---- src/wren_vm.c | 12 +++++------- 9 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/main.c b/src/main.c index e169b2e9..43eb64b0 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,5 @@ +#include "wren_common.h" + #include #include #include diff --git a/src/wren_common.h b/src/wren_common.h index 2f876f68..ee864cd0 100644 --- a/src/wren_common.h +++ b/src/wren_common.h @@ -138,4 +138,8 @@ #define inline _inline #endif +#ifdef _MSC_VER + #define _CRT_SECURE_NO_WARNINGS +#endif + #endif diff --git a/src/wren_compiler.c b/src/wren_compiler.c index 7417b228..62906099 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -1,17 +1,16 @@ +#include "wren_common.h" +#include "wren_compiler.h" +#include "wren_vm.h" +#if WREN_DEBUG_DUMP_COMPILED_CODE + #include "wren_debug.h" +#endif + #include #include #include #include #include -#include "wren_common.h" -#include "wren_compiler.h" -#include "wren_vm.h" - -#if WREN_DEBUG_DUMP_COMPILED_CODE - #include "wren_debug.h" -#endif - // This is written in bottom-up order, so the tokenization comes first, then // parsing/code generation. This minimizes the number of explicit forward // declarations needed. diff --git a/src/wren_core.c b/src/wren_core.c index 7499c7ad..4b69afdf 100644 --- a/src/wren_core.c +++ b/src/wren_core.c @@ -1,11 +1,12 @@ +#include "wren_common.h" +#include "wren_core.h" +#include "wren_value.h" + #include #include #include #include -#include "wren_core.h" -#include "wren_value.h" - // Binds a native method named [name] (in Wren) implemented using C function // [fn] to `ObjClass` [cls]. #define NATIVE(cls, name, function) \ diff --git a/src/wren_debug.c b/src/wren_debug.c index ce32f1ae..8519158c 100644 --- a/src/wren_debug.c +++ b/src/wren_debug.c @@ -1,7 +1,8 @@ -#include - +#include "wren_common.h" #include "wren_debug.h" +#include + void wrenDebugPrintStackTrace(WrenVM* vm, ObjFiber* fiber) { fprintf(stderr, "%s\n", fiber->error->value); diff --git a/src/wren_io.c b/src/wren_io.c index 3d8aecdd..f0e8c396 100644 --- a/src/wren_io.c +++ b/src/wren_io.c @@ -1,3 +1,4 @@ +#include "wren_common.h" #include "wren_io.h" #if WREN_USE_LIB_IO diff --git a/src/wren_utils.c b/src/wren_utils.c index 87e033aa..a0753826 100644 --- a/src/wren_utils.c +++ b/src/wren_utils.c @@ -1,8 +1,9 @@ -#include - +#include "wren_common.h" #include "wren_utils.h" #include "wren_vm.h" +#include + DEFINE_BUFFER(Byte, uint8_t); DEFINE_BUFFER(Int, int); DEFINE_BUFFER(String, char*); diff --git a/src/wren_value.c b/src/wren_value.c index 6ee6c12f..68f752c9 100644 --- a/src/wren_value.c +++ b/src/wren_value.c @@ -1,10 +1,11 @@ +#include "wren_common.h" +#include "wren_value.h" +#include "wren.h" +#include "wren_vm.h" + #include #include -#include "wren.h" -#include "wren_value.h" -#include "wren_vm.h" - // TODO: Tune these. // The initial (and minimum) capacity of a non-empty list object. #define LIST_MIN_CAPACITY (16) diff --git a/src/wren_vm.c b/src/wren_vm.c index ba274753..05091dee 100644 --- a/src/wren_vm.c +++ b/src/wren_vm.c @@ -1,18 +1,16 @@ -#include -#include -#include - -#include "wren.h" #include "wren_common.h" +#include "wren_vm.h" +#include "wren.h" #include "wren_compiler.h" #include "wren_core.h" #include "wren_debug.h" -#include "wren_vm.h" - #if WREN_USE_LIB_IO #include "wren_io.h" #endif +#include +#include +#include #if WREN_DEBUG_TRACE_MEMORY || WREN_DEBUG_TRACE_GC #include #endif From 222c2d5bbb9ec577109f84252763ddf3244406d0 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 21 Jan 2015 11:00:35 +0100 Subject: [PATCH 07/13] Adding Microsoft Visual Studio 2013 project and solution files. --- project/msvc2013/wren.sln | 22 +++++ project/msvc2013/wren/wren.vcxproj | 104 +++++++++++++++++++++ project/msvc2013/wren/wren.vcxproj.filters | 72 ++++++++++++++ 3 files changed, 198 insertions(+) create mode 100644 project/msvc2013/wren.sln create mode 100644 project/msvc2013/wren/wren.vcxproj create mode 100644 project/msvc2013/wren/wren.vcxproj.filters diff --git a/project/msvc2013/wren.sln b/project/msvc2013/wren.sln new file mode 100644 index 00000000..283ba5ba --- /dev/null +++ b/project/msvc2013/wren.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.31101.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wren", "wren/wren.vcxproj", "{EBF43135-4A7A-400A-8F23-DF49907025AA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EBF43135-4A7A-400A-8F23-DF49907025AA}.Debug|Win32.ActiveCfg = Debug|Win32 + {EBF43135-4A7A-400A-8F23-DF49907025AA}.Debug|Win32.Build.0 = Debug|Win32 + {EBF43135-4A7A-400A-8F23-DF49907025AA}.Release|Win32.ActiveCfg = Release|Win32 + {EBF43135-4A7A-400A-8F23-DF49907025AA}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/project/msvc2013/wren/wren.vcxproj b/project/msvc2013/wren/wren.vcxproj new file mode 100644 index 00000000..90551119 --- /dev/null +++ b/project/msvc2013/wren/wren.vcxproj @@ -0,0 +1,104 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {EBF43135-4A7A-400A-8F23-DF49907025AA} + Win32Proj + wren + + + + Application + true + v120 + Unicode + + + Application + false + v120 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + ..\..\..\src;..\..\..\include + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + ..\..\..\src;..\..\..\include + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/project/msvc2013/wren/wren.vcxproj.filters b/project/msvc2013/wren/wren.vcxproj.filters new file mode 100644 index 00000000..f1bb2392 --- /dev/null +++ b/project/msvc2013/wren/wren.vcxproj.filters @@ -0,0 +1,72 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file From 61674112c928294f6508b174707044c27553dae4 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 21 Jan 2015 11:01:09 +0100 Subject: [PATCH 08/13] Addressing odd Microsoft Visual Studio 2013 optimizer bug. --- src/wren_vm.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/wren_vm.c b/src/wren_vm.c index 05091dee..3ba12534 100644 --- a/src/wren_vm.c +++ b/src/wren_vm.c @@ -1100,6 +1100,12 @@ int wrenDefineGlobal(WrenVM* vm, const char* name, size_t length, Value value) return symbol; } +// TODO: The Microsoft 2013 compiler seems to oddly inline these two +// functions, resulting in a memory access violation. For now, we are +// disabling the optimizer itself. Need further investigation. +#ifdef _MSC_VER + #pragma optimize("", off) +#endif void wrenPinObj(WrenVM* vm, Obj* obj, WrenPinnedObj* pinned) { pinned->obj = obj; @@ -1111,6 +1117,9 @@ void wrenUnpinObj(WrenVM* vm) { vm->pinned = vm->pinned->previous; } +#ifdef _MSC_VER + #pragma optimize("", on) +#endif static void defineMethod(WrenVM* vm, const char* className, const char* methodName, int numParams, From 244218286ece4fcdebc1cc90d4407ec66b931e39 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 21 Jan 2015 11:06:22 +0100 Subject: [PATCH 09/13] Updating AUTHORS file. =) --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 9479ebde..d8ffac3f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -8,3 +8,4 @@ Paul Woolcock Evan Shaw Gavin Schulz Lukas Werling +Marco Lizza \ No newline at end of file From 6df15f209a893a69b7332f65884304b5fb6bfa1e Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Thu, 22 Jan 2015 16:41:19 +0100 Subject: [PATCH 10/13] Added simple "Math" library. --- src/wren_common.h | 7 +++ src/wren_math.c | 116 +++++++++++++++++++++++++++++++++++++++ src/wren_math.h | 15 +++++ test/math/abs.wren | 8 +++ test/math/all_tests.wren | 40 ++++++++++++++ test/math/ceil.wren | 4 ++ test/math/floor.wren | 4 ++ test/math/frac.wren | 4 ++ test/math/int.wren | 4 ++ test/math/rand.wren | 4 ++ 10 files changed, 206 insertions(+) create mode 100644 src/wren_math.c create mode 100644 src/wren_math.h create mode 100644 test/math/abs.wren create mode 100644 test/math/all_tests.wren create mode 100644 test/math/ceil.wren create mode 100644 test/math/floor.wren create mode 100644 test/math/frac.wren create mode 100644 test/math/int.wren create mode 100644 test/math/rand.wren diff --git a/src/wren_common.h b/src/wren_common.h index 5fd1d1fe..94512121 100644 --- a/src/wren_common.h +++ b/src/wren_common.h @@ -39,6 +39,13 @@ #define WREN_USE_LIB_IO 1 #endif +// If true, loads the "Math" class in the standard library. +// +// Defaults to on. +#ifndef WREN_USE_LIB_MATH +#define WREN_USE_LIB_MATH 1 +#endif + // These flags are useful for debugging and hacking on Wren itself. They are not // intended to be used for production code. They default to off. diff --git a/src/wren_math.c b/src/wren_math.c new file mode 100644 index 00000000..ce52c2be --- /dev/null +++ b/src/wren_math.c @@ -0,0 +1,116 @@ +#include "wren_common.h" +#include "wren_math.h" + +#if WREN_USE_LIB_MATH + +#include +#include +#include + +static int32_t _seed = 0x5752454e; // WREN :) + +static void mathAbs(WrenVM* vm) +{ + double value = wrenGetArgumentDouble(vm, 1); + double result = fabs(value); + wrenReturnDouble(vm, result); +} + +static void mathCeil(WrenVM* vm) +{ + double value = wrenGetArgumentDouble(vm, 1); + double result = ceil(value); + wrenReturnDouble(vm, result); +} + +static void mathFloor(WrenVM* vm) +{ + double value = wrenGetArgumentDouble(vm, 1); + double result = floor(value); + wrenReturnDouble(vm, result); +} + +static void mathInt(WrenVM* vm) +{ + double value = wrenGetArgumentDouble(vm, 1); + double integer; + double fractional = modf(value, &integer); + wrenReturnDouble(vm, integer); +} + +static void mathFrac(WrenVM* vm) +{ + double value = wrenGetArgumentDouble(vm, 1); + double integer; + double fractional = modf(value, &integer); + wrenReturnDouble(vm, fractional); +} + +static void mathSin(WrenVM* vm) +{ + double angle = wrenGetArgumentDouble(vm, 1); + double sine = sin(angle); + wrenReturnDouble(vm, sine); +} + +static void mathCos(WrenVM* vm) +{ + double angle = wrenGetArgumentDouble(vm, 1); + double cosine = cos(angle); + wrenReturnDouble(vm, cosine); +} + +static void mathTan(WrenVM* vm) +{ + double angle = wrenGetArgumentDouble(vm, 1); + double tangent = tan(angle); + wrenReturnDouble(vm, tangent); +} + +static void mathDeg(WrenVM* vm) +{ + double radians = wrenGetArgumentDouble(vm, 1); + double degrees = floor(radians * 57.2957795130823208768); + wrenReturnDouble(vm, degrees); +} + +static void mathRad(WrenVM* vm) +{ + double degrees = wrenGetArgumentDouble(vm, 1); + double radians = floor(degrees / 57.2957795130823208768); + wrenReturnDouble(vm, radians); +} + +static void mathSrand(WrenVM* vm) +{ + time_t now = time(NULL); + _seed = (int32_t)now; + wrenReturnNull(vm); +} + +static void mathRand(WrenVM* vm) +{ + // https://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/ + _seed = (214013 * _seed + 2531011); + int16_t value = (_seed >> 16) & 0x7FFF; + double result = (double)value / (double)(INT16_MAX - 1); + wrenReturnDouble(vm, result); +} + +void wrenLoadMathLibrary(WrenVM* vm) +{ + wrenDefineStaticMethod(vm, "Math", "abs", 1, mathAbs); + wrenDefineStaticMethod(vm, "Math", "ceil", 1, mathCeil); + wrenDefineStaticMethod(vm, "Math", "floor", 1, mathFloor); + wrenDefineStaticMethod(vm, "Math", "int", 1, mathInt); + wrenDefineStaticMethod(vm, "Math", "frac", 1, mathFrac); + wrenDefineStaticMethod(vm, "Math", "sin", 1, mathSin); + wrenDefineStaticMethod(vm, "Math", "cos", 1, mathCos); + wrenDefineStaticMethod(vm, "Math", "tan", 1, mathTan); + wrenDefineStaticMethod(vm, "Math", "deg", 1, mathDeg); + wrenDefineStaticMethod(vm, "Math", "rad", 1, mathRad); + wrenDefineStaticMethod(vm, "Math", "srand", 0, mathSrand); + wrenDefineStaticMethod(vm, "Math", "rand", 0, mathRand); +} + +#endif diff --git a/src/wren_math.h b/src/wren_math.h new file mode 100644 index 00000000..7f90942e --- /dev/null +++ b/src/wren_math.h @@ -0,0 +1,15 @@ +#ifndef wren_math_h +#define wren_math_h + +#include "wren.h" +#include "wren_common.h" + +// This module defines the Ramdp, class and its associated methods. The RNG is +// based upon "xorshift". +#if WREN_USE_LIB_MATH + +void wrenLoadMathLibrary(WrenVM* vm); + +#endif + +#endif diff --git a/test/math/abs.wren b/test/math/abs.wren new file mode 100644 index 00000000..92f4fae6 --- /dev/null +++ b/test/math/abs.wren @@ -0,0 +1,8 @@ +IO.print(Math.abs(123)) // expect: 123 +IO.print(Math.abs(-123)) // expect: 123 +IO.print(Math.abs(0)) // expect: 0 +IO.print(Math.abs(-0)) // expect: 0 +IO.print(Math.abs(-0.12)) // expect: 0.12 +IO.print(Math.abs(12.34)) // expect: 12.34 +IO.print(Math.abs(1.0)) // expect: 1 +IO.print(Math.abs(-1.0)) // expect: 1 diff --git a/test/math/all_tests.wren b/test/math/all_tests.wren new file mode 100644 index 00000000..4f4d9959 --- /dev/null +++ b/test/math/all_tests.wren @@ -0,0 +1,40 @@ +IO.print("abs") +IO.print(Math.abs(123)) // expect: 123 +IO.print(Math.abs(-123)) // expect: 123 +IO.print(Math.abs(0)) // expect: 0 +IO.print(Math.abs(-0)) // expect: 0 +IO.print(Math.abs(-0.12)) // expect: 0.12 +IO.print(Math.abs(12.34)) // expect: 12.34 + +IO.print(Math.abs(1.0)) // expect: 1 +IO.print(Math.abs(-1.0)) // expect: 1 + +IO.print("ceil") +IO.print(Math.ceil(2.3)) // expect: 3 +IO.print(Math.ceil(3.8)) // expect: 4 +IO.print(Math.ceil(-2.3)) // expect: -2 +IO.print(Math.ceil(-3.8)) // expect: -3 + +IO.print("floor") +IO.print(Math.floor(2.3)) // expect: 2 +IO.print(Math.floor(3.8)) // expect: 3 +IO.print(Math.floor(-2.3)) // expect: -3 +IO.print(Math.floor(-3.8)) // expect: -4 + +IO.print("int") +IO.print(Math.int(8)) // expect: 8 +IO.print(Math.int(12.34)) // expect: 12 +IO.print(Math.int(-8)) // expect: -8 +IO.print(Math.int(-12.34)) // expect: -12 + +IO.print("frac") +IO.print(Math.frac(8)) // expect: 0 +IO.print(Math.frac(12.34)) // expect: 0.34 +IO.print(Math.frac(-8)) // expect: -0 +IO.print(Math.frac(-12.34)) // expect: -0.34 + +IO.print("srand/rand") +Math.srand +for (i in 1..10) { + IO.print(Math.floor(Math.rand * 100)) +} diff --git a/test/math/ceil.wren b/test/math/ceil.wren new file mode 100644 index 00000000..6e8c3dfd --- /dev/null +++ b/test/math/ceil.wren @@ -0,0 +1,4 @@ +IO.print(Math.ceil(2.3)) // expect: 3 +IO.print(Math.ceil(3.8)) // expect: 4 +IO.print(Math.ceil(-2.3)) // expect: -2 +IO.print(Math.ceil(-3.8)) // expect: -3 diff --git a/test/math/floor.wren b/test/math/floor.wren new file mode 100644 index 00000000..687ba088 --- /dev/null +++ b/test/math/floor.wren @@ -0,0 +1,4 @@ +IO.print(Math.floor(2.3)) // expect: 2 +IO.print(Math.floor(3.8)) // expect: 3 +IO.print(Math.floor(-2.3)) // expect: -3 +IO.print(Math.floor(-3.8)) // expect: -4 diff --git a/test/math/frac.wren b/test/math/frac.wren new file mode 100644 index 00000000..04506f6d --- /dev/null +++ b/test/math/frac.wren @@ -0,0 +1,4 @@ +IO.print(Math.frac(8)) // expect: 0 +IO.print(Math.frac(12.34)) // expect: 0.34 +IO.print(Math.frac(-8)) // expect: -0 +IO.print(Math.frac(-12.34)) // expect: -0.34 diff --git a/test/math/int.wren b/test/math/int.wren new file mode 100644 index 00000000..a78a5037 --- /dev/null +++ b/test/math/int.wren @@ -0,0 +1,4 @@ +IO.print(Math.int(8)) // expect: 8 +IO.print(Math.int(12.34)) // expect: 12 +IO.print(Math.int(-8)) // expect: -8 +IO.print(Math.int(-12.34)) // expect: -12 diff --git a/test/math/rand.wren b/test/math/rand.wren new file mode 100644 index 00000000..890d9a6e --- /dev/null +++ b/test/math/rand.wren @@ -0,0 +1,4 @@ +Math.srand +for (i in 1..10) { + IO.print(Math.floor(Math.rand * 100)) +} From c88e61d01802cdb0222e54567d961f6a3a0b8c6d Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Thu, 22 Jan 2015 17:46:09 +0100 Subject: [PATCH 11/13] Adding "Math" lib into Wren. --- project/msvc2013/wren/wren.vcxproj | 4 ++- project/msvc2013/wren/wren.vcxproj.filters | 40 +++++++++++++--------- src/wren_common.h | 2 +- src/wren_vm.c | 6 ++++ 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/project/msvc2013/wren/wren.vcxproj b/project/msvc2013/wren/wren.vcxproj index 90551119..0a605e2f 100644 --- a/project/msvc2013/wren/wren.vcxproj +++ b/project/msvc2013/wren/wren.vcxproj @@ -1,4 +1,4 @@ - + @@ -83,6 +83,7 @@ + @@ -94,6 +95,7 @@ + diff --git a/project/msvc2013/wren/wren.vcxproj.filters b/project/msvc2013/wren/wren.vcxproj.filters index f1bb2392..78188b83 100644 --- a/project/msvc2013/wren/wren.vcxproj.filters +++ b/project/msvc2013/wren/wren.vcxproj.filters @@ -15,57 +15,63 @@ - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + + Source Files + + Source Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + + Header Files + + Header Files diff --git a/src/wren_common.h b/src/wren_common.h index 74f2fe9d..6d1c5a39 100644 --- a/src/wren_common.h +++ b/src/wren_common.h @@ -30,7 +30,7 @@ // Defaults to on. #ifndef WREN_COMPUTED_GOTO #ifdef _MSC_VER - // No computed gotos in Visual Studio. + // No computed gotos in Visual Studio. #define WREN_COMPUTED_GOTO 0 #else #define WREN_COMPUTED_GOTO 1 diff --git a/src/wren_vm.c b/src/wren_vm.c index 1210cab8..493b1cb5 100644 --- a/src/wren_vm.c +++ b/src/wren_vm.c @@ -7,6 +7,9 @@ #if WREN_USE_LIB_IO #include "wren_io.h" #endif +#if WREN_USE_LIB_MATH + #include "wren_math.h" +#endif #include #include @@ -72,6 +75,9 @@ WrenVM* wrenNewVM(WrenConfiguration* configuration) #if WREN_USE_LIB_IO wrenLoadIOLibrary(vm); #endif + #if WREN_USE_LIB_IO + wrenLoadMathLibrary(vm); + #endif return vm; } From 14642809333b348498d98ac720f639df8685c470 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Fri, 23 Jan 2015 10:05:10 +0100 Subject: [PATCH 12/13] Restoring original header files order and using MSVC project setting to disable non-secure CRT warnings. --- project/msvc2013/wren/wren.vcxproj | 4 ++-- src/main.c | 2 -- src/wren_common.h | 4 ---- src/wren_compiler.c | 15 ++++++++------- src/wren_core.c | 7 +++---- src/wren_debug.c | 5 ++--- src/wren_io.c | 1 - src/wren_utils.c | 5 ++--- src/wren_value.c | 9 ++++----- src/wren_vm.c | 25 ++++++++++++------------- 10 files changed, 33 insertions(+), 44 deletions(-) diff --git a/project/msvc2013/wren/wren.vcxproj b/project/msvc2013/wren/wren.vcxproj index 0a605e2f..40e8cb95 100644 --- a/project/msvc2013/wren/wren.vcxproj +++ b/project/msvc2013/wren/wren.vcxproj @@ -51,7 +51,7 @@ Level3 Disabled - WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;_LIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) ..\..\..\src;..\..\..\include @@ -67,7 +67,7 @@ MaxSpeed true true - WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;_LIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) ..\..\..\src;..\..\..\include diff --git a/src/main.c b/src/main.c index 43eb64b0..e169b2e9 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,3 @@ -#include "wren_common.h" - #include #include #include diff --git a/src/wren_common.h b/src/wren_common.h index 6d1c5a39..e3225a1c 100644 --- a/src/wren_common.h +++ b/src/wren_common.h @@ -145,8 +145,4 @@ #define inline _inline #endif -#ifdef _MSC_VER - #define _CRT_SECURE_NO_WARNINGS -#endif - #endif diff --git a/src/wren_compiler.c b/src/wren_compiler.c index 54545e70..9a8ccb75 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -1,16 +1,17 @@ -#include "wren_common.h" -#include "wren_compiler.h" -#include "wren_vm.h" -#if WREN_DEBUG_DUMP_COMPILED_CODE - #include "wren_debug.h" -#endif - #include #include #include #include #include +#include "wren_common.h" +#include "wren_compiler.h" +#include "wren_vm.h" + +#if WREN_DEBUG_DUMP_COMPILED_CODE + #include "wren_debug.h" +#endif + // This is written in bottom-up order, so the tokenization comes first, then // parsing/code generation. This minimizes the number of explicit forward // declarations needed. diff --git a/src/wren_core.c b/src/wren_core.c index 099a9a23..b5f63eb9 100644 --- a/src/wren_core.c +++ b/src/wren_core.c @@ -1,12 +1,11 @@ -#include "wren_common.h" -#include "wren_core.h" -#include "wren_value.h" - #include #include #include #include +#include "wren_core.h" +#include "wren_value.h" + // Binds a native method named [name] (in Wren) implemented using C function // [fn] to `ObjClass` [cls]. #define NATIVE(cls, name, function) \ diff --git a/src/wren_debug.c b/src/wren_debug.c index 8519158c..ce32f1ae 100644 --- a/src/wren_debug.c +++ b/src/wren_debug.c @@ -1,8 +1,7 @@ -#include "wren_common.h" -#include "wren_debug.h" - #include +#include "wren_debug.h" + void wrenDebugPrintStackTrace(WrenVM* vm, ObjFiber* fiber) { fprintf(stderr, "%s\n", fiber->error->value); diff --git a/src/wren_io.c b/src/wren_io.c index f0e8c396..3d8aecdd 100644 --- a/src/wren_io.c +++ b/src/wren_io.c @@ -1,4 +1,3 @@ -#include "wren_common.h" #include "wren_io.h" #if WREN_USE_LIB_IO diff --git a/src/wren_utils.c b/src/wren_utils.c index a0753826..87e033aa 100644 --- a/src/wren_utils.c +++ b/src/wren_utils.c @@ -1,9 +1,8 @@ -#include "wren_common.h" +#include + #include "wren_utils.h" #include "wren_vm.h" -#include - DEFINE_BUFFER(Byte, uint8_t); DEFINE_BUFFER(Int, int); DEFINE_BUFFER(String, char*); diff --git a/src/wren_value.c b/src/wren_value.c index 0e2e6fb0..b1a17b82 100644 --- a/src/wren_value.c +++ b/src/wren_value.c @@ -1,11 +1,10 @@ -#include "wren_common.h" -#include "wren_value.h" -#include "wren.h" -#include "wren_vm.h" - #include #include +#include "wren.h" +#include "wren_value.h" +#include "wren_vm.h" + // TODO: Tune these. // The initial (and minimum) capacity of a non-empty list object. #define LIST_MIN_CAPACITY (16) diff --git a/src/wren_vm.c b/src/wren_vm.c index 493b1cb5..4f6fed24 100644 --- a/src/wren_vm.c +++ b/src/wren_vm.c @@ -1,19 +1,18 @@ -#include "wren_common.h" -#include "wren_vm.h" -#include "wren.h" -#include "wren_compiler.h" -#include "wren_core.h" -#include "wren_debug.h" -#if WREN_USE_LIB_IO - #include "wren_io.h" -#endif -#if WREN_USE_LIB_MATH - #include "wren_math.h" -#endif - #include #include #include + +#include "wren.h" +#include "wren_common.h" +#include "wren_compiler.h" +#include "wren_core.h" +#include "wren_debug.h" +#include "wren_vm.h" + +#if WREN_USE_LIB_IO + #include "wren_io.h" +#endif + #if WREN_DEBUG_TRACE_MEMORY || WREN_DEBUG_TRACE_GC #include #endif From 2f12ad879dfeea5f18e42b7789b9cb58792778b0 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Fri, 23 Jan 2015 10:08:34 +0100 Subject: [PATCH 13/13] Pulling the "Math" library out. --- project/msvc2013/wren/wren.vcxproj | 2 - project/msvc2013/wren/wren.vcxproj.filters | 6 -- src/wren_common.h | 7 -- src/wren_math.c | 116 --------------------- src/wren_math.h | 15 --- src/wren_vm.c | 3 - test/math/abs.wren | 8 -- test/math/all_tests.wren | 40 ------- test/math/ceil.wren | 4 - test/math/floor.wren | 4 - test/math/frac.wren | 4 - test/math/int.wren | 4 - test/math/rand.wren | 4 - 13 files changed, 217 deletions(-) delete mode 100644 src/wren_math.c delete mode 100644 src/wren_math.h delete mode 100644 test/math/abs.wren delete mode 100644 test/math/all_tests.wren delete mode 100644 test/math/ceil.wren delete mode 100644 test/math/floor.wren delete mode 100644 test/math/frac.wren delete mode 100644 test/math/int.wren delete mode 100644 test/math/rand.wren diff --git a/project/msvc2013/wren/wren.vcxproj b/project/msvc2013/wren/wren.vcxproj index 40e8cb95..a264f553 100644 --- a/project/msvc2013/wren/wren.vcxproj +++ b/project/msvc2013/wren/wren.vcxproj @@ -83,7 +83,6 @@ - @@ -95,7 +94,6 @@ - diff --git a/project/msvc2013/wren/wren.vcxproj.filters b/project/msvc2013/wren/wren.vcxproj.filters index 78188b83..571da9be 100644 --- a/project/msvc2013/wren/wren.vcxproj.filters +++ b/project/msvc2013/wren/wren.vcxproj.filters @@ -39,9 +39,6 @@ Source Files - - Source Files - @@ -71,8 +68,5 @@ Header Files - - Header Files - \ No newline at end of file diff --git a/src/wren_common.h b/src/wren_common.h index e3225a1c..747c01bb 100644 --- a/src/wren_common.h +++ b/src/wren_common.h @@ -44,13 +44,6 @@ #define WREN_USE_LIB_IO 1 #endif -// If true, loads the "Math" class in the standard library. -// -// Defaults to on. -#ifndef WREN_USE_LIB_MATH -#define WREN_USE_LIB_MATH 1 -#endif - // These flags are useful for debugging and hacking on Wren itself. They are not // intended to be used for production code. They default to off. diff --git a/src/wren_math.c b/src/wren_math.c deleted file mode 100644 index ce52c2be..00000000 --- a/src/wren_math.c +++ /dev/null @@ -1,116 +0,0 @@ -#include "wren_common.h" -#include "wren_math.h" - -#if WREN_USE_LIB_MATH - -#include -#include -#include - -static int32_t _seed = 0x5752454e; // WREN :) - -static void mathAbs(WrenVM* vm) -{ - double value = wrenGetArgumentDouble(vm, 1); - double result = fabs(value); - wrenReturnDouble(vm, result); -} - -static void mathCeil(WrenVM* vm) -{ - double value = wrenGetArgumentDouble(vm, 1); - double result = ceil(value); - wrenReturnDouble(vm, result); -} - -static void mathFloor(WrenVM* vm) -{ - double value = wrenGetArgumentDouble(vm, 1); - double result = floor(value); - wrenReturnDouble(vm, result); -} - -static void mathInt(WrenVM* vm) -{ - double value = wrenGetArgumentDouble(vm, 1); - double integer; - double fractional = modf(value, &integer); - wrenReturnDouble(vm, integer); -} - -static void mathFrac(WrenVM* vm) -{ - double value = wrenGetArgumentDouble(vm, 1); - double integer; - double fractional = modf(value, &integer); - wrenReturnDouble(vm, fractional); -} - -static void mathSin(WrenVM* vm) -{ - double angle = wrenGetArgumentDouble(vm, 1); - double sine = sin(angle); - wrenReturnDouble(vm, sine); -} - -static void mathCos(WrenVM* vm) -{ - double angle = wrenGetArgumentDouble(vm, 1); - double cosine = cos(angle); - wrenReturnDouble(vm, cosine); -} - -static void mathTan(WrenVM* vm) -{ - double angle = wrenGetArgumentDouble(vm, 1); - double tangent = tan(angle); - wrenReturnDouble(vm, tangent); -} - -static void mathDeg(WrenVM* vm) -{ - double radians = wrenGetArgumentDouble(vm, 1); - double degrees = floor(radians * 57.2957795130823208768); - wrenReturnDouble(vm, degrees); -} - -static void mathRad(WrenVM* vm) -{ - double degrees = wrenGetArgumentDouble(vm, 1); - double radians = floor(degrees / 57.2957795130823208768); - wrenReturnDouble(vm, radians); -} - -static void mathSrand(WrenVM* vm) -{ - time_t now = time(NULL); - _seed = (int32_t)now; - wrenReturnNull(vm); -} - -static void mathRand(WrenVM* vm) -{ - // https://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/ - _seed = (214013 * _seed + 2531011); - int16_t value = (_seed >> 16) & 0x7FFF; - double result = (double)value / (double)(INT16_MAX - 1); - wrenReturnDouble(vm, result); -} - -void wrenLoadMathLibrary(WrenVM* vm) -{ - wrenDefineStaticMethod(vm, "Math", "abs", 1, mathAbs); - wrenDefineStaticMethod(vm, "Math", "ceil", 1, mathCeil); - wrenDefineStaticMethod(vm, "Math", "floor", 1, mathFloor); - wrenDefineStaticMethod(vm, "Math", "int", 1, mathInt); - wrenDefineStaticMethod(vm, "Math", "frac", 1, mathFrac); - wrenDefineStaticMethod(vm, "Math", "sin", 1, mathSin); - wrenDefineStaticMethod(vm, "Math", "cos", 1, mathCos); - wrenDefineStaticMethod(vm, "Math", "tan", 1, mathTan); - wrenDefineStaticMethod(vm, "Math", "deg", 1, mathDeg); - wrenDefineStaticMethod(vm, "Math", "rad", 1, mathRad); - wrenDefineStaticMethod(vm, "Math", "srand", 0, mathSrand); - wrenDefineStaticMethod(vm, "Math", "rand", 0, mathRand); -} - -#endif diff --git a/src/wren_math.h b/src/wren_math.h deleted file mode 100644 index 7f90942e..00000000 --- a/src/wren_math.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef wren_math_h -#define wren_math_h - -#include "wren.h" -#include "wren_common.h" - -// This module defines the Ramdp, class and its associated methods. The RNG is -// based upon "xorshift". -#if WREN_USE_LIB_MATH - -void wrenLoadMathLibrary(WrenVM* vm); - -#endif - -#endif diff --git a/src/wren_vm.c b/src/wren_vm.c index 4f6fed24..f42c23cf 100644 --- a/src/wren_vm.c +++ b/src/wren_vm.c @@ -74,9 +74,6 @@ WrenVM* wrenNewVM(WrenConfiguration* configuration) #if WREN_USE_LIB_IO wrenLoadIOLibrary(vm); #endif - #if WREN_USE_LIB_IO - wrenLoadMathLibrary(vm); - #endif return vm; } diff --git a/test/math/abs.wren b/test/math/abs.wren deleted file mode 100644 index 92f4fae6..00000000 --- a/test/math/abs.wren +++ /dev/null @@ -1,8 +0,0 @@ -IO.print(Math.abs(123)) // expect: 123 -IO.print(Math.abs(-123)) // expect: 123 -IO.print(Math.abs(0)) // expect: 0 -IO.print(Math.abs(-0)) // expect: 0 -IO.print(Math.abs(-0.12)) // expect: 0.12 -IO.print(Math.abs(12.34)) // expect: 12.34 -IO.print(Math.abs(1.0)) // expect: 1 -IO.print(Math.abs(-1.0)) // expect: 1 diff --git a/test/math/all_tests.wren b/test/math/all_tests.wren deleted file mode 100644 index 4f4d9959..00000000 --- a/test/math/all_tests.wren +++ /dev/null @@ -1,40 +0,0 @@ -IO.print("abs") -IO.print(Math.abs(123)) // expect: 123 -IO.print(Math.abs(-123)) // expect: 123 -IO.print(Math.abs(0)) // expect: 0 -IO.print(Math.abs(-0)) // expect: 0 -IO.print(Math.abs(-0.12)) // expect: 0.12 -IO.print(Math.abs(12.34)) // expect: 12.34 - -IO.print(Math.abs(1.0)) // expect: 1 -IO.print(Math.abs(-1.0)) // expect: 1 - -IO.print("ceil") -IO.print(Math.ceil(2.3)) // expect: 3 -IO.print(Math.ceil(3.8)) // expect: 4 -IO.print(Math.ceil(-2.3)) // expect: -2 -IO.print(Math.ceil(-3.8)) // expect: -3 - -IO.print("floor") -IO.print(Math.floor(2.3)) // expect: 2 -IO.print(Math.floor(3.8)) // expect: 3 -IO.print(Math.floor(-2.3)) // expect: -3 -IO.print(Math.floor(-3.8)) // expect: -4 - -IO.print("int") -IO.print(Math.int(8)) // expect: 8 -IO.print(Math.int(12.34)) // expect: 12 -IO.print(Math.int(-8)) // expect: -8 -IO.print(Math.int(-12.34)) // expect: -12 - -IO.print("frac") -IO.print(Math.frac(8)) // expect: 0 -IO.print(Math.frac(12.34)) // expect: 0.34 -IO.print(Math.frac(-8)) // expect: -0 -IO.print(Math.frac(-12.34)) // expect: -0.34 - -IO.print("srand/rand") -Math.srand -for (i in 1..10) { - IO.print(Math.floor(Math.rand * 100)) -} diff --git a/test/math/ceil.wren b/test/math/ceil.wren deleted file mode 100644 index 6e8c3dfd..00000000 --- a/test/math/ceil.wren +++ /dev/null @@ -1,4 +0,0 @@ -IO.print(Math.ceil(2.3)) // expect: 3 -IO.print(Math.ceil(3.8)) // expect: 4 -IO.print(Math.ceil(-2.3)) // expect: -2 -IO.print(Math.ceil(-3.8)) // expect: -3 diff --git a/test/math/floor.wren b/test/math/floor.wren deleted file mode 100644 index 687ba088..00000000 --- a/test/math/floor.wren +++ /dev/null @@ -1,4 +0,0 @@ -IO.print(Math.floor(2.3)) // expect: 2 -IO.print(Math.floor(3.8)) // expect: 3 -IO.print(Math.floor(-2.3)) // expect: -3 -IO.print(Math.floor(-3.8)) // expect: -4 diff --git a/test/math/frac.wren b/test/math/frac.wren deleted file mode 100644 index 04506f6d..00000000 --- a/test/math/frac.wren +++ /dev/null @@ -1,4 +0,0 @@ -IO.print(Math.frac(8)) // expect: 0 -IO.print(Math.frac(12.34)) // expect: 0.34 -IO.print(Math.frac(-8)) // expect: -0 -IO.print(Math.frac(-12.34)) // expect: -0.34 diff --git a/test/math/int.wren b/test/math/int.wren deleted file mode 100644 index a78a5037..00000000 --- a/test/math/int.wren +++ /dev/null @@ -1,4 +0,0 @@ -IO.print(Math.int(8)) // expect: 8 -IO.print(Math.int(12.34)) // expect: 12 -IO.print(Math.int(-8)) // expect: -8 -IO.print(Math.int(-12.34)) // expect: -12 diff --git a/test/math/rand.wren b/test/math/rand.wren deleted file mode 100644 index 890d9a6e..00000000 --- a/test/math/rand.wren +++ /dev/null @@ -1,4 +0,0 @@ -Math.srand -for (i in 1..10) { - IO.print(Math.floor(Math.rand * 100)) -}