factor/library/bootstrap/primitives.factor

431 lines
14 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: image
2005-12-25 22:18:25 -05:00
USING: alien arrays generic hashtables help io kernel
2006-09-06 17:19:41 -04:00
kernel-internals math modules namespaces parser sequences
strings vectors words ;
2005-08-19 21:46:12 -04:00
! Some very tricky code creating a bootstrap embryo in the
! host image.
2005-07-31 23:38:33 -04:00
2005-12-16 21:12:35 -05:00
"Creating primitives and basic runtime structures..." print flush
H{ } clone c-types set
2006-09-29 23:03:27 -04:00
"resource:/library/compiler/alien/primitive-types.factor" parse-file
! Bring up a bare cross-compiling vocabulary.
2005-08-22 15:33:18 -04:00
"syntax" vocab
2006-09-29 23:03:27 -04:00
H{ } clone source-files set
H{ } clone vocabularies set
H{ } clone class<map set
V{ } clone modules set
2005-03-07 22:11:36 -05:00
2006-05-28 17:31:54 -04:00
vocabularies get [ "syntax" set ] bind
H{ } clone articles set
2006-06-17 01:03:56 -04:00
parent-graph off
2006-06-22 22:36:56 -04:00
term-index off
2006-05-28 17:31:54 -04:00
crossref off
changed-words off
! Call the quotation parsed from primitive-types.factor
call
: make-primitive ( { vocab word } n -- )
2006-01-06 02:04:42 -05:00
>r first2 create f r> define ;
{
{ "execute" "words" }
{ "call" "kernel" }
{ "if" "kernel" }
{ "dispatch" "kernel-internals" }
{ "<vector>" "vectors" }
{ "rehash-string" "strings" }
{ "<sbuf>" "strings" }
{ ">fixnum" "math" }
{ ">bignum" "math" }
{ ">float" "math" }
{ "(fraction>)" "math-internals" }
{ "string>float" "math-internals" }
{ "float>string" "math-internals" }
{ "float>bits" "math" }
{ "double>bits" "math" }
{ "bits>float" "math" }
{ "bits>double" "math" }
{ "<complex>" "math-internals" }
{ "fixnum+" "math-internals" }
{ "fixnum+fast" "math-internals" }
{ "fixnum-" "math-internals" }
{ "fixnum-fast" "math-internals" }
{ "fixnum*" "math-internals" }
{ "fixnum/i" "math-internals" }
{ "fixnum/f" "math-internals" }
{ "fixnum-mod" "math-internals" }
{ "fixnum/mod" "math-internals" }
{ "fixnum-bitand" "math-internals" }
{ "fixnum-bitor" "math-internals" }
{ "fixnum-bitxor" "math-internals" }
{ "fixnum-bitnot" "math-internals" }
{ "fixnum-shift" "math-internals" }
{ "fixnum<" "math-internals" }
{ "fixnum<=" "math-internals" }
{ "fixnum>" "math-internals" }
{ "fixnum>=" "math-internals" }
{ "bignum=" "math-internals" }
{ "bignum+" "math-internals" }
{ "bignum-" "math-internals" }
{ "bignum*" "math-internals" }
{ "bignum/i" "math-internals" }
{ "bignum/f" "math-internals" }
{ "bignum-mod" "math-internals" }
{ "bignum/mod" "math-internals" }
{ "bignum-bitand" "math-internals" }
{ "bignum-bitor" "math-internals" }
{ "bignum-bitxor" "math-internals" }
{ "bignum-bitnot" "math-internals" }
{ "bignum-shift" "math-internals" }
{ "bignum<" "math-internals" }
{ "bignum<=" "math-internals" }
{ "bignum>" "math-internals" }
{ "bignum>=" "math-internals" }
{ "float+" "math-internals" }
{ "float-" "math-internals" }
{ "float*" "math-internals" }
{ "float/f" "math-internals" }
{ "float-mod" "math-internals" }
{ "float<" "math-internals" }
{ "float<=" "math-internals" }
{ "float>" "math-internals" }
{ "float>=" "math-internals" }
2006-05-29 04:27:30 -04:00
{ "(word)" "kernel-internals" }
{ "update-xt" "words" }
{ "word-xt" "words" }
{ "drop" "kernel" }
{ "2drop" "kernel" }
{ "3drop" "kernel" }
{ "dup" "kernel" }
{ "2dup" "kernel" }
{ "3dup" "kernel" }
{ "rot" "kernel" }
{ "-rot" "kernel" }
{ "dupd" "kernel" }
{ "swapd" "kernel" }
{ "nip" "kernel" }
{ "2nip" "kernel" }
{ "tuck" "kernel" }
{ "over" "kernel" }
{ "pick" "kernel" }
{ "swap" "kernel" }
{ ">r" "kernel" }
{ "r>" "kernel" }
{ "eq?" "kernel" }
{ "getenv" "kernel-internals" }
{ "setenv" "kernel-internals" }
{ "stat" "io" }
{ "(directory)" "io" }
2006-09-26 19:00:41 -04:00
{ "data-gc" "memory" }
{ "code-gc" "memory" }
{ "gc-time" "memory" }
{ "save-image" "memory" }
{ "datastack" "kernel" }
2006-05-14 23:09:47 -04:00
{ "retainstack" "kernel" }
{ "callstack" "kernel" }
{ "set-datastack" "kernel" }
2006-05-14 23:09:47 -04:00
{ "set-retainstack" "kernel" }
{ "set-callstack" "kernel" }
{ "exit" "kernel" }
2006-09-26 01:08:05 -04:00
{ "data-room" "memory" }
{ "code-room" "memory" }
{ "os-env" "kernel" }
{ "millis" "kernel" }
{ "type" "kernel" }
{ "tag" "kernel-internals" }
{ "cwd" "io" }
{ "cd" "io" }
{ "add-compiled-block" "assembler" }
{ "dlopen" "alien" }
{ "dlsym" "alien" }
{ "dlclose" "alien" }
{ "<byte-array>" "arrays" }
{ "<displaced-alien>" "alien" }
{ "alien-signed-cell" "alien" }
{ "set-alien-signed-cell" "alien" }
{ "alien-unsigned-cell" "alien" }
{ "set-alien-unsigned-cell" "alien" }
{ "alien-signed-8" "alien" }
{ "set-alien-signed-8" "alien" }
{ "alien-unsigned-8" "alien" }
{ "set-alien-unsigned-8" "alien" }
{ "alien-signed-4" "alien" }
{ "set-alien-signed-4" "alien" }
{ "alien-unsigned-4" "alien" }
{ "set-alien-unsigned-4" "alien" }
{ "alien-signed-2" "alien" }
{ "set-alien-signed-2" "alien" }
{ "alien-unsigned-2" "alien" }
{ "set-alien-unsigned-2" "alien" }
{ "alien-signed-1" "alien" }
{ "set-alien-signed-1" "alien" }
{ "alien-unsigned-1" "alien" }
{ "set-alien-unsigned-1" "alien" }
{ "alien-float" "alien" }
{ "set-alien-float" "alien" }
{ "alien-double" "alien" }
{ "set-alien-double" "alien" }
{ "alien>char-string" "alien" }
{ "string>char-alien" "alien" }
{ "alien>u16-string" "alien" }
{ "string>u16-alien" "alien" }
{ "throw" "errors" }
{ "string>memory" "kernel-internals" }
{ "memory>string" "kernel-internals" }
{ "alien-address" "alien" }
{ "slot" "kernel-internals" }
{ "set-slot" "kernel-internals" }
{ "char-slot" "kernel-internals" }
{ "set-char-slot" "kernel-internals" }
{ "resize-array" "arrays" }
{ "resize-string" "strings" }
2005-11-27 17:45:48 -05:00
{ "(hashtable)" "hashtables-internals" }
{ "<array>" "arrays" }
{ "<tuple>" "kernel-internals" }
{ "begin-scan" "memory" }
{ "next-object" "memory" }
{ "end-scan" "memory" }
{ "size" "memory" }
{ "die" "kernel" }
{ "finalize-compile" "assembler" }
{ "fopen" "io-internals" }
{ "fgetc" "io-internals" }
{ "fwrite" "io-internals" }
{ "fflush" "io-internals" }
{ "fclose" "io-internals" }
{ "expired?" "alien" }
{ "<wrapper>" "kernel" }
{ "(clone)" "kernel-internals" }
{ "array>tuple" "kernel-internals" }
{ "tuple>array" "generic" }
{ "array>vector" "vectors" }
2005-12-24 18:29:31 -05:00
{ "<string>" "strings" }
2006-05-16 16:50:51 -04:00
{ "<quotation>" "kernel" }
} dup length 3 swap [ + ] map-with [ make-primitive ] 2each
FORGET: make-primitive
2005-08-19 21:46:12 -04:00
! Okay, now we have primitives fleshed out. Bring up the generic
! word system.
: builtin-predicate ( class predicate -- )
[
over "type" word-prop dup
tag-mask < \ tag \ type ? , , \ eq? ,
] [ ] make define-predicate ;
2005-08-19 21:46:12 -04:00
: register-builtin ( class -- )
dup "type" word-prop builtins get set-nth ;
2005-08-19 21:46:12 -04:00
: define-builtin ( symbol type# predicate slotspec -- )
>r >r >r
dup intern-symbol
dup r> "type" set-word-prop
dup define-class
2005-08-19 21:46:12 -04:00
dup r> builtin-predicate
dup r> intern-slots 2dup "slots" set-word-prop
define-slots
register-builtin ;
H{ } clone typemap set
2005-12-24 18:29:31 -05:00
num-types f <array> builtins set
2005-08-19 21:46:12 -04:00
! These symbols are needed by the code that executes below
"object" "generic" create drop
"null" "generic" create drop
2005-08-22 16:01:13 -04:00
"fixnum?" "math" create t "inline" set-word-prop
"fixnum" "math" create 0 "fixnum?" "math" create { } define-builtin
2005-12-17 14:52:27 -05:00
"fixnum" "math" create ">fixnum" "math" lookup unit "coercer" set-word-prop
2005-08-19 21:46:12 -04:00
"bignum?" "math" create t "inline" set-word-prop
"bignum" "math" create 1 "bignum?" "math" create { } define-builtin
2005-12-17 14:52:27 -05:00
"bignum" "math" create ">bignum" "math" lookup unit "coercer" set-word-prop
2005-08-19 21:46:12 -04:00
2006-05-18 01:08:09 -04:00
"word?" "words" create t "inline" set-word-prop
"word" "words" create 2 "word?" "words" create
{
{ 1 fixnum { "hashcode" "kernel" } f }
{
2
object
{ "word-name" "words" }
{ "set-word-name" "words" }
2006-05-18 01:08:09 -04:00
}
{
3
object
{ "word-vocabulary" "words" }
{ "set-word-vocabulary" "words" }
}
{
4
object
{ "word-primitive" "words" }
{ "set-word-primitive" "words" }
}
{
5
object
{ "word-def" "words" }
{ "set-word-def" "words" }
}
{
6
object
{ "word-props" "words" }
{ "set-word-props" "words" }
}
{
7
object
{ "compiled?" "words" }
f
}
2006-05-18 01:08:09 -04:00
} define-builtin
"ratio?" "math" create t "inline" set-word-prop
2005-08-19 21:46:12 -04:00
"ratio" "math" create 4 "ratio?" "math" create
{
{ 1 integer { "numerator" "math" } f }
{ 2 integer { "denominator" "math" } f }
} define-builtin
2005-08-19 21:46:12 -04:00
"float?" "math" create t "inline" set-word-prop
"float" "math" create 5 "float?" "math" create { } define-builtin
2005-12-17 14:52:27 -05:00
"float" "math" create ">float" "math" lookup unit "coercer" set-word-prop
2005-08-19 21:46:12 -04:00
"complex?" "math" create t "inline" set-word-prop
2005-08-19 21:46:12 -04:00
"complex" "math" create 6 "complex?" "math" create
{
{ 1 real { "real" "math" } f }
{ 2 real { "imaginary" "math" } f }
} define-builtin
2005-08-19 21:46:12 -04:00
2006-05-18 01:08:09 -04:00
"wrapper?" "kernel" create t "inline" set-word-prop
"wrapper" "kernel" create 7 "wrapper?" "kernel" create
{ { 1 object { "wrapped" "kernel" } f } } define-builtin
2005-08-19 21:46:12 -04:00
"array?" "arrays" create t "inline" set-word-prop
"array" "arrays" create 8 "array?" "arrays" create
{ } define-builtin
2005-08-19 21:46:12 -04:00
"!f" "!syntax" create 9 "not" "kernel" create
{ } define-builtin
2005-08-19 21:46:12 -04:00
"hashtable?" "hashtables" create t "inline" set-word-prop
"hashtable" "hashtables" create 10 "hashtable?" "hashtables" create
{
{
1
fixnum
{ "hash-count" "hashtables" }
{ "set-hash-count" "hashtables-internals" }
} {
2
fixnum
{ "hash-deleted" "hashtables" }
{ "set-hash-deleted" "hashtables-internals" }
} {
3
array
{ "hash-array" "hashtables-internals" }
{ "set-hash-array" "hashtables-internals" }
}
} define-builtin
2005-08-19 21:46:12 -04:00
"vector?" "vectors" create t "inline" set-word-prop
"vector" "vectors" create 11 "vector?" "vectors" create
{
{
1
fixnum
{ "length" "sequences" }
{ "set-fill" "sequences-internals" }
} {
2
array
{ "underlying" "sequences-internals" }
{ "set-underlying" "sequences-internals" }
}
} define-builtin
2005-08-19 21:46:12 -04:00
"string?" "strings" create t "inline" set-word-prop
"string" "strings" create 12 "string?" "strings" create
{
{
1
fixnum
{ "length" "sequences" }
f
} {
2
2006-08-07 01:17:04 -04:00
object
{ "string-hashcode" "kernel-internals" }
{ "set-string-hashcode" "kernel-internals" }
}
} define-builtin
2005-08-19 21:46:12 -04:00
"sbuf?" "strings" create t "inline" set-word-prop
"sbuf" "strings" create 13 "sbuf?" "strings" create
{
{
1
fixnum
{ "length" "sequences" }
{ "set-fill" "sequences-internals" }
}
{
2
string
{ "underlying" "sequences-internals" }
{ "set-underlying" "sequences-internals" }
}
} define-builtin
2005-08-19 21:46:12 -04:00
2006-05-18 01:08:09 -04:00
"quotation?" "kernel" create t "inline" set-word-prop
"quotation" "kernel" create 14 "quotation?" "kernel" create
{ } define-builtin
2005-08-19 21:46:12 -04:00
"dll?" "alien" create t "inline" set-word-prop
2005-08-19 21:46:12 -04:00
"dll" "alien" create 15 "dll?" "alien" create
{ { 1 object { "dll-path" "alien" } f } } define-builtin
2005-08-19 21:46:12 -04:00
2006-05-18 01:08:09 -04:00
"alien" "alien" create 16 "alien?" "alien" create
{ { 1 object { "underlying-alien" "alien" } f } } define-builtin
2005-08-19 21:46:12 -04:00
"tuple?" "kernel" create t "inline" set-word-prop
"tuple" "kernel" create 17 "tuple?" "kernel" create
{ } define-builtin
2005-08-19 21:46:12 -04:00
"byte-array?" "arrays" create t "inline" set-word-prop
"byte-array" "arrays" create 18
"byte-array?" "arrays" create
{ } define-builtin
2005-08-21 01:17:37 -04:00
2005-09-05 17:36:20 -04:00
! Define general-t type, which is any object that is not f.
"general-t" "kernel" create dup define-symbol
f "!f" "!syntax" lookup builtins get remove [ ] subset
2005-09-05 17:36:20 -04:00
define-union
! Catch-all class for providing a default method.
"object" "generic" create [ drop t ] "predicate" set-word-prop
"object" "generic" create dup define-symbol
f builtins get [ ] subset define-union
! Null class with no instances.
"null" "generic" create [ drop f ] "predicate" set-word-prop
"null" "generic" create dup define-symbol f { } define-union
2005-08-19 21:46:12 -04:00
FORGET: builtin-predicate
FORGET: register-builtin
FORGET: define-builtin