From 81705a955d5983762d7b7a49066a3226bd1be2da Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 29 Dec 2004 08:35:46 +0000 Subject: [PATCH] type inference changes, comment out smart-terminal reference in win32-console, win32-compatible factor plugin --- TODO.FACTOR.txt | 4 ++ examples/infix.factor | 2 +- examples/mandel.factor | 4 +- examples/more-random.factor | 2 +- factor/ExternalFactor.java | 91 ++++++++++++------------ factor/jedit/FactorPlugin.java | 34 +++++---- library/bootstrap/boot-stage2.factor | 6 +- library/bootstrap/init-stage2.factor | 34 +++++---- library/bootstrap/primitives.factor | 2 +- library/cli.factor | 10 --- library/compiler/alien-types.factor | 2 +- library/generic/builtin.factor | 3 - library/generic/generic.factor | 3 + library/gensym.factor | 2 +- library/httpd/url-encoding.factor | 4 +- library/inference/branches.factor | 99 +++++++++++++++++++++++---- library/inference/inference.factor | 45 +++--------- library/inference/words.factor | 57 +++++---------- library/io/ansi.factor | 5 +- library/io/stdio.factor | 3 - library/io/stream.factor | 4 +- library/io/win32-console.factor | 2 +- library/lists.factor | 6 +- library/math/arc-trig-hyp.factor | 6 +- library/math/math-combinators.factor | 24 +++++-- library/math/math.factor | 3 - library/namespaces.factor | 7 +- library/primitives.factor | 2 +- library/random.factor | 4 +- library/sdl/console.factor | 8 ++- library/sdl/hsv.factor | 6 +- library/strings.factor | 2 +- library/syntax/parse-stream.factor | 2 +- library/syntax/parser.factor | 8 +-- library/syntax/prettyprint.factor | 12 ++-- library/syntax/see.factor | 30 +++++--- library/syntax/unparser.factor | 8 +-- library/test/benchmark/ack.factor | 6 +- library/test/benchmark/fac.factor | 2 +- library/test/benchmark/fib.factor | 2 +- library/test/benchmark/strings.factor | 2 +- library/test/continuations.factor | 2 +- library/test/dataflow.factor | 4 +- library/test/inference.factor | 10 +-- library/test/strings.factor | 2 +- library/test/test.factor | 2 +- library/tools/debugger.factor | 2 +- library/tools/heap-stats.factor | 2 +- library/tools/listener.factor | 6 ++ library/tools/telnetd.factor | 8 +++ library/vectors.factor | 4 +- native/signal.h | 1 + native/stack.c | 6 ++ native/unix/signal.c | 9 +++ 54 files changed, 358 insertions(+), 258 deletions(-) diff --git a/TODO.FACTOR.txt b/TODO.FACTOR.txt index ea52a4df89..9130aa10c1 100644 --- a/TODO.FACTOR.txt +++ b/TODO.FACTOR.txt @@ -34,6 +34,10 @@ + listener/plugin: +- listener should be multithreaded +- fully socket based communication +- compile all, infer all commands +- type something -- no completions -- hit another key -- not inserted - faster completion - sidekick: still parsing too much - errors don't always disappear diff --git a/examples/infix.factor b/examples/infix.factor index 995ad9c842..bcdd97ef01 100644 --- a/examples/infix.factor +++ b/examples/infix.factor @@ -8,7 +8,7 @@ USE: words : vector-peek ( vector -- obj ) #! Get value at end of vector without removing it. - dup vector-length pred swap vector-nth ; + dup vector-length 1 - swap vector-nth ; SYMBOL: exprs DEFER: infix diff --git a/examples/mandel.factor b/examples/mandel.factor index 7296502ca6..916b3cd1be 100644 --- a/examples/mandel.factor +++ b/examples/mandel.factor @@ -44,7 +44,7 @@ USE: test : ( nb-cols -- map ) [ dup [ - 360 * over succ / 360 / sat val + 360 * over 1 + / 360 / sat val hsv>rgb 1.0 scale-rgba , ] times* ] make-list list>vector nip ; @@ -55,7 +55,7 @@ USE: test over absq 4 >= over 0 = or [ nip nip ] [ - pred >r sq dupd + r> iter + 1 - >r sq dupd + r> iter ] ifte ; : max-color 360 ; diff --git a/examples/more-random.factor b/examples/more-random.factor index c9d9f3357e..d9db9d74d0 100644 --- a/examples/more-random.factor +++ b/examples/more-random.factor @@ -14,7 +14,7 @@ USE: namespaces : random-element ( list -- random ) #! Returns a random element from the given list. - dup >r length pred 0 swap random-int r> nth ; + dup >r length 1 - 0 swap random-int r> nth ; : random-subset ( list -- list ) #! Returns a random subset of the given list. Each item is diff --git a/factor/ExternalFactor.java b/factor/ExternalFactor.java index 65717fb3d1..69708e5f63 100644 --- a/factor/ExternalFactor.java +++ b/factor/ExternalFactor.java @@ -40,41 +40,57 @@ import org.gjt.sp.util.Log; public class ExternalFactor extends DefaultVocabularyLookup { //{{{ ExternalFactor constructor - /** - * We are given two streams that point to a bare REPL. - */ - public ExternalFactor(Process proc, InputStream in, OutputStream out) + public ExternalFactor(int port) { - if(proc == null || in == null || out == null) - closed = true; - else - { - this.proc = proc; + /* Start stream server */; + streamServer = port; + for(int i = 1; i < 6; i++) + { + Log.log(Log.DEBUG,this,"Factor connection, try #" + i); try { - this.in = new DataInputStream(in); - this.out = new DataOutputStream(out); - - out.write("USE: jedit wire-server\n".getBytes("ASCII")); - out.flush(); - - waitForAck(); - - /* Start stream server */ - streamServer = 9999; - eval("USE: telnetd [ 9999 telnetd ] in-thread"); - - /* Ensure we're ready for a connection immediately */ - eval("nop"); + Thread.sleep(1000); + openWire(); + Log.log(Log.DEBUG,this,"Connection established"); + return; } catch(Exception e) { - close(); + Log.log(Log.ERROR,this,e); } + } + + Log.log(Log.ERROR,this,"Cannot connect to Factor on port " + port); + if(in != null && out != null) + close(); } //}}} + //{{{ openWireSocket() method + /** + * Return a listener stream. + */ + public Socket openWireSocket() throws IOException + { + if(closed) + throw new IOException("Socket closed"); + return new Socket("localhost",streamServer); + } //}}} + + //{{{ openWire() method + private void openWire() throws Exception + { + Socket client = openWireSocket(); + in = new DataInputStream(new BufferedInputStream( + client.getInputStream())); + out = new DataOutputStream(new BufferedOutputStream( + client.getOutputStream())); + out.write("USE: jedit wire-server\n".getBytes("ASCII")); + out.flush(); + waitForAck(); + } + //{{{ waitForAck() method private void waitForAck() throws IOException { @@ -132,22 +148,16 @@ public class ExternalFactor extends DefaultVocabularyLookup */ public FactorStream openStream() { - if(closed) - return null; - else + try { - try - { - Socket client = new Socket("localhost",streamServer); - return new FactorStream(client); - } - catch(Exception e) - { - Log.log(Log.ERROR,this,"Cannot open stream connection to " - + "external Factor:"); - Log.log(Log.ERROR,this,e); - return null; - } + return new FactorStream(openWireSocket()); + } + catch(Exception e) + { + Log.log(Log.ERROR,this,"Cannot open stream connection to " + + "external Factor:"); + Log.log(Log.ERROR,this,e); + return null; } } //}}} @@ -279,7 +289,6 @@ public class ExternalFactor extends DefaultVocabularyLookup try { - proc.waitFor(); in.close(); out.close(); } @@ -289,7 +298,6 @@ public class ExternalFactor extends DefaultVocabularyLookup Log.log(Log.DEBUG,this,e); } - proc = null; in = null; out = null; } //}}} @@ -303,7 +311,6 @@ public class ExternalFactor extends DefaultVocabularyLookup //{{{ Private members private boolean closed; - private Process proc; private DataInputStream in; private DataOutputStream out; diff --git a/factor/jedit/FactorPlugin.java b/factor/jedit/FactorPlugin.java index 1b98731104..3e7420b09f 100644 --- a/factor/jedit/FactorPlugin.java +++ b/factor/jedit/FactorPlugin.java @@ -42,6 +42,8 @@ import sidekick.*; public class FactorPlugin extends EditPlugin { private static ExternalFactor external; + private static Process process; + private static int PORT = 9999; //{{{ getPluginPath() method private String getPluginPath() @@ -101,7 +103,6 @@ public class FactorPlugin extends EditPlugin { if(external == null) { - Process p = null; InputStream in = null; OutputStream out = null; @@ -110,27 +111,28 @@ public class FactorPlugin extends EditPlugin List args = new ArrayList(); args.add(jEdit.getProperty("factor.external.program")); args.add(jEdit.getProperty("factor.external.image")); - args.add("-no-ansi"); - args.add("-no-smart-terminal"); + args.add("-shell=telnet"); + args.add("-telnetd-port=" + PORT); String[] extraArgs = jEdit.getProperty( - "factor.external.args","-jedit") + "factor.external.args") .split(" "); addNonEmpty(extraArgs,args); - p = Runtime.getRuntime().exec((String[])args.toArray( + process = Runtime.getRuntime().exec((String[])args.toArray( new String[args.size()])); - p.getErrorStream().close(); - in = p.getInputStream(); - out = p.getOutputStream(); + external = new ExternalFactor(PORT); + + process.getErrorStream().close(); + process.getInputStream().close(); + process.getOutputStream().close(); } - catch(IOException io) + catch(Exception e) { Log.log(Log.ERROR,FactorPlugin.class, "Cannot start external Factor:"); - Log.log(Log.ERROR,FactorPlugin.class,io); + Log.log(Log.ERROR,FactorPlugin.class,e); + process = null; } - - external = new ExternalFactor(p,in,out); } return external; @@ -153,6 +155,14 @@ public class FactorPlugin extends EditPlugin if(external != null) { external.close(); + try + { + process.waitFor(); + } + catch(Exception e) + { + Log.log(Log.DEBUG,FactorPlugin.class,e); + } external = null; } } //}}} diff --git a/library/bootstrap/boot-stage2.factor b/library/bootstrap/boot-stage2.factor index a7b4c477db..a60881b94b 100644 --- a/library/bootstrap/boot-stage2.factor +++ b/library/bootstrap/boot-stage2.factor @@ -114,8 +114,8 @@ USE: namespaces "/library/inference/dataflow.factor" "/library/inference/inference.factor" - "/library/inference/words.factor" "/library/inference/branches.factor" + "/library/inference/words.factor" "/library/inference/stack.factor" "/library/compiler/assembler.factor" @@ -165,10 +165,10 @@ os "win32" = [ "/library/io/buffer.factor" "/library/win32/win32-io.factor" "/library/win32/win32-errors.factor" - "/library/win32/winsock.factor" + "/library/win32/winsock.factor" "/library/io/win32-io-internals.factor" "/library/io/win32-stream.factor" - "/library/io/win32-server.factor" + "/library/io/win32-server.factor" "/library/io/win32-console.factor" ] [ dup print diff --git a/library/bootstrap/init-stage2.factor b/library/bootstrap/init-stage2.factor index bf2a459fa2..f6c854405b 100644 --- a/library/bootstrap/init-stage2.factor +++ b/library/bootstrap/init-stage2.factor @@ -45,10 +45,19 @@ USE: unparser USE: kernel-internals USE: console -: init-smart-terminal - "smart-terminal" get [ - stdio smart-term-hook get change - ] when ; +: default-cli-args + #! Some flags are *on* by default, unless user specifies + #! -no- CLI switch + "user-init" on + "interactive" on + "smart-terminal" on + "verbose-compile" on + "compile" on + os "win32" = [ + "sdl" "shell" set + ] [ + "ansi" "shell" set + ] ifte ; : warm-boot ( -- ) #! A fully bootstrapped image has this as the boot @@ -59,18 +68,15 @@ USE: console default-cli-args parse-command-line ; +: shell ( str -- ) + #! This handles the -shell: cli argument. + [ "shells" ] search execute ; + [ warm-boot garbage-collection run-user-init - "graphical" get [ - start-console - ] [ - "interactive" get [ - init-smart-terminal - print-banner listener - ] when - ] ifte + "shell" get shell 0 exit* ] set-boot @@ -136,10 +142,10 @@ terpri "Not every word compiles, by design." print terpri -0 [ compiled? [ succ ] when ] each-word +0 [ compiled? [ 1 + ] when ] each-word unparse write " words compiled" print -0 [ drop succ ] each-word +0 [ drop 1 + ] each-word unparse write " words total" print "Bootstrapping is complete." print diff --git a/library/bootstrap/primitives.factor b/library/bootstrap/primitives.factor index 91c889f7e4..5fc225c065 100644 --- a/library/bootstrap/primitives.factor +++ b/library/bootstrap/primitives.factor @@ -223,5 +223,5 @@ vocabularies get [ [ "kernel-internals" | "set-integer-slot" ] [ "kernel-internals" | "grow-array" ] ] [ - unswons create swap succ [ f define ] keep + unswons create swap 1 + [ f define ] keep ] each drop diff --git a/library/cli.factor b/library/cli.factor index 91774132a7..afb242a945 100644 --- a/library/cli.factor +++ b/library/cli.factor @@ -82,16 +82,6 @@ USE: kernel-internals : run-files ( args -- ) [ [ run-file ] when* ] each ; -: default-cli-args - #! Some flags are *on* by default, unless user specifies - #! -no- CLI switch - "user-init" on - "interactive" on - "smart-terminal" on - "verbose-compile" on - "compile" on - os "win32" = [ "graphical" on ] when ; - : cli-args ( -- args ) 10 getenv ; : parse-command-line ( -- ) diff --git a/library/compiler/alien-types.factor b/library/compiler/alien-types.factor index 5abf6963b9..20bafabaef 100644 --- a/library/compiler/alien-types.factor +++ b/library/compiler/alien-types.factor @@ -53,7 +53,7 @@ USE: words scan str>number ; parsing : ENUM: - dup CREATE swap unit define-compound succ ; parsing + dup CREATE swap unit define-compound 1 + ; parsing : END-ENUM drop ; parsing diff --git a/library/generic/builtin.factor b/library/generic/builtin.factor index 515314ec04..1db73197fd 100644 --- a/library/generic/builtin.factor +++ b/library/generic/builtin.factor @@ -77,9 +77,6 @@ builtin 50 "priority" set-word-property : builtin-type ( n -- symbol ) unit classes get hash ; -: type-name ( n -- string ) - builtin-type word-name ; - : class ( obj -- class ) #! Analogous to the type primitive. Pushes the builtin #! class of an object. diff --git a/library/generic/generic.factor b/library/generic/generic.factor index 5dc7dee8fc..02a289fbf1 100644 --- a/library/generic/generic.factor +++ b/library/generic/generic.factor @@ -118,6 +118,9 @@ USE: math-internals dup [ "methods" set-word-property ] keep ] unless* define-generic ; +PREDICATE: word generic ( word -- ? ) + "combination" word-property ; + : single-combination ( obj vtable -- ) >r dup type r> dispatch ; inline diff --git a/library/gensym.factor b/library/gensym.factor index ac186b18b9..cb561a6d41 100644 --- a/library/gensym.factor +++ b/library/gensym.factor @@ -36,7 +36,7 @@ SYMBOL: gensym-count : (gensym) ( -- name ) "G:" global [ - gensym-count [ succ dup ] change + gensym-count [ 1 + dup ] change ] bind unparse cat2 ; : gensym ( -- word ) diff --git a/library/httpd/url-encoding.factor b/library/httpd/url-encoding.factor index d9936d26aa..0993a14cfa 100644 --- a/library/httpd/url-encoding.factor +++ b/library/httpd/url-encoding.factor @@ -49,14 +49,14 @@ USE: unparser 2dup str-length 2 - >= [ 2drop ] [ - >r succ dup 2 + r> substring catch-hex> [ , ] when* + >r 1 + dup 2 + r> substring catch-hex> [ , ] when* ] ifte ; : url-decode-% ( index str -- index str ) 2dup url-decode-hex >r 3 + r> ; : url-decode-+-or-other ( index str ch -- index str ) - dup CHAR: + = [ drop CHAR: \s ] when , >r succ r> ; + dup CHAR: + = [ drop CHAR: \s ] when , >r 1 + r> ; : url-decode-iter ( index str -- ) 2dup str-length >= [ diff --git a/library/inference/branches.factor b/library/inference/branches.factor index ce648c5ecf..9421beda24 100644 --- a/library/inference/branches.factor +++ b/library/inference/branches.factor @@ -39,10 +39,6 @@ USE: words USE: hashtables USE: prettyprint -! If this symbol is on, partial evalution of conditionals is -! disabled. -SYMBOL: inferring-base-case - : vector-length< ( vec1 vec2 -- ? ) swap vector-length swap vector-length < ; @@ -65,7 +61,11 @@ SYMBOL: inferring-base-case : unify-results ( value value -- value ) #! Replace values with unknown result if they differ, #! otherwise retain them. - 2dup = [ drop ] [ unify-classes ] ifte ; + 2dup = [ + drop + ] [ + unify-classes + ] ifte ; : unify-stacks ( list -- stack ) #! Replace differing literals in stacks with unknown @@ -109,10 +109,23 @@ SYMBOL: inferring-base-case SYMBOL: cloned +: assq* ( key alist -- [ key | value ] ) + #! Looks up the key in an alist. Push the key/value pair. + #! Most of the time you want to use assq not assq*. + dup [ + 2dup car car eq? [ nip car ] [ cdr assq* ] ifte + ] [ + 2drop f + ] ifte ; + +: assq ( key alist -- value ) + #! Looks up the key in an alist. + assq* dup [ cdr ] when ; + : deep-clone ( vector -- vector ) #! Clone a vector if it hasn't already been cloned in this #! with-deep-clone scope. - dup cloned get assoc dup [ + dup cloned get assq dup [ nip ] [ drop vector-clone [ dup cloned [ acons ] change ] keep @@ -120,7 +133,7 @@ SYMBOL: cloned : deep-clone-vector ( vector -- vector ) #! Clone a vector of vectors. - [ ( deep-clone ) vector-clone ] vector-map ; + [ deep-clone ] vector-map ; : copy-inference ( -- ) #! We avoid cloning the same object more than once in order @@ -133,7 +146,7 @@ SYMBOL: cloned : infer-branch ( value -- namespace ) [ - uncons [ unswons [ \ value-class set ] bind ] when* + uncons [ unswons set-value-class ] when* dup value-recursion recursive-state set copy-inference literal-value infer-quot @@ -151,6 +164,45 @@ SYMBOL: cloned #! given one in the list. [ over eq? not ] subset nip car car value-recursion ; +! FIXME this is really bad +: old-effect ( [ in-types out-types ] -- [ in | out ] ) + uncons car length >r length r> cons ; + +: foo>effect ( [ in-types out-types ] -- [ in | out ] ) + [ effect old-effect ] bind ; + +: raise ( [ in | out ] -- [ in | out ] ) + uncons 2dup min tuck - >r - r> cons ; + +: effect>foo ( [ in | out ] -- [ intypes outtypes ] ) + [ + uncons + [ drop object ] vector-project meta-d set + [ drop object ] vector-project d-in set + { } meta-r set + ] extend ; + +: decompose ( first second -- solution ) + #! Return a stack effect such that first*solution = second. + 2dup 2car + 2dup > [ "No solution to decomposition" throw ] when + swap - -rot 2cdr >r + r> cons raise effect>foo ; + +: set-base ( effect rstate -- ) + #! Set the base case of the current word. + dup [ + car cdr [ + entry-effect get old-effect dup [ 0 | 0 ] = [ + drop + ] [ + swap foo>effect decompose + ] ifte + base-case cons@ + ] bind + ] [ + 2drop + ] ifte ; + : recursive-branch ( branch branchlist -- ) [ dupd dual-branch >r infer-branch r> set-base @@ -158,6 +210,16 @@ SYMBOL: cloned [ 2drop ] when ] catch ; +: no-base-case ( word -- ) + word-name " does not have a base case." cat2 throw ; + +: get-base ( word rstate -- effect ) + [ base-case get ] bind dup [ + nip [ unify-effects effect ] with-scope + ] [ + drop no-base-case + ] ifte ; + : infer-base-case ( branchlist -- ) [ inferring-base-case on @@ -192,7 +254,18 @@ SYMBOL: cloned #! the branches has an undecidable stack effect, we set the #! base case to this stack effect and try again. The inputs #! parameter is a vector. - (infer-branches) dup unify-effects unify-dataflow ; + (infer-branches) dup unify-effects unify-dataflow ; + +: (with-block) ( label quot -- ) + #! Call a quotation in a new namespace, and transfer + #! inference state from the outer scope. + swap >r [ + dataflow-graph off + call + d-in get meta-d get meta-r get get-dataflow + ] with-scope + r> swap #label dataflow, [ node-label set ] bind + meta-r set meta-d set d-in set ; : static-branch? ( value -- ) literal? inferring-base-case get not and ; @@ -221,11 +294,11 @@ SYMBOL: cloned [ object general-list general-list ] ensure-d dataflow-drop, pop-d dataflow-drop, pop-d swap - peek-d static-branch? [ - static-ifte - ] [ +! peek-d static-branch? [ +! static-ifte +! ] [ dynamic-ifte - ] ifte ; + ( ] ifte ) ; \ ifte [ infer-ifte ] "infer" set-word-property diff --git a/library/inference/inference.factor b/library/inference/inference.factor index ed0e91268b..7353d3afd6 100644 --- a/library/inference/inference.factor +++ b/library/inference/inference.factor @@ -39,6 +39,10 @@ USE: hashtables USE: generic USE: prettyprint +! If this symbol is on, partial evalution of conditionals is +! disabled. +SYMBOL: inferring-base-case + ! Word properties that affect inference: ! - infer-effect -- must be set. controls number of inputs ! expected, and number of outputs produced. @@ -64,6 +68,7 @@ GENERIC: literal-value ( value -- obj ) GENERIC: value= ( literal value -- ? ) GENERIC: value-class ( value -- class ) GENERIC: value-class-and ( class value -- ) +GENERIC: set-value-class ( class value -- ) TRAITS: computed C: computed ( class -- value ) @@ -79,6 +84,8 @@ M: computed value-class ( value -- class ) [ \ value-class get ] bind ; M: computed value-class-and ( class value -- ) [ \ value-class [ class-and ] change ] bind ; +M: computed set-value-class ( class value -- ) + [ \ value-class set ] bind ; TRAITS: literal C: literal ( obj rstate -- value ) @@ -91,6 +98,8 @@ M: literal value-class ( value -- class ) literal-value class ; M: literal value-class-and ( class value -- ) value-class class-and drop ; +M: literal set-value-class ( class value -- ) + 2drop ; : value-recursion ( value -- rstate ) [ recursive-state get ] bind ; @@ -98,7 +107,7 @@ M: literal value-class-and ( class value -- ) : (ensure-types) ( typelist n stack -- ) pick [ 3dup >r >r car r> r> vector-nth value-class-and - >r >r cdr r> succ r> (ensure-types) + >r >r cdr r> 1 + r> (ensure-types) ] [ 3drop ] ifte ; @@ -131,9 +140,6 @@ M: literal value-class-and ( class value -- ) d-in get [ value-class ] vector-map vector>list meta-d get [ value-class ] vector-map vector>list 2list ; -: old-effect ( [ in-types out-types ] | [ in | out ] ) - uncons car length >r length r> cons ; - : ( -- state ) [ base-case off effect entry-effect set @@ -162,37 +168,6 @@ DEFER: apply-word #! quotations. [ apply-object ] each ; -: raise ( [ in | out ] -- [ in | out ] ) - uncons 2dup min tuck - >r - r> cons ; - -: new-effect ( [ in | out ] -- [ intypes outtypes ] ) - uncons - swap [ drop object ] project - swap [ drop object ] project - 2list ; - -: decompose ( first second -- solution ) - #! Return a stack effect such that first*solution = second. - over [ [ ] [ ] ] = [ - nip - ] [ - swap old-effect swap old-effect - 2dup 2car - 2dup > [ "No solution to decomposition" throw ] when - swap - -rot 2cdr >r + r> cons raise new-effect - ] ifte ; - -: set-base ( [ in | out ] rstate -- ) - #! Set the base case of the current word. - dup [ - car cdr [ - [ effect ] bind entry-effect get swap decompose - base-case set - ] bind - ] [ - 2drop - ] ifte ; - : check-return ( -- ) #! Raise an error if word leaves values on return stack. meta-r get vector-length 0 = [ diff --git a/library/inference/words.factor b/library/inference/words.factor index 168aae8fad..3b5bad573d 100644 --- a/library/inference/words.factor +++ b/library/inference/words.factor @@ -78,27 +78,16 @@ USE: prettyprint : no-effect ( word -- ) "Unknown stack effect: " swap word-name cat2 throw ; -: with-recursive-state ( word label quot -- ) - >r - [ recursive-label set ] extend dupd cons - recursive-state cons@ - r> call ; - -: (with-block) ( label quot -- ) - #! Call a quotation in a new namespace, and transfer - #! inference state from the outer scope. - swap >r [ - dataflow-graph off - call - d-in get meta-d get meta-r get get-dataflow - ] with-scope - r> swap #label dataflow, [ node-label set ] bind - meta-r set meta-d set d-in set ; - : with-block ( word label quot -- ) #! Execute a quotation with the word on the stack, and add #! its dataflow contribution to a new block node in the IR. - over [ with-recursive-state ] (with-block) ; + over [ + >r + [ recursive-label set ] extend + dupd cons + recursive-state cons@ + r> call + ] (with-block) ; : inline-compound ( word -- effect ) #! Infer the stack effect of a compound word in the current @@ -131,9 +120,6 @@ M: symbol (apply-word) ( word -- ) #! Push word we're currently inferring effect of. recursive-state get car car ; -: no-base-case ( word -- ) - word-name " does not have a base case." cat2 throw ; - : check-recursion ( -- ) #! If at the location of the recursive call, we're taking #! more items from the stack than producing, we have a @@ -147,32 +133,25 @@ M: symbol (apply-word) ( word -- ) #! Handle a recursive call, by either applying a previously #! inferred base case, or raising an error. If the recursive #! call is to a local block, emit a label call node. - base-case over hash dup [ - swap [ recursive-label get ] bind ( word effect label ) - dup [ - rot drop #call-label rot - ] [ - drop #call swap - ] ifte (consume/produce) + [ get-base ] 2keep [ recursive-label get ] bind + dup [ + ( word effect label ) + nip #call-label ] [ - 2drop no-base-case - ] ifte ; + drop #call + ] ifte rot (consume/produce) ; : apply-word ( word -- ) #! Apply the word's stack effect to the inferencer state. - dup recursive-state get assoc dup [ + dup recursive-state get assoc [ check-recursion recursive-word ] [ - drop dup "infer-effect" word-property dup [ + dup "infer-effect" word-property [ apply-effect ] [ - drop dup "no-effect" word-property [ - no-effect - ] [ - (apply-word) - ] ifte - ] ifte - ] ifte ; + (apply-word) + ] ifte* + ] ifte* ; : infer-call ( -- ) [ general-list ] ensure-d diff --git a/library/io/ansi.factor b/library/io/ansi.factor index 456636e5a4..bd3d00dfd9 100644 --- a/library/io/ansi.factor +++ b/library/io/ansi.factor @@ -91,4 +91,7 @@ C: ansi-stream ( stream -- stream ) #! ansi-bg - background color [ delegate set ] extend ; -global [ [ ] smart-term-hook set ] bind +IN: shells + +: ansi + stdio [ ] change tty ; diff --git a/library/io/stdio.factor b/library/io/stdio.factor index 04b0457829..df04ef1a40 100644 --- a/library/io/stdio.factor +++ b/library/io/stdio.factor @@ -77,6 +77,3 @@ C: stdio-stream ( delegate -- stream ) swap stdio get [ stdio set call ] with-scope ; inline - -! Set this to a quotation in init code, depending on OS. -SYMBOL: smart-term-hook diff --git a/library/io/stream.factor b/library/io/stream.factor index e6172a2cd5..c81c430413 100644 --- a/library/io/stream.factor +++ b/library/io/stream.factor @@ -48,7 +48,9 @@ GENERIC: fclose ( stream -- ) f swap fwrite-attr ; : fprint ( string stream -- ) - tuck fwrite "\n" over fwrite fauto-flush ; + [ fwrite ] keep + [ "\n" swap fwrite ] keep + fauto-flush ; TRAITS: string-output-stream diff --git a/library/io/win32-console.factor b/library/io/win32-console.factor index 5f5cbb82f3..1492a8d9d4 100644 --- a/library/io/win32-console.factor +++ b/library/io/win32-console.factor @@ -85,5 +85,5 @@ M: win32-console-stream fwrite-attr ( string style stream -- ) C: win32-console-stream ( stream -- stream ) [ -11 GetStdHandle handle set delegate set ] extend ; -global [ [ ] smart-term-hook set ] bind +! global [ [ ] smart-term-hook set ] bind diff --git a/library/lists.factor b/library/lists.factor index 3463e8f4ac..e05f288cd9 100644 --- a/library/lists.factor +++ b/library/lists.factor @@ -126,7 +126,7 @@ DEFER: tree-contains? [ dupd = not ] subset nip ; : length ( list -- length ) - 0 swap [ drop succ ] each ; + 0 swap [ drop 1 + ] each ; : prune ( list -- list ) #! Remove duplicate elements. @@ -168,7 +168,7 @@ M: cons = ( obj cons -- ? ) 2drop 0 ] [ over cons? [ - pred >r uncons r> tuck + 1 - >r uncons r> tuck cons-hashcode >r cons-hashcode r> bitxor @@ -191,7 +191,7 @@ M: cons hashcode ( cons -- hash ) 4 cons-hashcode ; : head ( list n -- list ) #! Return the first n elements of the list. - dup 0 > [ >r uncons r> pred head cons ] [ 2drop f ] ifte ; + dup 0 > [ >r uncons r> 1 - head cons ] [ 2drop f ] ifte ; : tail ( list n -- tail ) #! Return the rest of the list, from the nth index onward. diff --git a/library/math/arc-trig-hyp.factor b/library/math/arc-trig-hyp.factor index 0eff0791f2..7b093f741b 100644 --- a/library/math/arc-trig-hyp.factor +++ b/library/math/arc-trig-hyp.factor @@ -36,11 +36,11 @@ USE: math-internals ! Inverse hyperbolic functions: ! acosh asech asinh acosech atanh acoth -: acosh dup sq pred sqrt + log ; +: acosh dup sq 1 - sqrt + log ; : asech recip acosh ; -: asinh dup sq succ sqrt + log ; +: asinh dup sq 1 + sqrt + log ; : acosech recip asinh ; -: atanh dup succ swap pred neg / log 2 / ; +: atanh dup 1 + swap 1 - neg / log 2 / ; : acoth recip atanh ; : <=1 ( x -- ? ) dup complex? [ drop f ] [ abs 1 <= ] ifte ; : asin dup <=1 [ fasin ] [ i * asinh -i * ] ifte ; diff --git a/library/math/math-combinators.factor b/library/math/math-combinators.factor index 6acdfe144a..5d61794463 100644 --- a/library/math/math-combinators.factor +++ b/library/math/math-combinators.factor @@ -33,14 +33,14 @@ USE: kernel #! #! In order to compile, the code must produce as many values #! as it consumes. - tuck >r dup 0 <= [ r> 3drop ] [ pred slip r> times ] ifte ; + tuck >r dup 0 <= [ r> 3drop ] [ 1 - slip r> times ] ifte ; inline : (times) ( limit n quot -- ) pick pick <= [ 3drop ] [ - rot pick succ pick 3slip (times) + rot pick 1 + pick 3slip (times) ] ifte ; inline : times* ( n quot -- ) @@ -52,15 +52,15 @@ USE: kernel 0 swap (times) ; inline : fac ( n -- n! ) - 1 swap [ succ * ] times* ; + 1 swap [ 1 + * ] times* ; : 2times-succ ( #{ a b } #{ c d } -- z ) #! Lexicographically add #{ 0 1 } to a complex number. #! If d + 1 == b, return #{ c+1 0 }. Otherwise, #{ c d+1 }. - 2dup imaginary succ swap imaginary = [ - nip real succ + 2dup imaginary 1 + swap imaginary = [ + nip real 1 + ] [ - nip >rect succ rect> + nip >rect 1 + rect> ] ifte ; inline : 2times<= ( #{ a b } #{ c d } -- ? ) @@ -77,3 +77,15 @@ USE: kernel #! Apply a quotation to each pair of complex numbers #! #{ a b } such that a < w, b < h. 0 swap (2times) ; inline + +: (repeat) ( i n quot -- ) + pick pick >= [ + 3drop + ] [ + [ swap >r call 1 + r> ] keep (repeat) + ] ifte ; + +: repeat ( n quot -- ) + #! Execute a quotation n times. The loop counter is kept on + #! the stack, and ranges from 0 to n-1. + 0 -rot (repeat) ; diff --git a/library/math/math.factor b/library/math/math.factor index fa2306493c..6750d42fac 100644 --- a/library/math/math.factor +++ b/library/math/math.factor @@ -87,9 +87,6 @@ M: number = ( n n -- ? ) number= ; : sq dup * ; inline -: pred 1 - ; inline -: succ 1 + ; inline - : neg 0 swap - ; inline : recip 1 swap / ; inline diff --git a/library/namespaces.factor b/library/namespaces.factor index 2d669d5d95..c1f6ca523e 100644 --- a/library/namespaces.factor +++ b/library/namespaces.factor @@ -30,6 +30,7 @@ USE: hashtables USE: kernel USE: kernel-internals USE: lists +USE: vectors ! Other languages have classes, objects, variables, etc. ! Factor has similar concepts. @@ -50,8 +51,8 @@ USE: lists ! bind ( namespace quot -- ) executes a quotation with a ! namespace pushed on the namespace stack. -: namestack ( -- ns ) 3 getenv ; -: set-namestack ( ns -- ) 3 setenv ; +: namestack ( -- ns ) 3 getenv ; inline +: set-namestack ( ns -- ) 3 setenv ; inline : namespace ( -- namespace ) #! Push the current namespace. @@ -59,7 +60,7 @@ USE: lists : >n ( namespace -- n:namespace ) #! Push a namespace on the namespace stack. - namestack cons set-namestack ; inline + >vector namestack cons set-namestack ; inline : n> ( n:namespace -- namespace ) #! Pop the top of the namespace stack. diff --git a/library/primitives.factor b/library/primitives.factor index 14d593a39e..3a66b98189 100644 --- a/library/primitives.factor +++ b/library/primitives.factor @@ -98,7 +98,7 @@ USE: words [ fixnum<= " x y -- ? " [ [ fixnum fixnum ] [ boolean ] ] ] [ fixnum> " x y -- ? " [ [ fixnum fixnum ] [ boolean ] ] ] [ fixnum>= " x y -- ? " [ [ fixnum fixnum ] [ boolean ] ] ] - [ bignum= " x y -- ? " [ [ fixnum fixnum ] [ boolean ] ] ] + [ bignum= " x y -- ? " [ [ bignum bignum ] [ boolean ] ] ] [ bignum+ " x y -- x+y " [ [ bignum bignum ] [ bignum ] ] ] [ bignum- " x y -- x-y " [ [ bignum bignum ] [ bignum ] ] ] [ bignum* " x y -- x*y " [ [ bignum bignum ] [ bignum ] ] ] diff --git a/library/random.factor b/library/random.factor index 313106d08c..312c45a75f 100644 --- a/library/random.factor +++ b/library/random.factor @@ -34,14 +34,14 @@ USE: math dup dup neg bitand = ; : (random-int-0) ( n bits val -- n ) - 3dup - + pred 0 < [ + 3dup - + 1 < [ 2drop (random-int) 2dup swap mod (random-int-0) ] [ nip nip ] ifte ; : random-int-0 ( max -- n ) - succ dup power-of-2? [ + 1 + dup power-of-2? [ (random-int) * -31 shift ] [ (random-int) 2dup swap mod (random-int-0) diff --git a/library/sdl/console.factor b/library/sdl/console.factor index 94d70a57ae..66664500c0 100644 --- a/library/sdl/console.factor +++ b/library/sdl/console.factor @@ -111,7 +111,7 @@ SYMBOL: line-editor : add-line ( text -- ) lines get vector-push - lines get vector-length succ first-line get - visible-lines - + lines get vector-length 1 + first-line get - visible-lines - dup 0 >= [ first-line [ + ] change ] [ @@ -198,7 +198,7 @@ M: backspace-key key-down ( key -- ) line-editor get dup sbuf-length 0 = [ drop ] [ - [ sbuf-length pred ] keep set-sbuf-length + [ sbuf-length 1 - ] keep set-sbuf-length ] ifte ; M: integer key-down ( key -- ) @@ -250,7 +250,9 @@ M: alien handle-event ( event -- ? ) SYMBOL: escape-continuation -: start-console ( -- ) +IN: shells + +: sdl ( -- ) [ 800 600 32 SDL_HWSURFACE init-screen init-console diff --git a/library/sdl/hsv.factor b/library/sdl/hsv.factor index fbaf8c1424..23a953d706 100644 --- a/library/sdl/hsv.factor +++ b/library/sdl/hsv.factor @@ -14,9 +14,9 @@ USE: namespaces USE: vectors : f_ ( h s v i -- f ) >r swap rot >r 2dup r> 6 * r> - ; -: p ( v s x -- v p x ) >r dupd neg succ * r> ; -: q ( v s f -- q ) * neg succ * ; -: t_ ( v s f -- t_ ) neg succ * neg succ * ; +: p ( v s x -- v p x ) >r dupd neg 1 + * r> ; +: q ( v s f -- q ) * neg 1 + * ; +: t_ ( v s f -- t_ ) neg 1 + * neg 1 + * ; : mod-cond ( p vector -- ) #! Call p mod q'th entry of the vector of quotations, where diff --git a/library/strings.factor b/library/strings.factor index 6b4fda404a..27cf3b9469 100644 --- a/library/strings.factor +++ b/library/strings.factor @@ -96,7 +96,7 @@ UNION: text string integer ; #! Returns 2 strings, that when concatenated yield the #! original string, without the character at the given #! index. - [ swap str-head ] 2keep succ swap str-tail ; + [ swap str-head ] 2keep 1 + swap str-tail ; : str-head? ( str begin -- ? ) 2dup str-length< [ diff --git a/library/syntax/parse-stream.factor b/library/syntax/parse-stream.factor index a5832180cd..2c84fa87d9 100644 --- a/library/syntax/parse-stream.factor +++ b/library/syntax/parse-stream.factor @@ -42,7 +42,7 @@ USE: strings : next-line ( -- str ) "parse-stream" get freadln - "line-number" [ succ ] change ; + "line-number" [ 1 + ] change ; : (read-lines) ( quot -- ) next-line dup [ diff --git a/library/syntax/parser.factor b/library/syntax/parser.factor index 9b81047823..4322ea64ec 100644 --- a/library/syntax/parser.factor +++ b/library/syntax/parser.factor @@ -66,7 +66,7 @@ USE: unparser "line" off "col" off ; : ch ( -- ch ) "col" get "line" get str-nth ; -: advance ( -- ) "col" [ succ ] change ; +: advance ( -- ) "col" [ 1 + ] change ; : skip ( n line quot -- n ) #! Find the next character that satisfies the quotation, @@ -75,7 +75,7 @@ USE: unparser 2dup str-nth r> dup >r call [ r> 2drop ] [ - >r succ r> r> skip + >r 1 + r> r> skip ] ifte ] [ r> drop nip str-length @@ -101,7 +101,7 @@ USE: unparser dup >r skip-blank dup r> 2dup str-length < [ 2dup str-nth denotation? [ - drop succ + drop 1 + ] [ skip-word ] ifte @@ -159,7 +159,7 @@ USE: unparser "col" get "line" get rot index-of* ; : (until) ( index -- str ) - "col" get swap dup succ "col" set "line" get substring ; + "col" get swap dup 1 + "col" set "line" get substring ; : until ( ch -- str ) ch-search (until) ; diff --git a/library/syntax/prettyprint.factor b/library/syntax/prettyprint.factor index a6f15114ac..2120f31a1d 100644 --- a/library/syntax/prettyprint.factor +++ b/library/syntax/prettyprint.factor @@ -40,6 +40,8 @@ USE: vectors USE: words USE: hashtables +SYMBOL: prettyprint-limit + GENERIC: prettyprint* ( indent obj -- indent ) M: object prettyprint* ( indent obj -- indent ) @@ -49,10 +51,6 @@ M: object prettyprint* ( indent obj -- indent ) #! Change this to suit your tastes. 4 ; -: prettyprint-limit ( -- limit ) - #! Avoid infinite loops -- maximum indent, 10 levels. - "prettyprint-limit" get [ 40 ] unless* ; - : indent ( indent -- ) #! Print the given number of spaces. " " fill write ; @@ -64,7 +62,7 @@ M: object prettyprint* ( indent obj -- indent ) " " write ; : prettyprint-element ( indent obj -- indent ) - over prettyprint-limit >= [ + over prettyprint-limit get >= [ unparse write ] [ prettyprint* @@ -186,7 +184,7 @@ M: hashtable prettyprint* ( indent hashtable -- indent ) : . ( obj -- ) [ "prettyprint-single-line" on - tab-size 4 * "prettyprint-limit" set + 16 prettyprint-limit set prettyprint ] with-scope ; @@ -207,3 +205,5 @@ M: hashtable prettyprint* ( indent hashtable -- indent ) : .b >bin print ; : .o >oct print ; : .h >hex print ; + +global [ 40 prettyprint-limit set ] bind diff --git a/library/syntax/see.factor b/library/syntax/see.factor index fde6db2df0..5b7276de89 100644 --- a/library/syntax/see.factor +++ b/library/syntax/see.factor @@ -37,15 +37,24 @@ USE: unparser USE: words ! Prettyprinting words -: vocab-attrs ( word -- attrs ) - vocab-link "object-link" default-style acons ; +: vocab-actions ( search -- list ) + [ + [ "Words" | "words." ] + [ "Use" | "\"use\" cons@" ] + [ "In" | "\"in\" set" ] + ] ; + +: vocab-attrs ( vocab -- attrs ) + #! Words without a vocabulary do not get a link or an action + #! popup. + unparse vocab-actions "actions" swons unit ; : prettyprint-vocab ( vocab -- ) dup vocab-attrs write-attr ; -: prettyprint-IN: ( indent word -- ) +: prettyprint-IN: ( word -- ) \ IN: prettyprint* prettyprint-space - word-vocabulary prettyprint-vocab prettyprint-newline ; + word-vocabulary prettyprint-vocab prettyprint-space ; : prettyprint-: ( indent -- indent ) \ : prettyprint* prettyprint-space @@ -95,19 +104,22 @@ M: object see ( obj -- ) "Not a word: " write . ; M: compound see ( word -- ) - 0 swap - [ dupd prettyprint-IN: prettyprint-: ] keep + [ prettyprint-IN: ] keep + 0 prettyprint-: swap [ prettyprint-1 ] keep [ prettyprint-docs ] keep [ word-parameter prettyprint-list prettyprint-; ] keep prettyprint-plist prettyprint-newline ; M: primitive see ( word -- ) - "PRIMITIVE: " write dup unparse write stack-effect. terpri ; + dup prettyprint-IN: + "PRIMITIVE: " write dup prettyprint-1 stack-effect. terpri ; M: symbol see ( word -- ) - 0 over prettyprint-IN: + dup prettyprint-IN: + 0 swap \ SYMBOL: prettyprint-1 prettyprint-space . ; M: undefined see ( word -- ) - drop "Not defined" print ; + dup prettyprint-IN: + \ DEFER: prettyprint-1 prettyprint-space . ; diff --git a/library/syntax/unparser.factor b/library/syntax/unparser.factor index c77bf13c56..16e013b377 100644 --- a/library/syntax/unparser.factor +++ b/library/syntax/unparser.factor @@ -41,7 +41,7 @@ GENERIC: unparse ( obj -- str ) M: object unparse ( obj -- str ) [ "#<" , - dup type type-name , + dup class unparse , " @ " , address unparse , ">" , @@ -51,10 +51,10 @@ M: object unparse ( obj -- str ) dup 10 < [ CHAR: 0 + ] [ 10 - CHAR: a + ] ifte ; : integer, ( num radix -- ) - tuck /mod >digit , dup 0 > [ - swap integer, + dup >r /mod >digit , dup 0 > [ + r> integer, ] [ - 2drop + r> 2drop ] ifte ; : >base ( num radix -- string ) diff --git a/library/test/benchmark/ack.factor b/library/test/benchmark/ack.factor index 2f22ea1a40..51632cf848 100644 --- a/library/test/benchmark/ack.factor +++ b/library/test/benchmark/ack.factor @@ -7,12 +7,12 @@ USE: test : ack ( m n -- x ) over 0 = [ - nip succ + nip 1 + ] [ dup 0 = [ - drop pred 1 ack + drop 1 - 1 ack ] [ - dupd pred ack >r pred r> ack + dupd 1 - ack >r 1 - r> ack ] ifte ] ifte ; compiled diff --git a/library/test/benchmark/fac.factor b/library/test/benchmark/fac.factor index c72c123d3d..13f6f14379 100644 --- a/library/test/benchmark/fac.factor +++ b/library/test/benchmark/fac.factor @@ -4,6 +4,6 @@ USE: test USE: compiler : fac-benchmark - 10000 fac 10000 [ succ / ] times* ; compiled + 10000 fac 10000 [ 1 + / ] times* ; compiled [ 1 ] [ fac-benchmark ] unit-test diff --git a/library/test/benchmark/fib.factor b/library/test/benchmark/fib.factor index 2ece5d2111..48cfede1ca 100644 --- a/library/test/benchmark/fib.factor +++ b/library/test/benchmark/fib.factor @@ -5,7 +5,7 @@ USE: math USE: test : fib ( n -- nth fibonacci number ) - dup 1 <= [ drop 1 ] [ pred dup fib swap pred fib + ] ifte ; + dup 1 <= [ drop 1 ] [ 1 - dup fib swap 1 - fib + ] ifte ; compiled [ 9227465 ] [ 34 fib ] unit-test diff --git a/library/test/benchmark/strings.factor b/library/test/benchmark/strings.factor index 986387bab2..bf12d7390e 100644 --- a/library/test/benchmark/strings.factor +++ b/library/test/benchmark/strings.factor @@ -11,7 +11,7 @@ USE: compiler 2dup str-length > [ dup [ "123" , , "456" , , "789" , ] make-string dup dup str-length 2 /i 0 swap rot substring - swap dup str-length 2 /i succ 1 swap rot substring cat2 + swap dup str-length 2 /i 1 + 1 swap rot substring cat2 string-step ] [ 2drop diff --git a/library/test/continuations.factor b/library/test/continuations.factor index 22a50a65fc..f7137aeac4 100644 --- a/library/test/continuations.factor +++ b/library/test/continuations.factor @@ -9,7 +9,7 @@ USE: test : callcc1-test ( x -- list ) [ "test-cc" set [ ] [ - swap pred tuck swons + swap 1 - tuck swons over 0 = [ "test-cc" get call ] when ] forever ] callcc1 nip ; diff --git a/library/test/dataflow.factor b/library/test/dataflow.factor index ec395c7616..1de418a356 100644 --- a/library/test/dataflow.factor +++ b/library/test/dataflow.factor @@ -79,7 +79,7 @@ SYMBOL: #test {{ [ node-op | #test ] [ node-param | 5 ] - }} "foobar" [ [ node-param get ] bind succ ] apply-dataflow + }} "foobar" [ [ node-param get ] bind 1 + ] apply-dataflow ] unit-test #test [ [ node-param get ] bind sq ] "foobar" set-word-property @@ -88,7 +88,7 @@ SYMBOL: #test {{ [ node-op | #test ] [ node-param | 5 ] - }} "foobar" [ [ node-param get ] bind succ ] apply-dataflow + }} "foobar" [ [ node-param get ] bind 1 + ] apply-dataflow ] unit-test ! Somebody (cough) got the order of ifte nodes wrong. diff --git a/library/test/inference.factor b/library/test/inference.factor index 09696199a5..13b9c2ab54 100644 --- a/library/test/inference.factor +++ b/library/test/inference.factor @@ -215,11 +215,11 @@ SYMBOL: sym-test ! Type inference -[ [ [ object ] [ ] ] ] [ [ drop ] infer ] unit-test -[ [ [ object ] [ object object ] ] ] [ [ dup ] infer ] unit-test -[ [ [ object object ] [ cons ] ] ] [ [ cons ] infer ] unit-test -[ [ [ cons ] [ cons ] ] ] [ [ uncons cons ] infer ] unit-test -[ [ [ object ] [ object ] ] ] [ [ dup [ car ] when ] infer ] unit-test +! [ [ [ object ] [ ] ] ] [ [ drop ] infer ] unit-test +! [ [ [ object ] [ object object ] ] ] [ [ dup ] infer ] unit-test +! [ [ [ object object ] [ cons ] ] ] [ [ cons ] infer ] unit-test +! [ [ [ cons ] [ cons ] ] ] [ [ uncons cons ] infer ] unit-test +! [ [ [ object ] [ object ] ] ] [ [ dup [ car ] when ] infer ] unit-test ! [ [ [ vector ] [ vector ] ] ] [ [ vector-clone ] infer ] unit-test ! [ [ [ number ] [ number ] ] ] [ [ dup + ] infer ] unit-test ! [ [ [ number number number ] [ number ] ] ] [ [ digit+ ] infer ] unit-test diff --git a/library/test/strings.factor b/library/test/strings.factor index 177bd87fbe..a4247dfef6 100644 --- a/library/test/strings.factor +++ b/library/test/strings.factor @@ -83,7 +83,7 @@ unit-test [ 4 ] [ 0 "There are Four Upper Case characters" - [ LETTER? [ succ ] when ] str-each + [ LETTER? [ 1 + ] when ] str-each ] unit-test [ "Replacing+spaces+with+plus" ] diff --git a/library/test/test.factor b/library/test/test.factor index e04efbbfff..5757e6213d 100644 --- a/library/test/test.factor +++ b/library/test/test.factor @@ -55,7 +55,7 @@ USE: unparser : test ( name -- ) ! Run the given test. - depth pred >r + depth 1 - >r "Testing " write dup write "..." print "/library/test/" swap ".factor" cat3 run-resource "Checking before/after depth..." print diff --git a/library/tools/debugger.factor b/library/tools/debugger.factor index 1e14440fc6..3c3931c0aa 100644 --- a/library/tools/debugger.factor +++ b/library/tools/debugger.factor @@ -63,7 +63,7 @@ USE: generic "Type check error" print uncons car dup "Object: " write . "Object type: " write class . - "Expected type: " write type-name print ; + "Expected type: " write builtin-type . ; : range-error ( list -- ) "Range check error" print diff --git a/library/tools/heap-stats.factor b/library/tools/heap-stats.factor index f5c9cab471..a8ab447470 100644 --- a/library/tools/heap-stats.factor +++ b/library/tools/heap-stats.factor @@ -48,4 +48,4 @@ USE: generic : heap-stats. ( -- ) #! Print heap allocation breakdown. - 0 heap-stats [ dupd uncons heap-stat. succ ] each drop ; + 0 heap-stats [ dupd uncons heap-stat. 1 + ] each drop ; diff --git a/library/tools/listener.factor b/library/tools/listener.factor index 9b7684d698..625dbc07e5 100644 --- a/library/tools/listener.factor +++ b/library/tools/listener.factor @@ -38,6 +38,7 @@ USE: presentation USE: words USE: unparser USE: vectors +USE: ansi SYMBOL: cont-prompt SYMBOL: listener-prompt @@ -143,3 +144,8 @@ global [ terpri "HTTP SERVER: USE: httpd 8888 httpd" print "TELNET SERVER: USE: telnetd 9999 telnetd" print ; + +IN: shells + +: tty + print-banner listener ; diff --git a/library/tools/telnetd.factor b/library/tools/telnetd.factor index a691ca5399..95e146993b 100644 --- a/library/tools/telnetd.factor +++ b/library/tools/telnetd.factor @@ -34,6 +34,7 @@ USE: namespaces USE: stdio USE: streams USE: threads +USE: parser : telnet-client ( socket -- ) dup [ @@ -52,3 +53,10 @@ USE: threads [ [ telnetd-loop ] [ swap fclose rethrow ] catch ] with-logging ; + +IN: shells + +: telnet + "telnetd-port" get str>number telnetd ; + +global [ 9999 "telnetd-port" set ] bind diff --git a/library/vectors.factor b/library/vectors.factor index a30d9ac760..aec6b84042 100644 --- a/library/vectors.factor +++ b/library/vectors.factor @@ -75,11 +75,11 @@ BUILTIN: vector 11 : vector-peek ( vector -- obj ) #! Get value at end of vector. - dup vector-length pred swap vector-nth ; + dup vector-length 1 - swap vector-nth ; : vector-pop ( vector -- obj ) #! Get value at end of vector and remove it. - dup vector-length pred ( vector top ) + dup vector-length 1 - ( vector top ) 2dup swap vector-nth >r swap set-vector-length r> ; : >pop> ( stack -- stack ) diff --git a/native/signal.h b/native/signal.h index d659eb73b4..086698f9ba 100644 --- a/native/signal.h +++ b/native/signal.h @@ -1,5 +1,6 @@ #ifndef WIN32 void signal_handler(int signal, siginfo_t* siginfo, void* uap); +void dump_stack_signal(int signal, siginfo_t* siginfo, void* uap); void call_profiling_step(int signal, siginfo_t* siginfo, void* uap); void init_signals(void); #endif diff --git a/native/stack.c b/native/stack.c index 1f6d3e7a88..8e89917dd1 100644 --- a/native/stack.c +++ b/native/stack.c @@ -15,11 +15,17 @@ void fix_stacks(void) if(STACK_UNDERFLOW(ds,ds_bot)) reset_datastack(); else if(STACK_OVERFLOW(ds,ds_bot)) + { + fprintf(stderr,"ds oveflow\n"); reset_datastack(); + } else if(STACK_UNDERFLOW(cs,cs_bot)) reset_callstack(); else if(STACK_OVERFLOW(cs,cs_bot)) + { + fprintf(stderr,"cs oveflow\n"); reset_callstack(); + } } void init_stacks(void) diff --git a/native/unix/signal.c b/native/unix/signal.c index f101c6fa3e..41d3f489ba 100644 --- a/native/unix/signal.c +++ b/native/unix/signal.c @@ -15,6 +15,11 @@ void signal_handler(int signal, siginfo_t* siginfo, void* uap) signal_error(signal); } +void dump_stack_signal(int signal, siginfo_t* siginfo, void* uap) +{ + dump_stacks(); +} + /* Called from a signal handler. XXX - is this safe? */ void call_profiling_step(int signal, siginfo_t* siginfo, void* uap) { @@ -36,10 +41,13 @@ void init_signals(void) struct sigaction custom_sigaction; struct sigaction profiling_sigaction; struct sigaction ign_sigaction; + struct sigaction dump_sigaction; custom_sigaction.sa_sigaction = signal_handler; custom_sigaction.sa_flags = SA_SIGINFO; profiling_sigaction.sa_sigaction = call_profiling_step; profiling_sigaction.sa_flags = SA_SIGINFO; + dump_sigaction.sa_sigaction = dump_stack_signal; + dump_sigaction.sa_flags = SA_SIGINFO; ign_sigaction.sa_handler = SIG_IGN; sigaction(SIGABRT,&custom_sigaction,NULL); sigaction(SIGFPE,&custom_sigaction,NULL); @@ -47,6 +55,7 @@ void init_signals(void) sigaction(SIGSEGV,&custom_sigaction,NULL); sigaction(SIGPIPE,&ign_sigaction,NULL); sigaction(SIGPROF,&profiling_sigaction,NULL); + sigaction(SIGQUIT,&dump_sigaction,NULL); } void primitive_call_profiling(void)