Skip to content

Commit

Permalink
Added support for Factor (#2203)
Browse files Browse the repository at this point in the history
This adds support for the [Factor](https://factorcode.org) language.
  • Loading branch information
catb0t authored Feb 16, 2020
1 parent b24f734 commit f941102
Show file tree
Hide file tree
Showing 17 changed files with 1,170 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@
"require": "clike",
"owner": "simonreynolds7"
},
"factor": {
"title": "Factor",
"owner": "catb0t"
},
"firestore-security-rules": {
"title": "Firestore security rules",
"require": "clike",
Expand Down
403 changes: 403 additions & 0 deletions components/prism-factor.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions components/prism-factor.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 128 additions & 0 deletions examples/prism-factor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<h2>Comments</h2>
<pre><code>! FIXME: a comment

USE: multiline

![[ comment ]]
/* comment */</code></pre>

<h2>Strings</h2>
<pre><code>"a string" "\"" "\x8" "%s"

SBUF" asbdef"

USE: multiline

STRING: name
content
;

HEREDOC: marker
text
marker

[==[
str
ing
]==]
</code></pre>

<h2>Numbers</h2>
<pre><code>5 1/5 +9 -9 +1/5 -1/5 -1/5. 23+1/5 -23-1/5 23-1/5 ! NOTE: last one = word

+12.13 0.01 0e0 3E4 3e-4 3E-4 030 0xd 0o30 0b1100
-12.13 -0 -0.01 -0e0 -3E4 -3E-4 -030 -0xd -0o30 -0b1100

348756424956392657834385437598743583648756332457
-348756424956392657834385437598743583648756332457

NAN: a
NAN: 80000deadbeef

0b1.010p2 0x1.0p3 0x1.p1 0b1.111111p1111 ...</code></pre>

<h2>Sequences</h2>
<pre><code>{ 1 2 3 4 }
{ a b c d e t f }
{ "a" "b" "c" }

{ { a b } { c d } }
H{ { a b } { c d } }
H{ { "a" "b" } { "c" "d" } }
V{ 1 2 3 4 }
V{ "1" "2" "3" "4" }
BV{ 1 2 3 4 }</code></pre>

<h2>Regular Expressions</h2>
<pre><code>USE: regexp
R/ abcde?.*+\?\.\*\+\/\\\/idmsr-idmsr/idmsr-idmsr</code>
</pre>

<h2>Colon parsing words</h2>
<pre><code>: a ( -- ) ;
:: ; ! ; is not a word name
:: ;a ! ;a is a word name
USING: a b c ;
USE: a
IN: a.b
CHAR: a
GENERIC#: x 1 ( x: integer quot: ( x -- y ) -- )</code></pre>

<h2>Special words (builtins, conventions)</h2>
<pre><code>and not with map filter

new last-index + - neg

&lt;array&gt; <=> SYNTAX: x $[ xyz ]

set-x change-x with-variable ?of if* (gensym) hex. $description reader>> >>setter writer<<

string>number >hex base> mutater!
</code></pre>

<h2>Full example</h2>
<pre><code>USING: accessors arrays assocs combinators
combinators.short-circuit effects io kernel sequences
sequences.deep splitting strings vocabs words ;
IN: prism

: make-prism-syntax ( syntax-vocab -- seq )
vocab-words [
dup name>> ">>" = [ drop t ] [
{
[ "delimiter" word-prop ]
[ name>> last { CHAR: : CHAR: { CHAR: [ CHAR: ( CHAR: ) CHAR: ? CHAR: " } member? ]
[ name>> { "t" "f" } member? ]
} 1|| not
] if
] filter ;

: combinator? ( word -- ? )
[ "declared-effect" word-prop in>> flatten
[
[ effect? ] [ { "quots" "quot" } member? ] bi or
] any?
] [
"help" word-prop ?first flatten [ dup word? [ name>> ] when "quot" swap subseq? ] any?
] bi or ;

: classify ( vocab-spec -- seq )
vocab-words [
dup {
{ [ dup combinator? ] [ drop "combinator" ] }
{ [ dup "macro" word-prop ] [ drop "macro" ] }
[ drop "ordinary" ]
} cond 2array
] map ;

: print-strings ( strs -- )
[ name>> "'" dup surround ] map ", " join print ; recursive ! WARN: not recursive

: combinators. ( vocab-spec -- )
classify [ nip "combinator" = ] assoc-filter keys print-strings ; flushable

: ordinaries. ( vocab-spec -- )
classify [ nip "ordinary" = ] assoc-filter keys print-strings ; foldable

: macros. ( vocab-spec -- )
classify [ nip "macro" = ] assoc-filter keys print-strings ; inline</code></pre>
15 changes: 15 additions & 0 deletions tests/languages/factor/builtin_words_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
and not with map filter

----------------------------------------------------

[
[ "kernel-builtin", "and" ],
[ "kernel-builtin", "not" ],
[ "combinators", "with" ],
[ "combinators", "map" ],
[ "combinators", "filter" ]
]

----------------------------------------------------

some builtins
47 changes: 47 additions & 0 deletions tests/languages/factor/colon_words_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
: a ( -- ) ;
:: ; ! ; is not a word name
:: ;a ! ;a is a word name
USING: a b c ;
USE: a
IN: a.b
CHAR: a
#: a
GENERIC#: a

----------------------------------------------------

[
[ "colon-syntax", ": a" ],

[ "stack-effect-delimiter", "(" ],
[ "stack-effect-delimiter", "--" ],
[ "stack-effect-delimiter", ")" ],

[ "semicolon-or-setlocal", ";" ],

[ "normal-word", "::" ],
[ "semicolon-or-setlocal", ";" ],
[ "comment", [ "! ; is not a word name" ] ],

[ "colon-syntax", ":: ;a" ],
[ "comment", [ "! ;a is a word name" ] ],

[ "special-using",
[ "USING: ",
[ "string", "a" ],
[ "string", "b" ],
[ "string", "c" ]
]
],
[ "semicolon-or-setlocal", ";" ],
[ "colon-syntax", "USE: a" ],
[ "colon-syntax", "IN: a.b" ],
[ "colon-syntax", "CHAR: a" ],
[ "normal-word", "#:" ],
[ "normal-word", "a" ],
[ "colon-syntax", "GENERIC#: a" ]
]

----------------------------------------------------

colon-ended parsing words
74 changes: 74 additions & 0 deletions tests/languages/factor/comments_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
a! ! word
!a ! word
! comment
! bad
! fine
! "also a comment"
! : ( -- ) ;
! ! leading comment-like token
! whitespace before
words blah ! comment after code on a line

![[ comment ]]
"![[ string ]]"
![[ "comment" ]]

![[ comment]]
![==[ comment ]==]
![==[ comment]==]
![=[word ]=]
![=======[ words ]=======]

/* com
ment */
/* com
ment*/
/*word */

/* "comment" */
"/* "strings" */"


----------------------------------------------------

[
[ "conventionally-named-word", "a!"],
[ "comment", [ "! word" ] ],
[ "normal-word", "!a"],
[ "comment", [ "! word" ] ],
[ "comment", [ "! comment" ] ],

[ "normal-word", "!" ], [ "normal-word", "bad" ],
[ "comment", [ "! \tfine" ] ],

[ "comment", [ "! \"also a comment\"" ] ],
[ "comment", [ "! : ( -- ) ;" ] ],
[ "comment", [ "! ! leading comment-like token" ] ],
[ "comment", [ "! whitespace before" ] ],
[ "normal-word", "words" ],
[ "normal-word", "blah" ],
[ "comment", [ "! comment after code on a line" ] ],

[ "comment", [ "![[ comment ]]" ] ],
[ "string", [ "\"![[ string ]]\"" ] ],
[ "comment", [ "![[ \"comment\" ]]" ] ],
[ "comment", [ "![[ comment]]" ] ],
[ "comment", [ "![==[ comment ]==]" ] ],
[ "comment", [ "![==[ comment]==]" ] ],

[ "normal-word", "![=[word" ],
[ "normal-word", "]=]" ],
[ "normal-word", "![=======[" ],
[ "normal-word", "words" ],
[ "normal-word", "]=======]" ],
[ "comment", [ "/* com\r\nment */" ] ],
[ "comment", [ "/* com\r\nment*/" ] ],
[ "normal-word", "/*word" ],
[ "normal-word", "*/" ],
[ "comment", ["/* \"comment\" */"] ],
[ "string", ["\"/* \""] ],
"strings",
[ "string", ["\" */\""] ]
]

----------------------------------------------------
18 changes: 18 additions & 0 deletions tests/languages/factor/constructors_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<array> <byte-array> <string>

<=> <-->

----------------------------------------------------

[
[ "constructor-word", "<array>" ],
[ "constructor-word", "<byte-array>" ],
[ "constructor-word", "<string>" ],

[ "conventionally-named-word", "<=>" ],
[ "conventionally-named-word", "<-->" ]
]

----------------------------------------------------

ctors
56 changes: 56 additions & 0 deletions tests/languages/factor/normal_words_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
commas, primes'
set-x
change-x
with-x
new-x
>string
base>
string>number
+symbol+
que?
?of
?of*
reader>>
>>setter
writer<<
(detail)
mutater!
variant*
prettyprint.
$help

"no

----------------------------------------------------

[
[ "normal-word", "commas," ],
[ "normal-word", "primes'" ],
[ "conventionally-named-word", "set-x" ],
[ "conventionally-named-word", "change-x" ],
[ "conventionally-named-word", "with-x" ],
[ "conventionally-named-word", "new-x" ],
[ "conventionally-named-word", ">string" ],
[ "conventionally-named-word", "base>" ],
[ "conventionally-named-word", "string>number" ],
[ "conventionally-named-word", "+symbol+" ],
[ "conventionally-named-word", "que?" ],
[ "conventionally-named-word", "?of" ],
[ "conventionally-named-word", "?of*" ],
[ "conventionally-named-word", "reader>>" ],
[ "conventionally-named-word", ">>setter" ],
[ "conventionally-named-word", "writer<<" ],
[ "conventionally-named-word", "(detail)" ],
[ "conventionally-named-word", "mutater!" ],
[ "conventionally-named-word", "variant*" ],
[ "conventionally-named-word", "prettyprint." ],
[ "conventionally-named-word", "$help" ],
"\r\n\r\n\"no"
]

----------------------------------------------------

"Normal" words are not builtin well-known ones like "not", "and", "<=>", though they may be in the standard library or user-defined.
They may start with any character except `"` (double-quote), but may contain or end with double-quotes or any other non-whitespace character.

Conventionally named words follow the conventions outlined in prism-factor.js.
Loading

0 comments on commit f941102

Please sign in to comment.