import parse-define.patch
[nedit-bw.git] / core-typeof-syntax.patch
blob595ecc4fce84681359de46b4f5456ef1bfc50705
1 ---
3 source/highlightData.c | 2 -
4 source/interpret.c | 79 ++++++++++++++++++++++++++++++++++++++++---------
5 source/interpret.h | 1
6 source/parse.y | 10 ++++--
7 4 files changed, 76 insertions(+), 16 deletions(-)
9 diff --quilt old/source/interpret.c new/source/interpret.c
10 --- old/source/interpret.c
11 +++ new/source/interpret.c
12 @@ -150,10 +150,15 @@ static rbTreeNode *arrayEmptyAllocator(v
13 static rbTreeNode *arrayAllocateNode(rbTreeNode *src);
14 static int arrayEntryCopyToNode(rbTreeNode *dst, rbTreeNode *src);
15 static int arrayEntryCompare(rbTreeNode *left, rbTreeNode *right);
16 static void arrayDisposeNode(rbTreeNode *src);
17 static SparseArrayEntry *allocateSparseArrayEntry(void);
18 +static int typeOfIn(void);
19 +static int typeOfOut(void);
21 +/* is the intepreter in the special typeof() mode? */
22 +static int inTypeOfMode;
24 static const char *tagToStr(enum typeTags tag);
26 /*#define DEBUG_ASSEMBLY*/
27 /*#define DEBUG_STACK*/
28 @@ -236,10 +241,11 @@ static int (*OpFns[N_OPS])() = {returnNo
29 arrayRefAndAssignSetup, pushArgVal, pushArgCount, pushArgArray,
30 anonArrayOpen, anonArraySkip, anonArrayNextVal, anonArrayIndexVal,
31 anonArrayClose, namedArg1, namedArgN, swapTop2,
32 callSubroutineStackedN,
33 unpackArrayToArgs,
34 + typeOfIn, typeOfOut,
37 /* Stack-> symN-sym0(FP), nArgs, oldFP, retPC, argArray, argN-arg1, next, ... */
38 #define FP_ARG_COUNT_INDEX (-1)
39 #define FP_FUNCTION_NAME (-2) /* !! */
40 @@ -1409,11 +1415,11 @@ static int pushSymVal(void)
41 &symVal, &errMsg)) {
42 return execError(errMsg, s->name);
44 } else
45 return execError("reading non-variable: %s", s->name);
46 - if (symVal.tag == NO_TAG) {
47 + if (symVal.tag == NO_TAG && !inTypeOfMode) {
48 return execError("variable not set: %s", s->name);
51 PUSH(symVal);
53 @@ -1515,11 +1521,11 @@ static int pushArraySymVal(void)
54 if (initEmpty && dataPtr->tag == NO_TAG) {
55 dataPtr->tag = ARRAY_TAG;
56 dataPtr->val.arrayPtr = ArrayNew();
59 - if (dataPtr->tag == NO_TAG) {
60 + if (dataPtr->tag == NO_TAG && !inTypeOfMode) {
61 return execError("variable not set: %s", sym->name);
64 PUSH(*dataPtr);
66 @@ -2482,13 +2488,10 @@ static int callSubroutineFromSymbol(Symb
67 PreemptRequest = False;
68 /* NB nArgs < 0 implies presence of named args array in last position */
69 if (!sym->value.val.subr(FocusWindow, StackP, nArgs, &result, &errMsg))
70 return execError(errMsg, sym->name);
71 if (PC->func == fetchRetVal) {
72 - if (result.tag == NO_TAG) {
73 - return execError("%s does not return a value", sym->name);
74 - }
75 PUSH(result);
76 PC++;
78 return PreemptRequest ? STAT_PREEMPT : STAT_OK;
80 @@ -2579,11 +2582,12 @@ static int callSubroutineFromSymbol(Symb
81 /* Call the action routine and check for preemption */
82 PreemptRequest = False;
83 sym->value.val.xtproc(FocusWindow->lastFocus,
84 (XEvent *)&key_event, argList, &numArgs);
85 if (PC->func == fetchRetVal) {
86 - return execError("%s does not return a value", sym->name);
87 + PUSH(noValue);
88 + PC++;
90 return PreemptRequest ? STAT_PREEMPT : STAT_OK;
93 /* Calling a non subroutine symbol */
94 @@ -2815,18 +2819,16 @@ static int returnValOrNone(int valOnStac
95 PUSH(retVal);
96 } else {
97 PUSH(noValue);
99 } else if (PC->func == fetchRetVal) {
100 - if (valOnStack) {
101 + if (valOnStack) {
102 PUSH(retVal);
103 - PC++;
104 - } else {
105 - return execError(
106 - "using return value of %s which does not return a value",
107 - ((PC-2)->sym->name));
109 + } else {
110 + PUSH(noValue);
112 + PC++;
115 /* NULL return PC indicates end of program */
116 return PC == NULL ? STAT_DONE : STAT_OK;
118 @@ -3504,10 +3506,59 @@ static int deleteArrayElement(void)
119 return(execError("attempt to delete from non-array", NULL));
121 return(STAT_OK);
124 +static int typeOfIn(void)
126 + if (inTypeOfMode) {
127 + return(execError("I'm already in typeof-mode", NULL));
130 + inTypeOfMode = 1;
132 + return STAT_OK;
135 +static int typeOfOut(void)
137 + DataValue val;
138 + DataValue retVal;
140 + if (!inTypeOfMode) {
141 + return(execError("I'm not in typeof-mode", NULL));
144 + inTypeOfMode = 0;
146 + POP(val);
148 + retVal.tag = STRING_TAG;
149 + switch (val.tag) {
150 + case NO_TAG:
151 + retVal.val.str.rep = PERM_ALLOC_STR("UNDEFINED");
152 + break;
153 + case INT_TAG:
154 + retVal.val.str.rep = PERM_ALLOC_STR("INTEGER");
155 + break;
156 + case STRING_TAG:
157 + retVal.val.str.rep = PERM_ALLOC_STR("STRING");
158 + break;
159 + case ARRAY_TAG:
160 + retVal.val.str.rep = PERM_ALLOC_STR("ARRAY");
161 + break;
163 + retVal.val.str.len = strlen(retVal.val.str.rep);
165 + if (PC->func == fetchRetVal) {
166 + PUSH(retVal);
167 + PC++;
170 + return STAT_OK;
174 ** checks errno after operations which can set it. If an error occured,
175 ** creates appropriate error messages and returns false
177 static int errCheck(const char *s)
178 @@ -3822,10 +3873,12 @@ static void disasmInternal(Inst *inst, i
179 "NAMED_ARG1", /* namedArg1: "fn([...]=..., ...)" */
180 "NAMED_ARGN", /* namedArgN: "fn(..., [...]=...)" */
181 "SWAP_TOP2", /* swapTop2: cf namedArgN */
182 "SUBR_CALL_STACKED_N", /* callSubroutineStackedN */
183 "UNPACKTOARGS", /* unpackArrayToArgs */
184 + "TYPEOF_IN", /* typeOfIn */
185 + "TYPEOF_OUT", /* typeOfOut */
187 int i, j;
188 static size_t opLen;
190 if (!opLen) {
191 diff --quilt old/source/interpret.h new/source/interpret.h
192 --- old/source/interpret.h
193 +++ new/source/interpret.h
194 @@ -55,10 +55,11 @@ enum operations {OP_RETURN_NO_VAL, OP_RE
195 OP_ANONARRAY_OPEN, OP_ANONARRAY_SKIP, OP_ANONARRAY_NEXT_VAL,
196 OP_ANONARRAY_INDEX_VAL, OP_ANONARRAY_CLOSE,
197 OP_NAMED_ARG1, OP_NAMED_ARGN, OP_SWAP_TOP2,
198 OP_SUBR_CALL_STACKED_N,
199 OP_UNPACKTOARGS,
200 + OP_TYPEOF_IN, OP_TYPEOF_OUT,
201 N_OPS};
203 enum typeTags {NO_TAG, INT_TAG, STRING_TAG, ARRAY_TAG};
205 enum execReturnCodes {MACRO_TIME_LIMIT, MACRO_PREEMPT, MACRO_DONE, MACRO_ERROR};
206 diff --quilt old/source/parse.y new/source/parse.y
207 --- old/source/parse.y
208 +++ new/source/parse.y
209 @@ -91,11 +91,11 @@ static int nextSymIsField = 0;
210 enum operations oper;
211 AccumulatorData *acc;
213 %token <sym> NUMBER STRING SYMBOL FIELD
214 %token DELETE ARG_LOOKUP
215 -%token IF WHILE DO ELSE FOR BREAK CONTINUE RETURN
216 +%token IF WHILE DO ELSE FOR BREAK CONTINUE RETURN TYPEOF
217 %token <acc> DEFINE
218 %type <nArgs> arrlist arrentry
219 %type <nArgs> arglistopt arglist catlist fnarglsopt fnarglist fnarg
220 %type <inst> cond comastmts comastmtlst for while do else and or arrayexpr mark
221 %type <sym> evalsym
222 @@ -337,11 +337,16 @@ arglist: blank expr bla
223 catlist: numexpr %prec CONCAT { $$ = 1; }
224 | catlist numexpr %prec CONCAT { $$ = $1 + 1; }
227 /* function call and its argument lists */
228 -funccall: SYMBOL '(' fnarglsopt ')' {
229 +funccall: TYPEOF '(' {
230 + ADD_OP(OP_TYPEOF_IN);
231 + } evalsym ')' {
232 + ADD_OP(OP_TYPEOF_OUT);
234 + | SYMBOL '(' fnarglsopt ')' {
235 ADD_OP(OP_SUBR_CALL);
236 ADD_SYM(PromoteToGlobal($1)); ADD_IMMED($3);
238 | SYMBOL '(' blank '=' blank expr blank ')' {
239 /* a single array replaces the argument list */
240 @@ -700,10 +705,11 @@ static int yylex(void)
241 if (!strcmp(symName, "return")) return RETURN;
242 if (!strcmp(symName, "in")) return IN;
243 if (!strcmp(symName, "$args")) return ARG_LOOKUP;
244 if (!strcmp(symName, "delete") && follow_non_whitespace('(', SYMBOL, DELETE) == DELETE) return DELETE;
245 if (!strcmp(symName, "define")) return DEFINE;
246 + if (!strcmp(symName, "typeof")) return TYPEOF;
247 if (nextSymIsField) {
248 nextSymIsField = 0;
249 yylval.sym = InstallStringConstSymbol(symName);
250 return FIELD;
252 diff --quilt old/source/highlightData.c new/source/highlightData.c
253 --- old/source/highlightData.c
254 +++ new/source/highlightData.c
255 @@ -553,11 +553,11 @@ static char *DefaultPatternSets[] = {
256 Built-in Special Vars:\"(?<!\\Y)\\$(?:[1-9]|list_dialog_button|n_args|read_status|search_end|shell_cmd_status|string_dialog_button|sub_sep)>\":::String1::\n\
257 Built-in Subrs:\"<(?:append_file|beep|call|calltip|clipboard_to_string|dialog|filename_dialog|dict_(?:insert|complete|save|append|is_element)|focus_window|get_character|get_matching|get_pattern_(by_name|at_pos)|get_range|get_selection|get_style_(by_name|at_pos)|getenv|highlight_calltip_line|kill_calltip|length|list_dialog|max|min|rangeset_(?:add|create|destroy|get_by_name|includes|info|invert|range|set_color|set_mode|set_name|subtract)|read_file|replace_in_string|replace_range|replace_selection|replace_substring|search|search_string|select|select_rectangle|set_cursor_pos|set_transient|shell_command|split|string_compare|string_dialog|string_to_clipboard|substring|t_print|tolower|toupper|valid_number|write_file)(?=\\s*\\()\":::Subroutine::\n\
258 Menu Actions:\"<(?:new(?:_tab|_opposite)?|open|open-dialog|open_dialog|open-selected|open_selected|close|save|save-as|save_as|save-as-dialog|save_as_dialog|revert-to-saved|revert_to_saved|revert_to_saved_dialog|include-file|include_file|include-file-dialog|include_file_dialog|load-macro-file|load_macro_file|load-macro-file-dialog|load_macro_file_dialog|load-tags-file|load_tags_file|load-tags-file-dialog|load_tags_file_dialog|unload_tags_file|load_tips_file|load_tips_file_dialog|unload_tips_file|print|print-selection|print_selection|exit|undo|redo|delete|select-all|select_all|shift-left|shift_left|shift-left-by-tab|shift_left_by_tab|shift-right|shift_right|shift-right-by-tab|shift_right_by_tab|find|find-dialog|find_dialog|find-again|find_again|find-selection|find_selection|find_incremental|start_incremental_find|replace|replace-dialog|replace_dialog|replace-all|replace_all|replace-in-selection|replace_in_selection|replace-again|replace_again|replace_find|replace_find_same|replace_find_again|goto-line-number|goto_line_number|goto-line-number-dialog|goto_line_number_dialog|goto-selected|goto_selected|mark|mark-dialog|mark_dialog|goto-mark|goto_mark|goto-mark-dialog|goto_mark_dialog|match|select_to_matching|goto_matching|find-definition|find_definition|show_tip|split-pane|split_pane|close-pane|close_pane|detach_document(?:_dialog)?|move_document_dialog|(?:next|previous|last)_document|uppercase|lowercase|fill-paragraph|fill_paragraph|control-code-dialog|control_code_dialog|filter-selection-dialog|filter_selection_dialog|filter-selection|filter_selection|execute-command|execute_command|execute-command-dialog|execute_command_dialog|execute-command-line|execute_command_line|shell-menu-command|shell_menu_command|macro-menu-command|macro_menu_command|bg_menu_command|post_window_bg_menu|post_tab_context_menu|beginning-of-selection|beginning_of_selection|end-of-selection|end_of_selection|repeat_macro|repeat_dialog|raise_window|focus_pane|set_statistics_line|set_incremental_search_line|set_show_line_numbers|set_auto_indent|set_wrap_text|set_wrap_margin|set_highlight_syntax|set_make_backup_copy|set_incremental_backup|set_show_matching|set_match_syntax_based|set_overtype_mode|set_locked|set_tab_dist|set_em_tab_dist|set_use_tabs|set_fonts|set_language_mode)(?=\\s*\\()\":::Subroutine::\n\
259 Text Actions:\"<(?:self-insert|self_insert|grab-focus|grab_focus|extend-adjust|extend_adjust|extend-start|extend_start|extend-end|extend_end|secondary-adjust|secondary_adjust|secondary-or-drag-adjust|secondary_or_drag_adjust|secondary-start|secondary_start|secondary-or-drag-start|secondary_or_drag_start|process-bdrag|process_bdrag|move-destination|move_destination|move-to|move_to|move-to-or-end-drag|move_to_or_end_drag|end_drag|copy-to|copy_to|copy-to-or-end-drag|copy_to_or_end_drag|exchange|process-cancel|process_cancel|paste-clipboard|paste_clipboard|copy-clipboard|copy_clipboard|cut-clipboard|cut_clipboard|copy-primary|copy_primary|cut-primary|cut_primary|newline|newline-and-indent|newline_and_indent|newline-no-indent|newline_no_indent|delete-selection|delete_selection|delete-previous-character|delete_previous_character|delete-next-character|delete_next_character|delete-previous-word|delete_previous_word|delete-next-word|delete_next_word|delete-to-start-of-line|delete_to_start_of_line|delete-to-end-of-line|delete_to_end_of_line|forward-character|forward_character|backward-character|backward_character|key-select|key_select|process-up|process_up|process-down|process_down|process-shift-up|process_shift_up|process-shift-down|process_shift_down|process-home|process_home|forward-word|forward_word|backward-word|backward_word|forward-paragraph|forward_paragraph|backward-paragraph|backward_paragraph|beginning-of-line|beginning_of_line|end-of-line|end_of_line|beginning-of-file|beginning_of_file|end-of-file|end_of_file|next-page|next_page|previous-page|previous_page|page-left|page_left|page-right|page_right|toggle-overstrike|toggle_overstrike|scroll-up|scroll_up|scroll-down|scroll_down|scroll_left|scroll_right|scroll-to-line|scroll_to_line|select-all|select_all|deselect-all|deselect_all|focusIn|focusOut|process-return|process_return|process-tab|process_tab|insert-string|insert_string|mouse_pan)(?=\\s*\\()\":::Subroutine::\n\
260 Macro Hooks:\"<(?:(?:pre|post)_(?:open|save)|cursor_moved|modified|(?:losing_)?focus)_hook(?=\\s*\\()\":::Subroutine1::\n\
261 - Keyword:\"<(?:break|continue|define|delete|else|for|if|in|return|while)>\":::Keyword::\n\
262 + Keyword:\"<(?:break|continue|define|delete|else|for|if|in|return|typeof|while)>\":::Keyword::\n\
263 Braces:\"[{}\\[\\]]\":::Keyword::\n\
264 Global Variable:\"\\$[A-Za-z0-9_]+\":::Identifier1::\n\
265 String sq:\"'\":\"'\"::String::\n\
266 String:\"\"\"\":\"\"\"\":\"\\n\":String::\n\
267 String Escape Char:\"\\\\(?:[\\\\\"\"ntbrfave\\n]|[xX][0-9a-fA-F]{1,2}|0*[0-7]{1,3})\":::Text Escape:String:\n\