summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2024-03-25 22:16:48 -0400
committerKyle Gunger <kgunger12@gmail.com>2024-03-25 22:16:48 -0400
commit89a27e4159b2b01be96b5f8ca51832766e878c51 (patch)
tree6fdef2ee921598dfc44f1cf6e72ab555f0e20876
parent12679f9be4bd3a924ca0859a7ad133178513bace (diff)
len fix for variables
-rw-r--r--compiler.c26
-rw-r--r--tests/test_len.tnsl20
-rw-r--r--tnslc/iterator.tnsl39
-rw-r--r--tnslc/main.tnsl8
4 files changed, 41 insertions, 52 deletions
diff --git a/compiler.c b/compiler.c
index 05dbc99..cd6709d 100644
--- a/compiler.c
+++ b/compiler.c
@@ -2445,14 +2445,14 @@ bool tok_eq(Token *a, Token *b) {
#define TT_DELIMIT 5
#define TT_SPLITTR 6
-char *KEYWORDS = "module,export,asm,if,else,loop,label,goto,continue,break,return,import,as,using,struct,method,interface,enum,implements,operator,len,is";
+char *KEYWORDS = "module,export,asm,if,else,loop,label,goto,continue,break,return,import,as,using,struct,method,interface,enum,implements,operator,is";
char *KEYTYPES = "uint8,uint16,uint32,uint64,uint,int8,int16,int32,int64,int,float32,float64,float,comp64,comp,bool,vect,void,type";
char *LITERALS = "false,true";
char *RESERVED = "~`!@#$%^&*()[]{}+-=\"\'\\|:;/?>.<,";
char *OPS = "~`!%&|^*/+-=.<>@";
-char *MULTI_OPS = "==,&&,||,^^,!==,!&&,!||,!^^,!<,!>,<<,>>,!&,!|,!^,++,--,>==,<==,+=,-=,*=,/=,%=,!=,&=,|=,^=,~=,`=";
+char *MULTI_OPS = "==,&&,||,^^,!==,!&&,!||,!^^,!<,!>,<<,>>,!&,!|,!^,++,--,>==,<==,+=,-=,*=,/=,%=,!=,&=,|=,^=,~=,`=,len";
char *DELIMS = "()[]{}";
char *MULTI_DELIMS = ";:#";
@@ -2503,17 +2503,13 @@ int token_type(char*data) {
if (is_delim(data))
return TT_DELIMIT;
- else if (is_reserved(data[0])) {
- if (l == 1) {
- if (strchr(OPS, data[0]) != NULL)
- return TT_AUGMENT;
- else if (data[0] == ',' || data[0] == ';' || data[0] == ':')
- return TT_SPLITTR;
- } else if (l == 2 && in_csv(MULTI_OPS, data)) {
+ else if (is_reserved(data[0]) && l == 1) {
+ if (strchr(OPS, data[0]) != NULL)
return TT_AUGMENT;
- } else if (l == 3 && in_csv(MULTI_OPS, data)) {
+ else if (data[0] == ',' || data[0] == ';' || data[0] == ':')
+ return TT_SPLITTR;
+ } else if (in_csv(MULTI_OPS, data)) {
return TT_AUGMENT;
- }
} else if (in_csv(KEYTYPES, data)) {
return TT_KEYTYPE;
} else if (in_csv(KEYWORDS, data)) {
@@ -4651,7 +4647,7 @@ Variable _eval_dot(Scope *s, CompData *data, Vector *tokens, size_t start, size_
if (start == end - 1) {
Variable v = scope_get_var(s, &name);
if (v.name == NULL) {
- printf("ERROR: Failed to find variable or call in dot chain \"%s\" (%d:%d)\n\n", t->data, t->line, t->col);
+ printf("ERROR: Failed to find singlet variable or call in dot chain \"%s\" (%d:%d)\n\n", t->data, t->line, t->col);
p2_error = true;
}
art_end(&name);
@@ -4934,6 +4930,12 @@ Variable _eval(Scope *s, CompData *data, Vector *tokens, size_t start, size_t en
var_op_not(data, &out);
return out;
+ } else if (tok_str_eq(op_token, "len")){
+ out = var_init("#literal", typ_get_inbuilt("uint"));
+ out.location = LOC_LITL;
+ out.offset = rhs.type->size;
+ var_end(&rhs);
+ return out;
} else {
printf("ERROR: Unexpected prefix token\n");
p2_error = true;
diff --git a/tests/test_len.tnsl b/tests/test_len.tnsl
new file mode 100644
index 0000000..a23b4df
--- /dev/null
+++ b/tests/test_len.tnsl
@@ -0,0 +1,20 @@
+struct A {
+ int i, j, k, l
+}
+
+struct B {
+ uint a, b, c, d
+}
+
+struct C {
+ bool x,
+ uint32 y
+}
+
+/; main [int]
+ A a
+ B b
+ C c
+ return len a + len b + len c
+;/
+
diff --git a/tnslc/iterator.tnsl b/tnslc/iterator.tnsl
deleted file mode 100644
index 19dda8c..0000000
--- a/tnslc/iterator.tnsl
+++ /dev/null
@@ -1,39 +0,0 @@
-struct Iterator {
- ~void data,
- int size,
- current,
- _elsz
-}
-
-/; method Iterator
-
- /; init (~void data, int count, int elsz)
- self.data = data
- self.size = size
- self._elsz = elsz
- self.current = 0
- ;/
-
- /; next (int count)
- self.current = self.current + count
- /; if (self.current >= self.size)
- self.current = self.size
- ;/
- ;/
-
- /; prev (int count)
- self.current = self.current - count
- /; if (self.current < 0)
- self.current = 0
- ;/
- ;/
-
- /; get [~void]
- return self.data + current * _elsz
- ;/
-
- /; end [bool]
- return self.current >= self.size
- ;/
-
-;/
diff --git a/tnslc/main.tnsl b/tnslc/main.tnsl
index cf2e8ac..db68650 100644
--- a/tnslc/main.tnsl
+++ b/tnslc/main.tnsl
@@ -5,7 +5,13 @@
a.push(~c)
;/
-/; main [int]
+/; main (int argc, ~~uint8 argv) [int]
+
+ asm "mov r10, rdi"
+ asm "mov r11, rsi"
+
+ _print_num(PUSH_STR, argc)
+
Vector a
a.init(1)