Browse Source

Sol Part 7: The Update that Saved the World

Grissess 6 years ago
parent
commit
406a03c15e
10 changed files with 9053 additions and 9845 deletions
  1. 97
    6
      builtins.c
  2. 242
    0
      cdata.c
  3. 14
    5
      cdata.h
  4. 28
    5
      object.c
  5. 8030
    9258
      parser.output
  6. 520
    536
      parser.tab.c
  7. 28
    34
      parser.y
  8. 10
    1
      sol.h
  9. 8
    0
      state.c
  10. 76
    0
      test.sol

+ 97
- 6
builtins.c View File

@@ -646,8 +646,15 @@ sol_object_t *sol_f_list_mul(sol_state_t *state, sol_object_t *args) {
646 646
 }
647 647
 
648 648
 sol_object_t *sol_f_list_index(sol_state_t *state, sol_object_t *args) {
649
-	sol_object_t *ls = sol_list_get_index(state, args, 0), *b = sol_cast_int(state, sol_list_get_index(state, args, 1));
650
-	sol_object_t *res = sol_list_get_index(state, ls, b->ival);
649
+	sol_object_t *ls = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *ival;
650
+	sol_object_t *res;
651
+	if(sol_is_string(b)) {
652
+        res = sol_map_get(state, state->ListFuncs, b);
653
+	} else {
654
+        ival = sol_cast_int(state, b);
655
+        res = sol_list_get_index(state, ls, ival->ival);
656
+        if(ival!=b) sol_obj_free(ival);
657
+	}
651 658
 	sol_obj_free(ls);
652 659
 	sol_obj_free(b);
653 660
 	return res;
@@ -678,6 +685,81 @@ sol_object_t *sol_f_list_tostring(sol_state_t *state, sol_object_t *args) {
678 685
 	return sol_new_string(state, "<List>");
679 686
 }
680 687
 
688
+sol_object_t *sol_f_list_copy(sol_state_t *state, sol_object_t *args) {
689
+    sol_object_t *list = sol_list_get_index(state, args, 0);
690
+    sol_object_t *res = sol_list_copy(state, list);
691
+    sol_obj_free(list);
692
+    return res;
693
+}
694
+
695
+sol_object_t *sol_f_list_insert(sol_state_t *state, sol_object_t *args) {
696
+    sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_cast_int(state, sol_list_get_index(state, args, 1)), *obj = sol_list_get_index(state, args, 2);
697
+    sol_list_insert(state, list, idx->ival, obj);
698
+    sol_obj_free(list);
699
+    return sol_incref(state->None);
700
+}
701
+
702
+sol_object_t *sol_f_list_remove(sol_state_t *state, sol_object_t *args) {
703
+    sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_cast_int(state, sol_list_get_index(state, args, 1));
704
+    sol_list_remove(state, list, idx->ival);
705
+    sol_obj_free(list);
706
+    return sol_incref(state->None);
707
+}
708
+
709
+sol_object_t *sol_f_list_truncate(sol_state_t *state, sol_object_t *args) {
710
+    sol_object_t *list = sol_list_get_index(state, args, 0), *idx = sol_list_get_index(state, args, 1);
711
+    sol_object_t *res = sol_list_truncate(state, list, idx->ival);
712
+    sol_obj_free(list);
713
+    return res;
714
+}
715
+
716
+sol_object_t *sol_f_list_map(sol_state_t *state, sol_object_t *args) {
717
+    sol_object_t *list = sol_list_get_index(state, args, 0), *func = sol_list_get_index(state, args, 1);
718
+    sol_object_t *fargs = sol_new_list(state), *item;
719
+    int idx=0, len = sol_list_len(state, list);
720
+    sol_list_insert(state, fargs, 0, func);
721
+    while(idx<len) {
722
+        item = sol_list_get_index(state, list, idx);
723
+        sol_list_insert(state, fargs, 1, item);
724
+        sol_obj_free(item);
725
+        item = func->ops->call(state, fargs);
726
+        if(sol_has_error(state)) return list;
727
+        sol_list_remove(state, fargs, 1);
728
+        sol_list_set_index(state, list, idx, item);
729
+        sol_obj_free(item);
730
+        idx++;
731
+    }
732
+    sol_obj_free(fargs);
733
+    sol_obj_free(func);
734
+    return list;
735
+}
736
+
737
+sol_object_t *sol_f_list_filter(sol_state_t *state, sol_object_t *args) {
738
+    sol_object_t *list = sol_list_get_index(state, args, 0), *func = sol_list_get_index(state, args, 1);
739
+    sol_object_t *fargs = sol_new_list(state), *item, *ival;
740
+    int idx=0, len = sol_list_len(state, list);
741
+    sol_list_insert(state, fargs, 0, func);
742
+    while(idx<len) {
743
+        item = sol_list_get_index(state, list, idx);
744
+        sol_list_insert(state, fargs, 1, item);
745
+        sol_obj_free(item);
746
+        item = func->ops->call(state, fargs);
747
+        if(sol_has_error(state)) return list;
748
+        ival = sol_cast_int(state, item);
749
+        if(ival->ival) {
750
+            idx++;
751
+        } else {
752
+            sol_list_remove(state, list, idx);
753
+            len--;
754
+        }
755
+        if(ival!=item) sol_obj_free(item);
756
+        sol_obj_free(ival);
757
+    }
758
+    sol_obj_free(fargs);
759
+    sol_obj_free(func);
760
+    return list;
761
+}
762
+
681 763
 sol_object_t *sol_f_map_add(sol_state_t *state, sol_object_t *args) {
682 764
 	sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *map;
683 765
 	if(!sol_is_map(b)) {
@@ -697,14 +779,18 @@ sol_object_t *sol_f_map_index(sol_state_t *state, sol_object_t *args) {
697 779
 	sol_object_t *indexf = sol_map_get_name(state, map, "__index");
698 780
 	sol_object_t *res = NULL, *newls;
699 781
 	if(!sol_is_none(state, indexf)) {
700
-		if(indexf->ops->call && indexf->ops->call != sol_f_not_impl) {
782
+		if(indexf->ops->call && (sol_is_func(indexf) || sol_is_cfunc(indexf)) && indexf->ops->call != sol_f_not_impl) {
701 783
 			newls = sol_new_list(state);
702 784
 			sol_list_insert(state, newls, 0, indexf);
703 785
 			sol_list_append(state, newls, args);
704 786
 			res = indexf->ops->call(state, newls);
705 787
 			sol_obj_free(newls);
706 788
 		} else if(indexf->ops->index && indexf->ops->index != sol_f_not_impl) {
707
-			res = indexf->ops->index(state, args);
789
+            newls = sol_new_list(state);
790
+            sol_list_insert(state, newls, 0, indexf);
791
+            sol_list_insert(state, newls, 1, b);
792
+			res = indexf->ops->index(state, newls);
793
+			sol_obj_free(newls);
708 794
 		}
709 795
 	}
710 796
 	if(!res) res = sol_map_get(state, map, b);
@@ -719,7 +805,7 @@ sol_object_t *sol_f_map_setindex(sol_state_t *state, sol_object_t *args) {
719 805
 	sol_object_t *val = sol_list_get_index(state, args, 2);
720 806
 	sol_object_t *setindexf = sol_map_get_name(state, map, "__setindex"), *newls;
721 807
 	if(!sol_is_none(state, setindexf)) {
722
-		if(setindexf->ops->call && setindexf->ops->call != sol_f_not_impl) {
808
+		if(setindexf->ops->call && (sol_is_func(setindexf) || sol_is_cfunc(setindexf)) && setindexf->ops->call != sol_f_not_impl) {
723 809
 			newls = sol_new_list(state);
724 810
 			sol_list_insert(state, newls, 0, setindexf);
725 811
 			sol_list_append(state, newls, args);
@@ -727,7 +813,12 @@ sol_object_t *sol_f_map_setindex(sol_state_t *state, sol_object_t *args) {
727 813
 			sol_obj_free(newls);
728 814
 			return sol_incref(state->None);
729 815
 		} else if(setindexf->ops->setindex && setindexf->ops->setindex != sol_f_not_impl) {
730
-			setindexf->ops->setindex(state, args);
816
+			newls = sol_new_list(state);
817
+            sol_list_insert(state, newls, 0, setindexf);
818
+            sol_list_insert(state, newls, 1, b);
819
+            sol_list_insert(state, newls, 2, val);
820
+			sol_obj_free(setindexf->ops->index(state, newls));
821
+			sol_obj_free(newls);
731 822
 			return sol_incref(state->None);
732 823
 		}
733 824
 	}

+ 242
- 0
cdata.c View File

@@ -0,0 +1,242 @@
1
+#include "cdata.h"
2
+
3
+#include <stdint.h>
4
+#include <string.h>
5
+
6
+#define AS(arg, tp) ((tp *) (arg))
7
+#define AT(loc, tp, off) (*((tp *) ((char *) loc) + off))
8
+#define NEW(tp) malloc(sizeof(tp))
9
+#define ENSURE() if(!sol_cstruct_is_init) sol_cstruct_init()
10
+
11
+int sol_cstruct_is_init = 0;
12
+extern sol_ops_t sol_cstruct_spec_ops;
13
+extern sol_ops_t sol_cstruct_ops;
14
+
15
+void sol_cstruct_init(void) {
16
+    sol_ops_init(&sol_cstruct_spec_ops);
17
+    sol_ops_init(&sol_cstruct_ops);
18
+
19
+    sol_cstruct_ops.index = sol_f_cstruct_index;
20
+    sol_cstruct_ops.setindex = sol_f_cstruct_setindex;
21
+    sol_cstruct_is_init = 1;
22
+}
23
+
24
+sol_object_t *sol_new_cstruct_specs(sol_state_t *state) {
25
+    return sol_new_map(state);
26
+}
27
+
28
+sol_object_t *sol_new_cstruct_spec(sol_state_t *state, sol_spec_t type) {
29
+    ENSURE();
30
+    sol_object_t *res = sol_new_cdata(state, NEW(sol_memspec_t), &sol_cstruct_spec_ops);
31
+    AS(res->data, sol_memspec_t)->type = type;
32
+    return res;
33
+}
34
+
35
+void sol_cstruct_add_member(sol_state_t *state, sol_object_t *specs, sol_object_t *key, sol_memtype_t memtype, int offset) {
36
+    sol_object_t *spec = sol_new_cstruct_spec(state, SOL_CS_MEMBER);
37
+    AS(spec->data, sol_memspec_t)->memtype = memtype;
38
+    AS(spec->data, sol_memspec_t)->offset = offset;
39
+    sol_map_set(state, spec, key, spec);
40
+    sol_obj_free(spec);
41
+}
42
+
43
+void sol_cstruct_add_member_name(sol_state_t *state, sol_object_t *specs, char *name, sol_memtype_t memtype, int offset) {
44
+    sol_object_t *spec = sol_new_cstruct_spec(state, SOL_CS_MEMBER);
45
+    AS(spec->data, sol_memspec_t)->memtype = memtype;
46
+    AS(spec->data, sol_memspec_t)->offset = offset;
47
+    sol_map_set_name(state, spec, name, spec);
48
+    sol_obj_free(spec);
49
+}
50
+
51
+void sol_cstruct_add_func(sol_state_t *state, sol_object_t *specs, sol_object_t *key, sol_cfunc_t cfunc) {
52
+    sol_object_t *spec = sol_new_cstruct_spec(state, SOL_CS_CFUNC);
53
+    AS(spec->data, sol_memspec_t)->cfunc = cfunc;
54
+    sol_map_set(state, spec, key, spec);
55
+    sol_obj_free(spec);
56
+}
57
+
58
+void sol_cstruct_add_func_name(sol_state_t *state, sol_object_t *specs, char *name, sol_cfunc_t cfunc) {
59
+    sol_object_t *spec = sol_new_cstruct_spec(state, SOL_CS_CFUNC);
60
+    AS(spec->data, sol_memspec_t)->cfunc = cfunc;
61
+    sol_map_set_name(state, spec, name, spec);
62
+    sol_obj_free(spec);
63
+}
64
+
65
+sol_object_t *sol_new_cstruct(sol_state_t *state, void *data, sol_object_t *specs) {
66
+    sol_object_t *res = sol_new_cdata(state, NEW(sol_cstruct_t), &sol_cstruct_ops);
67
+    AS(res->data, sol_cstruct_t)->data = data;
68
+    AS(res->data, sol_cstruct_t)->specs = sol_incref(specs);
69
+    return res;
70
+}
71
+
72
+sol_object_t *sol_f_cstruct_index(sol_state_t *state, sol_object_t *args) {
73
+    sol_object_t *cstructobj = sol_list_get_index(state, args, 0), *idx = sol_list_get_index(state, args, 1);
74
+    sol_cstruct_t *cstruct = cstructobj->data;
75
+    sol_object_t *specobj = sol_map_get(state, AS(cstruct->data, sol_cstruct_t)->specs, idx), *res = NULL;
76
+    sol_memspec_t *spec;
77
+    char cbuf[2] = {0, 0};
78
+    if(sol_is_none(state, specobj)) {
79
+        sol_obj_free(specobj);
80
+        sol_obj_free(idx);
81
+        sol_obj_free(cstructobj);
82
+        return sol_set_error_string(state, "No such CStruct index");
83
+    }
84
+    spec = specobj->data;
85
+    switch(spec->type) {
86
+        case SOL_CS_MEMBER:
87
+            switch(spec->memtype) {
88
+                case SOL_INT:
89
+                    res = sol_new_int(state, AT(cstruct->data, int, spec->offset));
90
+                    break;
91
+
92
+                case SOL_INT8:
93
+                    res = sol_new_int(state, AT(cstruct->data, int8_t, spec->offset));
94
+                    break;
95
+
96
+                case SOL_INT16:
97
+                    res = sol_new_int(state, AT(cstruct->data, int16_t, spec->offset));
98
+                    break;
99
+
100
+                case SOL_INT32:
101
+                    res = sol_new_int(state, AT(cstruct->data, int32_t, spec->offset));
102
+                    break;
103
+
104
+                case SOL_INT64:
105
+                    res = sol_new_int(state, AT(cstruct->data, int64_t, spec->offset));
106
+                    break;
107
+
108
+                case SOL_UINT:
109
+                    res = sol_new_int(state, AT(cstruct->data, unsigned int, spec->offset));
110
+                    break;
111
+
112
+                case SOL_UINT8:
113
+                    res = sol_new_int(state, AT(cstruct->data, uint8_t, spec->offset));
114
+                    break;
115
+
116
+                case SOL_UINT16:
117
+                    res = sol_new_int(state, AT(cstruct->data, uint16_t, spec->offset));
118
+                    break;
119
+
120
+                case SOL_UINT32:
121
+                    res = sol_new_int(state, AT(cstruct->data, uint32_t, spec->offset));
122
+                    break;
123
+
124
+                /*case SOL_UINT64:
125
+                    res = sol_new_int(state, AT(cstruct->data, uint64_t, spec->offset));
126
+                    break;*/
127
+
128
+                case SOL_FLOAT:
129
+                    res = sol_new_float(state, AT(cstruct->data, float, spec->offset));
130
+                    break;
131
+
132
+                case SOL_DOUBLE:
133
+                    res = sol_new_float(state, AT(cstruct->data, double, spec->offset));
134
+                    break;
135
+
136
+                case SOL_CHAR:
137
+                    cbuf[0] = AT(cstruct->data, char, spec->offset);
138
+                    res = sol_new_string(state, cbuf);
139
+                    break;
140
+
141
+                case SOL_CSTR:
142
+                    res = sol_new_string(state, AT(cstruct->data, char *, spec->offset));
143
+                    break;
144
+
145
+                case SOL_CFUNC:
146
+                    res = sol_new_cfunc(state, AT(cstruct->data, sol_cfunc_t, spec->offset));
147
+                    break;
148
+            }
149
+            break;
150
+
151
+        case SOL_CS_CFUNC:
152
+            res = sol_new_cfunc(state, spec->cfunc);
153
+            break;
154
+    }
155
+    if(!res) res = sol_incref(state->None);
156
+    return res;
157
+}
158
+
159
+sol_object_t *sol_f_cstruct_setindex(sol_state_t *state, sol_object_t *args) {
160
+    sol_object_t *cstructobj = sol_list_get_index(state, args, 0), *idx = sol_list_get_index(state, args, 1), *val = sol_list_get_index(state, args, 2);
161
+    sol_cstruct_t *cstruct = cstructobj->data;
162
+    sol_object_t *specobj = sol_map_get(state, AS(cstruct->data, sol_cstruct_t)->specs, idx);
163
+    sol_memspec_t *spec;
164
+    char cbuf[2] = {0, 0};
165
+    if(sol_is_none(state, specobj)) {
166
+        sol_obj_free(specobj);
167
+        sol_obj_free(idx);
168
+        sol_obj_free(cstructobj);
169
+        return sol_set_error_string(state, "No such CStruct index");
170
+    }
171
+    spec = specobj->data;
172
+    switch(spec->type) {
173
+        case SOL_CS_MEMBER:
174
+            switch(spec->memtype) {
175
+                case SOL_INT:
176
+                    AT(cstruct->data, int, spec->offset) = sol_cast_int(state, val)->ival;
177
+                    break;
178
+
179
+                case SOL_INT8:
180
+                    AT(cstruct->data, int8_t, spec->offset) = sol_cast_int(state, val)->ival;
181
+                    break;
182
+
183
+                case SOL_INT16:
184
+                    AT(cstruct->data, int16_t, spec->offset) = sol_cast_int(state, val)->ival;
185
+                    break;
186
+
187
+                case SOL_INT32:
188
+                    AT(cstruct->data, int32_t, spec->offset) = sol_cast_int(state, val)->ival;
189
+                    break;
190
+
191
+                case SOL_INT64:
192
+                    AT(cstruct->data, int64_t, spec->offset) = sol_cast_int(state, val)->ival;
193
+                    break;
194
+
195
+                case SOL_UINT:
196
+                    AT(cstruct->data, unsigned int, spec->offset) = sol_cast_int(state, val)->ival;
197
+                    break;
198
+
199
+                case SOL_UINT8:
200
+                    AT(cstruct->data, uint8_t, spec->offset) = sol_cast_int(state, val)->ival;
201
+                    break;
202
+
203
+                case SOL_UINT16:
204
+                    AT(cstruct->data, uint16_t, spec->offset) = sol_cast_int(state, val)->ival;
205
+                    break;
206
+
207
+                case SOL_UINT32:
208
+                    AT(cstruct->data, uint32_t, spec->offset) = sol_cast_int(state, val)->ival;
209
+                    break;
210
+
211
+                /*case SOL_UINT64:
212
+                    AT(cstruct->data, uint64_t, spec->offset) = sol_cast_int(state, val)->ival;
213
+                    break;*/
214
+
215
+                case SOL_FLOAT:
216
+                    AT(cstruct->data, float, spec->offset) = sol_cast_float(state, val)->fval;
217
+                    break;
218
+
219
+                case SOL_DOUBLE:
220
+                    AT(cstruct->data, double, spec->offset) = sol_cast_float(state, val)->fval;
221
+                    break;
222
+
223
+                case SOL_CHAR:
224
+                    AT(cstruct->data, char, spec->offset) = sol_cast_string(state, val)->str[0];
225
+                    break;
226
+
227
+                case SOL_CSTR:
228
+                    AT(cstruct->data, char *, spec->offset) = strdup(sol_cast_string(state, val)->str);
229
+                    break;
230
+
231
+                case SOL_CFUNC:
232
+                    return sol_set_error_string(state, "Can't assign CFunc members");
233
+                    break;
234
+            }
235
+            break;
236
+
237
+        case SOL_CS_CFUNC:
238
+            return sol_set_error_string(state, "Can't assign CFunc members");
239
+            break;
240
+    }
241
+    return sol_incref(state->None);
242
+}

+ 14
- 5
cdata.h View File

@@ -19,12 +19,20 @@ typedef enum {
19 19
     SOL_CHAR,
20 20
     SOL_CSTR,
21 21
     SOL_CFUNC
22
-} sol_memtype_t
22
+} sol_memtype_t;
23
+
24
+typedef enum {
25
+    SOL_CS_MEMBER,
26
+    SOL_CS_CFUNC
27
+} sol_spec_t;
23 28
 
24 29
 typedef struct {
25
-    sol_memtype_t type;
30
+    sol_spec_t type;
26 31
     union {
27
-        int offset;
32
+        struct {
33
+            sol_memtype_t memtype;
34
+            int offset;
35
+        };
28 36
         sol_cfunc_t cfunc;
29 37
     };
30 38
 } sol_memspec_t;
@@ -35,13 +43,14 @@ typedef struct {
35 43
 } sol_cstruct_t;
36 44
 
37 45
 sol_object_t *sol_new_cstruct_specs(sol_state_t *);
38
-void sol_cstruct_add_member(sol_state_t *, sol_object_t *, sol_object_t *, sol_memspec_t, int);
39
-void sol_cstruct_add_member_name(sol_state_t *, sol_object_t *, char *, sol_memspec_t, int);
46
+void sol_cstruct_add_member(sol_state_t *, sol_object_t *, sol_object_t *, sol_memtype_t, int);
47
+void sol_cstruct_add_member_name(sol_state_t *, sol_object_t *, char *, sol_memtype_t, int);
40 48
 void sol_cstruct_add_func(sol_state_t *, sol_object_t *, sol_object_t *, sol_cfunc_t);
41 49
 void sol_cstruct_add_func_name(sol_state_t *, sol_object_t *, char *, sol_cfunc_t);
42 50
 
43 51
 sol_object_t *sol_new_cstruct(sol_state_t *, void *, sol_object_t *);
44 52
 
53
+extern sol_ops_t sol_cstruct_spec_ops;
45 54
 extern sol_ops_t sol_cstruct_ops;
46 55
 
47 56
 sol_object_t *sol_f_cstruct_index(sol_state_t *, sol_object_t *);

+ 28
- 5
object.c View File

@@ -319,14 +319,37 @@ sol_object_t *sol_list_remove(sol_state_t *state, sol_object_t *list, int idx) {
319 319
 sol_object_t *sol_list_copy(sol_state_t *state, sol_object_t *list) {
320 320
 	sol_object_t *newls = sol_new_list(state), *cur = list;
321 321
 	sol_object_t *res = newls;
322
-	while(cur->lvalue) {
323
-		newls->lvalue = sol_incref(cur->lvalue);
324
-		if(cur->lnext) {
322
+	while(cur) {
323
+		if(cur->lvalue) {
324
+            newls->lvalue = sol_incref(cur->lvalue);
325
+			newls->lnext = sol_alloc_object(state);
326
+			if(sol_has_error(state)) return sol_incref(state->None);
327
+			newls = newls->lnext;
328
+			newls->type = SOL_LCELL;
329
+			newls->ops = &(state->LCellOps);
330
+			newls->lvalue = NULL;
331
+			newls->lnext = NULL;
332
+		}
333
+		cur = cur->lnext;
334
+	}
335
+	return res;
336
+}
337
+
338
+sol_object_t *sol_list_truncate(sol_state_t *state, sol_object_t *list, int len) {
339
+	sol_object_t *newls = sol_new_list(state), *cur = list;
340
+	sol_object_t *res = newls;
341
+	int i = 0;
342
+	while(cur && i<len) {
343
+		if(cur->lvalue) {
344
+            newls->lvalue = sol_incref(cur->lvalue);
325 345
 			newls->lnext = sol_alloc_object(state);
326 346
 			if(sol_has_error(state)) return sol_incref(state->None);
327 347
 			newls = newls->lnext;
328 348
 			newls->type = SOL_LCELL;
329 349
 			newls->ops = &(state->LCellOps);
350
+			newls->lvalue = NULL;
351
+			newls->lnext = NULL;
352
+			i++;
330 353
 		}
331 354
 		cur = cur->lnext;
332 355
 	}
@@ -431,7 +454,7 @@ int sol_map_len(sol_state_t *state, sol_object_t *map) {
431 454
     return i;
432 455
 }
433 456
 
434
-sol_object_t *sol_map_submap(sol_state_t *state, sol_object_t *map, sol_object_t *key) {
457
+sol_object_t *sol_map_mcell(sol_state_t *state, sol_object_t *map, sol_object_t *key) {
435 458
     sol_object_t *list = sol_new_list(state), *res = NULL, *cur = map, *cmp;
436 459
     sol_list_insert(state, list, 0, key);
437 460
     while(cur) {
@@ -454,7 +477,7 @@ sol_object_t *sol_map_submap(sol_state_t *state, sol_object_t *map, sol_object_t
454 477
 }
455 478
 
456 479
 sol_object_t *sol_map_get(sol_state_t *state, sol_object_t *map, sol_object_t *key) {
457
-    sol_object_t *submap = sol_map_submap(state, map, key);
480
+    sol_object_t *submap = sol_map_mcell(state, map, key);
458 481
     sol_object_t *res;
459 482
     if(sol_is_map(submap)) {
460 483
         res = sol_incref(submap->mval);

+ 8030
- 9258
parser.output
File diff suppressed because it is too large
View File


+ 520
- 536
parser.tab.c
File diff suppressed because it is too large
View File


+ 28
- 34
parser.y View File

@@ -28,42 +28,41 @@
28 28
 %%
29 29
 
30 30
 program:
31
-  stmt { *program = AS_ST($1); }
31
+  stmt_list { *program = AS_ST($1); }
32
+;
33
+
34
+stmt_list:
35
+  stmt_list stmt {
36
+	stmtlist_node *cur = AS_ST($1)->stmtlist;
37
+	while(cur->next) cur = cur->next;
38
+	if(cur->stmt) {
39
+        cur->next = NEW(stmtlist_node);
40
+        cur = cur->next;
41
+	}
42
+    cur->stmt = $2;
43
+    cur->next = NULL;
44
+    $$ = $1;
45
+  }
46
+| /* empty */ {
47
+    $$ = NEW_ST();
48
+    AS_ST($$)->type = ST_LIST;
49
+    AS_ST($$)->stmtlist = NEW(stmtlist_node);
50
+    AS_ST($$)->stmtlist->stmt = NULL;
51
+    AS_ST($$)->stmtlist->next = NULL;
52
+}
32 53
 ;
33 54
 
34 55
 stmt:
35 56
   expr { $$ = NEW_ST(); AS_ST($$)->type = ST_EXPR; AS_ST($$)->expr = $1; }
36
-| IF expr THEN stmt END { $$ = NEW_ST(); AS_ST($$)->type = ST_IFELSE; AS_ST($$)->ifelse = NEW(ifelse_node); AS_ST($$)->ifelse->cond = $2; AS_ST($$)->ifelse->iftrue = $4; AS_ST($$)->ifelse->iffalse = NULL; }
37
-| IF expr THEN stmt ELSE stmt END { $$ = NEW_ST(); AS_ST($$)->type = ST_IFELSE; AS_ST($$)->ifelse = NEW(ifelse_node); AS_ST($$)->ifelse->cond = $2; AS_ST($$)->ifelse->iftrue = $4; AS_ST($$)->ifelse->iffalse = $6; }
38
-| WHILE expr DO stmt END { $$ = NEW_ST(); AS_ST($$)->type = ST_LOOP; AS_ST($$)->loop = NEW(loop_node); AS_ST($$)->loop->cond = $2; AS_ST($$)->loop->loop = $4; }
39
-| FOR IDENT IN expr DO stmt END { $$ = NEW_ST(); AS_ST($$)->type = ST_ITER; AS_ST($$)->iter = NEW(iter_node); AS_ST($$)->iter->var = $2; AS_ST($$)->iter->iter = $4; AS_ST($$)->iter->loop = $6; }
57
+| IF expr THEN stmt_list END { $$ = NEW_ST(); AS_ST($$)->type = ST_IFELSE; AS_ST($$)->ifelse = NEW(ifelse_node); AS_ST($$)->ifelse->cond = $2; AS_ST($$)->ifelse->iftrue = $4; AS_ST($$)->ifelse->iffalse = NULL; }
58
+| IF expr THEN stmt_list ELSE stmt_list END { $$ = NEW_ST(); AS_ST($$)->type = ST_IFELSE; AS_ST($$)->ifelse = NEW(ifelse_node); AS_ST($$)->ifelse->cond = $2; AS_ST($$)->ifelse->iftrue = $4; AS_ST($$)->ifelse->iffalse = $6; }
59
+| WHILE expr DO stmt_list END { $$ = NEW_ST(); AS_ST($$)->type = ST_LOOP; AS_ST($$)->loop = NEW(loop_node); AS_ST($$)->loop->cond = $2; AS_ST($$)->loop->loop = $4; }
60
+| FOR IDENT IN expr DO stmt_list END { $$ = NEW_ST(); AS_ST($$)->type = ST_ITER; AS_ST($$)->iter = NEW(iter_node); AS_ST($$)->iter->var = $2; AS_ST($$)->iter->iter = $4; AS_ST($$)->iter->loop = $6; }
40 61
 | RETURN expr { $$ = NEW_ST(); AS_ST($$)->type = ST_RET; AS_ST($$)->ret = NEW(ret_node); AS_ST($$)->ret->ret = $2; }
41 62
 | RETURN { $$ = NEW_ST(); AS_ST($$)->type = ST_RET; AS_ST($$)->ret = NEW(ret_node); AS_ST($$)->ret->ret = NULL; }
42 63
 | BREAK { $$ = NEW_ST(); AS_ST($$)->type = ST_BREAK; }
43 64
 | CONTINUE { $$ = NEW_ST(); AS_ST($$)->type = ST_CONT; }
44 65
 | stmt SEMICOLON { $$ = $1; }
45
-| stmt_list { $$ = $1; }
46
-;
47
-
48
-stmt_list:
49
-  stmt_list stmt {
50
-	stmtlist_node *cur = AS_ST($1)->stmtlist;
51
-	while(cur->next) cur = cur->next;
52
-	cur->next = NEW(stmtlist_node);
53
-	cur = cur->next;
54
-	cur->stmt = $2;
55
-	cur->next = NULL;
56
-	$$ = $1;
57
-  }
58
-| stmt stmt {
59
-	$$ = NEW_ST();
60
-	AS_ST($$)->type = ST_LIST;
61
-	AS_ST($$)->stmtlist = NEW(stmtlist_node);
62
-	AS_ST($$)->stmtlist->stmt = $1;
63
-	AS_ST($$)->stmtlist->next = NEW(stmtlist_node);
64
-	AS_ST($$)->stmtlist->next->stmt = $2;
65
-	AS_ST($$)->stmtlist->next->next = NULL;
66
-  }
67 66
 ;
68 67
 
69 68
 expr:
@@ -323,7 +322,7 @@ call_expr:
323 322
 ;
324 323
 
325 324
 funcdecl_expr:
326
-  FUNC IDENT LPAREN ident_list RPAREN opt_stmt END {
325
+  FUNC IDENT LPAREN ident_list RPAREN stmt_list END {
327 326
 	$$ = NEW_EX();
328 327
 	AS_EX($$)->type = EX_FUNCDECL;
329 328
 	AS_EX($$)->funcdecl = NEW(funcdecl_node);
@@ -331,7 +330,7 @@ funcdecl_expr:
331 330
 	AS_EX($$)->funcdecl->args = $4;
332 331
 	AS_EX($$)->funcdecl->body = $6;
333 332
 }
334
-| FUNC LPAREN ident_list RPAREN opt_stmt END {
333
+| FUNC LPAREN ident_list RPAREN stmt_list END {
335 334
 	$$ = NEW_EX();
336 335
 	AS_EX($$)->type = EX_FUNCDECL;
337 336
 	AS_EX($$)->funcdecl = NEW(funcdecl_node);
@@ -342,11 +341,6 @@ funcdecl_expr:
342 341
 | index_expr { $$ = $1; }
343 342
 ;
344 343
 
345
-opt_stmt:
346
-  stmt { $$ = $1; }
347
-| /* empty */ { $$ = NULL; }
348
-;
349
-
350 344
 index_expr:
351 345
   expr LBRACKET expr RBRACKET { $$ = NEW_EX(); AS_EX($$)->type = EX_INDEX; AS_EX($$)->index = NEW(index_node); AS_EX($$)->index->expr = $1; AS_EX($$)->index->index = $3; }
352 346
 | expr DOT IDENT {

+ 10
- 1
sol.h View File

@@ -106,6 +106,7 @@ typedef struct sol_tag_state_t {
106 106
 	sol_ops_t MCellOps;
107 107
 	sol_ops_t FuncOps;
108 108
 	sol_ops_t CFuncOps;
109
+	sol_object_t *ListFuncs;
109 110
 } sol_state_t;
110 111
 
111 112
 // state.c
@@ -200,6 +201,13 @@ sol_object_t *sol_f_list_len(sol_state_t *, sol_object_t *);
200 201
 sol_object_t *sol_f_list_iter(sol_state_t *, sol_object_t *);
201 202
 sol_object_t *sol_f_list_tostring(sol_state_t *, sol_object_t *);
202 203
 
204
+sol_object_t *sol_f_list_copy(sol_state_t *, sol_object_t *);
205
+sol_object_t *sol_f_list_insert(sol_state_t *, sol_object_t *);
206
+sol_object_t *sol_f_list_remove(sol_state_t *, sol_object_t *);
207
+sol_object_t *sol_f_list_truncate(sol_state_t *, sol_object_t *);
208
+sol_object_t *sol_f_list_map(sol_state_t *, sol_object_t *);
209
+sol_object_t *sol_f_list_filter(sol_state_t *, sol_object_t *);
210
+
203 211
 sol_object_t *sol_f_map_add(sol_state_t *, sol_object_t *);
204 212
 sol_object_t *sol_f_map_index(sol_state_t *, sol_object_t *);
205 213
 sol_object_t *sol_f_map_setindex(sol_state_t *, sol_object_t *);
@@ -253,13 +261,14 @@ void sol_list_set_index(sol_state_t *, sol_object_t *, int, sol_object_t *);
253 261
 void sol_list_insert(sol_state_t *, sol_object_t *, int, sol_object_t *);
254 262
 sol_object_t *sol_list_remove(sol_state_t *, sol_object_t *, int);
255 263
 sol_object_t *sol_list_copy(sol_state_t *, sol_object_t *);
264
+sol_object_t *sol_list_truncate(sol_state_t *, sol_object_t *, int);
256 265
 void sol_list_append(sol_state_t *, sol_object_t *, sol_object_t *);
257 266
 #define sol_list_push(st, ls, obj) sol_list_insert(st, ls, 0, obj);
258 267
 #define sol_list_pop(st, ls) sol_list_remove(st, ls, 0);
259 268
 
260 269
 sol_object_t *sol_new_map(sol_state_t *);
261 270
 int sol_map_len(sol_state_t *, sol_object_t *);
262
-sol_object_t *sol_map_submap(sol_state_t *, sol_object_t *, sol_object_t *);
271
+sol_object_t *sol_map_mcell(sol_state_t *, sol_object_t *, sol_object_t *);
263 272
 sol_object_t *sol_map_get(sol_state_t *, sol_object_t *, sol_object_t *);
264 273
 sol_object_t *sol_map_get_name(sol_state_t *, sol_object_t *, char *);
265 274
 void sol_map_set(sol_state_t *, sol_object_t *, sol_object_t *, sol_object_t *);

+ 8
- 0
state.c View File

@@ -135,6 +135,14 @@ int sol_state_init(sol_state_t *state) {
135 135
 	sol_map_set_name(state, iter, "map", sol_new_cfunc(state, sol_f_iter_map));
136 136
 	sol_map_set_name(state, globals, "iter", iter);
137 137
 	sol_obj_free(iter);
138
+
139
+	state->ListFuncs = sol_new_map(state);
140
+	sol_map_set_name(state, state->ListFuncs, "copy", sol_new_cfunc(state, sol_f_list_copy));
141
+	sol_map_set_name(state, state->ListFuncs, "insert", sol_new_cfunc(state, sol_f_list_insert));
142
+	sol_map_set_name(state, state->ListFuncs, "remove", sol_new_cfunc(state, sol_f_list_remove));
143
+	sol_map_set_name(state, state->ListFuncs, "truncate", sol_new_cfunc(state, sol_f_list_truncate));
144
+	sol_map_set_name(state, state->ListFuncs, "map", sol_new_cfunc(state, sol_f_list_map));
145
+	sol_map_set_name(state, state->ListFuncs, "filter", sol_new_cfunc(state, sol_f_list_filter));
138 146
 	if(sol_has_error(state)) goto cleanup;
139 147
 
140 148
 	// We're all set!

+ 76
- 0
test.sol View File

@@ -1,3 +1,9 @@
1
+print('--- Empty functions')
2
+
3
+func f() end
4
+
5
+print(f())
6
+
1 7
 print('--- While loop')
2 8
 a = 1
3 9
 while a < 10 do
@@ -94,3 +100,73 @@ end
94 100
 myiter.i = 1
95 101
 
96 102
 for i in myiter do print(i) end
103
+
104
+print('--- Method calls')
105
+
106
+d = {a = func(a, b) print(a, b) end}
107
+
108
+d.a(1, 2)
109
+d:a(3)
110
+
111
+print('--- Special methods')
112
+
113
+d = {__index = func(obj, key) print('Index', obj, key) return key end,
114
+     __setindex = func(obj, key, val) print('SetIndex', obj, key, val) end,
115
+     __call = func(obj, arg1, arg2) print('Call', obj, arg1, arg2) return arg1 end}
116
+
117
+print(d[3], d[5])
118
+
119
+d.a = 7
120
+
121
+print(d("q", "r"))
122
+
123
+e = {a=1, b=2}
124
+d = {__index = e, __setindex = e}
125
+
126
+print(d, d.a, d.b)
127
+
128
+d.c = 5
129
+d.b = 7
130
+
131
+print(d, e)
132
+
133
+print('--- Data sharing')
134
+
135
+d = {}
136
+e = [1 2 3 4 5]
137
+d.a = e
138
+d.b = e
139
+
140
+print(d)
141
+
142
+e[2]=7
143
+e[3]="c"
144
+
145
+print(d)
146
+
147
+d.a:insert(#(d.a), "q")
148
+d.b:remove(1)
149
+d.a[3]="f"
150
+
151
+print(d)
152
+
153
+print('--- Arithmetic structure operations')
154
+
155
+print([1 2 3]+[4 5])
156
+print([1 2 3]*5)
157
+print({a=1 b=2}+{c=3})
158
+
159
+print('--- Map/filter')
160
+
161
+l = [1 2 3 4 5]
162
+print(l)
163
+
164
+l:map(func (i) return i*3 end)
165
+print(l)
166
+
167
+l:filter(func (i) return i & 1 end)
168
+print(l)
169
+
170
+print('--- Map/filter chain')
171
+
172
+print([1 2 3 4 5]:map(func (i) return i * 3 end):filter(func (i) return i & 1 end))

Loading…
Cancel
Save