]> code.bitgloo.com Git - clyne/interpreter.git/commitdiff
single-letter vars, size & append, string indexing
authorClyne Sullivan <tullivan99@gmail.com>
Tue, 24 Apr 2018 18:13:14 +0000 (14:13 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Tue, 24 Apr 2018 18:13:14 +0000 (14:13 -0400)
12 files changed:
builtins.c
ops.c
parser.c
test1 [deleted file]
test2 [deleted file]
test3 [deleted file]
test4 [deleted file]
test5 [deleted file]
test6 [deleted file]
test7 [deleted file]
test8 [deleted file]
test9 [deleted file]

index e3be39c76b33061ca57e3af249cb3e04ea173fe9..ae1b00c249dd1f7d1a94e0947a2703275c8cf18b 100644 (file)
@@ -35,6 +35,7 @@
 #include "builtins.h"
 
 #include <stdlib.h>
+#include <string.h>
 
 #define IF_SIG    (uint32_t)-1
 #define WHILE_SIG (uint32_t)-2
@@ -56,6 +57,8 @@ int bn_while(instance *it);
 int bn_func(instance *it);
 int bn_solve(instance *it);
 int bn_array(instance *it);
+int bn_size(instance *it);
+int bn_append(instance *it);
 
 void iload_builtins(instance *it)
 {
@@ -65,6 +68,8 @@ void iload_builtins(instance *it)
        inew_cfunc(it, "func", bn_func);
        inew_cfunc(it, "solve", bn_solve);
        inew_cfunc(it, "array", bn_array);
+       inew_cfunc(it, "size", bn_size);
+       inew_cfunc(it, "append", bn_append);
 }
 
 /**
@@ -201,3 +206,46 @@ int bn_array(instance *it)
        a->value.p = (uint32_t)array;
        return 0;
 }
+
+int bn_size(instance *it)
+{
+       variable *a = igetarg(it, 0);
+       float f;
+       if (a->type == STRING)
+               f = strlen((char *)a->value.p);
+       else
+               f = a->array;
+       ipush(it, (uint32_t)make_varf(0, f));
+       return 0;
+}
+
+int bn_append(instance *it)
+{
+       variable *a = igetarg(it, 0);
+       variable *b = igetarg(it, 1);
+
+       if (a->type != STRING)
+               return 0;
+
+       if (b->type == NUMBER) {
+               int len = strlen((char *)a->value.p);
+               char *newstr = malloc(len + 2);
+               memcpy(newstr, (char *)a->value.p, len);
+               newstr[len] = b->value.f;
+               newstr[len + 1] = '\0';
+               free((void *)a->value.p);
+               a->value.p = (uint32_t)newstr;
+       } else if (b->type == STRING) {
+               int len1 = strlen((char *)a->value.p);
+               int len2 = strlen((char *)b->value.p);
+               char *newstr = malloc(len1 + len2);
+               memcpy(newstr, (char *)a->value.p, len1);
+               memcpy(newstr + len1, (char *)b->value.p, len2);
+               newstr[len1 + len2] = '\0';
+               free((void *)a->value.p);
+               a->value.p = (uint32_t)newstr;
+       }
+
+       ipush(it, (uint32_t)a);
+       return 0;
+}
diff --git a/ops.c b/ops.c
index f79503fcc62e1be73beac4329199c03fa984c8e1..bd22c403bf21a5e84f724b32f023c25cceeb4dfc 100644 (file)
--- a/ops.c
+++ b/ops.c
@@ -82,7 +82,7 @@ const char *opnames[] = {
        "+", "-", "<<", ">>",
        "<=", "<", ">=", ">",
        "==", "!=", "&", "^",
-       "|", "=" 
+       "|", "="
 };
 
 variable *igetop(const char *name, int *retlen)
@@ -102,8 +102,28 @@ variable *igetop(const char *name, int *retlen)
 
 OP_DEF(idx)
 {
-       if (a->array == 0)
-               return seterror(EBADPARAM);
+       if (a->array == 0) {
+               if (a->type != STRING)
+                       return seterror(EBADPARAM);
+
+               // string index
+               int idx = b->value.f;
+               for (int i = 0; i <= idx; i++) {
+                       if (((char *)a->value.p)[i] == '\0') {
+                               return seterror(EBADPARAM);
+                               /*// new buf
+                               char *newbuf = malloc(idx + 2);
+                               newbuf[idx + 1] = '\0';
+                               memcpy(newbuf, (char *)a->value.p, i);
+                               free((void *)a->value.p);
+                               break;*/
+                       }
+               }
+
+               (*r)->type = NUMBER;
+               (*r)->value.f = ((char *)a->value.p)[idx];
+               return 0;
+       }
 
        extern void itryfree(variable *);
        itryfree(*r);
