Last active
November 12, 2019 09:00
-
-
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)
This file contains 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 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