native factor can now parse most source files that comprise it

cvs
Slava Pestov 2004-07-19 04:34:03 +00:00
parent 68798f056a
commit cb758942aa
7 changed files with 67 additions and 27 deletions

View File

@ -1,3 +1,7 @@
+ native:
- parsing: -1, HEX:, #\, |
- minimal use/in for parse-stream
- prettyprint-1 - prettyprint-1
- {...} vectors - {...} vectors
- better .s - better .s
@ -10,9 +14,6 @@
- contains ==> contains? - contains ==> contains?
- telnetd: send errors on socket - telnetd: send errors on socket
- native 'see' - native 'see'
+ native:
- partition, sort - partition, sort
- inspector: sort - inspector: sort

View File

@ -54,9 +54,6 @@ USE: stack
#! If the quotation compiles, this combinator compiles. #! If the quotation compiles, this combinator compiles.
-rot >r >r call r> r> ; inline interpret-only -rot >r >r call r> r> ; inline interpret-only
: 3dip ( a b c quot -- )
swap >r swap >r swap >r call r> r> r> ;
: forever ( code -- ) : forever ( code -- )
#! The code is evaluated in an infinite loop. Typically, a #! The code is evaluated in an infinite loop. Typically, a
#! continuation is used to escape the infinite loop. #! continuation is used to escape the infinite loop.

View File

@ -30,6 +30,7 @@ USE: combinators
USE: continuations USE: continuations
USE: kernel USE: kernel
USE: inspector USE: inspector
USE: logic
USE: namespaces USE: namespaces
USE: stack USE: stack
USE: stdio USE: stdio
@ -41,8 +42,8 @@ USE: unparser
: parse-dump ( error -- ) : parse-dump ( error -- )
<% <%
"parse-name" get % ":" % "parse-name" get [ "<interactive>" ] unless* % ":" %
"line-number" get fixnum>str % ": " % "line-number" get [ 1 ] unless* fixnum>str % ": " %
error>str % error>str %
%> print %> print
@ -50,10 +51,12 @@ USE: unparser
<% "pos" get " " fill % "^" % %> print ; <% "pos" get " " fill % "^" % %> print ;
: in-parser? ( -- ? )
"line" get "pos" get and ;
: default-error-handler ( error -- ) : default-error-handler ( error -- )
#! Print the error and return to the top level. #! Print the error and return to the top level.
"parse-name" get [ parse-dump ] [ standard-dump ] ifte in-parser? [ parse-dump ] [ standard-dump ] ifte terpri
terpri
"Stacks have been reset." print "Stacks have been reset." print
":s :r :n :c show stacks at time of error." print ":s :r :n :c show stacks at time of error." print

View File

@ -91,6 +91,10 @@ USE: words
: java? f ; : java? f ;
: native? t ; : native? t ;
! No compiler...
: inline ;
: interpret-only ;
!!! HACK !!! HACK
IN: strings IN: strings

View File

@ -31,6 +31,7 @@ USE: combinators
USE: errors USE: errors
USE: lists USE: lists
USE: namespaces USE: namespaces
USE: stack
USE: stdio USE: stdio
USE: streams USE: streams
@ -39,22 +40,24 @@ USE: streams
"line-number" succ@ ; "line-number" succ@ ;
: (parse-stream) ( -- ) : (parse-stream) ( -- )
next-line [ print (parse-stream) ] when* ; next-line [ (parse) (parse-stream) ] when* ;
: <parse-stream ( name stream -- )
"parse-stream" set
"parse-name" set
0 "line-number" set ;
: parse-stream ( name stream -- ) : parse-stream ( name stream -- )
<namespace> [ <namespace> [
[ [
"parse-stream" set <parse-stream f (parse-stream) nreverse
"parse-name" set
0 "line-number" set
f (parse-stream) nreverse
] [ ] [
"parse-stream" get fclose rethrow "parse-stream" get fclose rethrow
] catch ] catch
] bind ; ] bind ;
: parse-file ( file -- code ) : parse-file ( file -- code )
"r" <file-stream> parse-stream ; dup "r" <file-stream> parse-stream ;
: run-file ( file -- ) : run-file ( file -- )
parse-file call ; parse-file call ;

