From 5d73e84daeee45f295643092d0956b03c07a67a4 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 4 Apr 2013 15:04:14 -0700 Subject: [PATCH] infix: adding pow(). --- extra/infix/infix-tests.factor | 1 + extra/infix/infix.factor | 11 ++++++++--- extra/infix/parser/parser.factor | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/extra/infix/infix-tests.factor b/extra/infix/infix-tests.factor index 342f436271..8a729aa344 100644 --- a/extra/infix/infix-tests.factor +++ b/extra/infix/infix-tests.factor @@ -8,6 +8,7 @@ IN: infix.tests [ 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- 1 -5* diff --git a/extra/infix/infix.factor b/extra/infix/infix.factor index 9cb01454bb..96887687dc 100644 --- a/extra/infix/infix.factor +++ b/extra/infix/infix.factor @@ -2,8 +2,9 @@ ! See http://factorcode.org/license.txt for BSD license. USING: accessors assocs combinators combinators.short-circuit effects fry infix.parser infix.ast kernel locals locals.parser -locals.types math math.order math.ranges multiline namespaces -parser quotations sequences summary words vocabs.parser ; +locals.types math math.functions math.order math.ranges +multiline namespaces parser quotations sequences summary +words vocabs.parser ; IN: infix @@ -18,13 +19,17 @@ M: local-not-defined summary : >local-word ( string -- word ) locals get ?at [ local-not-defined ] unless ; +ERROR: invalid-op string ; + : select-op ( string -- word ) { { "+" [ [ + ] ] } { "-" [ [ - ] ] } { "*" [ [ * ] ] } { "/" [ [ / ] ] } - [ drop [ mod ] ] + { "%" [ [ mod ] ] } + { "**" [ [ ^ ] ] } + [ invalid-op ] } case ; GENERIC: infix-codegen ( ast -- quot/number ) diff --git a/extra/infix/parser/parser.factor b/extra/infix/parser/parser.factor index def3898fad..5b02dd05c2 100644 --- a/extra/infix/parser/parser.factor +++ b/extra/infix/parser/parser.factor @@ -21,7 +21,7 @@ Terminal = ("-"|"+"):op Terminal:term => [[ term op "-" = [ ast-negation bo | Number | Array | Slice | Function | 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 Sum = Sum:s ("+"|"-"):op Product:p => [[ s p op ast-op boa ]]