Skip to content

Commit

Permalink
update interactive mode: more line
Browse files Browse the repository at this point in the history
  • Loading branch information
ysw421 committed Jul 19, 2024
1 parent 2f2b79e commit 3d873da
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 48 deletions.
3 changes: 2 additions & 1 deletion examples/test6.or
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
\begin{for}{i:=0}{i<4}{i:=i+1}print("@@ ", i, "\n")\end{for}
f(x):=print(1)
f(1)
2 changes: 1 addition & 1 deletion src/develop/develop_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void print_function(AST_function* checked_function)
void print_value(AST* ast)
{
AST_value* checked = ast->value.value_v;
fprintf(stderr, "value: ->type: %d\n", checked->type);
fprintf(stderr, "value: ->size: %ld\n", checked->size);
struct ast_value_stack_t* stack = checked->stack;

for (int i = 0; i < checked->size; i ++)
Expand Down
46 changes: 36 additions & 10 deletions src/server/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
#include <string.h>
#include <stdlib.h>

// #define BUFFER_SIZE 1024
int custom_tab_insert(int count, int key)
{
rl_insert_text("\t");
return 0;
}

void set_nonblocking(int sock)
{
Expand Down Expand Up @@ -39,7 +43,7 @@ void run_client(int port)

if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
printf("\nORORA Server Connection Failed \n");
return;
}

Expand All @@ -49,11 +53,25 @@ void run_client(int port)
printf("Version 0.0.1\n"
"(C) 2023 Orora Project\n\n");

rl_initialize();
rl_bind_key('\t', custom_tab_insert);

bool is_success_received = false;
bool is_more_line = false;
int right_space = 0;

char* prompt = (char*) const_strcat(const_strcat(ORORA_COLOR_H, ORORA_PROMPT), ORORA_COLOR_RESET);
while (1)
{
// char *input = readline(DIALOGUE);
char *input = readline(prompt);
char* input;
if (is_more_line)
{
input = readline(" ");
// for (int i = 0; i < right_space; i++)
// fprintf(stderr, "\t");
}
else
input = readline(prompt);
if (input == (void*) 0)
continue;
else if (!strcmp(input, "exit()\n"))
Expand All @@ -76,9 +94,10 @@ void run_client(int port)
break;
}

bool success_received = false;
is_success_received = false;
is_more_line = false;
int count = -1;
while (!success_received)
while (!is_success_received && !is_more_line)
{
count++;
MessageHeader header;
Expand Down Expand Up @@ -140,14 +159,21 @@ void run_client(int port)

if (status == ORORA_STATUS_SUCCESS)
{
success_received = true;
is_success_received = true;
if (strlen(response) == 0 && count > 0)
printf("\n");
fprintf(stderr, "\n");
else
printf("%s", response);
fprintf(stderr, "%s", response);
}
else if (status == ORORA_STATUS_MORE)
{
is_more_line = true;
right_space = atoi(response);
if (right_space < 0)
right_space = 0;
}
else
printf("%s", response);
fprintf(stderr, "%s", response);

free(response);
}
Expand Down
117 changes: 82 additions & 35 deletions src/server/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ void handle_signal(int sig)
running = 0;
}

bool is_end_interactive_line(Lexer* lexer)
int is_end_interactive_line(Lexer* root)
{
Lexer* lexer = malloc(sizeof(Lexer));
memcpy(lexer, root, sizeof(Lexer));
Token* token = (void*) 0;
int lpar = 0;
int rpar = 0;
Expand All @@ -23,11 +25,11 @@ bool is_end_interactive_line(Lexer* lexer)
int begin = 0;
int end = 0;

while ((token = lexer_get_token(root)) != (void*) 0)
while ((token = lexer_get_token(lexer)) != (void*) 0)
{
if (token->type == TOKEN_LPAREN)
if (token->type == TOKEN_LPAR)
lpar ++;
else if (token->type == TOKEN_RPAREN)
else if (token->type == TOKEN_RPAR)
rpar ++;
else if (token->type == TOKEN_LBRACE)
lbrace ++;
Expand All @@ -42,9 +44,10 @@ bool is_end_interactive_line(Lexer* lexer)
else if (token->type == TOKEN_END)
end ++;
}
free(lexer);
if (lpar == rpar && lbrace == rbrace && lsqb == rsqb && begin == end)
return true;
return false;
return 0;
return lpar - rpar + lbrace - rbrace + lsqb - rsqb + begin - end;
}

void run_daemon()
Expand All @@ -57,49 +60,93 @@ void run_daemon()
Envs* global_env = init_envs((void*) 0, init_env());
Envs* root_envs = init_envs(global_env, init_env());

char* input = (char*) 0;
size_t len_input = 0;
bool is_first_line = true;

