Browse Source

Sol Part 58: Insert Witty Commit Message Here!

Graham Northup 4 years ago
parent
commit
6bcb85a19e
Signed by: Grissess <grissess@nexusg.org> GPG Key ID: 5D000E6F539376FB
14 changed files with 284 additions and 208 deletions
  1. 6
    2
      Makefile
  2. 1
    0
      ast.h
  3. 1
    0
      astprint.c
  4. 15
    5
      builtins.c
  5. 2
    2
      cdata.c
  6. 7
    1
      object.c
  7. 86
    95
      parser.tab.c
  8. 4
    13
      parser.y
  9. 24
    2
      runtime.c
  10. 14
    5
      sol.h
  11. 18
    3
      solrun.c
  12. 85
    80
      state.c
  13. 1
    0
      tests/_lib.sol
  14. 20
    0
      tests/lang_method.sol

+ 6
- 2
Makefile View File

@@ -4,8 +4,9 @@ OBJ= lex.yy.o parser.tab.o dsl/seq.o dsl/list.o dsl/array.o dsl/generic.o astpri
4 4
 
5 5
 .PHONY: all test
6 6
 
7
-all: $(OBJ)
8
-	git submodule init && git submodule sync && git submodule update
7
+all: dsl sol
8
+	
9
+sol: $(OBJ)
9 10
 	gcc $(CFLAGS) $? $(LDFLAGS) -o sol
10 11
 
