11#include "util.h"
22
3- #include <stdlib.h>
43#include <stdbool.h>
4+ #include <stdio.h>
5+ #include <stdlib.h>
56#include <string.h>
67
7- #include <lua.h>
8- #include <lauxlib.h>
9-
10- #include "linenoise.h"
11- #include "commands.h"
128#include "laco.h"
139
14- /* Check if line is incomplete */
15- static bool incomplete (lua_State * L , int status ) {
16- bool result = false;
17-
18- if (status == LUA_ERRSYNTAX ) {
19- size_t lmess ;
20- const char * mess = lua_tolstring (L , -1 , & lmess );
21-
22- /* Check if the error ends in '<eof>' */
23- size_t eof_size = sizeof (LUA_QL ("<eof>" )) - 1 ;
24- const char * mess_end = mess + lmess - eof_size ;
25- if (strstr (mess , LUA_QL ("<eof>" )) == mess_end ) {
26- lua_pop (L , 1 );
27- result = true;
28- }
29- }
30-
31- return result ;
32- }
33-
34- /* Check if line can be printed */
35- static bool is_printable (lua_State * L , int status ) {
36- bool result = false;
37-
38- if (status == LUA_ERRSYNTAX ) {
39- const char * mess = lua_tostring (L , -1 );
40-
41- bool is_literal = strstr (mess , "unexpected symbol" ) != NULL ;
42- bool is_variable = strstr (mess , "'=' expected" ) != NULL ;
43-
44- if (is_literal || is_variable ) {
45- /* pop off error message */
46- lua_pop (L , 1 );
47-
48- const char * literal = lua_tostring (L , -1 );
49- lua_pop (L , 1 );
50-
51- lua_pushfstring (L , "return %s" , literal );
52-
53- result = true;
54- }
55- } else if (lua_type (L , -1 ) == LUA_TFUNCTION ) {
56- const char * func = lua_tostring (L , -2 );
57-
58- /* check for a return statement */
59- bool is_assignment = strstr (func , "=" );
60-
61- if (!strstr (func , "return " ) && !is_assignment ) {
62- lua_pop (L , 2 );
63- lua_pushfstring (L , "return %s" , func );
64-
65- result = true;
66- }
67- }
68-
69- return result ;
70- }
71-
72- static char * get_line (LacoState * laco , const char * prompt ) {
73- char * line = linenoise (prompt );
74-
75- if (line != NULL ) {
76- linenoiseHistoryAdd (line );
77-
78- if (line [0 ] == ':' ) {
79- laco_handle_command (laco , line );
80- }
81- }
82-
83- return line ;
84- }
85-
86- /* Push a line to the stack and store in history */
87- static bool pushline (LacoState * laco , bool isFirstLine ) {
88- const char * prompt = (isFirstLine ) ? "> " : "... " ;
89- char * line = get_line (laco , prompt );
90- lua_State * L = laco_get_laco_lua_state (laco );
91- bool result = false;
92-
93- if (line != NULL ) {
94- lua_pushstring (L , line );
95-
96- free (line );
97- result = true;
98- }
99-
100- return result ;
101- }
102-
10310static inline void ignore_extra (const char chr , char * * string_ptr ) {
10411 if (* string_ptr == NULL ) return ;
10512
@@ -110,52 +17,6 @@ static inline void ignore_extra(const char chr, char** string_ptr) {
11017
11118/* External API */
11219
113- int laco_load_line (LacoState * laco ) {
114- int status = laco_get_laco_status (laco );
115- lua_State * L = laco_get_laco_lua_state (laco );
116-
117- lua_settop (L , 0 );
118-
119- if (!pushline (laco , true)) return -1 ;
120-
121- /* Until complete line */
122- while (true) {
123- status = luaL_loadbuffer (L , lua_tostring (L , 1 ), lua_strlen (L , 1 ),
124- "=stdin" );
125-
126- if (is_printable (L , status )) continue ;
127- if (!incomplete (L , status )) break ;
128- if (!pushline (laco , false)) return -1 ;
129-
130- lua_pushliteral (L , "\n" );
131- lua_insert (L , -2 );
132- lua_concat (L , 3 );
133- }
134- lua_remove (L , 1 );
135- laco_set_laco_status (laco , status );
136-
137- return status ;
138- }
139-
140- void laco_handle_line (LacoState * laco ) {
141- int status = laco_get_laco_status (laco );
142- lua_State * L = laco_get_laco_lua_state (laco );
143-
144- if (status == 0 ) {
145- status = lua_pcall (L , 0 , LUA_MULTRET , 0 );
146- }
147-
148- laco_report_error (laco , status );
149-
150- if (status == 0 && lua_gettop (L ) > 0 ) {
151- status = laco_print_type (laco );
152-
153- laco_report_error (laco , status );
154- }
155-
156- laco_set_laco_status (laco , status );
157- }
158-
15920void laco_kill (LacoState * laco , int status , const char * message ) {
16021 laco_destroy_laco_state (laco );
16122
0 commit comments