Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 50 lines (37 sloc) 1.751 kb
e8f2c14 Jon Added README
authored
1 = Exalted Math Parser
2
3 This is a very simple project both to teach myself to use Treetop, and to
4 implement simple (or not so simple) parsing of mathematics for Exalted character
5 sheets. Although I'm sure it could be used for other simple math situations.
6 It consists of two parts; parser and abstract syntax tree. The parser
7 constructs the abstract syntax tree as it parses the input string. The AST can
492c001 Jon Update README
authored
8 then be used to compute the value of the expression with a given context.
9 The AST can also simply itself, factoring out constant operations in some cases.
e8f2c14 Jon Added README
authored
10
11 == Examples
12
492c001 Jon Update README
authored
13 @parser = ExaltedMath::MathsParser.new
e8f2c14 Jon Added README
authored
14
15 # simple maths
492c001 Jon Update README
authored
16 # The parser returns the AST
17 # it raises a ParseFailedError in the case of error
e8f2c14 Jon Added README
authored
18 @parser.ast('3 + 4')
492c001 Jon Update README
authored
19 #=> simple_ast
e8f2c14 Jon Added README
authored
20
21 # symbolic values
22 @parser.ast('Essence * 4')
492c001 Jon Update README
authored
23 #=> symbolic_ast
e8f2c14 Jon Added README
authored
24
25 # complex maths
26 @parser.ast('(Essence * 4) + Willpower + highest[2](Compassion,Conviction,Temperance,Valor)')
492c001 Jon Update README
authored
27 #=> complex_ast
e8f2c14 Jon Added README
authored
28
29 # evaluate the Ast
492c001 Jon Update README
authored
30 simple_ast.value
e8f2c14 Jon Added README
authored
31 #=> 7
32
33 # evaluate a more complex Ast
492c001 Jon Update README
authored
34 symbolic_ast.value({'essence' => 4})
e8f2c14 Jon Added README
authored
35 #=> 16
36
37 == Syntax
38
39 The syntax supported by the parser is pretty simple. In the examples above,
40 almost all of it has been demonstrated.
41
2d68e47 Jon More readme tweaks. Take 2
authored
42 [[0-9]+] A number
43 [[A-Za-z]+] A stat. This is looked up from the context.
44 [spec:"..."] A speciality. This is looked up from the context.
45 [highest(stat|number,...)] Has the value of the highest component.
46 [highest[n](stat|number,...)] Has the value of the highest n components.
47 [lowest(stat|number,...)] Has the value of the lowest component.
48 [lowest[2](stat|number,...)] Has the value of the lowest n components.
e8f2c14 Jon Added README
authored
49
Something went wrong with that request. Please try again.