Skip to content

Instantly share code, notes, and snippets.

@horatiu-udrea
Last active November 12, 2019 09:00
Show Gist options
  • Save horatiu-udrea/e2c086746a1334996e324db34ba2e658 to your computer and use it in GitHub Desktop.
Save horatiu-udrea/e2c086746a1334996e324db34ba2e658 to your computer and use it in GitHub Desktop.
ToyLanguageInterpreter: Create a program from the given statements (unite them with CompundStatements)
public class ProgramUtils
{
private ProgramUtils()
{
}
public static List<Statement> generatePrograms()
{
return List.of(program());
}
private static Statement program()
{
return concatenate(
new VariableDeclaration(new IntType(), "a"),
new AssignStatement("a", new ValueExpression(new IntValue(3))),
new PrintStatement(new VariableExpression("a"))
);
}
private static Statement concatenate(Statement... statements)
{
if (statements.length > 1)
{
return new CompoundStatement(statements[0], concatenate(Arrays.copyOfRange(statements, 1, statements.length)));
}
if (statements.length == 1)
{
return statements[0];
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment