aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2018-04-24 14:13:14 -0400
committerClyne Sullivan <tullivan99@gmail.com>2018-04-24 14:13:14 -0400
commitb761569b3aabfb9cd2034d737d4223de534d3dcb (patch)
tree422cf96fb9fcc08307ecccccc190230c778a8e36
parent7f64da46ffe3b2e2a2b46a3dd2effc4f4f7d45da (diff)
single-letter vars, size & append, string indexing
-rw-r--r--builtins.c48
-rw-r--r--ops.c26
-rw-r--r--parser.c6
-rw-r--r--test118
-rw-r--r--test217
-rw-r--r--test325
-rw-r--r--test413
-rw-r--r--test510
-rw-r--r--test610
-rw-r--r--test714
-rw-r--r--test817
-rw-r--r--test97
12 files changed, 75 insertions, 136 deletions
diff --git a/builtins.c b/builtins.c
index e3be39c..ae1b00c 100644
--- a/builtins.c
+++ b/builtins.c
@@ -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 f79503f..bd22c40 100644
--- 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);
diff --git a/parser.c b/parser.c
index 2ae390b..279bb6e 100644
--- 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
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
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
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
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
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
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
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
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
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)
-