void shell (func_t * scope) { /** * shell - when FACT is run by default it comes to here. The * main user interface for FACT, this function grabs an * expression of input, parses it, runs it, and repeats. It * will continue to do so until the user does a C-d, C-c, * returns a value from the main scope, or calls the exit * function. * * @scope - the scope to use when evaluating expressions. */ FACT_t returned; // The value returned by the interpreter. unsigned int end_line; // ... unsigned int hold_line; // ... int *newlines; char *input; char *hold_input; // Used in the main loop to check for else clauses. char **tokenized; // input, tokenized. char **hold_tokens; // Also used to check for else clauses. /* Before we start, print out the copyright info, logo and * a guide to some helpful functions. */ print_logo (); printf ("The FACT programming language interactive shell\n" "© 2010, 2011 Matthew Plant, under the GPL version 3.\n"); hold_input = NULL; scope->file_name = "stdin"; scope->line = 1; end_line = 0; for (;;) // Heh, that looks like a spider. { /* Set the line number to end_line, in case we missed any while evaluating * the last expression. */ scope->line += end_line; end_line = 0; // Then, get raw input for an entire expression. if (hold_input == NULL) input = get_input (stdin, &end_line, "S>", "C>"); else { input = hold_input; hold_input = NULL; } /* We do two checks for EOF signals: once before tokenizing, and once after. * I am not completely sure as to why this is the case, but I do remember at * some point it didn't exit so I added the second check. */ if (input == NULL) break; // Tokenize the input. tokenized = get_words (input); if (tokenized == NULL) break; /* If the first token in the expression is if/on_error, continue to get input * as long as the first token is else. I could forsee this being an issue in * places where the else is placed erroneosly, but that'll be fixed later I * assume. */ if ((tokenized[0][0] == '\n' && (!tokcmp (tokenized[1], "if") || !tokcmp (tokenized[1], "error"))) || (!tokcmp (tokenized[0], "if") || !tokcmp (tokenized[0], "error"))) { for (;;) { /* Go through all the steps we went through from the start of the loop * down to here. */ hold_input = get_input (stdin, &end_line, "?>", "C>"); if (hold_input == NULL|| (hold_tokens = get_words (hold_input)) == NULL) break; // Check to see if the statement starts with else. if ((hold_tokens[0][0] == '\n' && !tokcmp (hold_tokens[1], "else")) || !tokcmp (hold_tokens[0], "else")) { input[strlen (input) - 1] = '\0'; input = combine_strs (input, hold_input); hold_input = NULL; } else break; } printf ("\n"); tokenized = get_words (input); } // Get the newlines and parse the string. tokenized = parse (tokenized, "stdin", scope->line, &newlines); if (tokenized == NULL) continue; // Reset the instruction pointer. reset_ip (); // Evaluate the expression. returned = eval_expression (scope, make_syn_tree (tokenized, newlines)); /* If there were errors, print them out. Otherwise, * print the value of the variable or the name of * the function returned. */ if (returned.type == ERROR_TYPE) errorman_dump (returned.error); else if (returned.type == VAR_TYPE) printf ("Returned value: %s\n", mpc_get_str (returned.v_point->data)); else printf ("Returned object [%s]\n", returned.f_point->name); /* Check to see if the value is to be returned, and * if so, exit. */ if (returned.return_signal) break; } puts ("\nExiting..."); }
//========================================================================================================== //========================================================================================================== void MServer::reset_connection(bool last_failed) { sip_connection->clear(); reset_ip(last_failed); }