• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqtwindows誰得cocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

Baremetal Lisp interpreter and compiler for low-resource devices


Commit MetaInfo

Revision00e48640fd91b31d93cc08a071f8e58ec7b6cb8a (tree)
Zeit2020-09-14 09:00:11
AutorAlaskanEmily <emily@alas...>
CommiterAlaskanEmily

Log Message

Add stdin, stdout, and stderr files

Ändern Zusammenfassung

Diff

--- a/sl_i.c
+++ b/sl_i.c
@@ -22,6 +22,8 @@
2222 #include "sl_i_builtin.h"
2323 #include "sl_x.h"
2424
25+#include <stddef.h>
26+
2527 #define SL_I_BIND_CAP_GROWTH 64
2628 #define SL_I_REC_CAP_GROWTH 32
2729 #define SL_I_DEF_CAP_INIT 16
@@ -37,13 +39,19 @@
3739
3840 /*****************************************************************************/
3941
42+static const struct SL_S_Atom sl_i_stdin_name = SL_S_STATIC_ATOM("stdin");
43+static const struct SL_S_Atom sl_i_stdout_name = SL_S_STATIC_ATOM("stdout");
44+static const struct SL_S_Atom sl_i_stderr_name = SL_S_STATIC_ATOM("stderr");
45+
46+/*****************************************************************************/
47+
4048 SL_S_FUNC(void) SL_I_InitRuntime(struct SL_I_Runtime *rt){
4149 unsigned i;
4250
43- SL_S_MemSet(rt, 0, sizeof(struct SL_I_Runtime));
51+ /* Clear up to, but not including, the file ops. */
52+ SL_S_MemSet(rt, 0, offsetof(struct SL_I_Runtime, io));
4453 rt->frames = &(rt->global);
4554
46- rt->num_binds = 4;
4755 rt->cap_binds = SL_I_BIND_CAP_GROWTH;
4856 rt->binds = SL_S_Malloc(SL_I_BIND_CAP_GROWTH * sizeof(struct SL_I_Bind));
4957 i = 0;
@@ -60,10 +68,33 @@ SL_S_FUNC(void) SL_I_InitRuntime(struct SL_I_Runtime *rt){
6068 SL_I_SETUP_BUILTIN(SL_I_Print, sl_i_print_name, 1);
6169 SL_I_SETUP_BUILTIN(SL_I_Open, sl_i_open_name, 2);
6270 SL_I_SETUP_BUILTIN(SL_I_Close, sl_i_close_name, 1);
63- SL_I_SETUP_BUILTIN(SL_I_Read, sl_i_read_name, 2);
64- SL_I_SETUP_BUILTIN(SL_I_Write, sl_i_write_name, 2);
65-
71+ SL_I_SETUP_BUILTIN(SL_I_Read, sl_i_read_name, 3);
72+ SL_I_SETUP_BUILTIN(SL_I_Write, sl_i_write_name, 3);
73+ SL_I_SETUP_BUILTIN(SL_I_WriteAtom, sl_i_write_atom_name, 2);
74+ rt->num_binds = i;
75+
6676 #undef SL_I_SETUP_BUILTIN
77+
78+ rt->global.cap_defs = SL_I_DEF_CAP_INIT;
79+ rt->global.defs = SL_S_Malloc(SL_I_DEF_CAP_INIT * sizeof(struct SL_X_Def));
80+ i = 0;
81+
82+#define SL_I_SETUP_FILE(WHAT) do{ \
83+ (rt->WHAT ## _ptr).ref = ~(sl_s_ref_t)0; \
84+ SL_S_PTR_SET_LINE(&(rt->WHAT ## _ptr), 0); \
85+ (rt->WHAT ## _ptr).data = (rt->io.x_ ## WHAT); \
86+ rt->global.defs[i].value = SL_S_MK_POINTER(&(rt->WHAT ## _ptr)); \
87+ rt->global.defs[i].name = &(sl_i_ ## WHAT ## _name); \
88+ rt->global.defs[i].hint = &sl_x_ptr_hint; \
89+ i++; \
90+}while(0)
91+
92+ SL_I_SETUP_FILE(stdin);
93+ SL_I_SETUP_FILE(stdout);
94+ SL_I_SETUP_FILE(stderr);
95+
96+#undef SL_I_SETUP_FILE
97+ rt->global.num_defs = i;
6798 }
6899
69100 /*****************************************************************************/
@@ -695,6 +726,10 @@ SL_S_FUNC(void) *SL_I_Execute(struct SL_I_Runtime *rt, const void *value){
695726 return (void*)value;
696727 }
697728 }
729+ else if(!SL_S_IS_LIST(value)){
730+ SL_S_INCREF(value);
731+ return (void*)value;
732+ }
698733 else{
699734 /* Welcome to the jungle. */
700735 list = SL_S_PTR_FROM_TAG(value);
--- a/sl_i.h
+++ b/sl_i.h
@@ -86,6 +86,8 @@ struct SL_I_Runtime{
8686 const char *pending_error;
8787 void *error_free_ptr;
8888
89+ /* Handled specially. */
90+ struct SL_S_Pointer stdin_ptr, stdout_ptr, stderr_ptr;
8991 struct SL_X_FileOps io;
9092 };
9193
--- a/sl_i_builtin.c
+++ b/sl_i_builtin.c
@@ -260,3 +260,39 @@ SL_S_FUNC(void) *SL_I_Write(struct SL_I_Runtime *rt,
260260 }
261261 }
262262
263+/*****************************************************************************/
264+
265+const struct SL_S_Atom sl_i_write_atom_name = SL_S_STATIC_ATOM("write-atom");
266+#define SL_I_WRITE_ATOM_ARITY 2
267+
268+SL_S_FUNC(void) *SL_I_WriteAtom(struct SL_I_Runtime *rt,
269+ const struct SL_S_List *args){
270+
271+ const void *matched_args[SL_I_WRITE_ATOM_ARITY];
272+ const struct SL_S_Pointer *file;
273+ const struct SL_S_Atom *data;
274+ int r;
275+
276+ /* Little hack, we can re-use the write flags. */
277+ if(SL_S_Match(args,
278+ matched_args,
279+ sl_i_write_flags + 1,
280+ SL_I_WRITE_ATOM_ARITY,
281+ SL_I_WRITE_ATOM_ARITY) != SL_I_WRITE_ATOM_ARITY){
282+
283+ rt->pending_error = "Args error in write-atom";
284+ return SL_S_NIL;
285+ }
286+
287+ file = SL_S_PTR_FROM_TAG(matched_args[0]);
288+ data = SL_S_PTR_FROM_TAG(matched_args[1]);
289+
290+ r = rt->io.write(file->data, data->text, data->len);
291+ if(r >= 0 && r < SL_X_N_INTEGERS){
292+ return SL_S_MK_ATOM(sl_x_integers + r);
293+ }
294+ else{
295+ return SL_S_MK_ATOM(SL_S_IntToAtom(r, 10));
296+ }
297+}
298+
--- a/sl_i_builtin.h
+++ b/sl_i_builtin.h
@@ -72,6 +72,12 @@ SL_S_FUNC(void) *SL_I_Write(struct SL_I_Runtime *rt,
7272
7373 /*****************************************************************************/
7474
75+const extern struct SL_S_Atom sl_i_write_atom_name;
76+SL_S_FUNC(void) *SL_I_WriteAtom(struct SL_I_Runtime *rt,
77+ const struct SL_S_List *args);
78+
79+/*****************************************************************************/
80+
7581 #ifdef __cplusplus
7682 } // extern "C"
7783 #endif
--- a/sl_i_main.c
+++ b/sl_i_main.c
@@ -43,8 +43,8 @@ int main(int argc, char *argv[]){
4343 path.len = strlen(argv[1]);
4444 path.text = argv[1];
4545
46- SL_I_InitRuntime(&rt);
4746 SL_I_RUNTIME_LIBC_SETUP(&(rt.io));
47+ SL_I_InitRuntime(&rt);
4848
4949 src = SL_X_BufferFile(&(rt.io), &path);
5050 if(src == SL_S_NIL){
--- /dev/null
+++ b/tests/file1.lsp
@@ -0,0 +1,4 @@
1+
2+
3+(write-atom stdout "Hello world\n")
4+