infix: literally just a string dsl.
parent
dccba5f9c3
commit
55df44923f
|
@ -3,23 +3,23 @@
|
|||
USING: help.syntax help.markup math math.functions prettyprint locals sequences ;
|
||||
IN: infix
|
||||
|
||||
HELP: [infix
|
||||
{ $syntax "[infix ... infix]" }
|
||||
HELP: \infix[[
|
||||
{ $syntax "infix[[ ... ]]" }
|
||||
{ $description "Parses the infix code inside the brackets, converts it to stack code and executes it." }
|
||||
{ $examples
|
||||
{ $example
|
||||
"USING: infix prettyprint ;"
|
||||
"IN: scratchpad"
|
||||
"[infix 8+2*3 infix] ."
|
||||
"infix[[ 8+2*3 ]] ."
|
||||
"14"
|
||||
} $nl
|
||||
{ $link postpone: \[infix } " isn't that useful by itself, as it can only access literal numbers and no variables. It is designed to be used together with locals; for example with " { $link postpone: \:: } " :"
|
||||
{ $link postpone: \infix[[ } " isn't that useful by itself, as it can only access literal numbers and no variables. It is designed to be used together with locals; for example with " { $link postpone: \:: } " :"
|
||||
{ $example
|
||||
"USING: infix locals math.functions prettyprint ;"
|
||||
"IN: scratchpad"
|
||||
":: quadratic-equation ( a b c -- z- z+ )"
|
||||
" [infix (-b-sqrt(b*b-4*a*c)) / (2*a) infix]"
|
||||
" [infix (-b+sqrt(b*b-4*a*c)) / (2*a) infix] ;"
|
||||
" infix[[ (-b-sqrt(b*b-4*a*c)) / (2*a) ]]"
|
||||
" infix[[ (-b+sqrt(b*b-4*a*c)) / (2*a) ]] ;"
|
||||
"1 0 -1 quadratic-equation . ."
|
||||
"1.0\n-1.0"
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ HELP: [infix
|
|||
ARTICLE: "infix" "Infix notation"
|
||||
"The " { $vocab-link "infix" } " vocabulary implements support for infix notation in Factor source code."
|
||||
{ $subsections
|
||||
postpone: \[infix
|
||||
postpone: \infix[[
|
||||
postpone: \INFIX::
|
||||
}
|
||||
"The usual infix math operators are supported:"
|
||||
|
@ -43,7 +43,7 @@ ARTICLE: "infix" "Infix notation"
|
|||
"The standard precedence rules apply: Grouping with parentheses before " { $snippet "*" } ", " { $snippet "/" } "and " { $snippet "%" } " before " { $snippet "+" } " and " { $snippet "-" } "."
|
||||
{ $example
|
||||
"USE: infix"
|
||||
"[infix 5-40/10*2 infix] ."
|
||||
"infix[[ 5-40/10*2 ]] ."
|
||||
"-3"
|
||||
}
|
||||
$nl
|
||||
|
@ -55,34 +55,34 @@ $nl
|
|||
{ $example
|
||||
"USING: infix locals math.functions ;"
|
||||
":: binary_entropy ( p -- h )"
|
||||
" [infix -(p*log(p) + (1-p)*log(1-p)) / log(2) infix] ;"
|
||||
"[infix binary_entropy( sqrt(0.25) ) infix] ."
|
||||
" infix[[ -(p*log(p) + (1-p)*log(1-p)) / log(2) ]] ;"
|
||||
"infix[[ binary_entropy( sqrt(0.25) ) ]] ."
|
||||
"1.0"
|
||||
}
|
||||
$nl
|
||||
"You can access " { $vocab-link "sequences" } " inside infix expressions with the familiar " { $snippet "seq[index]" } " notation."
|
||||
{ $example
|
||||
"USING: arrays locals infix ;"
|
||||
"let[ { 1 2 3 4 } :> myarr [infix myarr[4/2]*3 infix] ] ."
|
||||
"let[ { 1 2 3 4 } :> myarr infix[[ myarr[4/2]*3 ]] ] ."
|
||||
"9"
|
||||
}
|
||||
$nl
|
||||
"You can create sub-" { $vocab-link "sequences" } " inside infix expressions using " { $snippet "seq[from:to]" } " notation."
|
||||
{ $example
|
||||
"USING: arrays locals infix ;"
|
||||
"let[ \"foobar\" :> s [infix s[0:3] infix] ] ."
|
||||
"let[ \"foobar\" :> s infix[[ s[0:3] ]] ] ."
|
||||
"\"foo\""
|
||||
}
|
||||
$nl
|
||||
"Additionally, you can step through " { $vocab-link "sequences" } " with " { $snippet "seq[from:to:step]" } " notation."
|
||||
{ $example
|
||||
"USING: arrays locals infix ;"
|
||||
"let[ \"reverse\" :> s [infix s[::-1] infix] ] ."
|
||||
"let[ \"reverse\" :> s infix[[ s[::-1] ]] ] ."
|
||||
"\"esrever\""
|
||||
}
|
||||
{ $example
|
||||
"USING: arrays locals infix ;"
|
||||
"let[ \"0123456789\" :> s [infix s[::2] infix] ] ."
|
||||
"let[ \"0123456789\" :> s infix[[ s[::2] ]] ] ."
|
||||
"\"02468\""
|
||||
}
|
||||
;
|
||||
|
|
|
@ -4,19 +4,19 @@ USING: infix infix.private kernel literals locals math
|
|||
math.constants math.functions sequences tools.test ;
|
||||
IN: infix.tests
|
||||
|
||||
{ 0 } [ [infix 0 infix] ] unit-test
|
||||
{ 0.5 } [ [infix 3.0/6 infix] ] unit-test
|
||||
{ 1+2/3 } [ [infix 5/3 infix] ] unit-test
|
||||
{ 3 } [ [infix 2*7%3+1 infix] ] unit-test
|
||||
{ 1419857 } [ [infix 17**5 infix] ] unit-test
|
||||
{ 1 } [ [infix 2-
|
||||
{ 0 } [ infix[[ 0 ]] ] unit-test
|
||||
{ 0.5 } [ infix[[ 3.0/6 ]] ] unit-test
|
||||
{ 1+2/3 } [ infix[[ 5/3 ]] ] unit-test
|
||||
{ 3 } [ infix[[ 2*7%3+1 ]] ] unit-test
|
||||
{ 1419857 } [ infix[[ 17**5 ]] ] unit-test
|
||||
{ 1 } [ infix[[ 2-
|
||||
1
|
||||
-5*
|
||||
0 infix] ] unit-test
|
||||
0 ]] ] unit-test
|
||||
|
||||
{ 0.0 } [ [infix sin(0) infix] ] unit-test
|
||||
{ 10 } [ [infix lcm(2,5) infix] ] unit-test
|
||||
{ 1.0 } [ [infix +cos(-0*+3) infix] ] unit-test
|
||||
{ 0.0 } [ infix[[ sin(0) ]] ] unit-test
|
||||
{ 10 } [ infix[[ lcm(2,5) ]] ] unit-test
|
||||
{ 1.0 } [ infix[[ +cos(-0*+3) ]] ] unit-test
|
||||
|
||||
{ f } [ 2 \ gcd check-word ] unit-test ! multiple return values
|
||||
{ f } [ 1 \ drop check-word ] unit-test ! no return value
|
||||
|
@ -24,32 +24,32 @@ IN: infix.tests
|
|||
|
||||
: qux ( -- x ) 2 ;
|
||||
{ t } [ 0 \ qux check-word ] unit-test
|
||||
{ 8 } [ [infix qux()*3+2 infix] ] unit-test
|
||||
{ 8 } [ infix[[ qux()*3+2 ]] ] unit-test
|
||||
: foobar ( x -- y ) 1 + ;
|
||||
{ t } [ 1 \ foobar check-word ] unit-test
|
||||
{ 4 } [ [infix foobar(3*5%12) infix] ] unit-test
|
||||
{ 4 } [ infix[[ foobar(3*5%12) ]] ] unit-test
|
||||
: stupid_function ( x x x x x -- y ) + + + + ;
|
||||
{ t } [ 5 \ stupid_function check-word ] unit-test
|
||||
{ 10 } [ [infix stupid_function (0, 1, 2, 3, 4) infix] ] unit-test
|
||||
{ 10 } [ infix[[ stupid_function (0, 1, 2, 3, 4) ]] ] unit-test
|
||||
|
||||
{ -1 } [ let[ 1 :> a [infix -a infix] ] ] unit-test
|
||||
{ -1 } [ let[ 1 :> a infix[[ -a ]] ] ] unit-test
|
||||
|
||||
{ char: f } [ let[ "foo" :> s [infix s[0] infix] ] ] unit-test
|
||||
{ char: r } [ let[ "bar" :> s [infix s[-1] infix] ] ] unit-test
|
||||
{ "foo" } [ let[ "foobar" :> s [infix s[0:3] infix] ] ] unit-test
|
||||
{ "foo" } [ let[ "foobar" :> s [infix s[:3] infix] ] ] unit-test
|
||||
{ "bar" } [ let[ "foobar" :> s [infix s[-3:] infix] ] ] unit-test
|
||||
{ "boof" } [ let[ "foobar" :> s [infix s[-3::-1] infix] ] ] unit-test
|
||||
{ "foobar" } [ let[ "foobar" :> s [infix s[:] infix] ] ] unit-test
|
||||
{ "foa" } [ let[ "foobar" :> s [infix s[::2] infix] ] ] unit-test
|
||||
{ "bar" } [ let[ "foobar" :> s [infix s[-3:100] infix] ] ] unit-test
|
||||
{ "foobar" } [ let[ "foobar" :> s [infix s[-100:100] infix] ] ] unit-test
|
||||
{ "olh" } [ let[ "hello" :> s [infix s[4::-2] infix] ] ] unit-test
|
||||
{ "rb" } [ let[ "foobar" :> s [infix s[:1:-2] infix] ] ] unit-test
|
||||
{ "foa" } [ let[ "foobar" :> s [infix s[:-1:2] infix] ] ] unit-test
|
||||
{ "rbo" } [ let[ "foobar" :> s [infix s[::-2] infix] ] ] unit-test
|
||||
{ "rbo" } [ let[ "foobar" :> s [infix s[:0:-2] infix] ] ] unit-test
|
||||
{ "rb" } [ let[ "foobar" :> s [infix s[:-5:-2] infix] ] ] unit-test
|
||||
{ char: f } [ let[ "foo" :> s infix[[ s[0] ]] ] ] unit-test
|
||||
{ char: r } [ let[ "bar" :> s infix[[ s[-1] ]] ] ] unit-test
|
||||
{ "foo" } [ let[ "foobar" :> s infix[[ s[0:3] ]] ] ] unit-test
|
||||
{ "foo" } [ let[ "foobar" :> s infix[[ s[:3] ]] ] ] unit-test
|
||||
{ "bar" } [ let[ "foobar" :> s infix[[ s[-3:] ]] ] ] unit-test
|
||||
{ "boof" } [ let[ "foobar" :> s infix[[ s[-3::-1] ]] ] ] unit-test
|
||||
{ "foobar" } [ let[ "foobar" :> s infix[[ s[:] ]] ] ] unit-test
|
||||
{ "foa" } [ let[ "foobar" :> s infix[[ s[::2] ]] ] ] unit-test
|
||||
{ "bar" } [ let[ "foobar" :> s infix[[ s[-3:100] ]] ] ] unit-test
|
||||
{ "foobar" } [ let[ "foobar" :> s infix[[ s[-100:100] ]] ] ] unit-test
|
||||
{ "olh" } [ let[ "hello" :> s infix[[ s[4::-2] ]] ] ] unit-test
|
||||
{ "rb" } [ let[ "foobar" :> s infix[[ s[:1:-2] ]] ] ] unit-test
|
||||
{ "foa" } [ let[ "foobar" :> s infix[[ s[:-1:2] ]] ] ] unit-test
|
||||
{ "rbo" } [ let[ "foobar" :> s infix[[ s[::-2] ]] ] ] unit-test
|
||||
{ "rbo" } [ let[ "foobar" :> s infix[[ s[:0:-2] ]] ] ] unit-test
|
||||
{ "rb" } [ let[ "foobar" :> s infix[[ s[:-5:-2] ]] ] ] unit-test
|
||||
|
||||
INFIX:: foo ( x y -- z ) x**2-abs(y) ;
|
||||
|
||||
|
@ -58,11 +58,11 @@ INFIX:: foo ( x y -- z ) x**2-abs(y) ;
|
|||
{ "foobar" } [
|
||||
let[ "foo" :> foo
|
||||
let[ "bar" :> bar
|
||||
[infix append(foo, bar) infix]
|
||||
infix[[ append(foo, bar) ]]
|
||||
]
|
||||
]
|
||||
] unit-test
|
||||
|
||||
{ "foobar" } [ [infix append("foo", "bar") infix] ] unit-test
|
||||
{ "foobar" } [ infix[[ append("foo", "bar") ]] ] unit-test
|
||||
|
||||
${ pi } [ [infix pi infix] ] unit-test
|
||||
${ pi } [ infix[[ pi ]] ] unit-test
|
||||
|
|
|
@ -126,8 +126,8 @@ M: ast-function infix-codegen
|
|||
|
||||
PRIVATE>
|
||||
|
||||
SYNTAX: [infix
|
||||
"infix]" parse-infix-quotation suffix! \ call suffix! ;
|
||||
SYNTAX: \infix[[
|
||||
"]]" parse-infix-quotation suffix! \ call suffix! ;
|
||||
|
||||
<PRIVATE
|
||||
|
||||
|
|
Loading…
Reference in New Issue