1 Subject: typeof language support
3 This is a proposal for solving the variable existence checking problem.
5 The typeof macro subroutine in Thorsten Haudes Patch collection
6 changes the semantics of the macro language to far into the unusable,
7 because it allows NO_TAG variables everywhere.
9 This approach only changes the semantics in one case: if a function
10 retrns nothing (i.e. NO_TAG) this can be assign to a variable.
12 My trick is to switch the interpreter into a "typeof-mode" where it is
13 allowed to pass the value of a symbol with NO_TAG onto the stack.
15 The syntax is currently limited to check only the type of a symbol.
16 But IMHO this is not a limitation. With this you can check the type of
17 a variable and also the return type of a function, by assigning it to
18 a variable first. To assign the return value to a variable first is
19 not a limitation, else you must evaluate the function twice if you
20 need the return value.
22 For thinks like type(array[key]) we have already the in syntax, which
23 should suffice for this.
27 source/highlightData.c | 2 -
28 source/interpret.c | 74 ++++++++++++++++++++++++++++++++++++++++---------
30 source/parse.y | 12 +++++--
31 4 files changed, 73 insertions(+), 17 deletions(-)
33 diff --quilt old/source/interpret.c new/source/interpret.c
34 --- old/source/interpret.c
35 +++ new/source/interpret.c
36 @@ -105,6 +105,9 @@ static int arrayEntryCompare(rbTreeNode
37 static void arrayDisposeNode(rbTreeNode *src);
38 static SparseArrayEntry *allocateSparseArrayEntry(void);
40 +/* is the intepreter in the special typeof() mode? */
41 +static int inTypeOfMode;
43 static const char *tagToStr(enum typeTags tag);
45 /*#define DEBUG_ASSEMBLY*/
46 @@ -1420,7 +1423,7 @@ static int pushSymVal(void)
49 return execError("reading non-variable: %s", s->name);
50 - if (symVal.tag == NO_TAG) {
51 + if (symVal.tag == NO_TAG && !inTypeOfMode) {
52 return execError("variable not set: %s", s->name);
55 @@ -1548,7 +1551,7 @@ static int pushArraySymVal(void)
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 @@ -2519,9 +2522,6 @@ static int callSubroutineFromSymbol(Symb
65 nArgs, &result, &errMsg))
66 return execError(errMsg, sym->name);
67 if (PC->func == fetchRetVal) {
68 - if (result.tag == NO_TAG) {
69 - return execError("%s does not return a value", sym->name);
74 @@ -2588,7 +2588,8 @@ static int callSubroutineFromSymbol(Symb
75 (XEvent *)&key_event, argList, &numArgs);
76 XtFree((char *)argList);
77 if (PC->func == fetchRetVal) {
78 - return execError("%s does not return a value", sym->name);
82 return PreemptRequest ? STAT_PREEMPT : STAT_OK;
84 @@ -2814,14 +2815,12 @@ static int returnValOrNone(int valOnStac
87 } else if (PC->func == fetchRetVal) {
94 - "using return value of %s which does not return a value",
95 - ((PC-2)->sym->name));
103 /* NULL return PC indicates end of program */
104 @@ -3576,6 +3575,55 @@ static int deleteArrayElement(void)
108 +static int typeOfIn(void)
110 + if (inTypeOfMode) {
111 + return(execError("I'm already in typeof-mode", NULL));
119 +static int typeOfOut(void)
124 + if (!inTypeOfMode) {
125 + return(execError("I'm not in typeof-mode", NULL));
132 + retVal.tag = STRING_TAG;
135 + retVal.val.str.rep = PERM_ALLOC_STR("UNDEFINED");
138 + retVal.val.str.rep = PERM_ALLOC_STR("INTEGER");
141 + retVal.val.str.rep = PERM_ALLOC_STR("STRING");
144 + retVal.val.str.rep = PERM_ALLOC_STR("ARRAY");
147 + retVal.val.str.len = strlen(retVal.val.str.rep);
149 + if (PC->func == fetchRetVal) {
158 ** checks errno after operations which can set it. If an error occured,
159 ** creates appropriate error messages and returns false
160 diff --quilt old/source/parse.y new/source/parse.y
161 --- old/source/parse.y
162 +++ new/source/parse.y
163 @@ -107,7 +107,7 @@ typedef struct LVinst {
164 %token <sym> STRING SYMBOL FIELD
166 %token DELETE ARG_LOOKUP
167 -%token IF WHILE DO ELSE FOR BREAK CONTINUE RETURN DEFINE
168 +%token IF WHILE DO ELSE FOR BREAK CONTINUE RETURN DEFINE TYPEOF
169 %type <num> arglistopt arglist catlist fnarglsopt fnarglist fnarg
170 %type <inst> cond comastmts comastmtlst for while do else and or arrayexpr mark
172 @@ -480,8 +480,13 @@ catlist: numexpr %prec CONC
173 | catlist numexpr %prec CONCAT { $$ = $1 + 1; }
176 -/* function calls */
177 -funccall: SYMBOL '(' fnarglsopt ')' {
178 +/* function calls and typeof */
179 +funccall: TYPEOF '(' {
180 + ADD_OP(OP_TYPEOF_IN);
182 + ADD_OP(OP_TYPEOF_OUT);
184 + | SYMBOL '(' fnarglsopt ')' {
185 ADD_OP(OP_SUBR_CALL);
186 ADD_SYM(PromoteToGlobal($1)); ADD_IMMED($3);
188 @@ -867,6 +872,7 @@ static int yylex(void)
189 if (!strcmp(symName, "$args")) return ARG_LOOKUP;
190 if (!strcmp(symName, "delete") && follow_non_whitespace('(', SYMBOL, DELETE) == DELETE) return DELETE;
191 if (!strcmp(symName, "define")) return DEFINE;
192 + if (!strcmp(symName, "typeof")) return TYPEOF;
193 if (nextSymIsField) {
195 yylval.sym = InstallStringConstSymbol(symName);
196 diff --quilt old/source/highlightData.c new/source/highlightData.c
197 --- old/source/highlightData.c
198 +++ new/source/highlightData.c
199 @@ -555,7 +555,7 @@ static char *DefaultPatternSets[] = {
200 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\
201 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\
202 Macro Hooks:\"<(?:(?:pre|post)_(?:open|save)|cursor_moved|modified|(?:losing_)?focus|language_mode)_hook(?=\\s*\\()\":::Subroutine1::\n\
203 - Keyword:\"<(?:break|continue|define|delete|do|else|for|if|in|return|while)>\":::Keyword::\n\
204 + Keyword:\"<(?:break|continue|define|delete|do|else|for|if|in|return|typeof|while)>\":::Keyword::\n\
205 Braces:\"[{}\\[\\]()<>,.:;~!&|^%*/?=+-]\":::Keyword::\n\
206 Global Variable:\"\\$[A-Za-z0-9_]+\":::Identifier1::\n\
207 String sq:\"'\":\"'\"::String::\n\
208 diff --quilt old/source/ops.h new/source/ops.h
211 @@ -57,3 +57,5 @@ OP(NAMED_ARGN, namedArgN)
212 OP(SWAP_TOP2, swapTop2) /* pop(v1,v2), push(v1,v2) */
213 OP(SUBR_CALL_STACKED_N, callSubroutineStackedN) /*s*/ /* pop(N,a,pN..p1), call(s) */
214 OP(UNPACKTOARGS, unpackArrayToArgs) /* pop(a), push(a[1]..a[N],a,-(N+1)) */
215 +OP(TYPEOF_IN, typeOfIn) /* enable typeof() */
216 +OP(TYPEOF_OUT, typeOfOut) /* pop(v), push(typeof(v)) */