View File

@ -37,6 +37,7 @@ USE: stack
USE: strings USE: strings
USE: words USE: words
USE: vocabularies USE: vocabularies
USE: unparser
! Number parsing ! Number parsing
@ -83,7 +84,7 @@ USE: vocabularies
#! "hello world" #! "hello world"
#! #!
#! Will call the parsing word ". #! Will call the parsing word ".
ch "\"" str-contains? ; ch "\"!" str-contains? ;
: (scan) ( -- start end ) : (scan) ( -- start end )
skip-blank "pos" get skip-blank "pos" get
@ -130,6 +131,9 @@ USE: vocabularies
: until-eol ( ch -- str ) : until-eol ( ch -- str )
"line" get str-length (until) ; "line" get str-length (until) ;
: next-ch ( -- ch )
end? [ "Unexpected EOF" throw ] [ ch advance ] ifte ;
!!! Parsing words. 'builtins' is a stupid vocabulary name now !!! Parsing words. 'builtins' is a stupid vocabulary name now
!!! that it does not contain Java words anymore! !!! that it does not contain Java words anymore!
@ -143,13 +147,6 @@ IN: builtins
: [ f ; parsing : [ f ; parsing
: ] nreverse swons ; parsing : ] nreverse swons ; parsing
! Comments
: ( ")" until drop ; parsing
: ! until-eol drop ; parsing
! String literal
: " "\"" until swons ; parsing
! Colon defs ! Colon defs
: : : :
#! Begin a word definition. Word name follows. #! Begin a word definition. Word name follows.
@ -160,5 +157,42 @@ IN: builtins
nreverse define ; parsing nreverse define ; parsing
! Vocabularies ! Vocabularies
: DEFER: scan "in" get create drop ; parsing
: USE: scan "use" cons@ ; parsing : USE: scan "use" cons@ ; parsing
: IN: scan dup "use" cons@ "in" set ; parsing : IN: scan dup "use" cons@ "in" set ; parsing
! \x
: escape ( ch -- esc )
[
[ #\e | #\\e ]
[ #\n | #\\n ]
[ #\r | #\\r ]
[ #\t | #\\t ]
[ #\s | #\\s ]
[ #\\s | #\\s ]
[ #\0 | #\\0 ]
[ #\\\ | #\\\ ]
[ #\\" | #\\" ]
] assoc ;
! String literal
: scan-escape ( -- )
next-ch escape dup [ % ] [ drop "Bad escape" throw ] ifte ;
: scan-string ( -- )
next-ch dup #\" = [
drop
] [
dup #\\\ = [ drop scan-escape ] [ % ] ifte scan-string
] ifte ;
: "
#! Note the ugly hack to carry the new value of 'pos' from
#! the <% %> scope up to the original scope.
<% scan-string "pos" get %> swap "pos" set swons ; parsing
! Comments
: ( ")" until drop ; parsing
: ! until-eol drop ; parsing
: #! until-eol drop ; parsing

View File

@ -13,10 +13,7 @@ void primitive_open_file(void)
{ {
char* mode = to_c_string(untag_string(env.dt)); char* mode = to_c_string(untag_string(env.dt));
char* path = to_c_string(untag_string(dpop())); char* path = to_c_string(untag_string(dpop()));
printf("fopen %s %s\n",path,mode);
FILE* file = fopen(path,mode); FILE* file = fopen(path,mode);
if(file == 0)
printf("error %d\n",errno);
env.dt = handle(file); env.dt = handle(file);
} }
@ -72,4 +69,5 @@ void primitive_close(void)
{ {
HANDLE* h = untag_handle(env.dt); HANDLE* h = untag_handle(env.dt);
fclose((FILE*)h->object); fclose((FILE*)h->object);
env.dt = dpop();
} }