infix: adding pow().

db4
John Benediktsson 2013-04-04 15:04:14 -07:00
parent d544cfb956
commit 5d73e84dae
3 changed files with 10 additions and 4 deletions

View File

@ -8,6 +8,7 @@ IN: infix.tests
[ 0.5 ] [ [infix 3.0/6 infix] ] unit-test [ 0.5 ] [ [infix 3.0/6 infix] ] unit-test
[ 1+2/3 ] [ [infix 5/3 infix] ] unit-test [ 1+2/3 ] [ [infix 5/3 infix] ] unit-test
[ 3 ] [ [infix 2*7%3+1 infix] ] unit-test [ 3 ] [ [infix 2*7%3+1 infix] ] unit-test
[ 1419857 ] [ [infix 17**5 infix] ] unit-test
[ 1 ] [ [infix 2- [ 1 ] [ [infix 2-
1 1
-5* -5*

View File

@ -2,8 +2,9 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs combinators combinators.short-circuit USING: accessors assocs combinators combinators.short-circuit
effects fry infix.parser infix.ast kernel locals locals.parser effects fry infix.parser infix.ast kernel locals locals.parser
locals.types math math.order math.ranges multiline namespaces locals.types math math.functions math.order math.ranges
parser quotations sequences summary words vocabs.parser ; multiline namespaces parser quotations sequences summary
words vocabs.parser ;
IN: infix IN: infix
@ -18,13 +19,17 @@ M: local-not-defined summary
: >local-word ( string -- word ) : >local-word ( string -- word )
locals get ?at [ local-not-defined ] unless ; locals get ?at [ local-not-defined ] unless ;
ERROR: invalid-op string ;
: select-op ( string -- word ) : select-op ( string -- word )
{ {
{ "+" [ [ + ] ] } { "+" [ [ + ] ] }
{ "-" [ [ - ] ] } { "-" [ [ - ] ] }
{ "*" [ [ * ] ] } { "*" [ [ * ] ] }
{ "/" [ [ / ] ] } { "/" [ [ / ] ] }
[ drop [ mod ] ] { "%" [ [ mod ] ] }
{ "**" [ [ ^ ] ] }
[ invalid-op ]
} case ; } case ;
GENERIC: infix-codegen ( ast -- quot/number ) GENERIC: infix-codegen ( ast -- quot/number )

View File

@ -21,7 +21,7 @@ Terminal = ("-"|"+"):op Terminal:term => [[ term op "-" = [ ast-negation bo
| Number | Array | Slice | Function | Number | Array | Slice | Function
| Identifier => [[ ast-local boa ]] | Identifier => [[ ast-local boa ]]
Product = Product:p ("*"|"/"|"%"):op Terminal:term => [[ p term op ast-op boa ]] Product = Product:p ("**"|"*"|"/"|"%"):op Terminal:term => [[ p term op ast-op boa ]]
| Terminal | Terminal
Sum = Sum:s ("+"|"-"):op Product:p => [[ s p op ast-op boa ]] Sum = Sum:s ("+"|"-"):op Product:p => [[ s p op ast-op boa ]]