• R/O
  • SSH
  • HTTPS

jpl: Commit


Commit MetaInfo

Revision477 (tree)
Time2019-03-09 11:49:51
Authorjakobthomsen

Log Message

allow id-labels

Change Summary

Incremental Difference

--- trunk/experimental2/compiler.compiled.c (revision 476)
+++ trunk/experimental2/compiler.compiled.c (revision 477)
@@ -379,7 +379,6 @@
379379 // for a Total Imperative Programming Language. (c) 8.3.2019 by Jakob Thomsen
380380 /////////////////////////////////////////////////////////////////////////////
381381
382- // intro
383382 printf("#include <ctype.h>\n");
384383 printf("#include <stdbool.h>\n");
385384 printf("#include <stdint.h>\n");
@@ -763,7 +762,6 @@
763762 case 3:
764763 {
765764
766- // outro
767765 printf(" default:\n");
768766 printf(" fprintf(stderr, \"no such state %%d\\n\", (int)memory[tuple2_access0(state)].value);\n");
769767 printf(" return (int)memory[tuple2_access0(state)].value;\n");
@@ -786,11 +784,24 @@
786784
787785 break;
788786 }
787+ case 4:
788+ {
789+
790+ result = 0;
791+ while(isalpha(ungetc(getchar(), stdin)))
792+ {
793+ result <<= 8;
794+ result |= (uint64_t)((uint8_t)getchar());
795+ }
796+ tuple2_free(state);
797+ state = stack_pop(&callstack);
798+ stack_push(&callstack, result);
799+
800+ break;
801+ }
789802 case 1:
790803 {
791-
792- // intro
793- memory[tuple2_access0(state)].value = -1LL;
804+memory[tuple2_access0(state)].value = -1LL;
794805 stack_push(&callstack, state);
795806 state = tuple2_alloc(2, 0);
796807 result = 0;
@@ -803,13 +814,32 @@
803814 while(true)
804815 {
805816 while(isspace(ungetc(getchar(), stdin))) (void)getchar(); // skip WS
806- if(!isdigit(ungetc(getchar(), stdin)))
817+ if(!isdigit(ungetc(getchar(), stdin)) && !isalpha(ungetc(getchar(), stdin)))
807818 {
808819 memory[tuple2_access0(state)].value = 0;
809820 break;
810821 }
811822 printf(" case ");
812- while(isdigit(ungetc(getchar(), stdin))) putchar(getchar()); // echo label
823+ if(isdigit(ungetc(getchar(), stdin)))
824+ {
825+ while(isdigit(ungetc(getchar(), stdin)))
826+ putchar(getchar()); // echo label
827+ }
828+ else
829+ if(isalpha(ungetc(getchar(), stdin)))
830+ {
831+ printf("/*");
832+ uint64_t tmp = 0;
833+ while(isalpha(ungetc(getchar(), stdin)))
834+ {
835+ uint8_t c = (uint8_t)getchar();
836+ printf("%c", (char)c);
837+ tmp <<= 8;
838+ tmp |= (uint64_t)c;
839+ }
840+ printf("*/");
841+ printf("%lluLLU", (unsigned long long)tmp);
842+ }
813843 printf(":\n");
814844 while(isspace(ungetc(getchar(), stdin))) (void)getchar(); // skip WS
815845 if(123 == ungetc(getchar(), stdin))
@@ -862,6 +892,34 @@
862892 printf(" {\n");
863893 printf(" result = stack_pop(&callstack);\n");
864894 }
895+ else if(isalpha(c))
896+ {
897+ 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)))
903+ {
904+ c = (uint8_t)getchar();
905+ printf("%c", (char)c);
906+ tmp <<= 8;
907+ tmp |= (uint64_t)c;
908+ }
909+ printf("*/");
910+
911+ uint64_t dest = --tmpaddr;
912+ printf("memory[tuple2_access0(state)].value = %lluLLU;\n", (long long)dest); // use negative to display numbers concisely
913+ printf(" stack_push(&callstack, state);\n");
914+ printf(" state = tuple2_alloc(%lluLLU, 0);\n", (unsigned long long)tmp); // echo label
915+ printf(" result = 0;\n");
916+ printf(" break;\n");
917+ printf(" }\n");
918+ //printf(" case %lluLLU:\n", (unsigned long long)--tmpaddr);
919+ printf(" case %lluLLU:\n", (long long)dest); // use negative to display numbers concisely
920+ printf(" {\n");
921+ printf(" result = stack_pop(&callstack);\n");
922+ }
865923 else if(64 == c)
866924 {
867925 printf(" result = ");
@@ -882,8 +940,6 @@
882940 //break;
883941 }
884942 memory[tuple2_access0(state)].value = 0;
885-
886- // outro
887943 memory[tuple2_access0(state)].value = -2LL;
888944 stack_push(&callstack, state);
889945 state = tuple2_alloc(3, 0);
--- trunk/experimental2/compiler.source.c (revision 476)
+++ trunk/experimental2/compiler.source.c (revision 477)
@@ -1,6 +1,5 @@
11 2
22 {
3-
43 `
54 /////////////////////////////////////////////////////////////////////////////
65 // Another attempt writing a self-hosting compiler with own memory-management
@@ -7,7 +6,6 @@
76 // for a Total Imperative Programming Language. (c) 8.3.2019 by Jakob Thomsen
87 /////////////////////////////////////////////////////////////////////////////
98
10- // intro
119 printf("#include <ctype.h>\n");
1210 printf("#include <stdbool.h>\n");
1311 printf("#include <stdint.h>\n");
@@ -384,10 +382,10 @@
384382 printf(" {\n");
385383 `
386384 }
385+
387386 3
388387 {
389388 `
390- // outro
391389 printf(" default:\n");
392390 printf(" fprintf(stderr, \"no such state %%d\\n\", (int)memory[tuple2_access0(state)].value);\n");
393391 printf(" return (int)memory[tuple2_access0(state)].value;\n");
@@ -406,23 +404,52 @@
406404 printf("\n");
407405 `
408406 }
409-1
407+
408+4
410409 {
411410 `
412- // intro
411+ result = 0;
412+ while(isalpha(ungetc(getchar(), stdin)))
413+ {
414+ result <<= 8;
415+ result |= (uint64_t)((uint8_t)getchar());
416+ }
413417 `
418+}
419+
420+1
421+{
414422 2
415423 `
416424 while(true)
417425 {
418426 while(isspace(ungetc(getchar(), stdin))) (void)getchar(); // skip WS
419- if(!isdigit(ungetc(getchar(), stdin)))
427+ if(!isdigit(ungetc(getchar(), stdin)) && !isalpha(ungetc(getchar(), stdin)))
420428 {
421429 memory[tuple2_access0(state)].value = 0;
422430 break;
423431 }
424432 printf(" case ");
425- while(isdigit(ungetc(getchar(), stdin))) putchar(getchar()); // echo label
433+ if(isdigit(ungetc(getchar(), stdin)))
434+ {
435+ while(isdigit(ungetc(getchar(), stdin)))
436+ putchar(getchar()); // echo label
437+ }
438+ else
439+ if(isalpha(ungetc(getchar(), stdin)))
440+ {
441+ printf("/*");
442+ uint64_t tmp = 0;
443+ while(isalpha(ungetc(getchar(), stdin)))
444+ {
445+ uint8_t c = (uint8_t)getchar();
446+ printf("%c", (char)c);
447+ tmp <<= 8;
448+ tmp |= (uint64_t)c;
449+ }
450+ printf("*/");
451+ printf("%lluLLU", (unsigned long long)tmp);
452+ }
426453 printf(":\n");
427454 while(isspace(ungetc(getchar(), stdin))) (void)getchar(); // skip WS
428455 if(123 == ungetc(getchar(), stdin))
@@ -475,6 +502,34 @@
475502 printf(" {\n");
476503 printf(" result = stack_pop(&callstack);\n");
477504 }
505+ else if(isalpha(c))
506+ {
507+ 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)))
513+ {
514+ c = (uint8_t)getchar();
515+ printf("%c", (char)c);
516+ tmp <<= 8;
517+ tmp |= (uint64_t)c;
518+ }
519+ printf("*/");
520+
521+ uint64_t dest = --tmpaddr;
522+ printf("memory[tuple2_access0(state)].value = %lluLLU;\n", (long long)dest); // use negative to display numbers concisely
523+ printf(" stack_push(&callstack, state);\n");
524+ printf(" state = tuple2_alloc(%lluLLU, 0);\n", (unsigned long long)tmp); // echo label
525+ printf(" result = 0;\n");
526+ printf(" break;\n");
527+ printf(" }\n");
528+ //printf(" case %lluLLU:\n", (unsigned long long)--tmpaddr);
529+ printf(" case %lluLLU:\n", (long long)dest); // use negative to display numbers concisely
530+ printf(" {\n");
531+ printf(" result = stack_pop(&callstack);\n");
532+ }
478533 else if(64 == c)
479534 {
480535 printf(" result = ");
@@ -495,8 +550,6 @@
495550 //break;
496551 }
497552 memory[tuple2_access0(state)].value = 0;
498-
499- // outro
500553 `
501554 3
502555
Show on old repository browser