1 ! Copyright (C) 2009 Philipp Brüschweiler
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: infix.ast infix.tokenizer kernel multiline peg.ebnf
4 sequences strings vectors ;
8 Number = . ?[ ast-value? ]?
9 Identifier = . ?[ string? ]?
10 Array = Identifier:i "[" Sum:s "]" => [[ i s ast-array boa ]]
11 Slice1 = Identifier:i "[" Sum?:from ":" Sum?:to "]" => [[ i from to f ast-slice boa ]]
12 Slice2 = Identifier:i "[" Sum?:from ":" Sum?:to ":" Sum?:step "]" => [[ i from to step ast-slice boa ]]
13 Slice = Slice1 | Slice2
14 Function = Identifier:i "(" FunArgs?:a ")" => [[ i a [ V{ } ] unless* ast-function boa ]]
16 FunArgs = FunArgs:a "," Sum:s => [[ s a push a ]]
17 | Sum:s => [[ s 1vector ]]
19 Terminal = ("-"|"+"):op Terminal:term => [[ term op "-" = [ ast-negation boa ] when ]]
20 | "(" Sum:s ")" => [[ s ]]
21 | Number | Array | Slice | Function
22 | Identifier => [[ ast-local boa ]]
24 Product = Product:p ("**"|"*"|"/"|"%"):op Terminal:term => [[ p term op ast-op boa ]]
27 Sum = Sum:s ("+"|"-"):op Product:p => [[ s p op ast-op boa ]]
34 : build-infix-ast ( string -- ast )
35 tokenize-infix parse-infix ;