index 2ae390b33d5df2f20f11856b551258532d693cef..279bb6e14114a639aae55f521040dcd070f2904c 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -404,8 +404,10 @@ variable **iparse(instance *it, const char *s)
                // variable or function
                if (isalpha(s[offset])) {
                        size_t end = offset + 1;
-                       while (isalnum(s[end]))
-                               end++;
+                       if (!isupper(s[offset])) {
+                               while (isalnum(s[end]))
+                                       end++;
+                       }
                        char *name = strnclone(s + offset, end - offset);
                        ops[ooffset++] = igetvar(it, name);
                        free(name);
diff --git a/test1 b/test1
deleted file mode 100644 (file)
index cc04423..0000000
--- a/test1
+++ /dev/null
@@ -1,18 +0,0 @@
-# test1
-# arithmetic tests
-# looking for proper basic function, respect for order of ops,
-# and respect for parentheses
-
-print(2 + 5)
-print(14 - 9)
-print(3 * 8 + 3)
-print(9 - 3 / 2)
-print(3 * (8 + 3))
-print((9 - 3) / 2)
-print((4 + 5) * ((9 - 1) + 3))
-print(5 - 3 + 4)
-print("")
-print(-4)
-print(-4 + -3)
-print(-8+13)
-print(4- -9)
diff --git a/test2 b/test2
deleted file mode 100644 (file)
index 3a03b15..0000000
--- a/test2
+++ /dev/null
@@ -1,17 +0,0 @@
-# test2
-# variable and function tests
-# show variable recognition and proper c-function handling
-
-print(a * 1)
-print(3 + b)
-
-print(a = 5)
-print(a * 1)
-
-print(c = 4)
-print(a / c)
-
-print(b = 2)
-print(d = 8)
-
-print(d + (e = 4))
diff --git a/test3 b/test3
deleted file mode 100644 (file)
index 6bf6064..0000000
--- a/test3
+++ /dev/null
@@ -1,25 +0,0 @@
-# test3
-# verify builtin functions, conditionals and such
-
-a = 5
-
-func(checka) {
-       if (a == 5) {
-               print("a == 5")
-       } else {
-               print("a != 5")
-       }
-}
-
-checka()
-
-print("Increment a...")
-a = a + 1
-checka()
-
-d = 0
-while (d < 20) {
-       print(d)
-       d = d + 1
-}
-
diff --git a/test4 b/test4
deleted file mode 100644 (file)
index b44dae1..0000000
--- a/test4
+++ /dev/null
@@ -1,13 +0,0 @@
-# test4
-# find memory leaks
-
-#x = 4
-#y = solve("x-2")
-#print(y)
-#print("")
-
-#y = solve("3*x")
-#print(y)
-
-y = solve("-")
-#y = solve("x*x*x*x*x")
diff --git a/test5 b/test5
deleted file mode 100644 (file)
index 74ce439..0000000
--- a/test5
+++ /dev/null
@@ -1,10 +0,0 @@
-# test5
-# solver test
-
-while (1) {
-       input = gets
-       if (input == "exit") {
-               exit
-       }
-       print(solve(input))
-}
diff --git a/test6 b/test6
deleted file mode 100644 (file)
index bc17de2..0000000
--- a/test6
+++ /dev/null
@@ -1,10 +0,0 @@
-# test6
-# used to fix memory leak in setting defined strings
-
-test = "hello"
-test = "fail"
-a = test
-test = "acd"
-
-print(a)
-print(test)
diff --git a/test7 b/test7
deleted file mode 100644 (file)
index dd4defb..0000000
--- a/test7
+++ /dev/null
@@ -1,14 +0,0 @@
-j = 6
-
-func(test) {
-       i = 0
-       while (i < 10) {
-               i = i + 1
-       }
-
-       j = 15
-}
-
-print(i)
-print(" ")
-print(j)
diff --git a/test8 b/test8
deleted file mode 100644 (file)
index 8081b4c..0000000
--- a/test8
+++ /dev/null
@@ -1,17 +0,0 @@
-array(a, 10)
-
-i = 0
-while (i < 10) {
-       a.i = i * 2
-       i = i + 1
-}
-
-i = 0
-while (i < 10) {
-       print(a.i)
-       i = i + 1
-}
-
-a.20 = 143
-print(a.20)
-print(a.19)
diff --git a/test9 b/test9
deleted file mode 100644 (file)
index 3cfcd55..0000000
--- a/test9
+++ /dev/null
@@ -1,7 +0,0 @@
-x = 9
-print(3x)
-print(2x + 5)
-print(x - 9x)
-print(5 + (3x))
-print((x - 4 * 5)2)
-