11 12
 test: all $(sort $(patsubst tests/%.sol,test_%,$(wildcard tests/*.sol)))
@@ -13,6 +14,9 @@ test: all $(sort $(patsubst tests/%.sol,test_%,$(wildcard tests/*.sol)))
13 14
 test_%: tests/%.sol
14 15
 	./sol r $?
15 16
 
17
+dsl:
18
+	git submodule init && git submodule sync && git submodule update
19
+
16 20
 %.o: %.c
17 21
 	gcc -c -o $@ $? $(CFLAGS)
18 22
 

+ 1
- 0
ast.h View File

@@ -121,6 +121,7 @@ typedef struct {
121 121
 typedef struct {
122 122
 	expr_node *expr;
123 123
 	exprlist_node *args;
124
+	char *method;
124 125
 } call_node;
125 126
 
126 127
 typedef struct tag_identlist_node {

+ 1
- 0
astprint.c View File

@@ -261,6 +261,7 @@ void prex(sol_state_t *state, expr_node *node, int lev) {
261 261
 			lev++;
262 262
 			prlev(state, lev, "Expr:");
263 263
 			prex(state, node->call->expr, lev + 1);
264
+			prlev(state, lev, "Method: %s\n", node->call->method);
264 265
 			prlev(state, lev, "Args:");
265 266
 			cure = node->call->args;
266 267
 			while(cure && cure->expr) {

+ 15
- 5
builtins.c View File

@@ -943,7 +943,7 @@ sol_object_t *sol_f_str_index(sol_state_t *state, sol_object_t *args) {
943 943
 }
944 944
 
945 945
 sol_object_t *sol_f_str_iter(sol_state_t *state, sol_object_t *args) {
946
-	return sol_new_cfunc(state, sol_f_iter_str);
946
+	return sol_new_cfunc(state, sol_f_iter_str, "iter.str");
947 947
 }
948 948
 
949 949
 sol_object_t *sol_f_str_toint(sol_state_t *state, sol_object_t *args) {
@@ -1172,7 +1172,7 @@ sol_object_t *sol_f_list_len(sol_state_t *state, sol_object_t *args) {
1172 1172
 }
1173 1173
 
1174 1174
 sol_object_t *sol_f_list_iter(sol_state_t *state, sol_object_t *args) {
1175
-	return sol_new_cfunc(state, sol_f_iter_list);
1175
+	return sol_new_cfunc(state, sol_f_iter_list, "iter.list");
1176 1176
 }
1177 1177
 
1178 1178
 sol_object_t *sol_f_list_tostring(sol_state_t *state, sol_object_t *args) {
@@ -1390,7 +1390,7 @@ sol_object_t *sol_f_map_len(sol_state_t *state, sol_object_t *args) {
1390 1390
 }
1391 1391
 
1392 1392
 sol_object_t *sol_f_map_iter(sol_state_t *state, sol_object_t *args) {
1393
-	return sol_new_cfunc(state, sol_f_iter_map);
1393
+	return sol_new_cfunc(state, sol_f_iter_map, "iter.map");
1394 1394
 }
1395 1395
 
1396 1396
 sol_object_t *sol_f_map_tostring(sol_state_t *state, sol_object_t *args) {
@@ -1581,7 +1581,17 @@ sol_object_t *sol_f_cfunc_call(sol_state_t *state, sol_object_t *args) {
1581 1581
 }
1582 1582
 
1583 1583
 sol_object_t *sol_f_cfunc_tostring(sol_state_t *state, sol_object_t *args) {
1584
-	return sol_new_string(state, "<CFunction>");
1584
+	sol_object_t *cfunc = sol_list_get_index(state, args, 0), *ret;
1585
+	char *s = malloc(256 * sizeof(char));
1586
+	if(cfunc->cfname) {
1587
+		snprintf(s, 256, "<CFunction %s>", cfunc->cfname);
1588
+	} else {
1589
+		snprintf(s, 256, "<CFunction>");
1590
+	}
1591
+	ret = sol_new_string(state, s);
1592
+	free(s);
1593
+	sol_obj_free(cfunc);
1594
+	return ret;
1585 1595
 }
1586 1596
 
1587 1597
 sol_object_t *sol_f_astnode_call(sol_state_t *state, sol_object_t *args) {
@@ -2726,7 +2736,7 @@ sol_object_t *sol_f_stream_open(sol_state_t *state, sol_object_t *args) {
2726 2736
 	sol_object_t *fn = sol_list_get_index(state, args, 0), *mode = sol_list_get_index(state, args, 1);
2727 2737
 	sol_object_t *sfn = sol_cast_string(state, fn), *imode = sol_cast_int(state, mode);
2728 2738
 	sol_modes_t m = imode->ival;
2729
-	char *smode = sol_FileModes[m];
2739
+	char *smode = sol_FileModes[(m >= 0 && m < (sizeof(sol_FileModes) / sizeof(char *))) ? m : 0];
2730 2740
 	FILE *f;
2731 2741
 	sol_obj_free(mode);
2732 2742
 	sol_obj_free(imode);

+ 2
- 2
cdata.c View File

@@ -162,7 +162,7 @@ sol_object_t *sol_f_cstruct_index(sol_state_t *state, sol_object_t *args) {
162 162
 					break;
163 163
 
164 164
 				case SOL_MT_CFUNC:
165
-					res = sol_new_cfunc(state, AT(cstruct->data, sol_cfunc_t, spec->offset));
165
+					res = sol_new_cfunc(state, AT(cstruct->data, sol_cfunc_t, spec->offset), NULL);
166 166
 					break;
167 167
 
168 168
 				case SOL_MT_PTR:
@@ -172,7 +172,7 @@ sol_object_t *sol_f_cstruct_index(sol_state_t *state, sol_object_t *args) {
172 172
 			break;
173 173
 
174 174
 		case SOL_CS_CFUNC:
175
-			res = sol_new_cfunc(state, spec->cfunc);
175
+			res = sol_new_cfunc(state, spec->cfunc, NULL);
176 176
 			break;
177 177
 	}
178 178
 	if(!res) {

+ 7
- 1
object.c View File

@@ -504,15 +504,21 @@ sol_object_t *sol_f_mcell_free(sol_state_t *state, sol_object_t *mcell) {
504 504
 	return 0;
505 505
 }*/
506 506
 
507
-sol_object_t *sol_new_cfunc(sol_state_t *state, sol_cfunc_t cfunc) {
507
+sol_object_t *sol_new_cfunc(sol_state_t *state, sol_cfunc_t cfunc, char *name) {
508 508
 	sol_object_t *res = sol_alloc_object(state);
509 509
 	res->type = SOL_CFUNCTION;
510 510
 	res->ops = &(state->CFuncOps);
511 511
 	res->cfunc = cfunc;
512
+	res->cfname = name ? strdup(name) : NULL;
512 513
 	sol_init_object(state, res);
513 514
 	return res;
514 515
 }
515 516
 
517
+sol_object_t *sol_f_cfunc_free(sol_state_t *state, sol_object_t *cfunc) {
518
+	free(cfunc->cfname);
519
+	return cfunc;
520
+}
521
+
516 522
 sol_object_t *sol_new_cdata(sol_state_t *state, void *cdata, sol_ops_t *ops) {
517 523
 	sol_object_t *res = sol_alloc_object(state);
518 524
 	res->type = SOL_CDATA;

+ 86
- 95
parser.tab.c View File

@@ -512,11 +512,11 @@ static const yytype_uint16 yyrline[] =
512 512
      289,   293,   294,   298,   299,   300,   301,   302,   303,   304,
513 513
      308,   309,   310,   314,   315,   316,   317,   321,   322,   326,
514 514
      327,   332,   333,   334,   335,   336,   337,   341,   342,   346,
515
-     347,   351,   352,   369,   373,   381,   389,   400,   404,   405,
516
-     416,   420,   421,   435,   436,   440,   441,   442,   443,   444,
517
-     445,   449,   450,   451,   455,   459,   460,   465,   466,   497,
518
-     498,   532,   544,   569,   573,   574,   579,   580,   592,   597,
519
-     609,   610
515
+     347,   351,   352,   360,   364,   372,   380,   391,   395,   396,
516
+     407,   411,   412,   426,   427,   431,   432,   433,   434,   435,
517
+     436,   440,   441,   442,   446,   450,   451,   456,   457,   488,
518
+     489,   523,   535,   560,   564,   565,   570,   571,   583,   588,
519
+     600,   601
520 520
 };
521 521
 #endif
522 522
 
@@ -2266,7 +2266,7 @@ yyreduce:
2266 2266
 
2267 2267
   case 71:
2268 2268
 #line 351 "parser.y" /* yacc.c:1646  */
2269
-    { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_CALL; AS_EX((yyval))->call = NEW(call_node); AS_EX((yyval))->call->expr = (yyvsp[-3]); AS_EX((yyval))->call->args = (yyvsp[-1]); }
2269
+    { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_CALL; AS_EX((yyval))->call = NEW(call_node); AS_EX((yyval))->call->expr = (yyvsp[-3]); AS_EX((yyval))->call->args = (yyvsp[-1]); AS_EX((yyval))->call->method = NULL; }
2270 2270
 #line 2271 "parser.tab.c" /* yacc.c:1646  */
2271 2271
     break;
2272 2272
 
@@ -2276,30 +2276,21 @@ yyreduce:
2276 2276
 	(yyval) = NEW_EX();
2277 2277
 	AS_EX((yyval))->type = EX_CALL;
2278 2278
 	AS_EX((yyval))->call = NEW(call_node);
2279
-	AS_EX((yyval))->call->expr = NEW_EX();
2280
-	AS_EX((yyval))->call->expr->type = EX_INDEX;
2281
-	AS_EX((yyval))->call->expr->index = NEW(index_node);
2282
-	AS_EX((yyval))->call->expr->index->expr = (yyvsp[-5]);
2283
-	AS_EX((yyval))->call->expr->index->index = NEW_EX();
2284
-	AS_EX((yyval))->call->expr->index->index->type = EX_LIT;
2285
-	AS_EX((yyval))->call->expr->index->index->lit = NEW(lit_node);
2286
-	AS_EX((yyval))->call->expr->index->index->lit->type = LIT_STRING;
2287
-	AS_EX((yyval))->call->expr->index->index->lit->str = (yyvsp[-3]);
2288
-	AS_EX((yyval))->call->args = NEW(exprlist_node);
2289
-	AS_EX((yyval))->call->args->expr = ex_copy((yyvsp[-5]));
2290
-	AS_EX((yyval))->call->args->next = (yyvsp[-1]);
2279
+	AS_EX((yyval))->call->expr = (yyvsp[-5]);
2280
+	AS_EX((yyval))->call->args = (yyvsp[-1]);
2281
+	AS_EX((yyval))->call->method = (yyvsp[-3]);
2291 2282
 }
2292
-#line 2293 "parser.tab.c" /* yacc.c:1646  */
2283
+#line 2284 "parser.tab.c" /* yacc.c:1646  */
2293 2284
     break;
2294 2285
 
2295 2286
   case 73:
2296
-#line 369 "parser.y" /* yacc.c:1646  */
2287
+#line 360 "parser.y" /* yacc.c:1646  */
2297 2288
     { (yyval) = (yyvsp[0]); }
2298
-#line 2299 "parser.tab.c" /* yacc.c:1646  */
2289
+#line 2290 "parser.tab.c" /* yacc.c:1646  */
2299 2290
     break;
2300 2291
 
2301 2292
   case 74:
2302
-#line 373 "parser.y" /* yacc.c:1646  */
2293
+#line 364 "parser.y" /* yacc.c:1646  */
2303 2294
     {
2304 2295
 	(yyval) = NEW_EX();
2305 2296
 	AS_EX((yyval))->type = EX_FUNCDECL;
@@ -2308,11 +2299,11 @@ yyreduce:
2308 2299
 	AS_EX((yyval))->funcdecl->params = (yyvsp[-3]);
2309 2300
 	AS_EX((yyval))->funcdecl->body = (yyvsp[-1]);
2310 2301
 }
2311
-#line 2312 "parser.tab.c" /* yacc.c:1646  */
2302
+#line 2303 "parser.tab.c" /* yacc.c:1646  */
2312 2303
     break;
2313 2304
 
2314 2305
   case 75:
2315
-#line 381 "parser.y" /* yacc.c:1646  */
2306
+#line 372 "parser.y" /* yacc.c:1646  */
2316 2307
     {
2317 2308
 	(yyval) = NEW_EX();
2318 2309
 	AS_EX((yyval))->type = EX_FUNCDECL;
@@ -2321,11 +2312,11 @@ yyreduce:
2321 2312
 	AS_EX((yyval))->funcdecl->params = (yyvsp[-3]);
2322 2313
 	AS_EX((yyval))->funcdecl->body = (yyvsp[-1]);
2323 2314
 }
2324
-#line 2325 "parser.tab.c" /* yacc.c:1646  */
2315
+#line 2316 "parser.tab.c" /* yacc.c:1646  */
2325 2316
     break;
2326 2317
 
2327 2318
   case 76:
2328
-#line 389 "parser.y" /* yacc.c:1646  */
2319
+#line 380 "parser.y" /* yacc.c:1646  */
2329 2320
     {
2330 2321
 	(yyval) = NEW_EX();
2331 2322
 	AS_EX((yyval))->type = EX_FUNCDECL;
@@ -2337,23 +2328,23 @@ yyreduce:
2337 2328
 	AS_EX((yyval))->funcdecl->body->ret = NEW(ret_node);
2338 2329
 	AS_EX((yyval))->funcdecl->body->ret->ret = (yyvsp[-1]);
2339 2330
 }
2340
-#line 2341 "parser.tab.c" /* yacc.c:1646  */
2331
+#line 2332 "parser.tab.c" /* yacc.c:1646  */
2341 2332
     break;
2342 2333
 
2343 2334
   case 77:
2344
-#line 400 "parser.y" /* yacc.c:1646  */
2335
+#line 391 "parser.y" /* yacc.c:1646  */
2345 2336
     { (yyval) = (yyvsp[0]); }
2346
-#line 2347 "parser.tab.c" /* yacc.c:1646  */
2337
+#line 2338 "parser.tab.c" /* yacc.c:1646  */
2347 2338
     break;
2348 2339
 
2349 2340
   case 78:
2350
-#line 404 "parser.y" /* yacc.c:1646  */
2341
+#line 395 "parser.y" /* yacc.c:1646  */
2351 2342
     { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_INDEX; AS_EX((yyval))->index = NEW(index_node); AS_EX((yyval))->index->expr = (yyvsp[-3]); AS_EX((yyval))->index->index = (yyvsp[-1]); }
2352
-#line 2353 "parser.tab.c" /* yacc.c:1646  */
2343
+#line 2344 "parser.tab.c" /* yacc.c:1646  */
2353 2344
     break;
2354 2345
 
2355 2346
   case 79:
2356
-#line 405 "parser.y" /* yacc.c:1646  */
2347
+#line 396 "parser.y" /* yacc.c:1646  */
2357 2348
     {
2358 2349
     (yyval) = NEW_EX();
2359 2350
     AS_EX((yyval))->type = EX_INDEX;
@@ -2365,23 +2356,23 @@ yyreduce:
2365 2356
     AS_EX((yyval))->index->index->lit->type = LIT_STRING;
2366 2357
     AS_EX((yyval))->index->index->lit->str = (yyvsp[0]);
2367 2358
 }
2368
-#line 2369 "parser.tab.c" /* yacc.c:1646  */
2359
+#line 2360 "parser.tab.c" /* yacc.c:1646  */
2369 2360
     break;
2370 2361
 
2371 2362
   case 80:
2372
-#line 416 "parser.y" /* yacc.c:1646  */
2363
+#line 407 "parser.y" /* yacc.c:1646  */
2373 2364
     { (yyval) = (yyvsp[0]); }
2374
-#line 2375 "parser.tab.c" /* yacc.c:1646  */
2365
+#line 2366 "parser.tab.c" /* yacc.c:1646  */
2375 2366
     break;
2376 2367
 
2377 2368
   case 81:
2378
-#line 420 "parser.y" /* yacc.c:1646  */
2369
+#line 411 "parser.y" /* yacc.c:1646  */
2379 2370
     { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_INDEX; AS_EX((yyval))->index = NEW(index_node); AS_EX((yyval))->index->expr = (yyvsp[-3]); AS_EX((yyval))->index->index = (yyvsp[-1]); }
2380
-#line 2381 "parser.tab.c" /* yacc.c:1646  */
2371
+#line 2372 "parser.tab.c" /* yacc.c:1646  */
2381 2372
     break;
2382 2373
 
2383 2374
   case 82:
2384
-#line 421 "parser.y" /* yacc.c:1646  */
2375
+#line 412 "parser.y" /* yacc.c:1646  */
2385 2376
     {
2386 2377
     (yyval) = NEW_EX();
2387 2378
     AS_EX((yyval))->type = EX_INDEX;
@@ -2393,105 +2384,105 @@ yyreduce:
2393 2384
     AS_EX((yyval))->index->index->lit->type = LIT_STRING;
2394 2385
     AS_EX((yyval))->index->index->lit->str = (yyvsp[0]);
2395 2386
 }
2396
-#line 2397 "parser.tab.c" /* yacc.c:1646  */
2387
+#line 2388 "parser.tab.c" /* yacc.c:1646  */
2397 2388
     break;
2398 2389
 
2399 2390
   case 83:
2400
-#line 435 "parser.y" /* yacc.c:1646  */
2391
+#line 426 "parser.y" /* yacc.c:1646  */
2401 2392
     { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_REF; AS_EX((yyval))->ref = NEW(ref_node); AS_EX((yyval))->ref->ident = (yyvsp[0]); }
2402
-#line 2403 "parser.tab.c" /* yacc.c:1646  */
2393
+#line 2394 "parser.tab.c" /* yacc.c:1646  */
2403 2394
     break;
2404 2395
 
2405 2396
   case 84:
2406
-#line 436 "parser.y" /* yacc.c:1646  */
2397
+#line 427 "parser.y" /* yacc.c:1646  */
2407 2398
     { (yyval) = (yyvsp[0]); }
2408
-#line 2409 "parser.tab.c" /* yacc.c:1646  */
2399
+#line 2400 "parser.tab.c" /* yacc.c:1646  */
2409 2400
     break;
2410 2401
 
2411 2402
   case 85:
2412
-#line 440 "parser.y" /* yacc.c:1646  */
2403
+#line 431 "parser.y" /* yacc.c:1646  */
2413 2404
     { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_LIT; AS_EX((yyval))->lit = NEW(lit_node); AS_EX((yyval))->lit->type = LIT_INT; AS_EX((yyval))->lit->ival = *AS((yyvsp[0]), long); free((yyvsp[0])); }
2414
-#line 2415 "parser.tab.c" /* yacc.c:1646  */
2405
+#line 2406 "parser.tab.c" /* yacc.c:1646  */
2415 2406
     break;
2416 2407
 
2417 2408
   case 86:
2418
-#line 441 "parser.y" /* yacc.c:1646  */
2409
+#line 432 "parser.y" /* yacc.c:1646  */
2419 2410
     { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_LIT; AS_EX((yyval))->lit = NEW(lit_node); AS_EX((yyval))->lit->type = LIT_INT; AS_EX((yyval))->lit->ival = -(*AS((yyvsp[0]), long)); free((yyvsp[0])); }
2420
-#line 2421 "parser.tab.c" /* yacc.c:1646  */
2411
+#line 2412 "parser.tab.c" /* yacc.c:1646  */
2421 2412
     break;
2422 2413
 
2423 2414
   case 87:
2424
-#line 442 "parser.y" /* yacc.c:1646  */
2415
+#line 433 "parser.y" /* yacc.c:1646  */
2425 2416
     { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_LIT; AS_EX((yyval))->lit = NEW(lit_node); AS_EX((yyval))->lit->type = LIT_FLOAT; AS_EX((yyval))->lit->fval = *AS((yyvsp[0]), double); free((yyvsp[0])); }
2426
-#line 2427 "parser.tab.c" /* yacc.c:1646  */
2417
+#line 2418 "parser.tab.c" /* yacc.c:1646  */
2427 2418
     break;
2428 2419
 
2429 2420
   case 88:
2430
-#line 443 "parser.y" /* yacc.c:1646  */
2421
+#line 434 "parser.y" /* yacc.c:1646  */
2431 2422
     { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_LIT; AS_EX((yyval))->lit = NEW(lit_node); AS_EX((yyval))->lit->type = LIT_STRING; AS_EX((yyval))->lit->str = (yyvsp[0]); }
2432
-#line 2433 "parser.tab.c" /* yacc.c:1646  */
2423
+#line 2424 "parser.tab.c" /* yacc.c:1646  */
2433 2424
     break;
2434 2425
 
2435 2426
   case 89:
2436
-#line 444 "parser.y" /* yacc.c:1646  */
2427
+#line 435 "parser.y" /* yacc.c:1646  */
2437 2428
     { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_LIT; AS_EX((yyval))->lit = NEW(lit_node); AS_EX((yyval))->lit->type = LIT_NONE; }
2438
-#line 2439 "parser.tab.c" /* yacc.c:1646  */
2429
+#line 2430 "parser.tab.c" /* yacc.c:1646  */
2439 2430
     break;
2440 2431
 
2441 2432
   case 90:
2442
-#line 445 "parser.y" /* yacc.c:1646  */
2433
+#line 436 "parser.y" /* yacc.c:1646  */
2443 2434
     { (yyval) = (yyvsp[0]); }
2444
-#line 2445 "parser.tab.c" /* yacc.c:1646  */
2435
+#line 2436 "parser.tab.c" /* yacc.c:1646  */
2445 2436
     break;
2446 2437
 
2447 2438
   case 91:
2448
-#line 449 "parser.y" /* yacc.c:1646  */
2439
+#line 440 "parser.y" /* yacc.c:1646  */
2449 2440
     { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_LISTGEN; AS_EX((yyval))->listgen = NEW(listgen_node); AS_EX((yyval))->listgen->list = (yyvsp[-1]); }
2450
-#line 2451 "parser.tab.c" /* yacc.c:1646  */
2441
+#line 2442 "parser.tab.c" /* yacc.c:1646  */
2451 2442
     break;
2452 2443
 
2453 2444
   case 92:
2454
-#line 450 "parser.y" /* yacc.c:1646  */
2445
+#line 441 "parser.y" /* yacc.c:1646  */
2455 2446
     { (yyval) = NEW_EX(); AS_EX((yyval))->type = EX_MAPGEN; AS_EX((yyval))->mapgen = NEW(mapgen_node); AS_EX((yyval))->mapgen->map = (yyvsp[-1]); }
2456
-#line 2457 "parser.tab.c" /* yacc.c:1646  */
2447
+#line 2448 "parser.tab.c" /* yacc.c:1646  */
2457 2448
     break;
2458 2449
 
2459 2450
   case 93:
2460
-#line 451 "parser.y" /* yacc.c:1646  */
2451
+#line 442 "parser.y" /* yacc.c:1646  */
2461 2452
     { (yyval) = (yyvsp[0]); }
2462
-#line 2463 "parser.tab.c" /* yacc.c:1646  */
2453
+#line 2454 "parser.tab.c" /* yacc.c:1646  */
2463 2454
     break;
2464 2455
 
2465 2456
   case 94:
2466
-#line 455 "parser.y" /* yacc.c:1646  */
2457
+#line 446 "parser.y" /* yacc.c:1646  */
2467 2458
     { (yyval) = (yyvsp[-1]); }
2468
-#line 2469 "parser.tab.c" /* yacc.c:1646  */
2459
+#line 2460 "parser.tab.c" /* yacc.c:1646  */
2469 2460
     break;
2470 2461
 
2471 2462
   case 95:
2472
-#line 459 "parser.y" /* yacc.c:1646  */
2463
+#line 450 "parser.y" /* yacc.c:1646  */
2473 2464
     { (yyval) = NULL; }
2474
-#line 2475 "parser.tab.c" /* yacc.c:1646  */
2465
+#line 2466 "parser.tab.c" /* yacc.c:1646  */
2475 2466
     break;
2476 2467
 
2477 2468
   case 96:
2478
-#line 460 "parser.y" /* yacc.c:1646  */
2469
+#line 451 "parser.y" /* yacc.c:1646  */
2479 2470
     {
2480 2471
 	(yyval) = NEW(exprlist_node);
2481 2472
 	AS((yyval), exprlist_node)->expr = (yyvsp[0]);
2482 2473
 	AS((yyval), exprlist_node)->next = NULL;
2483 2474
 }
2484
-#line 2485 "parser.tab.c" /* yacc.c:1646  */
2475
+#line 2476 "parser.tab.c" /* yacc.c:1646  */
2485 2476
     break;
2486 2477
 
2487 2478
   case 97:
2488
-#line 465 "parser.y" /* yacc.c:1646  */
2479
+#line 456 "parser.y" /* yacc.c:1646  */
2489 2480
     { (yyval) = (yyvsp[-1]); }
2490
-#line 2491 "parser.tab.c" /* yacc.c:1646  */
2481
+#line 2482 "parser.tab.c" /* yacc.c:1646  */
2491 2482
     break;
2492 2483
 
2493 2484
   case 98:
2494
-#line 466 "parser.y" /* yacc.c:1646  */
2485
+#line 457 "parser.y" /* yacc.c:1646  */
2495 2486
     {
2496 2487
 	exprlist_node *cur = (yyvsp[-1]);
2497 2488
 	while(cur->next) cur = cur->next;
@@ -2501,17 +2492,17 @@ yyreduce:
2501 2492
 	cur->next = NULL;
2502 2493
 	(yyval) = (yyvsp[-1]);
2503 2494
 }
2504
-#line 2505 "parser.tab.c" /* yacc.c:1646  */
2495
+#line 2496 "parser.tab.c" /* yacc.c:1646  */
2505 2496
     break;
2506 2497
 
2507 2498
   case 99:
2508
-#line 497 "parser.y" /* yacc.c:1646  */
2499
+#line 488 "parser.y" /* yacc.c:1646  */
2509 2500
     { (yyval) = NULL; }
2510
-#line 2511 "parser.tab.c" /* yacc.c:1646  */
2501
+#line 2502 "parser.tab.c" /* yacc.c:1646  */
2511 2502
     break;
2512 2503
 
2513 2504
   case 100:
2514
-#line 498 "parser.y" /* yacc.c:1646  */
2505
+#line 489 "parser.y" /* yacc.c:1646  */
2515 2506
     {
2516 2507
 	paramlist_node *pl = (yyvsp[-3]);
2517 2508
 	identlist_node *curk;
@@ -2546,11 +2537,11 @@ yyreduce:
2546 2537
 	curv->next = NULL;
2547 2538
 	(yyval) = pl;
2548 2539
 }
2549
-#line 2550 "parser.tab.c" /* yacc.c:1646  */
2540
+#line 2541 "parser.tab.c" /* yacc.c:1646  */
2550 2541
     break;
2551 2542
 
2552 2543
   case 101:
2553
-#line 532 "parser.y" /* yacc.c:1646  */
2544
+#line 523 "parser.y" /* yacc.c:1646  */
2554 2545
     {
2555 2546
 	paramlist_node *pl = (yyvsp[-2]);
2556 2547
 	if(!pl) {
@@ -2563,11 +2554,11 @@ yyreduce:
2563 2554
 	pl-> rest = (yyvsp[0]);
2564 2555
 	(yyval) = pl;
2565 2556
 }
2566
-#line 2567 "parser.tab.c" /* yacc.c:1646  */
2557
+#line 2558 "parser.tab.c" /* yacc.c:1646  */
2567 2558
     break;
2568 2559
 
2569 2560
   case 102:
2570
-#line 544 "parser.y" /* yacc.c:1646  */
2561
+#line 535 "parser.y" /* yacc.c:1646  */
2571 2562
     {
2572 2563
 	paramlist_node *pl = (yyvsp[-1]);
2573 2564
 	identlist_node *cura;
@@ -2593,39 +2584,39 @@ yyreduce:
2593 2584
 	cura->next = NULL;
2594 2585
 	(yyval) = pl;
2595 2586
 }
2596
-#line 2597 "parser.tab.c" /* yacc.c:1646  */
2587
+#line 2588 "parser.tab.c" /* yacc.c:1646  */
2597 2588
     break;
2598 2589
 
2599 2590
   case 103:
2600
-#line 569 "parser.y" /* yacc.c:1646  */
2591
+#line 560 "parser.y" /* yacc.c:1646  */
2601 2592
     { (yyval) = (yyvsp[-1]); }
2602
-#line 2603 "parser.tab.c" /* yacc.c:1646  */
2593
+#line 2594 "parser.tab.c" /* yacc.c:1646  */
2603 2594
     break;
2604 2595
 
2605 2596
   case 104:
2606
-#line 573 "parser.y" /* yacc.c:1646  */
2597
+#line 564 "parser.y" /* yacc.c:1646  */
2607 2598
     { (yyval) = NULL; }
2608
-#line 2609 "parser.tab.c" /* yacc.c:1646  */
2599
+#line 2600 "parser.tab.c" /* yacc.c:1646  */
2609 2600
     break;
2610 2601
 
2611 2602
   case 105:
2612
-#line 574 "parser.y" /* yacc.c:1646  */
2603
+#line 565 "parser.y" /* yacc.c:1646  */
2613 2604
     {
2614 2605
 	(yyval) = NEW(assoclist_node);
2615 2606
 	AS((yyval), assoclist_node)->item = (yyvsp[0]);
2616 2607
 	AS((yyval), assoclist_node)->next = NULL;
2617 2608
 }
2618
-#line 2619 "parser.tab.c" /* yacc.c:1646  */
2609
+#line 2610 "parser.tab.c" /* yacc.c:1646  */
2619 2610
     break;
2620 2611
 
2621 2612
   case 106:
2622
-#line 579 "parser.y" /* yacc.c:1646  */
2613
+#line 570 "parser.y" /* yacc.c:1646  */
2623 2614
     { (yyval) = (yyvsp[-1]); }
2624
-#line 2625 "parser.tab.c" /* yacc.c:1646  */
2615
+#line 2616 "parser.tab.c" /* yacc.c:1646  */
2625 2616
     break;
2626 2617
 
2627 2618
   case 107:
2628
-#line 580 "parser.y" /* yacc.c:1646  */
2619
+#line 571 "parser.y" /* yacc.c:1646  */
2629 2620
     {
2630 2621
 	assoclist_node *cur = (yyvsp[-1]);
2631 2622
 	while(cur->next) cur = cur->next;
@@ -2635,21 +2626,21 @@ yyreduce:
2635 2626
 	cur->next = NULL;
2636 2627
 	(yyval) = (yyvsp[-1]);
2637 2628
 }
2638
-#line 2639 "parser.tab.c" /* yacc.c:1646  */
2629
+#line 2630 "parser.tab.c" /* yacc.c:1646  */
2639 2630
     break;
2640 2631
 
2641 2632
   case 108:
2642
-#line 592 "parser.y" /* yacc.c:1646  */
2633
+#line 583 "parser.y" /* yacc.c:1646  */
2643 2634
     {
2644 2635
 	(yyval) = NEW(associtem_node);
2645 2636
 	AS((yyval), associtem_node)->key = (yyvsp[-3]);
2646 2637
 	AS((yyval), associtem_node)->value = (yyvsp[0]);
2647 2638
 }
2648
-#line 2649 "parser.tab.c" /* yacc.c:1646  */
2639
+#line 2640 "parser.tab.c" /* yacc.c:1646  */
2649 2640
     break;
2650 2641
 
2651 2642
   case 109:
2652
-#line 597 "parser.y" /* yacc.c:1646  */
2643
+#line 588 "parser.y" /* yacc.c:1646  */
2653 2644
     {
2654 2645
 	(yyval) = NEW(associtem_node);
2655 2646
 	AS((yyval), associtem_node)->key = NEW_EX();
@@ -2659,11 +2650,11 @@ yyreduce:
2659 2650
 	AS((yyval), associtem_node)->key->lit->str = (yyvsp[-2]);
2660 2651
 	AS((yyval), associtem_node)->value = (yyvsp[0]);
2661 2652
 }
2662
-#line 2663 "parser.tab.c" /* yacc.c:1646  */
2653
+#line 2654 "parser.tab.c" /* yacc.c:1646  */
2663 2654
     break;
2664 2655
 
2665 2656
 
2666
-#line 2667 "parser.tab.c" /* yacc.c:1646  */
2657
+#line 2658 "parser.tab.c" /* yacc.c:1646  */
2667 2658
       default: break;
2668 2659
     }
2669 2660
   /* User semantic actions sometimes alter yychar, and that requires
@@ -2898,7 +2889,7 @@ yyreturn:
2898 2889
 #endif
2899 2890
   return yyresult;
2900 2891
 }
2901
-#line 613 "parser.y" /* yacc.c:1906  */
2892
+#line 604 "parser.y" /* yacc.c:1906  */
2902 2893
 
2903 2894
 
2904 2895
 // TODO

+ 4
- 13
parser.y View File

@@ -348,23 +348,14 @@ ulen_expr:
348 348
 ;
349 349
 
350 350
 call_expr:
351
-  call_expr LPAREN expr_list RPAREN { $$ = NEW_EX(); AS_EX($$)->type = EX_CALL; AS_EX($$)->call = NEW(call_node); AS_EX($$)->call->expr = $1; AS_EX($$)->call->args = $3; }
351
+  call_expr LPAREN expr_list RPAREN { $$ = NEW_EX(); AS_EX($$)->type = EX_CALL; AS_EX($$)->call = NEW(call_node); AS_EX($$)->call->expr = $1; AS_EX($$)->call->args = $3; AS_EX($$)->call->method = NULL; }
352 352
 | call_expr COLON IDENT LPAREN expr_list RPAREN {
353 353
 	$$ = NEW_EX();
354 354
 	AS_EX($$)->type = EX_CALL;
355 355
 	AS_EX($$)->call = NEW(call_node);
356
-	AS_EX($$)->call->expr = NEW_EX();
357
-	AS_EX($$)->call->expr->type = EX_INDEX;
358
-	AS_EX($$)->call->expr->index = NEW(index_node);
359
-	AS_EX($$)->call->expr->index->expr = $1;
360
-	AS_EX($$)->call->expr->index->index = NEW_EX();
361
-	AS_EX($$)->call->expr->index->index->type = EX_LIT;
362
-	AS_EX($$)->call->expr->index->index->lit = NEW(lit_node);
363
-	AS_EX($$)->call->expr->index->index->lit->type = LIT_STRING;
364
-	AS_EX($$)->call->expr->index->index->lit->str = $3;
365
-	AS_EX($$)->call->args = NEW(exprlist_node);
366
-	AS_EX($$)->call->args->expr = ex_copy($1);
367
-	AS_EX($$)->call->args->next = $5;
356
+	AS_EX($$)->call->expr = $1;
357
+	AS_EX($$)->call->args = $5;
358
+	AS_EX($$)->call->method = $3;
368 359
 }
369 360
 | funcdecl_expr { $$ = $1; }
370 361
 ;

+ 24
- 2
runtime.c View File

@@ -168,6 +168,7 @@ expr_node *ex_copy(expr_node *old) {
168 168
 			new->call = NEW(call_node);
169 169
 			new->call->expr = ex_copy(old->call->expr);
170 170
 			new->call->args = exl_copy(old->call->args);
171
+			new->call->method = old->call->method ? strdup(old->call->method) : NULL;
171 172
 			break;
172 173
 
173 174
 		case EX_FUNCDECL:
@@ -406,6 +407,7 @@ void ex_free(expr_node *expr) {
406 407
 		case EX_CALL:
407 408
 			ex_free(expr->call->expr);
408 409
 			exl_free(expr->call->args);
410
+			free(expr->call->method);
409 411
 			free(expr->call);
410 412
 			break;
411 413
 
@@ -764,7 +766,25 @@ sol_object_t *sol_eval_inner(sol_state_t *state, expr_node *expr, jmp_buf jmp) {
764 766
 			ERR_CHECK(state);
765 767
 			list = sol_new_list(state);
766 768
 			ERR_CHECK(state);
767
-			sol_list_insert(state, list, 0, value);
769
+			if(expr->call->method) {
770
+				left = sol_incref(value);
771
+				sol_list_insert(state, list, 0, value);
772
+				right = sol_new_string(state, expr->call->method);
773
+				sol_list_insert(state, list, 1, right);
774
+				sol_obj_free(right);
775
+				res = CALL_METHOD(state, value, index, list);
776
+				sol_obj_free(value);
777
+				value = sol_incref(res);
778
+				sol_obj_free(res);
779
+				ERR_CHECK(state);
780
+				sol_obj_free(list);
781
+				list = sol_new_list(state);
782
+				sol_list_insert(state, list, 0, value);
783
+				sol_list_insert(state, list, 1, left);
784
+				sol_obj_free(left);
785
+			} else {
786
+				sol_list_insert(state, list, 0, value);
787
+			}
768 788
 			cure = expr->call->args;
769 789
 			while(cure) {
770 790
 				if(cure->expr) {
@@ -774,8 +794,8 @@ sol_object_t *sol_eval_inner(sol_state_t *state, expr_node *expr, jmp_buf jmp) {
774 794
 				cure = cure->next;
775 795
 			}
776 796
 			res = CALL_METHOD(state, value, call, list);
777
-			sol_obj_free(value);
778 797
 			sol_obj_free(list);
798
+			sol_obj_free(value);
779 799
 			ERR_CHECK(state);
780 800
 			return res;
781 801
 			break;
@@ -793,6 +813,8 @@ sol_object_t *sol_eval_inner(sol_state_t *state, expr_node *expr, jmp_buf jmp) {
793 813
 					curi = curi->next;
794 814
 					cure = cure->next;
795 815
 				}
816
+			} else {
817
+				res->rest = NULL;
796 818
 			}
797 819
 			if(expr->funcdecl->name) {
798 820
 				sol_state_assign_l_name(state, expr->funcdecl->name, res);

+ 14
- 5
sol.h View File

@@ -10,7 +10,7 @@
10 10
 #include "dsl/dsl.h"
11 11
 
12 12
 /** The version of the project, as made available through `debug.version`. */
13
-#define VERSION "0.3a0"
13
+#define SOL_VERSION "0.3a1"
14 14
 /** The hexadecimal version of the project, formatted 0xAAIIRPP where:
15 15
  * 
16 16
  * - AA is the two-digit major version
@@ -20,7 +20,7 @@
20 20
  *
21 21
  * This value is guaranteed to only monotonically increase by revision.
22 22
  */
23
-#define HEXVER 0x0003A00
23
+#define SOL_HEXVER 0x0003A01
24 24
 
25 25
 #ifndef SOL_ICACHE_MIN
26 26
 /** The smallest integer to cache. */
@@ -335,8 +335,12 @@ typedef struct sol_tag_object_t {
335 335
 			/* For `SOL_FUNCTION`, the name of an argument that receives extra parameters as a list (otherwise NULL). */
336 336
 			char *rest;
337 337
 		};
338
-		/** For `SOL_CFUNCTION`, the C function pointer. */
339
-		sol_cfunc_t cfunc;
338
+		struct {
339
+			/** For `SOL_CFUNCTION`, the C function pointer. */
340
+			sol_cfunc_t cfunc;
341
+			/** For `SOL_CFUNCTION`, the name of this function, or NULL. */
342
+			char *cfname;
343
+		};
340 344
 		/** For `SOL_STMT` and `SOL_EXPR`, the `stmt_node` or `expr_node` pointer, respectively. */
341 345
 		void *node;
342 346
 		struct {
@@ -418,8 +422,12 @@ typedef struct sol_tag_state_t {
418 422
 #endif
419 423
 	sol_object_t *lastvalue; ///< Holds the value of the last expression evaluated, returned by an `if` expression
420 424
 	sol_object_t *loopvalue; ///< Holds an initially-empty list appended to by `continue <expr>` or set to another object by `break <expr>`
425
+	unsigned short features; ///< A flag field used to control the Sol initialization processs
421 426
 } sol_state_t;
422 427
 
428
+/** Don't run user initialization files. */
429
+#define SOL_FT_NO_USR_INIT 0x0001
430
+
423 431
 // state.c
424 432
 
425 433
 /** Initializes the state.
@@ -1021,7 +1029,7 @@ void sol_map_invert(sol_state_t *, sol_object_t *);
1021 1029
 // sol_object_t *sol_new_stmtnode(sol_state_t *, stmt_node *);
1022 1030
 // sol_object_t *sol_new_exprnode(sol_state_t *, expr_node *);
1023 1031
 
1024
-sol_object_t *sol_new_cfunc(sol_state_t *, sol_cfunc_t);
1032
+sol_object_t *sol_new_cfunc(sol_state_t *, sol_cfunc_t, char *);
1025 1033
 sol_object_t *sol_new_cdata(sol_state_t *, void *, sol_ops_t *);
1026 1034
 
1027 1035
 sol_object_t *sol_new_buffer(sol_state_t *, void *, ssize_t, sol_owntype_t, sol_freefunc_t, sol_movefunc_t);
@@ -1067,6 +1075,7 @@ sol_object_t *sol_f_list_free(sol_state_t *, sol_object_t *);
1067 1075
 sol_object_t *sol_f_map_free(sol_state_t *, sol_object_t *);
1068 1076
 sol_object_t *sol_f_mcell_free(sol_state_t *, sol_object_t *);
1069 1077
 sol_object_t *sol_f_func_free(sol_state_t *, sol_object_t *);
1078
+sol_object_t *sol_f_cfunc_free(sol_state_t *, sol_object_t *);
1070 1079
 sol_object_t *sol_f_astnode_free(sol_state_t *, sol_object_t *);
1071 1080
 sol_object_t *sol_f_buffer_free(sol_state_t *, sol_object_t *);
1072 1081
 sol_object_t *sol_f_dylib_free(sol_state_t *, sol_object_t *);

+ 18
- 3
solrun.c View File

@@ -6,10 +6,12 @@ int main(int argc, char **argv) {
6 6
 	stmt_node *program;
7 7
 	sol_state_t state;
8 8
 	char *c;
9
-	int printtree = 0;
9
+	int printtree = 0, clean = 1;
10 10
 	FILE *prgstream = stdin;
11 11
 	int result = 0;
12 12
 
13
+	state.features = 0;
14
+
13 15
 	if(argc > 1) {
14 16
 		c = argv[1];
15 17
 		while(*c) {
@@ -28,6 +30,11 @@ int main(int argc, char **argv) {
28 30
 						return 2;
29 31
 					}
30 32
 					prgstream = fopen(argv[2], "r");
33
+					break;
34
+
35
+				case 'i':
36
+					state.features |= SOL_FT_NO_USR_INIT;
37
+					break;
31 38
 			}
32 39
 			c++;
33 40
 		}
@@ -49,13 +56,21 @@ int main(int argc, char **argv) {
49 56
 		fclose(prgstream);
50 57
 	}
51 58
 
52
-	sol_state_init(&state);
59
+	if(!sol_state_init(&state)) {
60
+		printf("State init error (internal bug)\n");
61
+		result = 2;
62
+		clean = 0;
63
+		goto out_results;
64
+	}
65
+
53 66
 	if(printtree) {
54 67
 		st_print(&state, program);
55 68
 	}
56 69
 
57 70
 	sol_exec(&state, program);
58 71
 
72
+out_results:
73
+
59 74
 	if(sol_has_error(&state)) {
60 75
 		printf("Error: ");
61 76
 		ob_print(state.error);
@@ -72,7 +87,7 @@ int main(int argc, char **argv) {
72 87
 		}
73 88
 	}
74 89
 	st_free(program);
75
-	sol_state_cleanup(&state);
90
+	if(clean) sol_state_cleanup(&state);
76 91
 
77 92
 	return result;
78 93
 }

+ 85
- 80
state.c View File

@@ -157,6 +157,7 @@ int sol_state_init(sol_state_t *state) {
157 157
 	state->CFuncOps.tname = "cfunction";
158 158
 	state->CFuncOps.call = sol_f_cfunc_call;
159 159
 	state->CFuncOps.tostring = sol_f_cfunc_tostring;
160
+	state->CFuncOps.free = sol_f_cfunc_free;
160 161
 
161 162
 	state->ASTNodeOps.tname = "astnode";
162 163
 	state->ASTNodeOps.call = sol_f_astnode_call;
@@ -233,35 +234,37 @@ int sol_state_init(sol_state_t *state) {
233 234
 
234 235
 	// NB: None is actually a keyword in the language--it doesn't need to be a
235 236
 	// global (see parser.y)
236
-	sol_map_borrow_name(state, globals, "OutOfMemory", state->OutOfMemory);
237
-	sol_map_borrow_name(state, globals, "toint", sol_new_cfunc(state, sol_f_toint));
238
-	sol_map_borrow_name(state, globals, "tofloat", sol_new_cfunc(state, sol_f_tofloat));
239
-	sol_map_borrow_name(state, globals, "tostring", sol_new_cfunc(state, sol_f_tostring));
240
-	sol_map_borrow_name(state, globals, "try", sol_new_cfunc(state, sol_f_try));
241
-	sol_map_borrow_name(state, globals, "apply", sol_new_cfunc(state, sol_f_apply));
242
-	sol_map_borrow_name(state, globals, "error", sol_new_cfunc(state, sol_f_error));
243
-	sol_map_borrow_name(state, globals, "type", sol_new_cfunc(state, sol_f_type));
244
-	sol_map_borrow_name(state, globals, "prepr", sol_new_cfunc(state, sol_f_prepr));
245
-	sol_map_borrow_name(state, globals, "print", sol_new_cfunc(state, sol_f_print));
246
-	sol_map_borrow_name(state, globals, "rawget", sol_new_cfunc(state, sol_f_rawget));
247
-	sol_map_borrow_name(state, globals, "rawset", sol_new_cfunc(state, sol_f_rawset));
248
-	sol_map_borrow_name(state, globals, "range", sol_new_cfunc(state, sol_f_range));
249
-	sol_map_borrow_name(state, globals, "exec", sol_new_cfunc(state, sol_f_exec));
250
-	sol_map_borrow_name(state, globals, "eval", sol_new_cfunc(state, sol_f_eval));
251
-	sol_map_borrow_name(state, globals, "execfile", sol_new_cfunc(state, sol_f_execfile));
252
-	sol_map_borrow_name(state, globals, "parse", sol_new_cfunc(state, sol_f_parse));
253
-	sol_map_borrow_name(state, globals, "ord", sol_new_cfunc(state, sol_f_ord));
254
-	sol_map_borrow_name(state, globals, "chr", sol_new_cfunc(state, sol_f_chr));
237
+	// OutOfMemory (and other important singlets) are set, not borrowed,
238
+	// because the state still holds a reference to them
239
+	sol_map_set_name(state, globals, "OutOfMemory", state->OutOfMemory);
240
+	sol_map_borrow_name(state, globals, "toint", sol_new_cfunc(state, sol_f_toint, "toint"));
241
+	sol_map_borrow_name(state, globals, "tofloat", sol_new_cfunc(state, sol_f_tofloat, "tofloat"));
242
+	sol_map_borrow_name(state, globals, "tostring", sol_new_cfunc(state, sol_f_tostring, "tostring"));
243
+	sol_map_borrow_name(state, globals, "try", sol_new_cfunc(state, sol_f_try, "try"));
244
+	sol_map_borrow_name(state, globals, "apply", sol_new_cfunc(state, sol_f_apply, "apply"));
245
+	sol_map_borrow_name(state, globals, "error", sol_new_cfunc(state, sol_f_error, "error"));
246
+	sol_map_borrow_name(state, globals, "type", sol_new_cfunc(state, sol_f_type, "type"));
247
+	sol_map_borrow_name(state, globals, "prepr", sol_new_cfunc(state, sol_f_prepr, "prepr"));
248
+	sol_map_borrow_name(state, globals, "print", sol_new_cfunc(state, sol_f_print, "print"));
249
+	sol_map_borrow_name(state, globals, "rawget", sol_new_cfunc(state, sol_f_rawget, "rawget"));
250
+	sol_map_borrow_name(state, globals, "rawset", sol_new_cfunc(state, sol_f_rawset, "rawset"));
251
+	sol_map_borrow_name(state, globals, "range", sol_new_cfunc(state, sol_f_range, "range"));
252
+	sol_map_borrow_name(state, globals, "exec", sol_new_cfunc(state, sol_f_exec, "exec"));
253
+	sol_map_borrow_name(state, globals, "eval", sol_new_cfunc(state, sol_f_eval, "eval"));
254
+	sol_map_borrow_name(state, globals, "execfile", sol_new_cfunc(state, sol_f_execfile, "execfile"));
255
+	sol_map_borrow_name(state, globals, "parse", sol_new_cfunc(state, sol_f_parse, "parse"));
256
+	sol_map_borrow_name(state, globals, "ord", sol_new_cfunc(state, sol_f_ord, "ord"));
257
+	sol_map_borrow_name(state, globals, "chr", sol_new_cfunc(state, sol_f_chr, "chr"));
255 258
 
256 259
 	mod = sol_new_map(state);
257
-	sol_map_borrow_name(state, mod, "getref", sol_new_cfunc(state, sol_f_debug_getref));
258
-	sol_map_borrow_name(state, mod, "setref", sol_new_cfunc(state, sol_f_debug_setref));
259
-	sol_map_borrow_name(state, mod, "closure", sol_new_cfunc(state, sol_f_debug_closure));
260
-	sol_map_borrow_name(state, mod, "globals", sol_new_cfunc(state, sol_f_debug_globals));
261
-	sol_map_borrow_name(state, mod, "locals", sol_new_cfunc(state, sol_f_debug_locals));
262
-	sol_map_borrow_name(state, mod, "scopes", sol_new_cfunc(state, sol_f_debug_scopes));
263
-	sol_map_borrow_name(state, mod, "version", sol_new_string(state, VERSION));
264
-	sol_map_borrow_name(state, mod, "hexversion", sol_new_int(state, HEXVER));
260
+	sol_map_borrow_name(state, mod, "getref", sol_new_cfunc(state, sol_f_debug_getref, "debug.getref"));
261
+	sol_map_borrow_name(state, mod, "setref", sol_new_cfunc(state, sol_f_debug_setref, "debug.setref"));
262
+	sol_map_borrow_name(state, mod, "closure", sol_new_cfunc(state, sol_f_debug_closure, "debug.closure"));
263
+	sol_map_borrow_name(state, mod, "globals", sol_new_cfunc(state, sol_f_debug_globals, "debug.globals"));
264
+	sol_map_borrow_name(state, mod, "locals", sol_new_cfunc(state, sol_f_debug_locals, "debug.locals"));
265
+	sol_map_borrow_name(state, mod, "scopes", sol_new_cfunc(state, sol_f_debug_scopes, "debug.scopes"));
266
+	sol_map_borrow_name(state, mod, "version", sol_new_string(state, SOL_VERSION));
267
+	sol_map_borrow_name(state, mod, "hexversion", sol_new_int(state, SOL_HEXVER));
265 268
 #ifdef SOL_ICACHE
266 269
 	sol_map_borrow_name(state, mod, "icache_min", sol_new_int(state, SOL_ICACHE_MIN));
267 270
 	sol_map_borrow_name(state, mod, "icache_max", sol_new_int(state, SOL_ICACHE_MAX));
@@ -270,15 +273,15 @@ int sol_state_init(sol_state_t *state) {
270 273
 	sol_obj_free(mod);
271 274
 
272 275
 	mod = sol_new_map(state);
273
-	sol_map_borrow_name(state, mod, "str", sol_new_cfunc(state, sol_f_iter_str));
274
-	sol_map_borrow_name(state, mod, "list", sol_new_cfunc(state, sol_f_iter_list));
275
-	sol_map_borrow_name(state, mod, "map", sol_new_cfunc(state, sol_f_iter_map));
276
+	sol_map_borrow_name(state, mod, "str", sol_new_cfunc(state, sol_f_iter_str, "iter.str"));
277
+	sol_map_borrow_name(state, mod, "list", sol_new_cfunc(state, sol_f_iter_list, "iter.list"));
278
+	sol_map_borrow_name(state, mod, "map", sol_new_cfunc(state, sol_f_iter_map, "iter.map"));
276 279
 	sol_register_module_name(state, "iter", mod);
277 280
 	sol_obj_free(mod);
278 281
 
279 282
 	mod = sol_new_map(state);
280
-	sol_map_borrow_name(state, mod, "readline", sol_new_cfunc(state, sol_f_readline_readline));
281
-	sol_map_borrow_name(state, mod, "add_history", sol_new_cfunc(state, sol_f_readline_add_history));
283
+	sol_map_borrow_name(state, mod, "readline", sol_new_cfunc(state, sol_f_readline_readline, "readline.readline"));
284
+	sol_map_borrow_name(state, mod, "add_history", sol_new_cfunc(state, sol_f_readline_add_history, "readline.add_history"));
282 285
 	sol_register_module_name(state, "readline", mod);
283 286
 	sol_obj_free(mod);
284 287
 
@@ -332,7 +335,7 @@ int sol_state_init(sol_state_t *state) {
332 335
 	sol_map_borrow_name(state, mod, "KIND_STMT", sol_new_int(state, -1));
333 336
 	sol_map_borrow_name(state, mod, "KIND_EXPR", sol_new_int(state, -2));
334 337
 	sol_map_invert(state, mod);
335
-	sol_map_borrow_name(state, mod, "print", sol_new_cfunc(state, sol_f_ast_print));
338
+	sol_map_borrow_name(state, mod, "print", sol_new_cfunc(state, sol_f_ast_print, "ast.print"));
336 339
 	sol_register_module_name(state, "ast", mod);
337 340
 
338 341
 	btype = sol_new_map(state);
@@ -397,10 +400,10 @@ int sol_state_init(sol_state_t *state) {
397 400
 	sol_map_invert(state, bobj);
398 401
 
399 402
 	mod = sol_new_map(state);
400
-	sol_map_borrow_name(state, mod, "new", sol_new_cfunc(state, sol_f_buffer_new));
401
-	sol_map_borrow_name(state, mod, "fromstring", sol_new_cfunc(state, sol_f_buffer_fromstring));
402
-	sol_map_borrow_name(state, mod, "fromobject", sol_new_cfunc(state, sol_f_buffer_fromobject));
403
-	sol_map_borrow_name(state, mod, "fromaddress", sol_new_cfunc(state, sol_f_buffer_fromaddress));
403
+	sol_map_borrow_name(state, mod, "new", sol_new_cfunc(state, sol_f_buffer_new, "buffer.new"));
404
+	sol_map_borrow_name(state, mod, "fromstring", sol_new_cfunc(state, sol_f_buffer_fromstring, "buffer.fromstring"));
405
+	sol_map_borrow_name(state, mod, "fromobject", sol_new_cfunc(state, sol_f_buffer_fromobject, "buffer.fromobject"));
406
+	sol_map_borrow_name(state, mod, "fromaddress", sol_new_cfunc(state, sol_f_buffer_fromaddress, "buffer.fromaddress"));
404 407
 	sol_map_set_name(state, mod, "type", btype);
405 408
 	sol_map_set_name(state, mod, "sizeof", bsize);
406 409
 	sol_map_set_name(state, mod, "objtype", bobj);
@@ -421,44 +424,44 @@ int sol_state_init(sol_state_t *state) {
421 424
 	sol_map_borrow_name(state, mod, "SEEK_END", sol_new_int(state, SEEK_END));
422 425
 	sol_map_borrow_name(state, mod, "ALL", sol_new_string(state, "ALL"));
423 426
 	sol_map_borrow_name(state, mod, "LINE", sol_new_string(state, "LINE"));
424
-	sol_map_borrow_name(state, mod, "open", sol_new_cfunc(state, sol_f_stream_open));
425
-	sol_map_borrow_name(state, mod, "__setindex", sol_new_cfunc(state, sol_f_io_setindex));
426
-	sol_map_borrow_name(state, mod, "__index", sol_new_cfunc(state, sol_f_io_index));
427
+	sol_map_borrow_name(state, mod, "open", sol_new_cfunc(state, sol_f_stream_open, "io.open"));
428
+	sol_map_borrow_name(state, mod, "__setindex", sol_new_cfunc(state, sol_f_io_setindex, "io.__setindex"));
429
+	sol_map_borrow_name(state, mod, "__index", sol_new_cfunc(state, sol_f_io_index, "io.__index"));
427 430
 	sol_register_module_name(state, "io", mod);
428 431
 	sol_obj_free(mod);
429 432
 
430 433
 	meths = sol_new_map(state);
431
-	sol_map_borrow_name(state, meths, "get", sol_new_cfunc(state, sol_f_buffer_get));
432
-	sol_map_borrow_name(state, meths, "set", sol_new_cfunc(state, sol_f_buffer_set));
433
-	sol_map_borrow_name(state, meths, "address", sol_new_cfunc(state, sol_f_buffer_address));
434
-	sol_map_borrow_name(state, meths, "size", sol_new_cfunc(state, sol_f_buffer_size));
434
+	sol_map_borrow_name(state, meths, "get", sol_new_cfunc(state, sol_f_buffer_get, "buffer.get"));
435
+	sol_map_borrow_name(state, meths, "set", sol_new_cfunc(state, sol_f_buffer_set, "buffer.set"));
436
+	sol_map_borrow_name(state, meths, "address", sol_new_cfunc(state, sol_f_buffer_address, "buffer.address"));
437
+	sol_map_borrow_name(state, meths, "size", sol_new_cfunc(state, sol_f_buffer_size, "buffer.size"));
435 438
 	sol_register_methods_name(state, "buffer", meths);
436 439
 	sol_obj_free(meths);
437 440
 
438 441
 	meths = sol_new_map(state);
439
-	sol_map_borrow_name(state, meths, "copy", sol_new_cfunc(state, sol_f_list_copy));
440
-	sol_map_borrow_name(state, meths, "insert", sol_new_cfunc(state, sol_f_list_insert));
441
-	sol_map_borrow_name(state, meths, "remove", sol_new_cfunc(state, sol_f_list_remove));
442
-	sol_map_borrow_name(state, meths, "truncate", sol_new_cfunc(state, sol_f_list_truncate));
443
-	sol_map_borrow_name(state, meths, "map", sol_new_cfunc(state, sol_f_list_map));
444
-	sol_map_borrow_name(state, meths, "filter", sol_new_cfunc(state, sol_f_list_filter));
442
+	sol_map_borrow_name(state, meths, "copy", sol_new_cfunc(state, sol_f_list_copy, "list.copy"));
443
+	sol_map_borrow_name(state, meths, "insert", sol_new_cfunc(state, sol_f_list_insert, "list.insert"));
444
+	sol_map_borrow_name(state, meths, "remove", sol_new_cfunc(state, sol_f_list_remove, "list.remove"));
445
+	sol_map_borrow_name(state, meths, "truncate", sol_new_cfunc(state, sol_f_list_truncate, "list.truncate"));
446
+	sol_map_borrow_name(state, meths, "map", sol_new_cfunc(state, sol_f_list_map, "list.map"));
447
+	sol_map_borrow_name(state, meths, "filter", sol_new_cfunc(state, sol_f_list_filter, "list.filter"));
445 448
 	sol_register_methods_name(state, "list", meths);
446 449
 	sol_obj_free(meths);
447 450
 
448 451
 	meths = sol_new_map(state);
449
-	sol_map_borrow_name(state, meths, "read", sol_new_cfunc(state, sol_f_stream_read));
450
-	sol_map_borrow_name(state, meths, "write", sol_new_cfunc(state, sol_f_stream_write));
451
-	sol_map_borrow_name(state, meths, "seek", sol_new_cfunc(state, sol_f_stream_seek));
452
-	sol_map_borrow_name(state, meths, "tell", sol_new_cfunc(state, sol_f_stream_tell));
453
-	sol_map_borrow_name(state, meths, "flush", sol_new_cfunc(state, sol_f_stream_flush));
454
-	sol_map_borrow_name(state, meths, "eof", sol_new_cfunc(state, sol_f_stream_eof));
452
+	sol_map_borrow_name(state, meths, "read", sol_new_cfunc(state, sol_f_stream_read, "stream.read"));
453
+	sol_map_borrow_name(state, meths, "write", sol_new_cfunc(state, sol_f_stream_write, "stream.write"));
454
+	sol_map_borrow_name(state, meths, "seek", sol_new_cfunc(state, sol_f_stream_seek, "stream.seek"));
455
+	sol_map_borrow_name(state, meths, "tell", sol_new_cfunc(state, sol_f_stream_tell, "stream.tell"));
456
+	sol_map_borrow_name(state, meths, "flush", sol_new_cfunc(state, sol_f_stream_flush, "stream.flush"));
457
+	sol_map_borrow_name(state, meths, "eof", sol_new_cfunc(state, sol_f_stream_eof, "stream.eof"));
455 458
 	sol_register_methods_name(state, "stream", meths);
456 459
 	sol_obj_free(meths);
457 460
 
458 461
 	meths = sol_new_map(state);
459
-	sol_map_borrow_name(state, meths, "sub", sol_new_cfunc(state, sol_f_str_sub));
460
-	sol_map_borrow_name(state, meths, "split", sol_new_cfunc(state, sol_f_str_split));
461
-	sol_map_borrow_name(state, meths, "find", sol_new_cfunc(state, sol_f_str_find));
462
+	sol_map_borrow_name(state, meths, "sub", sol_new_cfunc(state, sol_f_str_sub, "str.sub"));
463
+	sol_map_borrow_name(state, meths, "split", sol_new_cfunc(state, sol_f_str_split, "str.split"));
464
+	sol_map_borrow_name(state, meths, "find", sol_new_cfunc(state, sol_f_str_find, "str.find"));
462 465
 	sol_register_methods_name(state, "string", meths);
463 466
 	sol_obj_free(meths);
464 467
 
@@ -469,23 +472,9 @@ int sol_state_init(sol_state_t *state) {
469 472
 	// Perform initialization based on the user profile, if so requested.
470 473
 	// TODO: Make this switchable at runtime.
471 474
 	
472
-	for(i = 0; i < LENGTH(sol_AbsInitPaths); i++) {
473
-		fp = fopen(sol_AbsInitPaths[i], "r");
474
-		if(fp) {
475
-			stmt = sol_compile_file(fp);
476
-			sol_exec(state, stmt);
477
-			st_free(stmt);
478
-			fclose(fp);
479
-		}
480
-	}
481
-
482
-	suffix = getenv("HOME");
483
-	if(suffix) {
484
-		strncpy(sol_TempPath, suffix, TMP_PATH_SZ);
485
-		suffix = sol_TempPath + strlen(sol_TempPath);
486
-		for(i = 0; i < LENGTH(sol_HomeInitPaths); i++) {
487
-			strncpy(suffix, sol_HomeInitPaths[i], TMP_PATH_SZ - (suffix - sol_TempPath));
488
-			fp = fopen(sol_TempPath, "r");
475
+	if(!(state->features & SOL_FT_NO_USR_INIT)) {
476
+		for(i = 0; i < LENGTH(sol_AbsInitPaths); i++) {
477
+			fp = fopen(sol_AbsInitPaths[i], "r");
489 478
 			if(fp) {
490 479
 				stmt = sol_compile_file(fp);
491 480
 				sol_exec(state, stmt);
@@ -493,10 +482,26 @@ int sol_state_init(sol_state_t *state) {
493 482
 				fclose(fp);
494 483
 			}
495 484
 		}
496
-	}
497
-	
498
-	if(sol_has_error(state)) {
499
-		goto cleanup;
485
+
486
+		suffix = getenv("HOME");
487
+		if(suffix) {
488
+			strncpy(sol_TempPath, suffix, TMP_PATH_SZ);
489
+			suffix = sol_TempPath + strlen(sol_TempPath);
490
+			for(i = 0; i < LENGTH(sol_HomeInitPaths); i++) {
491
+				strncpy(suffix, sol_HomeInitPaths[i], TMP_PATH_SZ - (suffix - sol_TempPath));
492
+				fp = fopen(sol_TempPath, "r");
493
+				if(fp) {
494
+					stmt = sol_compile_file(fp);
495
+					sol_exec(state, stmt);
496
+					st_free(stmt);
497
+					fclose(fp);
498
+				}
499
+			}
500
+		}
501
+		
502
+		if(sol_has_error(state)) {
503
+			goto cleanup;
504
+		}
500 505
 	}
501 506
 
502 507
 	// We're all set!

+ 1
- 0
tests/_lib.sol View File

@@ -2,6 +2,7 @@ func assert(x, msg, _test_count = 0)
2 2
 	_test_count += 1
3 3
 	io.stdout:write('Test ' + tostring(_test_count) + ': ' + msg)
4 4
 	if !x then
5
+		print("")
5 6
 		error("Assertion failed: " + tostring(msg))
6 7
 	end
7 8
 	print("...passed")

+ 20
- 0
tests/lang_method.sol View File

@@ -0,0 +1,20 @@
1
+execfile("tests/_lib.sol")
2
+
3
+func count(v, c=0, lv=0)
4
+	c += 1
5
+	lv = v
6
+end
7
+
8
+d = {method = count}
9
+d:method()
10
+
11
+assert_eq(d, count.closure.lv, "method called with self")
12
+assert_eq(1, count.closure.c, "method evaluated once (expr is map)")
13
+
14
+func count(c = 0)
15
+	c += 1
16
+	return {v = c, method = func(self) return self.v end}
17
+end
18
+
19
+assert_eq(count():method(), count.closure.c, "method evaluation consistent")
20
+assert_eq(1, count.closure.c, "method evaluated once (expr is call)")

Loading…
Cancel
Save