• R/O
  • SSH
  • HTTPS

jpl: Commit


Commit MetaInfo

Revision480 (tree)
Time2019-03-10 02:15:44
Authorjakobthomsen

Log Message

factor-out lookahead char

Change Summary

Incremental Difference

--- trunk/experimental2/compiler.compiled.c (revision 479)
+++ trunk/experimental2/compiler.compiled.c (revision 480)
@@ -811,49 +811,49 @@
811811 {
812812 result = stack_pop(&callstack);
813813
814- while(true)
814+ uint32_t c = (uint32_t)getchar();
815+
816+ while((uint32_t)c <= 255)
815817 {
816- while(isspace(ungetc(getchar(), stdin))) (void)getchar(); // skip WS
817- if(!isdigit(ungetc(getchar(), stdin)) && !isalpha(ungetc(getchar(), stdin)))
818+ while(isspace(c)) c = (uint32_t)getchar(); // skip WS
819+ if((unsigned int)c > 255)
818820 {
819821 memory[tuple2_access0(state)].value = 0;
820822 break;
821823 }
822824 printf(" case ");
823- //if(isdigit(ungetc(getchar(), stdin)))
825+ //if(isdigit(c))
824826 //{
825- // while(isdigit(ungetc(getchar(), stdin)))
826- // putchar(getchar()); // echo label
827+ // while(isdigit(c))
828+ // putchar(c = (uint32_t)getchar()); // echo label
827829 //}
828830 //else
829- if(isalpha(ungetc(getchar(), stdin)))
831+ if(isalpha(c))
830832 {
831833 printf("/*");
832834 uint64_t tmp = 0;
833- while(isalpha(ungetc(getchar(), stdin)))
835+ while(isalpha(c))
834836 {
835- uint8_t c = (uint8_t)getchar();
836837 printf("%c", (char)c);
837838 tmp <<= 8;
838- tmp |= (uint64_t)c;
839+ tmp |= (uint64_t)(c & 255);
840+ c = (uint32_t)getchar();
839841 }
840842 printf("*/");
841843 printf("%lluLLU", (unsigned long long)tmp);
842844 }
843845 printf(":\n");
844- while(isspace(ungetc(getchar(), stdin))) (void)getchar(); // skip WS
845- if(123 == ungetc(getchar(), stdin))
846+
847+ while(isspace(c)) c = (uint32_t)getchar(); // skip WS
848+
849+ if(123 != c) // match brace open
846850 {
847- (void)getchar(); // match brace open
848- }
849- else
850- {
851- fprintf(stderr, "missing '{', unexpected char '%c'\n", getchar());
851+ fprintf(stderr, "missing '{', unexpected char '%c'\n", c);
852852 exit(-1);
853853 }
854854 printf(" {\n");
855855 bool in_c_code = false;
856- for(uint32_t c = (uint32_t)getchar(); c <= 255; c = (uint32_t)getchar())
856+ for(c = (uint32_t)getchar(); c <= 255; c = (uint32_t)getchar())
857857 {
858858 if(96 == c)
859859 {
@@ -895,16 +895,13 @@
895895 else if(isalpha(c))
896896 {
897897 printf("/*");
898- uint64_t tmp = 0;
899- printf("%c", (char)c);
900- tmp <<= 8;
901- tmp |= (uint64_t)c;
902- while(isalpha(ungetc(getchar(), stdin)))
898+ uint64_t id = 0;
899+ while(isalpha(c))
903900 {
904- c = (uint8_t)getchar();
905901 printf("%c", (char)c);
906- tmp <<= 8;
907- tmp |= (uint64_t)c;
902+ id <<= 8;
903+ id |= (uint64_t)(c & 255);
904+ c = (uint32_t)getchar();
908905 }
909906 printf("*/");
910907
@@ -911,7 +908,7 @@
911908 uint64_t dest = --tmpaddr;
912909 printf("memory[tuple2_access0(state)].value = %lluLLU;\n", (long long)dest); // use negative to display numbers concisely
913910 printf(" stack_push(&callstack, state);\n");
914- printf(" state = tuple2_alloc(%lluLLU, 0);\n", (unsigned long long)tmp); // echo label
911+ printf(" state = tuple2_alloc(%lluLLU, 0);\n", (unsigned long long)id); // echo label
915912 printf(" result = 0;\n");
916913 printf(" break;\n");
917914 printf(" }\n");
@@ -937,8 +934,10 @@
937934 printf("\n");
938935 printf(" break;\n");
939936 printf(" }\n");
940- //break;
937+
938+ c = (uint32_t)getchar();
941939 }
940+
942941 memory[tuple2_access0(state)].value = 0;
943942 /*outro*/memory[tuple2_access0(state)].value = 18446744073709551614LLU;
944943 stack_push(&callstack, state);
--- trunk/experimental2/compiler.source.c (revision 479)
+++ trunk/experimental2/compiler.source.c (revision 480)
@@ -421,49 +421,49 @@
421421 {
422422 intro
423423 `
424- while(true)
424+ uint32_t c = (uint32_t)getchar();
425+
426+ while((uint32_t)c <= 255)
425427 {
426- while(isspace(ungetc(getchar(), stdin))) (void)getchar(); // skip WS
427- if(!isdigit(ungetc(getchar(), stdin)) && !isalpha(ungetc(getchar(), stdin)))
428+ while(isspace(c)) c = (uint32_t)getchar(); // skip WS
429+ if((unsigned int)c > 255)
428430 {
429431 memory[tuple2_access0(state)].value = 0;
430432 break;
431433 }
432434 printf(" case ");
433- //if(isdigit(ungetc(getchar(), stdin)))
435+ //if(isdigit(c))
434436 //{
435- // while(isdigit(ungetc(getchar(), stdin)))
436- // putchar(getchar()); // echo label
437+ // while(isdigit(c))
438+ // putchar(c = (uint32_t)getchar()); // echo label
437439 //}
438440 //else
439- if(isalpha(ungetc(getchar(), stdin)))
441+ if(isalpha(c))
440442 {
441443 printf("/*");
442444 uint64_t tmp = 0;
443- while(isalpha(ungetc(getchar(), stdin)))
445+ while(isalpha(c))
444446 {
445- uint8_t c = (uint8_t)getchar();
446447 printf("%c", (char)c);
447448 tmp <<= 8;
448- tmp |= (uint64_t)c;
449+ tmp |= (uint64_t)(c & 255);
450+ c = (uint32_t)getchar();
449451 }
450452 printf("*/");
451453 printf("%lluLLU", (unsigned long long)tmp);
452454 }
453455 printf(":\n");
454- while(isspace(ungetc(getchar(), stdin))) (void)getchar(); // skip WS
455- if(123 == ungetc(getchar(), stdin))
456+
457+ while(isspace(c)) c = (uint32_t)getchar(); // skip WS
458+
459+ if(123 != c) // match brace open
456460 {
457- (void)getchar(); // match brace open
458- }
459- else
460- {
461- fprintf(stderr, "missing '{', unexpected char '%c'\n", getchar());
461+ fprintf(stderr, "missing '{', unexpected char '%c'\n", c);
462462 exit(-1);
463463 }
464464 printf(" {\n");
465465 bool in_c_code = false;
466- for(uint32_t c = (uint32_t)getchar(); c <= 255; c = (uint32_t)getchar())
466+ for(c = (uint32_t)getchar(); c <= 255; c = (uint32_t)getchar())
467467 {
468468 if(96 == c)
469469 {
@@ -505,16 +505,13 @@
505505 else if(isalpha(c))
506506 {
507507 printf("/*");
508- uint64_t tmp = 0;
509- printf("%c", (char)c);
510- tmp <<= 8;
511- tmp |= (uint64_t)c;
512- while(isalpha(ungetc(getchar(), stdin)))
508+ uint64_t id = 0;
509+ while(isalpha(c))
513510 {
514- c = (uint8_t)getchar();
515511 printf("%c", (char)c);
516- tmp <<= 8;
517- tmp |= (uint64_t)c;
512+ id <<= 8;
513+ id |= (uint64_t)(c & 255);
514+ c = (uint32_t)getchar();
518515 }
519516 printf("*/");
520517
@@ -521,7 +518,7 @@
521518 uint64_t dest = --tmpaddr;
522519 printf("memory[tuple2_access0(state)].value = %lluLLU;\n", (long long)dest); // use negative to display numbers concisely
523520 printf(" stack_push(&callstack, state);\n");
524- printf(" state = tuple2_alloc(%lluLLU, 0);\n", (unsigned long long)tmp); // echo label
521+ printf(" state = tuple2_alloc(%lluLLU, 0);\n", (unsigned long long)id); // echo label
525522 printf(" result = 0;\n");
526523 printf(" break;\n");
527524 printf(" }\n");
@@ -547,8 +544,10 @@
547544 printf("\n");
548545 printf(" break;\n");
549546 printf(" }\n");
550- //break;
547+
548+ c = (uint32_t)getchar();
551549 }
550+
552551 memory[tuple2_access0(state)].value = 0;
553552 `
554553 outro
Show on old repository browser