private static Expression CreateExpression(ITokenConsumeScope consumer) { using (var scope = consumer.Begin()) { Expression left = null; foreach (var fac in unaryFactories.Concat(operandFactories)) { left = fac(scope); if (left != null) { scope.Commit(); break; } } if (left == null) { return null; } foreach (var func in binaryFactories) { var cmd = func(left, consumer); if (cmd != null) { scope.Commit(); return cmd; } } return left; } }
private LightCommand GenerateCommand(ITokenConsumeScope consumer) { Func<ITokenConsumeScope, LightCommand>[] factories = new Func<ITokenConsumeScope, LightCommand>[] { GenerateToggleCommand, GenerateOffCommand, GenerateOnCommand }; using (var scope = consumer.Begin()) { foreach (var func in factories) { LightCommand cmd = func(consumer); if (cmd != null) { scope.Commit(); return cmd; } } } return null; }
private RouteInfo EatRoute(ITokenConsumeScope scope) { using (var inner = scope.Begin()) { string from, to; int distance; if ( inner.EatLiteral(out from) .EatNamedLiteral("to") .EatLiteral(out to) .Eat(TokenType.Equal) .EatInt(out distance) .Ok()) { inner.Commit(); return new RouteInfo(from, to, distance); } else { return null; } } }
private LightCommand GenerateToggleCommand(ITokenConsumeScope scope) { using (var local = scope.Begin()) { int x1, y1, x2, y2; if ( !local.EatNamedLiteral("toggle") .EatInt(out x1) .EatComma() .EatInt(out y1) .EatNamedLiteral("through") .EatInt(out x2) .EatComma() .EatInt(out y2) .Ok()) { return null; } local.Commit(); return new LightCommand(new Rectangle<int>(new Point2D<int>(x1, y1), new Point2D<int>(x2, y2)), LightCommandType.Toggle); } }
public static VariableExpression CreateVariableExpression(ITokenConsumeScope scope) { using (var local = scope.Begin()) { string variableName; if (local.EatLiteral(out variableName).Ok()) { local.Commit(); return new VariableExpression(variableName); } return null; } }
public Expression CreateExpressions(ITokenConsumeScope consumer) { return CreateExpression(consumer); }
public static Expression CreateBinaryExpression(int bits, Expression left, ITokenConsumeScope scope) { using (var local = scope.Begin()) { string literal; if (left != null && local.EatLiteral(out literal).Ok()) { var right = CreateExpression(local); if (right == null) { return null; } switch (literal) { case "LSHIFT": local.Commit(); return new LeftShiftExpression(bits, left, right); case "AND": local.Commit(); return new AndExpression(bits, left, right); case "OR": local.Commit(); return new OrOperation(bits, left, right); case "RSHIFT": local.Commit(); return new RightShiftExpression(bits, left, right); } } return null; } }
public static Expression CreateNotExpression(int bits, ITokenConsumeScope scope) { using (var local = scope.Begin()) { if (local.EatNamedLiteral("NOT").Ok()) { var exp = CreateExpression(local); if (exp != null) { local.Commit(); return new NotExpression(bits, exp); } } return null; } }
public static AssignStatement CreateAssignExpression(ITokenConsumeScope scope) { using (var local = scope.Begin()) { var left = CreateExpression(local); if (left != null && local.Eat(TokenType.SingleArrowRight).Ok()) { var right = CreateVariableExpression(local); if (right != null) { local.Commit(); return new AssignStatement(left, right); } } return null; } }
public static NumberExpression CreateNumberExpression(int bits, ITokenConsumeScope scope) { using (var local = scope.Begin()) { int variableValue; if (local.EatInt(out variableValue).Ok()) { local.Commit(); return new NumberExpression(bits, variableValue); } return null; } }