00001
00002
00003 #include "config.h"
00004 #include <stdio.h>
00005 #include <math.h>
00006 #ifdef HAVE_STDLIB_H
00007 # include <stdlib.h>
00008 #endif
00009
00010 #define BENCHMARK_TESTING 0
00011
00012 #if BENCHMARK_TESTING
00013 # include <sys/time.h>
00014 # include <sys/resource.h>
00015 # include <unistd.h>
00016 #endif
00017
00018 #include "slang.h"
00019
00020 static int open_readline (void);
00021 static void close_readline (void);
00022
00023 static void help (void)
00024 {
00025 puts("ALL statements MUST be terminated with a ';' character, e.g., quit();\n");
00026 puts("Available functions:");
00027 puts(" cos, sin, tan, atan, acos, asin, exp, log, sqrt, fabs, log10, pow, PI, E");
00028 puts("\nas well as other intrinsic S-Lang functions.");
00029 puts("See S-Lang language documentation for further details.\n");
00030 SLang_run_hooks ("calc_help", 0);
00031 }
00032
00033
00034 static void quit_calc (void)
00035 {
00036 close_readline ();
00037 exit (1);
00038 }
00039
00040 static void exit_calc (int *status)
00041 {
00042 close_readline ();
00043 exit (*status);
00044 }
00045
00046
00047
00048
00049 static SLang_Intrin_Fun_Type Calc_Intrinsics [] =
00050 {
00051 MAKE_INTRINSIC("quit", quit_calc, SLANG_VOID_TYPE, 0),
00052 MAKE_INTRINSIC_I("exit", exit_calc, SLANG_VOID_TYPE),
00053 MAKE_INTRINSIC("help", help, SLANG_VOID_TYPE, 0),
00054 SLANG_END_INTRIN_FUN_TABLE
00055 };
00056
00057 typedef struct
00058 {
00059 int i_value;
00060 char *s_value;
00061 double d_value;
00062 }
00063 My_Struct_Type;
00064
00065 static My_Struct_Type My_Struct =
00066 {
00067 -41,
00068 NULL,
00069 7.18
00070 };
00071
00072 static My_Struct_Type *My_Struct_Ptr = &My_Struct;
00073
00074 static SLang_IStruct_Field_Type My_Struct_Field_Table [] =
00075 {
00076 MAKE_ISTRUCT_FIELD(My_Struct_Type, i_value, "i", SLANG_INT_TYPE, 0),
00077 MAKE_ISTRUCT_FIELD(My_Struct_Type, s_value, "s", SLANG_STRING_TYPE, 0),
00078 MAKE_ISTRUCT_FIELD(My_Struct_Type, d_value, "d", SLANG_DOUBLE_TYPE, 0),
00079 SLANG_END_ISTRUCT_TABLE
00080 };
00081
00082
00083 static int add_my_struct_type (void)
00084 {
00085 return SLadd_istruct_table (My_Struct_Field_Table,
00086 (VOID_STAR) &My_Struct_Ptr,
00087 "MyS");
00088 }
00089
00090 static int take_input (void);
00091
00092 int main (int argc, char **argv)
00093 {
00094 if ((-1 == SLang_init_all ())
00095
00096 || (-1 == add_my_struct_type ())
00097 || (-1 == SLadd_intrin_fun_table (Calc_Intrinsics, NULL)))
00098 {
00099 fprintf(stderr, "Unable to initialize S-Lang.\n");
00100 exit (1);
00101 }
00102
00103 SLang_Traceback = 1;
00104
00105 if (argc == 1)
00106 SLang_load_file("calc.sl");
00107
00108 while (--argc && !SLang_get_error ())
00109 {
00110 argv++;
00111 SLang_load_file (*argv);
00112 }
00113
00114 fputs("Type 'help();' for help and a list of available functions.\n", stdout);
00115 fputs("All statements must end with a ';' character, e.g, 2*7+3;\n", stdout);
00116 fputs("\nType `quit;' to exit this program.\n", stdout);
00117
00118 if (-1 == open_readline ())
00119 return 1;
00120
00121 while (1)
00122 {
00123 if (SLang_get_error ())
00124 {
00125 SLang_restart (1);
00126
00127 }
00128 SLKeyBoard_Quit = 0;
00129 take_input ();
00130 }
00131 }
00132
00133
00134
00135 static SLrline_Type *Calc_RLI;
00136 SLang_Load_Type *Readline_Load_Object;
00137
00138 static char *read_using_readline (SLang_Load_Type *x)
00139 {
00140 static char *input_hook = "calc_take_input_hook";
00141 char *prompt, *line;
00142
00143 if (x->parse_level == 0)
00144 {
00145 if ((input_hook != NULL)
00146 && (-1 == SLang_run_hooks (input_hook, 0)))
00147 {
00148 input_hook = NULL;
00149 return NULL;
00150 }
00151 prompt = "Calc> ";
00152 }
00153 else
00154 prompt = " ";
00155
00156 if (NULL == (line = SLrline_read_line (Calc_RLI, prompt, NULL)))
00157 return SLmake_string ("quit();");
00158
00159 putc ('\n', stdout); fflush (stdout);
00160
00161 SLrline_save_line (Calc_RLI);
00162 return line;
00163 }
00164
00165 static int open_readline (void)
00166 {
00167 if (SLang_init_tty (-1, 0, 1))
00168 {
00169 fprintf(stderr, "Unable to initialize tty.\n");
00170 return -1;
00171 }
00172 SLang_set_abort_signal (NULL);
00173
00174 if (Calc_RLI == NULL)
00175 {
00176 Calc_RLI = SLrline_open (80, SL_RLINE_BLINK_MATCH);
00177 if (Calc_RLI == NULL)
00178 return -1;
00179 }
00180
00181 if (NULL == (Readline_Load_Object = SLallocate_load_type ("<stdin>")))
00182 {
00183 close_readline ();
00184 return -1;
00185 }
00186
00187 Readline_Load_Object->read = read_using_readline;
00188 return 0;
00189 }
00190
00191 static void close_readline (void)
00192 {
00193 if (Readline_Load_Object != NULL)
00194 {
00195 SLdeallocate_load_type (Readline_Load_Object);
00196 Readline_Load_Object = NULL;
00197 }
00198 SLrline_close (Calc_RLI);
00199 Calc_RLI = NULL;
00200 SLang_reset_tty ();
00201 }
00202
00203 static int take_input (void)
00204 {
00205 return SLang_load_object (Readline_Load_Object);
00206 }