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