Skip to content

Instantly share code, notes, and snippets.

@9214
Last active June 21, 2019 15:15
Show Gist options
  • Save 9214/e6378c30f21af4c75436692776fa9589 to your computer and use it in GitHub Desktop.
Save 9214/e6378c30f21af4c75436692776fa9589 to your computer and use it in GitHub Desktop.
Extended MATH dialect.
Red [
Title: "Extended MATH dialect"
Author: 9214
Date: 12-Nov-18
]
math: function [
"Evaluate expression using PEMDAS precedence rules"
body [any-list!]
/local match
][
table: [
'**
['* | '/ | '% | '//]
['+ | '-]
]
wrap: [change copy match [skip operator skip] (do match)]
body: copy/deep body
do also body foreach operator table [
parse body rule: [
any [
if (operator = '**) skip operator rule [fail]
| ahead paren! into rule [fail]
| some [ahead wrap] [fail]
| skip
]
]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment