Skip to content

Instantly share code, notes, and snippets.

View toptensoftware's full-sized avatar

Brad Robinson toptensoftware

View GitHub Profile
@toptensoftware
toptensoftware / IMemoryBusDebug.cs
Created October 31, 2016 09:37
IMemoryBusDebug
[Flags]
public enum MemoryState : byte
{
Invalid = 0,
Valid = 0x01,
Changed = 0x02,
InvalidUnchanged = 0,
InvalidChanged = Changed,
ValidUnchanged = Valid,
class MyLibrary
{
public MyLibrary()
{
pi = Math.PI;
}
public double pi { get; private set; }
public double r { get; set; }
public class ReflectionContext : IContext
{
public ReflectionContext(object targetObject)
{
_targetObject = targetObject;
}
object _targetObject;
public double ResolveVariable(string name)
class MyFunctionContext : IContext
{
public MyFunctionContext()
{
}
public double ResolveVariable(string name)
{
throw new InvalidDataException($"Unknown variable: '{name}'");
}
// 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)
{
public interface IContext
{
double ResolveVariable(string name);
double CallFunction(string name, double[] arguments);
}
public class NodeFunctionCall : Node
{
public NodeFunctionCall(string functionName, Node[] arguments)
{
class MyContext : IContext
{
public MyContext(double r)
{
_r = r;
}
double _r;
public double ResolveVariable(string name)
// Variable
if (_tokenizer.Token == Token.Identifier)
{
var node = new NodeVariable(_tokenizer.Identifier);
_tokenizer.NextToken();
return node;
}
// 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)
// 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();