while (running)
{
ssize_t num_read = read(STDIN_FILENO, buffer, BUFFER_SIZE - 1);
if (num_read > 0)
if (num_read <= 0)
{
buffer[num_read] = '\0';
syslog(LOG_NOTICE, "Received command: %s", buffer);
if (num_read == 0)
syslog(LOG_NOTICE, "Parent closed pipe, exiting");
else
syslog(LOG_ERR, "Read error: %s", strerror(errno));
break;
}

off_t* len_p = (off_t*) malloc(sizeof(off_t));
*len_p = (off_t) num_read;

Lexer* root = init_lexer(buffer, len_p);
#include "develop/develop_mode.h"
print_tokens(root);
Parser* parser = init_parser(root);
AST* ast_tree = parser_parse(parser);

if (setjmp(interactive_mode_buf) == 0)
if (is_first_line)
{
input = malloc(num_read + 1);
if (input == NULL)
{
AST_value_stack* new_value = visitor_visit(root_envs, ast_tree->value.compound_v->items[0])->output;
if (new_value)
{
if (!(new_value->type == AST_VALUE_NULL && ast_tree->value.compound_v->items[0]->type == AST_FUNCTION))
visitor_print_function_value(new_value);
orora_write("\n", ORORA_STATUS_SUCCESS);
}
else
orora_write("", ORORA_STATUS_SUCCESS);
syslog(LOG_ERR, "Memory allocation failed");
break;
}
else
memcpy(input, buffer, num_read);
len_input = num_read;
input[len_input] = '\0';
is_first_line = false;
}
else
{
char *temp = realloc(input, len_input + num_read + 1);
if (temp == NULL)
{
orora_write("", ORORA_STATUS_SUCCESS);
syslog(LOG_ERR, "Memory reallocation failed");
free(input);
break;
}
input = temp;
memcpy(input + len_input, buffer, num_read);
len_input += num_read;
input[len_input] = '\0';
}
else if (num_read == 0)

char last_char = input[len_input];
input[len_input] = '\0';
syslog(LOG_NOTICE, "Received command: %s", input);

off_t* len_p = (off_t*) malloc(sizeof(off_t));
*len_p = (off_t) len_input;

Lexer* root = init_lexer(input, len_p);
int right_space = is_end_interactive_line(root);
if (right_space == 0)
{
syslog(LOG_NOTICE, "Parent closed pipe, exiting");
break;
is_first_line = true;
}
else
{
syslog(LOG_ERR, "Read error: %s", strerror(errno));
break;
input[len_input] = last_char;
orora_write(int_to_string(right_space), ORORA_STATUS_MORE);
// orora_write("", ORORA_STATUS_SUCCESS);
continue;
}
// #include "develop/develop_mode.h"
// print_tokens(root);
Parser* parser = init_parser(root);
AST* ast_tree = parser_parse(parser);

if (setjmp(interactive_mode_buf) == 0)
{
AST* ast = ast_tree->value.compound_v->items[0];
AST_value_stack* new_value = visitor_visit(root_envs, ast)->output;
if (new_value)
{
if (new_value->type != AST_VALUE_NULL || !(ast->type == AST_FUNCTION
|| ast->type == AST_CODE
|| (ast->type == AST_VALUE && ast->value.value_v->stack->type == AST_VALUE_CODE)))
visitor_print_function_value(new_value);
orora_write("\n", ORORA_STATUS_SUCCESS);
}
else
orora_write("", ORORA_STATUS_SUCCESS);
}
else
{
orora_write("", ORORA_STATUS_SUCCESS);
}
}
syslog(LOG_NOTICE, "Orora exiting");
Expand Down
2 changes: 1 addition & 1 deletion src/server/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#define PORT_FILE "/tmp/orora_port.txt"
#define SOCKET_PATH "/tmp/orora_socket"

#define ORORA_PROMPT "orora> "
#define ORORA_PROMPT "&orora> "
#define DIALOGUE "%s%s%s", ORORA_COLOR_H, ORORA_PROMPT, ORORA_COLOR_RESET

int read_port_from_file();
Expand Down
1 change: 1 addition & 0 deletions src/server/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define ORORA_STATUS_SUCCESS 0x00000000
#define ORORA_STATUS_ERROR 0x10000000
#define ORORA_STATUS_CONSOLE 0x30000000
#define ORORA_STATUS_MORE 0x40000000

// #define CODE_ERROR "500"
// #define CODE_CONSOLE "300"
Expand Down
8 changes: 8 additions & 0 deletions src/visitor/visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,18 @@ AST_value_stack* visitor_get_value_from_function
// visitor_get_envs_from_function(envs, ast_function, env_function);

// return visitor_function_value(envs, ast_function);

Env_function* env_function =
visitor_get_function(envs, ast_function);
if (!env_function)
{
if (!strcmp(ast_function->name, "print"))
{
AST* ast = init_ast(AST_FUNCTION, (void*) 0, (void*) 0);
ast->value.function_v = ast_function;
visitor_print_function(envs, ast);
return init_ast_value_stack(AST_VALUE_NULL, (void*) 0);
}
visitor_nondefine_function_error(ast_function);
}

Expand Down

0 comments on commit 3d873da

Please sign in to comment.