Browse Source

start on math library

Kajetan Johannes Hammerle 3 years ago
parent
commit
30161cbb64
7 changed files with 82 additions and 6 deletions
  1. 2 0
      Main.c
  2. 29 0
      libraries/Math.c
  3. 6 0
      libraries/Math.h
  4. 0 5
      libraries/Time.c
  5. 6 1
      meson.build
  6. 26 0
      tests/system/math
  7. 13 0
      tests/system/math.out

+ 2 - 0
Main.c

@@ -4,6 +4,7 @@
 
 #include "Compiler.h"
 #include "Test.h"
+#include "libraries/Math.h"
 #include "libraries/Time.h"
 #include "tokenizer/Tokenizer.h"
 #include "utils/Functions.h"
@@ -43,6 +44,7 @@ int main(int argAmount, const char** args) {
     gfsInit();
     gstsInit();
     lTimeRegister();
+    lMathRegister();
     start(argAmount, args);
     gstsDelete();
     gfsDelete();

+ 29 - 0
libraries/Math.c

@@ -0,0 +1,29 @@
+#include <math.h>
+
+#include "libraries/Math.h"
+#include "utils/Functions.h"
+
+#define FLOAT_FUNCTION(Name, Function)                                         \
+    static void l##Name(Script* sc) {                                          \
+        float f;                                                               \
+        if(sPopFloat(sc, &f)) {                                                \
+            sPushFloat(sc, Function(f));                                       \
+        }                                                                      \
+    }
+
+FLOAT_FUNCTION(Sin, sinf)
+FLOAT_FUNCTION(Cos, cosf)
+FLOAT_FUNCTION(Tan, tanf)
+
+void lAddFloatFunction(const char* name, ScriptFunction sf) {
+    Function f;
+    gfInit(&f, name, dtFloat(), sf);
+    gfAddArgument(&f, dtFloat());
+    gfsAdd(&f);
+}
+
+void lMathRegister() {
+    lAddFloatFunction("sin", lSin);
+    lAddFloatFunction("cos", lCos);
+    lAddFloatFunction("tan", lTan);
+}

+ 6 - 0
libraries/Math.h

@@ -0,0 +1,6 @@
+#ifndef MATH_H
+#define MATH_H
+
+void lMathRegister();
+
+#endif

+ 0 - 5
libraries/Time.c

@@ -12,11 +12,6 @@ static void lGetMillis(Script* sc) {
         sError(sc, "cannot get clock time: %s", strerror(errno));
         return;
     }
-
-    // time_t t = (time.tv_nsec / 1000000L + time.tv_sec * 1000L) / 1000l;
-    // struct tm* wusi = localtime(&t);
-    // wusi->printf("%d\n", wusi->tm_hour);
-
     sPushInt64(sc, time.tv_nsec / 1000000L + time.tv_sec * 1000L);
 }
 

+ 6 - 1
meson.build

@@ -15,9 +15,14 @@ src = [
     'vm/ByteCode.c',
     'vm/Script.c', 
     'vm/Arrays.c',
-    'libraries/Time.c'
+    'libraries/Time.c',
+    'libraries/Math.c'
 ]
 
+cc = meson.get_compiler('c')
+mathDep = cc.find_library('m', required : true)
+
 executable('lonely_tiger', 
     sources: src,
+    dependencies : mathDep,
     c_args: ['-Wall', '-Wextra', '-pedantic', '-Werror', '-O3'])

+ 26 - 0
tests/system/math

@@ -0,0 +1,26 @@
+const float PI = 3.14159265358979323846;
+
+float abs(float f) {
+    if(f < 0.0) {
+        return -f;
+    }
+    return f;
+}
+
+void main() {
+    test(abs(sin(0.0 * PI)));
+    test(sin(0.5 * PI));
+    test(abs(sin(1.0 * PI)));
+    test(sin(1.5 * PI));
+    test(abs(sin(2.0 * PI)));
+    
+    test(cos(0.0 * PI));
+    test(abs(cos(0.5 * PI)));
+    test(cos(1.0 * PI));
+    test(abs(cos(1.5 * PI)));
+    test(cos(2.0 * PI));
+    
+    test(abs(tan(0.0 * PI)));
+    test(abs(tan(1.0 * PI)));
+    test(abs(tan(2.0 * PI)));
+}

+ 13 - 0
tests/system/math.out

@@ -0,0 +1,13 @@
+0.00
+1.00
+0.00
+-1.00
+0.00
+1.00
+0.00
+-1.00
+0.00
+1.00
+0.00
+0.00
+0.00