native/unix/signal.o \
native/unix/read.o \
native/unix/write.o \
- native/unix/ffi.o
+ native/unix/ffi.o \
+ native/debug.o
default:
@echo "Run 'make' with one of the following parameters:"
object-tag here-as swap
string-type >header emit
dup str-length emit
- dup hashcode emit
+ dup hashcode fixnum-tag immediate emit
pack-string
align-here ;
>r dup vector-length [
f swap pick set-vector-nth
] times* r>
- [ unswons pick set-hash ] each
+ [ unswons pick set-hash ] each drop
] cons cons
boot-quot [ append ] change ;
[ "strings" | "str-nth" ]
[ "strings" | "str-compare" ]
[ "strings" | "str=" ]
- [ "strings" | "str-hashcode" ]
[ "strings" | "index-of*" ]
[ "strings" | "substring" ]
[ "strings" | "str-reverse" ]
: alist>hash ( alist -- hash )
37 <hashtable> swap [ unswons pick set-hash ] each ;
+
+! In case I break hashing:
+
+! : hash* ( key table -- value )
+! hash>alist assoc* ;
+!
+! : set-hash ( value key table -- )
+! dup vector-length [
+! ( value key table index )
+! >r 3dup r>
+! ( value key table value key table index )
+! [
+! swap vector-nth
+! ( value key table value key alist )
+! set-assoc
+! ] keep
+! ( value key table new-assoc index )
+! pick set-vector-nth
+! ] times* 3drop ;
[ str-nth " n str -- ch " [ [ integer string ] [ integer ] ] ]
[ str-compare " str str -- -1/0/1 " [ [ string string ] [ integer ] ] ]
[ str= " str str -- ? " [ [ string string ] [ boolean ] ] ]
- [ str-hashcode " str -- n " [ [ string ] [ integer ] ] ]
[ index-of* " n str/ch str -- n " [ [ integer string text ] [ integer ] ] ]
[ substring " start end str -- str " [ [ integer integer string ] [ string ] ] ]
[ str-reverse " str -- str " [ [ string ] [ string ] ] ]
! Define methods bound to primitives
BUILTIN: string 12
-M: string hashcode str-hashcode ;
+M: string hashcode 2 slot ;
M: string = str= ;
: str-length ( str -- len ) >string 1 integer-slot ; inline
-BUILTIN: sbuf 13
+BUILTIN: sbuf 13
M: sbuf hashcode sbuf-hashcode ;
M: sbuf = sbuf= ;
--- /dev/null
+#include "factor.h"
+
+#ifdef F_DEBUG
+
+bool equals(CELL obj1, CELL obj2)
+{
+ if(type_of(obj1) == STRING_TYPE
+ && type_of(obj2) == STRING_TYPE)
+ {
+ return string_compare(untag_string(obj1),untag_string(obj2)) == 0;
+ }
+ else
+ return (obj1 == obj2);
+}
+
+CELL assoc(CELL alist, CELL key)
+{
+ if(TAG(alist) != CONS_TYPE)
+ {
+ fprintf(stderr,"Not an alist: %ld\n",alist);
+ return F;
+ }
+
+ {
+ CELL pair = untag_cons(alist)->car;
+ if(TAG(pair) != CONS_TYPE)
+ {
+ fprintf(stderr,"Not a pair: %ld\n",alist);
+ return F;
+ }
+
+ if(equals(untag_cons(pair)->car,key))
+ return untag_cons(pair)->cdr;
+ else
+ return assoc(untag_cons(alist)->cdr,key);
+ }
+}
+
+void print_cons(CELL cons)
+{
+ fprintf(stderr,"[ ");
+
+ do
+ {
+ print_obj(untag_cons(cons)->car);
+ fprintf(stderr," ");
+ cons = untag_cons(cons)->cdr;
+ }
+ while(TAG(cons) == CONS_TYPE);
+
+ if(cons != F)
+ {
+ fprintf(stderr,"| ");
+ print_obj(cons);
+ fprintf(stderr," ");
+ }
+ fprintf(stderr,"]");
+}
+
+void print_word(F_WORD* word)
+{
+ CELL name = assoc(word->plist,tag_object(from_c_string("name")));
+ if(type_of(name) == STRING_TYPE)
+ fprintf(stderr,"%s",to_c_string(untag_string(name)));
+ else
+ {
+ fprintf(stderr,"#<not a string: ");
+ print_obj(name);
+ fprintf(stderr,">");
+ }
+
+ fprintf(stderr," (#%ld)",word->primitive);
+}
+
+void print_string(F_STRING* str)
+{
+ fprintf(stderr,"\"");
+ fprintf(stderr,"%s",to_c_string(str));
+ fprintf(stderr,"\"");
+}
+
+void print_obj(CELL obj)
+{
+ switch(type_of(obj))
+ {
+ case CONS_TYPE:
+ print_cons(obj);
+ break;
+ case WORD_TYPE:
+ print_word(untag_word(obj));
+ break;
+ case STRING_TYPE:
+ print_string(untag_string(obj));
+ break;
+ case F_TYPE:
+ fprintf(stderr,"f");
+ break;
+ default:
+ fprintf(stderr,"#<type %ld @ %ld>",type_of(obj),obj);
+ break;
+ }
+}
+
+void print_stack(CELL* start, CELL* end)
+{
+ while(start < end)
+ {
+ print_obj(*start);
+ fprintf(stderr,"\n");
+ start++;
+ }
+}
+
+void dump_stacks(void)
+{
+ fprintf(stderr,"*** Data stack:\n");
+ print_stack((CELL*)ds_bot,(CELL*)(ds + CELLS));
+ fprintf(stderr,"*** Call stack:\n");
+ print_stack((CELL*)cs_bot,(CELL*)(cs + CELLS));
+ fprintf(stderr,"*** Call frame:\n");
+ print_obj(callframe);
+ fprintf(stderr,"\n");
+ fprintf(stderr,"*** Executing:\n");
+ print_word(executing);
+ fprintf(stderr,"\n");
+ fflush(stderr);
+}
+
+#else
+
+void dump_stacks(void)
+{
+ fprintf(stderr,"Stack dumping disabled -- recompile with F_DEBUG\n");
+}
+
+#endif
--- /dev/null
+#define F_DEBUG 1
+
+CELL assoc(CELL alist, CELL key);
+void print_cons(CELL cons);
+void print_word(F_WORD* word);
+void print_string(F_STRING* str);
+void print_obj(CELL obj);
+void print_stack(CELL* start, CELL* end);
+void dump_stacks(void);
fprintf(stderr,"Error: %ld\n",to_fixnum(error));
else if(type_of(error) == STRING_TYPE)
fprintf(stderr,"Error: %s\n",to_c_string(untag_string(error)));
+
+ dump_stacks();
+
fflush(stderr);
exit(1);
#include "compiler.h"
#include "relocate.h"
#include "ffi.h"
+#include "debug.h"
#endif /* __FACTOR_H__ */
void primitive_alien_1(void);
void primitive_set_alien_1(void);
void fixup_dll(DLL* dll);
+void collect_dll(DLL* dll);
void fixup_alien(ALIEN* alien);
void collect_alien(ALIEN* alien);
collect_alien((ALIEN*)scan);
break;
case DLL_TYPE:
- collect_dll((ALIEN*)scan);
+ collect_dll((DLL*)scan);
break;
}
}
primitive_string_nth,
primitive_string_compare,
primitive_string_eq,
- primitive_string_hashcode,
primitive_index_of,
primitive_substring,
primitive_string_reverse,
void rehash_string(F_STRING* str)
{
- str->hashcode = hash_string(str,str->capacity);
+ str->hashcode = tag_fixnum(hash_string(str,str->capacity));
}
/* untagged */
dpush(F);
}
-void primitive_string_hashcode(void)
-{
- drepl(tag_fixnum(untag_string(dpeek())->hashcode));
-}
-
CELL index_of_ch(CELL index, F_STRING* string, CELL ch)
{
while(index < string->capacity)
CELL header;
/* untagged */
CELL capacity;
- /* untagged */
- F_FIXNUM hashcode;
+ /* tagged */
+ CELL hashcode;
} F_STRING;
INLINE F_STRING* untag_string(CELL tagged)
F_FIXNUM string_compare(F_STRING* s1, F_STRING* s2);
void primitive_string_compare(void);
void primitive_string_eq(void);
-void primitive_string_hashcode(void);
void primitive_index_of(void);
void primitive_substring(void);
void string_reverse(F_STRING* s, int len);
typedef struct {
/* TAGGED header */
CELL header;
- /* untagged hashcode */
+ /* TAGGED hashcode */
CELL hashcode;
/* untagged execution token: jump here to execute word */
CELL xt;