diff --git a/sms_src/examples/benchmark.sms b/sms_src/examples/benchmark.sms index 7cf295af..ce6d92d5 100755 --- a/sms_src/examples/benchmark.sms +++ b/sms_src/examples/benchmark.sms @@ -19,7 +19,7 @@ let f = (n) => if( n <= 1 ) - n + 1 else f(n - 1) + f(n - 2); benchmark(:f(31)); diff --git a/sms_src/examples/benchmark_fastfib.sms b/sms_src/examples/benchmark_fastfib.sms new file mode 100644 index 00000000..76287161 --- /dev/null +++ b/sms_src/examples/benchmark_fastfib.sms @@ -0,0 +1,34 @@ +#!/usr/local/bin/sms -qm 1800 +# Use this to benchmark something +# I came up with a faster fib that doesn't memoize + +{ + let t1 =[]; + let t2 =[]; + let time_elapsed =0; + let a =0; + let benchmark = (e) => { + putln("Benchmarking: " str+ toStr(e)); + + t1 = time(); + putln(toStr(eval(e))); + t2 = time(); + + putln("Total time:"); + time_elapsed = 1000 * (t2[0] - t1[0]) + 0.001 * (t2[1] - t1[1]); + putln(toStr(time_elapsed) str+ " milliseconds") + }; + + let first5 = [1,1,2,3,5]; + let f = (n) => { + if(n<5) + return first5[n]; + let s2= f(n-2); + let s3= f(n-3); + let s4= f(n-4); + + return 2*s3+s4+s2 ; + }; + + benchmark(:f(31)); +}; diff --git a/sms_src/examples/repl.sms b/sms_src/examples/repl.sms index 2e69eb6c..f931029c 100755 --- a/sms_src/examples/repl.sms +++ b/sms_src/examples/repl.sms @@ -5,7 +5,7 @@ { let in = ""; let ans = []; - let env = {}; + let env = _scratch; while(true){ put("> "); in = input(); diff --git a/src/main/sms.h b/src/main/sms.h index 68f99670..1c21f652 100644 --- a/src/main/sms.h +++ b/src/main/sms.h @@ -95,7 +95,6 @@ enum sm_object_type { #include "memory/sm_space.h" #include "memory/sm_gc.h" #include "sm_global.h" -#include "terminal/sm_terminal_input.h" #include "terminal/sm_terminal_output.h" #include "engine/str.h" #include "engine/sm_diff.h" diff --git a/src/main/terminal/process_esc.c b/src/main/terminal/process_esc.c deleted file mode 100644 index f87ec1bb..00000000 --- a/src/main/terminal/process_esc.c +++ /dev/null @@ -1,6 +0,0 @@ -// Read https://raw.githubusercontent.com/reginaldford/sms/main/LICENSE.txt for license information - -#include "../sms.h" - -// -void process_esc(char *input) { printf("esc: %s\n", input); } diff --git a/src/main/terminal/process_esc.h b/src/main/terminal/process_esc.h deleted file mode 100644 index 5ba583d0..00000000 --- a/src/main/terminal/process_esc.h +++ /dev/null @@ -1,3 +0,0 @@ -// Read https://raw.githubusercontent.com/reginaldford/sms/main/LICENSE.txt for license information - -void process_esc(char *input); diff --git a/src/main/terminal/process_ext1.c b/src/main/terminal/process_ext1.c deleted file mode 100644 index 9d4613a8..00000000 --- a/src/main/terminal/process_ext1.c +++ /dev/null @@ -1,6 +0,0 @@ -// Read https://raw.githubusercontent.com/reginaldford/sms/main/LICENSE.txt for license information - -#include "../sms.h" - -// arrow keys -void process_ext1(char *input) { printf("ext1: %s\n", input); } diff --git a/src/main/terminal/process_ext1.h b/src/main/terminal/process_ext1.h deleted file mode 100644 index e3de231f..00000000 --- a/src/main/terminal/process_ext1.h +++ /dev/null @@ -1,3 +0,0 @@ -// Read https://raw.githubusercontent.com/reginaldford/sms/main/LICENSE.txt for license information - -void process_ext1(char *input); diff --git a/src/main/terminal/process_ext2.c b/src/main/terminal/process_ext2.c deleted file mode 100644 index 43541835..00000000 --- a/src/main/terminal/process_ext2.c +++ /dev/null @@ -1,6 +0,0 @@ -// Read https://raw.githubusercontent.com/reginaldford/sms/main/LICENSE.txt for license information - -#include "../sms.h" - -// -void process_ext2(char *input) { printf("ext2: %s\n", input); } diff --git a/src/main/terminal/process_ext2.h b/src/main/terminal/process_ext2.h deleted file mode 100644 index 8e38256a..00000000 --- a/src/main/terminal/process_ext2.h +++ /dev/null @@ -1,3 +0,0 @@ -// Read https://raw.githubusercontent.com/reginaldford/sms/main/LICENSE.txt for license information - -void process_ext2(char *input); diff --git a/src/main/terminal/sm_terminal_input.c b/src/main/terminal/sm_terminal_input.c deleted file mode 100644 index 746888c1..00000000 --- a/src/main/terminal/sm_terminal_input.c +++ /dev/null @@ -1,148 +0,0 @@ -// Read https://raw.githubusercontent.com/reginaldford/sms/main/LICENSE.txt for license information - -#include "../sms.h" - -/* Relies on comformity to POSIX and vt100/ANSI color code output. -The terminal input is handled by switching the terminal to non-canonical mode. In this mode, -character printing is manual (even when the user types). A state machine in this file will sort the -different incoming escape codes due to standard unix-like behavior on a vt100-like terminal emulator -when the user presses certain key combinations or keys. The state machine parses the input in real -time, and calls the functions for escape codes, extended escape codes, and extra-extended escape -codes which each have their own file.*/ - -// arrow keys, alt+letters -#include "process_esc.h" -// ctrl+letter -#include "process_ext1.h" -// alt+arrow, ctrl+arrow, ctrl+alt+arrow, ctrl+shift+arrow -#include "process_ext2.h" - -// bison generated parser's global line number :-( -extern int yylineno; - -// levels of input state -typedef enum { normal, esc, extended1, extended2 } sm_terminal_state; - -// we will make this infinite later, via OS malloc -#define MAX_BUFFER_LEN 1024 -int cursor_pos = 0; -int input_len = 0; -sm_terminal_state current_state = normal; // State machine parsing -char input_buffer[MAX_BUFFER_LEN]; // Might have to use OS malloc to improve -char esc_buffer[16]; // For esc codes -int eb_cursor = 0; // Escape buffer cursor - -// State machine based on sm_terminal_state enum -void process_character(char c) { - switch (current_state) { - case normal: - if (c == 27) { // begins escape code - current_state = esc; - eb_cursor = 0; - } else if (c != 127 && c != 8) { // not backspace - putc(c, stdout); - fflush(stdout); - input_buffer[input_len++] = c; - } else if (input_len > 0) { // backspace - putc('\b', stdout); - putc(' ', stdout); - putc('\b', stdout); - fflush(stdout); - input_buffer[input_len] = '\0'; - input_len--; - } - break; - case extended1: // c is last char or this is an extended esc - esc_buffer[eb_cursor++] = c; - if (c == 49) { - current_state = extended2; - } else { - esc_buffer[eb_cursor++] = '\0'; - current_state = normal; - eb_cursor = 0; - process_ext1(esc_buffer); - } - break; - case extended2: - if (eb_cursor >= 4) { // extended sequence is 5 total characters - esc_buffer[eb_cursor++] = c; - esc_buffer[eb_cursor++] = '\0'; - eb_cursor = 0; - current_state = normal; - process_ext2(esc_buffer); - } else { - esc_buffer[eb_cursor++] = c; - } - break; - case esc: - esc_buffer[eb_cursor++] = c; - if (c >= 48 && c <= 57) { // 0-9 - esc_buffer[eb_cursor] = '\0'; - eb_cursor = 0; - process_esc(input_buffer); - current_state = normal; - } else if (c >= 97 && c <= 122) { // lowercase letters - esc_buffer[eb_cursor] = '\0'; - eb_cursor = 0; - process_esc(input_buffer); - current_state = normal; - } else if (c >= 65 && c <= 90) { // uppercase letters - esc_buffer[eb_cursor] = '\0'; - eb_cursor = 0; - process_esc(input_buffer); - current_state = normal; - } else if (c == 91) { // starts a longer esc - current_state = extended1; - } else { // unsupported esc, reset state - eb_cursor = 0; - current_state = normal; - } - break; - } -} - -// Returns the last non-whitespace character of the user input. -// If we reach the beginning in the search, we just return ' '. -char last_non_whitespace(char *cstr, int len) { - for (int i = len - 1; i >= 0; i--) { - if (!isspace(cstr[i])) - return cstr[i]; - } - return ' '; -} - -// "smartput" -// Prints the terminal prompt, and allows the user to input a command -// Parses the input when enter is pressed and the last token is a ; -sm_parse_result sm_terminal_smartput() { - struct termios term_attr; - - // Set the terminal to non-canonical mode - tcgetattr(STDIN_FILENO, &term_attr); - term_attr.c_lflag &= ~(ICANON | ECHO); - tcsetattr(STDIN_FILENO, TCSANOW, &term_attr); - - sm_terminal_print_prompt(); - - char c; - // read returns # of bytes read or -1 - // We read 1 byte at a time - while (read(STDIN_FILENO, &c, 1) > 0) { - process_character(c); - if (c == 10) { - if (last_non_whitespace(input_buffer, input_len) != '\\') { // use \ to extend your line - // restore terminal attributes - int stdin_fd = fileno(stdin); - struct termios term_attr; - tcgetattr(stdin_fd, &term_attr); - term_attr.c_lflag |= ICANON; - tcsetattr(stdin_fd, TCSANOW, &term_attr); - input_buffer[input_len] = '\0'; // cstr termination - sm_parse_result pr = sm_parse_cstr(input_buffer, input_len); // - input_len = 0; - return pr; - } - } - } - return (sm_parse_result){.return_val = -1, .parsed_object = NULL}; -} diff --git a/src/main/terminal/sm_terminal_input.h b/src/main/terminal/sm_terminal_input.h deleted file mode 100644 index d4cac89b..00000000 --- a/src/main/terminal/sm_terminal_input.h +++ /dev/null @@ -1,4 +0,0 @@ -// Read https://raw.githubusercontent.com/reginaldford/sms/main/LICENSE.txt for license information - -sm_parse_result sm_terminal_prompt(); -sm_parse_result sm_terminal_smartput();