Paste: simple infix math with locals

Author: levi
Mode: factor
Date: Thu, 4 Dec 2008 09:11:22
Plain Text |
USING: peg peg.ebnf math math.parser sequences kernel multiline parser strings
       fry ;

IN: infix-math

EBNF: parse-infix
s      = (" " | "\r" | "\t" | "\n")* => [[ drop ignore ]]
digit  = [0-9]                       => [[ digit> ]]
number = s (digit)+ s                => [[ 10 digits>integer '[ _ ] ]]
ident  = s [a-zA-Z]+ s               => [[ >string search '[ _ ] ]]
value  =   number
         | ident
         | (s "(" exp ")" s)         => [[ second ]]
fac    =   fac "*" value             => [[ first3 nip '[ @ @ * ] ]]
         | fac "/" value             => [[ first3 nip '[ @ @ / ] ]]
         | value
exp    =   exp "+" fac               => [[ first3 nip '[ @ @ + ] ]]
         | exp "-" fac               => [[ first3 nip '[ @ @ - ] ]]
         | fac
;EBNF

: I[ "]" parse-multiline-string parse-infix parsed \ call parsed ; parsing

New Annotation

Summary:
Author:
Mode:
Body: