Commit 55a56e92ed65cab197308de1d594f155f8891154

  • avatar
  • Antti-Juhani Kaijanaho <antti-juhani @kaij…aho.fi> (Committer)
  • Fri Nov 16 15:11:03 EET 2012
  • avatar
  • Antti-Juhani Kaijanaho <antti-juhani @kaij…aho.fi> (Author)
  • Fri Nov 16 15:11:03 EET 2012
Move type inference above symbol table definitions

That way the high-level stuff is before the low-level details.

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@kaijanaho.fi>
hm.y
(169 / 170)
  
558558}
559559
560560/**************************************************************************
561 SYMBOL TABLE DEFINITIONS
562***************************************************************************/
563
564/* We use a simple hash table with chaining. We keep track of local
565 definitions and what they shadow by using a stack. */
566
567static uint32_t hash_string(const char *s)
568{
569 // 32-bit FNV-1a hash
570 uint32_t hash = UINT32_C(2166136261);
571 for (unsigned char c = *s; c != 0; c = *s++) {
572 hash = (hash ^ c) * UINT32_C(16777619);
573 }
574 return hash;
575}
576
577struct sym_entry {
578 char *s;
579 struct term *te;
580 struct polytype *ty;
581 struct sym_entry *next;
582};
583
584enum { symtab_size = 1024*1024 };
585static struct sym_entry *symtab[symtab_size];
586static struct sym_entry *shadow_stack;
587
588struct binding_iterator {
589 struct sym_entry *se;
590};
591static struct binding_iterator *local_bindings_begin(void)
592{
593 struct binding_iterator *rv;
594 NEW(rv);
595 rv->se = shadow_stack;
596 return rv;
597}
598static bool local_bindings_end(struct binding_iterator *it)
599{
600 if (it->se != 0) return false;
601 free(it);
602 return true;
603}
604static void local_bindings_next(struct binding_iterator *it)
605{
606 it->se = it->se->next;
607}
608static const char *local_binding_name(struct binding_iterator *it)
609{
610 return it->se->s;
611}
612
613static void bind_globally(const char *s, struct term *te, struct polytype *ty)
614{
615 assert(shadow_stack == 0);
616 size_t hash = hash_string(s) % symtab_size;
617 struct sym_entry **bucket = &symtab[hash];
618 for (struct sym_entry **sep = bucket; *sep != 0; sep = &(*sep)->next) {
619 struct sym_entry *se = *sep;
620 if (strcmp(se->s, s) == 0) {
621 se->te = te;
622 se->ty = ty;
623 return;
624 }
625 }
626 struct sym_entry *se;
627 NEW(se);
628 se->s = dup_str(s);
629 se->te = te;
630 se->ty = ty;
631 se->next = *bucket;
632 *bucket = se;
633}
634
635static void bind_locally(const char *s, struct term *te, struct polytype *ty)
636{
637 size_t hash = hash_string(s) % symtab_size;
638 struct sym_entry **bucket = &symtab[hash];
639 // Remove the old binding, if any, and save it in se.
640 struct sym_entry *se = 0;
641 for (struct sym_entry **sep = bucket; *sep != 0; sep = &(*sep)->next) {
642 if (strcmp((*sep)->s, s) == 0) {
643 se = *sep;
644 *sep = se->next;
645 break;
646 }
647 }
648 if (se == 0) {
649 /* There was no old binding; make up one to indicate
650 that nothing is being shadowed. */
651 NEW(se);
652 se->s = dup_str(s);
653 se->te = 0;
654 se->ty = 0;
655 }
656 /* Push the old binding on the shadow stack. */
657 se->next = shadow_stack;
658 shadow_stack = se;
659 /* Create and insert the new binding. */
660 NEW(se);
661 se->s = dup_str(s);
662 se->te = te;
663 se->ty = ty;
664 se->next = *bucket;
665 *bucket = se;
666
667}
668
669static void undo_local_binding(const char *s,
670 struct term *te,
671 struct polytype *ty)
672{
673 /* Pop the shadowed entry from the stack. */
674 struct sym_entry *old = shadow_stack;
675 assert(strcmp(old->s, s) == 0);
676 shadow_stack = old->next;
677
678 /* If the shadowed entry was just to note that nothing was
679 shadowed, delete it. */
680 if (old->te == 0 && old->ty == 0) {
681 free(old->s);
682 free(old);
683 old = 0;
684 }
685
686 // Find the current entry.
687 size_t hash = hash_string(s) % symtab_size;
688 struct sym_entry **bucket = &symtab[hash];
689 for (struct sym_entry **sep = bucket; *sep != 0; sep = &(*sep)->next) {
690 if (strcmp((*sep)->s, s) == 0) {
691 // Found it. Delete it.
692 struct sym_entry *se = *sep;
693 assert(se->te == te);
694 assert(se->ty == ty);
695 *sep = se->next;
696 free(se->s);
697 free(se);
698
699 // Reinsert the shadowed binding, if any.
700 if (old != 0) {
701 old->next = *bucket;
702 *bucket = old;
703 }
704 return;
705 }
706 }
707 /* If we reach this spot, there was no entry to remove, which
708 is a programmer's error. */
709 assert(0);
710}
711
712static struct term *lookup_definition(const char *s)
713{
714 size_t hash = hash_string(s) % symtab_size;
715 for (struct sym_entry *se = symtab[hash]; se != 0; se = se->next) {
716 if (strcmp(se->s, s) == 0) return se->te;
717 }
718 return 0;
719}
720
721static struct polytype *lookup_type(const char *s)
722{
723 size_t hash = hash_string(s) % symtab_size;
724 for (struct sym_entry *se = symtab[hash]; se != 0; se = se->next) {
725 if (strcmp(se->s, s) == 0) return se->ty;
726 }
727 return 0;
728}
729
730/**************************************************************************
731561 HINDLEY-MILNER TYPE INFERENCE
732562***************************************************************************/
733563
754754 assert(0);
755755}
756756
757/**************************************************************************
758 SYMBOL TABLE DEFINITIONS
759***************************************************************************/
760
761/* We use a simple hash table with chaining. We keep track of local
762 definitions and what they shadow by using a stack. */
763
764static uint32_t hash_string(const char *s)
765{
766 // 32-bit FNV-1a hash
767 uint32_t hash = UINT32_C(2166136261);
768 for (unsigned char c = *s; c != 0; c = *s++) {
769 hash = (hash ^ c) * UINT32_C(16777619);
770 }
771 return hash;
772}
773
774struct sym_entry {
775 char *s;
776 struct term *te;
777 struct polytype *ty;
778 struct sym_entry *next;
779};
780
781enum { symtab_size = 1024*1024 };
782static struct sym_entry *symtab[symtab_size];
783static struct sym_entry *shadow_stack;
784
785struct binding_iterator {
786 struct sym_entry *se;
787};
788static struct binding_iterator *local_bindings_begin(void)
789{
790 struct binding_iterator *rv;
791 NEW(rv);
792 rv->se = shadow_stack;
793 return rv;
794}
795static bool local_bindings_end(struct binding_iterator *it)
796{
797 if (it->se != 0) return false;
798 free(it);
799 return true;
800}
801static void local_bindings_next(struct binding_iterator *it)
802{
803 it->se = it->se->next;
804}
805static const char *local_binding_name(struct binding_iterator *it)
806{
807 return it->se->s;
808}
809
810static void bind_globally(const char *s, struct term *te, struct polytype *ty)
811{
812 assert(shadow_stack == 0);
813 size_t hash = hash_string(s) % symtab_size;
814 struct sym_entry **bucket = &symtab[hash];
815 for (struct sym_entry **sep = bucket; *sep != 0; sep = &(*sep)->next) {
816 struct sym_entry *se = *sep;
817 if (strcmp(se->s, s) == 0) {
818 se->te = te;
819 se->ty = ty;
820 return;
821 }
822 }
823 struct sym_entry *se;
824 NEW(se);
825 se->s = dup_str(s);
826 se->te = te;
827 se->ty = ty;
828 se->next = *bucket;
829 *bucket = se;
830}
831
832static void bind_locally(const char *s, struct term *te, struct polytype *ty)
833{
834 size_t hash = hash_string(s) % symtab_size;
835 struct sym_entry **bucket = &symtab[hash];
836 // Remove the old binding, if any, and save it in se.
837 struct sym_entry *se = 0;
838 for (struct sym_entry **sep = bucket; *sep != 0; sep = &(*sep)->next) {
839 if (strcmp((*sep)->s, s) == 0) {
840 se = *sep;
841 *sep = se->next;
842 break;
843 }
844 }
845 if (se == 0) {
846 /* There was no old binding; make up one to indicate
847 that nothing is being shadowed. */
848 NEW(se);
849 se->s = dup_str(s);
850 se->te = 0;
851 se->ty = 0;
852 }
853 /* Push the old binding on the shadow stack. */
854 se->next = shadow_stack;
855 shadow_stack = se;
856 /* Create and insert the new binding. */
857 NEW(se);
858 se->s = dup_str(s);
859 se->te = te;
860 se->ty = ty;
861 se->next = *bucket;
862 *bucket = se;
863
864}
865
866static void undo_local_binding(const char *s,
867 struct term *te,
868 struct polytype *ty)
869{
870 /* Pop the shadowed entry from the stack. */
871 struct sym_entry *old = shadow_stack;
872 assert(strcmp(old->s, s) == 0);
873 shadow_stack = old->next;
874
875 /* If the shadowed entry was just to note that nothing was
876 shadowed, delete it. */
877 if (old->te == 0 && old->ty == 0) {
878 free(old->s);
879 free(old);
880 old = 0;
881 }
882
883 // Find the current entry.
884 size_t hash = hash_string(s) % symtab_size;
885 struct sym_entry **bucket = &symtab[hash];
886 for (struct sym_entry **sep = bucket; *sep != 0; sep = &(*sep)->next) {
887 if (strcmp((*sep)->s, s) == 0) {
888 // Found it. Delete it.
889 struct sym_entry *se = *sep;
890 assert(se->te == te);
891 assert(se->ty == ty);
892 *sep = se->next;
893 free(se->s);
894 free(se);
895
896 // Reinsert the shadowed binding, if any.
897 if (old != 0) {
898 old->next = *bucket;
899 *bucket = old;
900 }
901 return;
902 }
903 }
904 /* If we reach this spot, there was no entry to remove, which
905 is a programmer's error. */
906 assert(0);
907}
908
909static struct term *lookup_definition(const char *s)
910{
911 size_t hash = hash_string(s) % symtab_size;
912 for (struct sym_entry *se = symtab[hash]; se != 0; se = se->next) {
913 if (strcmp(se->s, s) == 0) return se->te;
914 }
915 return 0;
916}
917
918static struct polytype *lookup_type(const char *s)
919{
920 size_t hash = hash_string(s) % symtab_size;
921 for (struct sym_entry *se = symtab[hash]; se != 0; se = se->next) {
922 if (strcmp(se->s, s) == 0) return se->ty;
923 }
924 return 0;
925}
757926
758927/**************************************************************************
759928 ABSTRACT SYNTAX DEFINITIONS FOR TERMS