Skip to content

Commit 75af8cc

Browse files
committed
Move line functions to a separate file
1 parent b60d4fd commit 75af8cc

File tree

3 files changed

+151
-141
lines changed

3 files changed

+151
-141
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ include_directories("${laco_SOURCE_DIR}/src" "${DEPS_DIR}/linenoise")
44

55
set(SOURCE ${DEPS_DIR}/linenoise/linenoise.c
66
main.c util.c util/print.c
7+
util/line.c
78
commands/debugger.c
89
flags.c commands.c laco.c)
910

src/util.c

Lines changed: 2 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,12 @@
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-
10310
static 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-
15920
void laco_kill(LacoState* laco, int status, const char* message) {
16021
laco_destroy_laco_state(laco);
16122

src/util/line.c

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

0 commit comments

Comments
 (0)