@@ -0,0 +1,1718 @@
+package edu.harvard.seas.cs266.naptime;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0Apublic class Grammar %7B%0A%0A%09public abstract class Expression %7B%0A%09%09private Sexp sexp;%0A%09%09%0A%09%09public Expression(Sexp sexp, String name) throws InvalidSexpException %7B%0A%09%09%09// Make sure this is the correct S-expression%0A%09%09%09if (!sexp.firstAtomEquals(name))%0A%09%09%09%09throw new InvalidSexpException(String.format(%22Expected '%25s', found '%25s'%22, name, sexp.getFirstAtom()));%0A%09%09%09%0A%09%09%09// Store the original S-expression for reference%0A%09%09%09this.sexp = sexp;%0A%09%09%7D%0A%09%09%0A%09%09public String toString() %7B%0A%09%09%09return sexp.toString();%0A%09%09%7D%0A%09%09%0A%09%09public abstract Boolean eval();%0A%09%7D%0A%09%0A%09public static class ExpressionFactory %7B%0A%09%09public final static Grammar grammar = new Grammar();%0A%09%09%0A%09%09public static Expression build(Sexp sexp) throws InvalidSexpException %7B%0A%09%09%09String name = sexp.getFirstAtom();%0A%09%09%09if (name.equals(%22%22))%0A%09%09%09%09throw new InvalidSexpException(%22Expression did not start with atom%22);%0A%09%09%09else if (name.equals(Step.name))%0A%09%09%09%09return grammar.new Step(sexp);%0A%09%09%09else%0A%09%09%09%09throw new InvalidSexpException(String.format(%22Unexpected expression name '%25s'%22, name));%0A%09%09%7D%0A%09%7D%0A%09%0A%09public class Step extends Expression %7B%0A%09%09public final static String name = %22step%22;%0A%09%09%0A%09%09private List%3CExpression%3E steps = new ArrayList%3CExpression%3E();%0A%09%09%0A%09%09public Step(Sexp sexp) throws InvalidSexpException %7B%0A%09%09%09super(sexp, name);%0A%09%09%09%0A%09%09%09for (Object child: sexp.getChildrenAfterFirst()) %7B%0A%09%09%09%09if (child.getClass() == Sexp.class) %7B%0A%09%09%09%09%09steps.add(ExpressionFactory.build((Sexp)child));%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09public Boolean eval() %7B%0A%09%09%09// Evaluate each expression in turn%0A%09%09%09Boolean success = true;%0A%09%09%09for (Expression step: steps) %7B%0A%09%09%09%09success = step.eval() && success;%0A%09%09%09%7D%0A%09%09%09return success;%0A%09%09%7D%0A%09%7D%0A%7D%0A

create initial Expression interface and Step implementation; more expression types to follow