static clj_Result read_comment(clj_Reader *r, wint_t initch) { wint_t c; do { c = pop_char(r); } while (!ends_line(c) && c != WEOF); return ok_read(c); }
static clj_Result read_typed_string(clj_Type type, const wchar_t *prefix, clj_Reader *r) { wint_t c; StringBuffer strbuf; int escape = 0; strbuf_init(&strbuf, 80); // C'mon now, how big is your terminal? strbuf_appends(&strbuf, prefix); while (1) { c = pop_char(r); switch (c) { case WEOF: strbuf_free(&strbuf); reader_error(r, CLJ_UNEXPECTED_EOF); case L'\\': strbuf_append(&strbuf, c); escape = !escape; break; case L'"': strbuf_append(&strbuf, c); if (escape) { escape = 0; break; } else { emit(r, type, strbuf.chars); strbuf_free(&strbuf); return CLJ_MORE; } default: escape = 0; strbuf_append(&strbuf, c); } } }
static clj_Result read_dispatch(clj_Reader *r, wint_t initch) { form_reader dispatch_reader; wint_t c = pop_char(r); if ((dispatch_reader = get_dispatch_reader(c))) { return dispatch_reader(r, c); } else { assert(0); //TODO tagged types and unknown dispatch macros } }
main() { int type; double op2; char c; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push_token(NUMBER, atof(s)); break; case '+': case '-': case '/': case '*': case '%': while (((c = top_char()) != EOF) && islesseq(type, c)) push_token(pop_char(), -1); push_char(type); break; case '(': push_char(type); break; case ')': while (((c = pop_char()) != EOF) && c != '(') push_token(c, -1); break; case '\n': while ((c = pop_char()) != EOF) push_token(c, -1); calc(); break; case 'q': return; default: printf("error: unknown command %s\n", s); break; } } }
static clj_Result read_token(clj_Type type, clj_Reader *r, wint_t initch, size_t initial_capacity, char_pred terminates) { wint_t c; StringBuffer strbuf; strbuf_init(&strbuf, initial_capacity); strbuf_append(&strbuf, initch); while (1) { c = pop_char(r); if (WEOF == c || is_clj_whitespace(c) || terminates(c)) { push_char(r, c); emit(r, type, strbuf.chars); strbuf_free(&strbuf); break; } else { strbuf_append(&strbuf, c); } } return ok_read(c); }
static clj_Result read_form(clj_Reader *r) { clj_Result result; form_reader macro_reader; wint_t c; while (WEOF != (c = pop_char(r))) { if (is_clj_whitespace(c)) { continue; } else if ((macro_reader = get_macro_reader(c))) { return macro_reader(r, c); } else if (at_number(r, c)) { return read_number(r, c); } else { return read_symbol(r, c); } } if (r->depth > 0) { reader_error(r, CLJ_UNEXPECTED_EOF); } return CLJ_EOF; };
gboolean press_event( GtkWidget* widget, GdkEventKey* event, GtkLabel *label ) { if( !global.is_connected ) { gtk_label_set_text( label_grab, OFFLINE_MESSAGE ); return( TRUE ); } char command[ COMMAND_BUFFER_SIZE ] = ""; /* Modificators */ if( event->state & GDK_SHIFT_MASK ) strcat( command, "shift+" ); if( event->state & GDK_CONTROL_MASK ) strcat( command, "ctrl+" ); /* Main buttons */ buttons b[] = { { GDK_KEY_Return, "Return" }, { GDK_KEY_Home, "Home" }, { GDK_KEY_Left, "Left" }, { GDK_KEY_End, "End" }, { GDK_KEY_Up, "Up" }, { GDK_KEY_Delete, "Delete" }, { GDK_KEY_Right, "Right" }, { GDK_KEY_Insert, "Insert" }, { GDK_KEY_Down, "Down" }, { GDK_KEY_Page_Up, "Page_Up" }, { GDK_KEY_Escape, "Escape" }, { GDK_KEY_Page_Down, "Page_Down" }, { GDK_KEY_BackSpace, "BackSpace" }, { GDK_KEY_Menu, "Menu" }, { GDK_KEY_space, "space" }, { GDK_KEY_Tab, "Tab" }, { GDK_KEY_Alt_L, "alt" }, { GDK_KEY_Super_L, "super" } }; const int b_count = sizeof(b) / sizeof(buttons); for( int i = 0; i < b_count; i++ ) { if( event->keyval == b[i].key_code ) { strcat( command, b[i].key_name ); break; } } /* Chars, digits and functional keys */ for( int i = GDK_KEY_a; i <= GDK_KEY_z; i++ ) { if( ( event->keyval == i ) || ( event->keyval == (i - 0x20) ) ) { sprintf( command, "%s%c", command, (char) i ); break; } } for( int i = GDK_KEY_0; i <= GDK_KEY_9; i++ ) { if( event->keyval == i ) { sprintf( command, "%s%c", command, (char) i ); break; } } for( int i = GDK_KEY_F1; i <= GDK_KEY_F12; i++ ) { if( event->keyval == i ) { sprintf( command, "%sF%i", command, (i - GDK_KEY_F1 + 1) ); break; } } /* Send keys */ if( strlen( command ) != 0 ) { if( command[ strlen(command) - 1] == '+' ) pop_char( command ); gtk_label_set_text( label_grab, command ); send_key( command ); } return( FALSE ); }
static wint_t skip_whitespace(clj_Reader *r) { wint_t c; while (is_clj_whitespace(c = pop_char(r))); return c; }
static wint_t peek_char(clj_Reader *r) { wchar_t c = pop_char(r); push_char(r, c); return c; }
int main( int argc, char * argv[] ) { int tmp; int i = 0; int num1; int num2; int new_num; char op; char * src; char cur_op; char top_op; printf( "expression value demo begin !\n" ); #if 0 push_int( 100 ); push_int( 200 ); tmp = pop_int(); printf( "pop is %d \n", tmp ); tmp = pop_int(); printf( "pop is %d \n", tmp ); #endif if( argc > 1 ) printf( "input expression string is %s \n", argv[1] ); else exit(0); src = argv[1]; push_char( '\0'); while( 1 ) { printf( "src[%d] = %c \n", i, src[i] ); get_num: /* get a number */ num1 = get_number( src[i] ); if( num1 >= 0 ) printf( "get a number : %d \n", num1 ); else break; /* push this number */ printf( "push current int : %d \n", num1 ); push_int( num1 ); /* get the next op */ i++; cur_op = get_operator( src[i] ); if( cur_op > 0 ) printf( "get a operator: %c \n", cur_op ); else if( cur_op == '\0' ) { printf( "get a operator == 0: %c \n", cur_op ); } cmp_op: top_op = get_top_char(); printf( "get top stack operator: %c \n", top_op ); /* if current op > stack top op, then push current op */ if( get_prio( cur_op ) > get_prio( top_op ) ) { printf( "<push> push current op : %c \n", cur_op ); push_char( cur_op ); printf( "<push> push ok!\n" ); } else { op = pop_char(); num2 = pop_int(); if( op == '\0' ) { printf( "op == 0, last result = %d \n", num2 ); exit( 0 ); } num1 = pop_int(); printf( "<calc> pop 2 num and 1 op, do calc: %d %c %d \n", num1, op, num2 ); new_num = calc( num1, num2, op ); printf( "get calc result: %d \n", new_num ); /* push this new number */ printf( "push new int : %d \n", new_num ); push_int( new_num ); goto cmp_op; } /* get the next op */ i++; } return 0; }