This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Flags] | |
public enum MemoryState : byte | |
{ | |
Invalid = 0, | |
Valid = 0x01, | |
Changed = 0x02, | |
InvalidUnchanged = 0, | |
InvalidChanged = Changed, | |
ValidUnchanged = Valid, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyLibrary | |
{ | |
public MyLibrary() | |
{ | |
pi = Math.PI; | |
} | |
public double pi { get; private set; } | |
public double r { get; set; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReflectionContext : IContext | |
{ | |
public ReflectionContext(object targetObject) | |
{ | |
_targetObject = targetObject; | |
} | |
object _targetObject; | |
public double ResolveVariable(string name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyFunctionContext : IContext | |
{ | |
public MyFunctionContext() | |
{ | |
} | |
public double ResolveVariable(string name) | |
{ | |
throw new InvalidDataException($"Unknown variable: '{name}'"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variable | |
if (_tokenizer.Token == Token.Identifier) | |
{ | |
// Capture the name and skip it | |
var name = _tokenizer.Identifier; | |
_tokenizer.NextToken(); | |
// Parens indicate a function call, otherwise just a variable | |
if (_tokenizer.Token != Token.OpenParens) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IContext | |
{ | |
double ResolveVariable(string name); | |
double CallFunction(string name, double[] arguments); | |
} | |
public class NodeFunctionCall : Node | |
{ | |
public NodeFunctionCall(string functionName, Node[] arguments) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyContext : IContext | |
{ | |
public MyContext(double r) | |
{ | |
_r = r; | |
} | |
double _r; | |
public double ResolveVariable(string name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variable | |
if (_tokenizer.Token == Token.Identifier) | |
{ | |
var node = new NodeVariable(_tokenizer.Identifier); | |
_tokenizer.NextToken(); | |
return node; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Expression context | |
public interface IContext | |
{ | |
double ResolveVariable(string name); | |
} | |
// Represents a variable (or a constant) in an expression. eg: "2 * pi" | |
public class NodeVariable : Node | |
{ | |
public NodeVariable(string variableName) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Identifier - starts with letter or underscore | |
if (char.IsLetter(_currentChar) || _currentChar == '_') | |
{ | |
var sb = new StringBuilder(); | |
// Accept letter, digit or underscore | |
while (char.IsLetterOrDigit(_currentChar) || _currentChar == '_') | |
{ | |
sb.Append(_currentChar); | |
NextChar(); |