Skip to content

Commit

Permalink
Added some code samples to the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterderycke committed Oct 15, 2012
1 parent 7c38a26 commit 6e5a5e9
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion README.md
Expand Up @@ -19,4 +19,38 @@ During the optimization phase, the abstract syntax tree is optimized for executi
In this phase the abstract syntax tree is executed in either interpreted mode or in dynamic compilation mode.

## Examples
Jace.NET can be used in a couple of ways
Jace.NET can be used in a couple of ways:

To directly execute a given mathematical function using the provided variables:
```csharp
Dictionary<string, double> variables = new Dictionary<string, double>();
variables.Add("var1", 2.5);
variables.Add("var2", 3.4);

CalculationEngine engine = new CalculationEngine();
double result = engine.Calculate("var1*var2", variables);
```

To build a .NET Func accepting a dictionary as input containing the values for each variable:
```csharp
CalculationEngine engine = new CalculationEngine();
Func<Dictionary<string, double>, double> function = engine.Build("var1+2/(3*otherVariable)");

Dictionary<string, double> variables = new Dictionary<string, double>();
variables.Add("var1", 2);
variables.Add("otherVariable", 4.2);

double result = function(2, 4.2);
```

To build a typed .NET Func:
```csharp
CalculationEngine engine = new CalculationEngine();
Func<int, double, double> function = (Func<int, double, double>)engine.Function("var1+2/(3*otherVariable)")
.Parameter("var1", DataType.Integer)
.Parameter("otherVariable", DataType.FloatingPoint)
.Result(DataType.FloatingPoint)
.Build();

double result = function(2, 4.2);
```

0 comments on commit 6e5a5e9

Please sign in to comment.