mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-27 02:48:11 +08:00
Add math-parser library (#2033)
* start of parser * ops forgot * reorder files and work on executer * start of parser * ops forgot * reorder files and work on executer * Cleanup and fix tests * Integrate into the editor * added unit checking at parse time * fix tests * fix issues * fix editor intergration * update pest grammer to support units * units should be working, need to set up tests to know * make unit type store exponants as i32 * remove scale, insted just multiply the literal by the scale * unit now contains empty unit,remove options * add more tests and implement almost all unary operators * add evaluation context and variables * function calling, api might be refined later * add constants, change function call to not be as built into the parser and add tests * add function definitions * remove meval * remove raw-rs from workspace * add support for numberless units * fix unit handleing logic, add some "unit" tests(haha) * make it so units cant do implcit mul with idents * add bench and better tests * fix editor api * remove old test * change hashmap context to use deref * change constants to use hashmap instad of function --------- Co-authored-by: hypercube <0hypercube@gmail.com> Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
co-authored by
hypercube
Keavon Chambers
parent
51ce51ea8c
commit
9fb494764c
@@ -0,0 +1,60 @@
|
||||
WHITESPACE = _{ " " | "\t" }
|
||||
|
||||
// TODO: Proper indentation and formatting
|
||||
program = _{ SOI ~ expr ~ EOI }
|
||||
|
||||
expr = { atom ~ (infix ~ atom)* }
|
||||
atom = _{ prefix? ~ primary ~ postfix? }
|
||||
infix = _{ add | sub | mul | div | pow | paren }
|
||||
add = { "+" } // Addition
|
||||
sub = { "-" } // Subtraction
|
||||
mul = { "*" } // Multiplication
|
||||
div = { "/" } // Division
|
||||
mod = { "%" } // Modulo
|
||||
pow = { "^" } // Exponentiation
|
||||
paren = { "" } // Implicit multiplication operator
|
||||
|
||||
prefix = _{ neg | sqrt }
|
||||
neg = { "-" } // Negation
|
||||
sqrt = { "sqrt" }
|
||||
|
||||
postfix = _{ fac }
|
||||
fac = { "!" } // Factorial
|
||||
|
||||
primary = _{ ("(" ~ expr ~ ")") | lit | constant | fn_call | ident }
|
||||
fn_call = { ident ~ "(" ~ expr ~ ("," ~ expr)* ~ ")" }
|
||||
ident = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
|
||||
lit = { unit | ((float | int) ~ unit?) }
|
||||
|
||||
float = @{ int ~ "." ~ int? ~ exp? | int ~ exp }
|
||||
exp = _{ ^"e" ~ ("+" | "-")? ~ int }
|
||||
int = @{ ASCII_DIGIT+ }
|
||||
|
||||
unit = ${ (scale ~ base_unit) | base_unit ~ !ident}
|
||||
base_unit = _{ meter | second | gram }
|
||||
meter = { "m" }
|
||||
second = { "s" }
|
||||
gram = { "g" }
|
||||
|
||||
scale = _{ nano | micro | milli | centi | deci | deca | hecto | kilo | mega | giga | tera }
|
||||
nano = { "n" }
|
||||
micro = { "µ" | "u" }
|
||||
milli = { "m" }
|
||||
centi = { "c" }
|
||||
deci = { "d" }
|
||||
deca = { "da" }
|
||||
hecto = { "h" }
|
||||
kilo = { "k" }
|
||||
mega = { "M" }
|
||||
giga = { "G" }
|
||||
tera = { "T" }
|
||||
|
||||
// Constants
|
||||
constant = { infinity | imaginary_unit | pi | tau | euler_number | golden_ratio | gravity_acceleration }
|
||||
infinity = { "inf" | "INF" | "infinity" | "INFINITY" | "∞" }
|
||||
imaginary_unit = { "i" | "I" }
|
||||
pi = { "pi" | "PI" | "π" }
|
||||
tau = { "tau" | "TAU" | "τ" }
|
||||
euler_number = { "e" }
|
||||
golden_ratio = { "phi" | "PHI" | "φ" }
|
||||
gravity_acceleration = { "G" }
|
||||
Reference in New Issue
Block a user