diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2018-04-24 14:13:14 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2018-04-24 14:13:14 -0400 |
commit | b761569b3aabfb9cd2034d737d4223de534d3dcb (patch) | |
tree | 422cf96fb9fcc08307ecccccc190230c778a8e36 | |
parent | 7f64da46ffe3b2e2a2b46a3dd2effc4f4f7d45da (diff) |
single-letter vars, size & append, string indexing
-rw-r--r-- | builtins.c | 48 | ||||
-rw-r--r-- | ops.c | 26 | ||||
-rw-r--r-- | parser.c | 6 | ||||
-rw-r--r-- | test1 | 18 | ||||
-rw-r--r-- | test2 | 17 | ||||
-rw-r--r-- | test3 | 25 | ||||
-rw-r--r-- | test4 | 13 | ||||
-rw-r--r-- | test5 | 10 | ||||
-rw-r--r-- | test6 | 10 | ||||
-rw-r--r-- | test7 | 14 | ||||
-rw-r--r-- | test8 | 17 | ||||
-rw-r--r-- | test9 | 7 |
12 files changed, 75 insertions, 136 deletions
@@ -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; +} @@ -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); @@ -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); @@ -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) @@ -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)) @@ -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 -} - @@ -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") @@ -1,10 +0,0 @@ -# test5 -# solver test - -while (1) { - input = gets - if (input == "exit") { - exit - } - print(solve(input)) -} @@ -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) @@ -1,14 +0,0 @@ -j = 6 - -func(test) { - i = 0 - while (i < 10) { - i = i + 1 - } - - j = 15 -} - -print(i) -print(" ") -print(j) @@ -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) @@ -1,7 +0,0 @@ -x = 9 -print(3x) -print(2x + 5) -print(x - 9x) -print(5 + (3x)) -print((x - 4 * 5)2) - |