Browse Source

Sol Part 39: Now with 100% more arrows!

Graham Northup 5 years ago
parent
commit
cd32a6ff54
5 changed files with 48 additions and 7 deletions
  1. 2
    1
      Makefile
  2. 31
    0
      builtins.c
  3. 4
    4
      interp.sol
  4. 5
    2
      sol.h
  5. 6
    0
      state.c

+ 2
- 1
Makefile View File

@@ -1,9 +1,10 @@
1 1
 CFLAGS= -g 
2
+LDFLAGS= -lm -ldl -lreadline
2 3
 OBJ= lex.yy.o parser.tab.o dsl/seq.o dsl/list.o dsl/array.o dsl/generic.o astprint.o runtime.o gc.o object.o state.o builtins.o solrun.o
3 4
 
4 5
 all: $(OBJ)
5 6
 	git submodule init && git submodule sync && git submodule update
6
-	gcc $(CFLAGS) $? -o sol -lm -ldl
7
+	gcc $(CFLAGS) $? $(LDFLAGS) -o sol 
7 8
 
8 9
 %.o: %.c
9 10
 	gcc -c -o $@ $? $(CFLAGS)

+ 31
- 0
builtins.c View File

@@ -4,6 +4,8 @@
4 4
 #include <math.h>
5 5
 #include <stdint.h>
6 6
 #include <dlfcn.h>
7
+#include <readline/readline.h>
8
+#include <readline/history.h>
7 9
 #include "ast.h"
8 10
 #include "dsl/dsl.h"
9 11
 
@@ -458,6 +460,35 @@ sol_object_t *sol_f_debug_scopes(sol_state_t *state, sol_object_t *args) {
458 460
 	return sol_incref(state->scopes);
459 461
 }
460 462
 
463
+sol_object_t *sol_f_readline_readline(sol_state_t *state, sol_object_t *args) {
464
+	sol_object_t *obj, *objstr, *res;
465
+	char *line;
466
+	if(sol_list_len(state, args) > 0) {
467
+		obj = sol_list_get_index(state, args, 0);
468
+		objstr = sol_cast_string(state, obj);
469
+		line = readline(objstr->str);
470
+		sol_obj_free(obj);
471
+		sol_obj_free(objstr);
472
+	} else {
473
+		line = readline("");
474
+	}
475
+	if(line) {
476
+		res = sol_new_string(state, line);
477
+		free(line);
478
+	} else {
479
+		res = sol_new_string(state, "");
480
+	}
481
+	return res;
482
+}
483
+
484
+sol_object_t *sol_f_readline_add_history(sol_state_t *state, sol_object_t *args) {
485
+	sol_object_t *line = sol_list_get_index(state, args, 0), *linestr = sol_cast_string(state, line);
486
+	add_history(linestr->str);
487
+	sol_obj_free(linestr);
488
+	sol_obj_free(line);
489
+	return sol_incref(state->None);
490
+}
491
+
461 492
 void _sol_freef_seq_iter(void *iter, size_t sz) {
462 493
 	dsl_free_seq_iter((dsl_seq_iter *) iter);
463 494
 }

+ 4
- 4
interp.sol View File

@@ -19,12 +19,12 @@ quit = exit
19 19
 
20 20
 while __interp.running do
21 21
 	if #__interp.buffer then
22
-		io.stdout << __interp.ps2
22
+		__interp.prompt = __interp.ps2
23 23
 	else
24
-		io.stdout << __interp.ps1
24
+		__interp.prompt = __interp.ps1
25 25
 	end
26
-	__interp.line = io.stdin:read(io.LINE)
27
-	__interp.line = __interp.line:sub(0, -1)
26
+	__interp.line = readline.readline(__interp.prompt)
27
+	if #__interp.line then readline.add_history(__interp.line) end
28 28
 	--prepr(__interp.line)
29 29
 	--prepr(__interp)
30 30
 	if (__interp.line:sub(-4, None)=="then") then

+ 5
- 2
sol.h View File

@@ -9,8 +9,8 @@
9 9
 #include <stdarg.h>
10 10
 #include "dsl/dsl.h"
11 11
 
12
-#define VERSION "0.1a4"
13
-#define HEXVER 0x0001A04
12
+#define VERSION "0.1a5"
13
+#define HEXVER 0x0001A05
14 14
 
15 15
 #ifndef SOL_ICACHE_MIN
16 16
 #define SOL_ICACHE_MIN -128
@@ -282,6 +282,9 @@ sol_object_t *sol_f_iter_str(sol_state_t *, sol_object_t *);
282 282
 sol_object_t *sol_f_iter_list(sol_state_t *, sol_object_t *);
283 283
 sol_object_t *sol_f_iter_map(sol_state_t *, sol_object_t *);
284 284
 
285
+sol_object_t *sol_f_readline_readline(sol_state_t *, sol_object_t *);
286
+sol_object_t *sol_f_readline_add_history(sol_state_t *, sol_object_t *);
287
+
285 288
 sol_object_t *sol_f_ast_print(sol_state_t *, sol_object_t *);
286 289
 
287 290
 sol_object_t *sol_f_singlet_tostring(sol_state_t *, sol_object_t *);

+ 6
- 0
state.c View File

@@ -251,6 +251,12 @@ int sol_state_init(sol_state_t *state) {
251 251
 	sol_register_module_name(state, "iter", mod);
252 252
 	sol_obj_free(mod);
253 253
 
254
+	mod = sol_new_map(state);
255
+	sol_map_borrow_name(state, mod, "readline", sol_new_cfunc(state, sol_f_readline_readline));
256
+	sol_map_borrow_name(state, mod, "add_history", sol_new_cfunc(state, sol_f_readline_add_history));
257
+	sol_register_module_name(state, "readline", mod);
258
+	sol_obj_free(mod);
259
+
254 260
 	mod = sol_new_map(state);
255 261
 	sol_map_borrow_name(state, mod, "ST_EXPR", sol_new_int(state, ST_EXPR));
256 262
 	sol_map_borrow_name(state, mod, "ST_IFELSE", sol_new_int(state, ST_IFELSE));

Loading…
Cancel
Save