-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaBlockMap.cs
More file actions
45 lines (42 loc) · 1.81 KB
/
JavaBlockMap.cs
File metadata and controls
45 lines (42 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
namespace COMP_3951_BlockForge_TechPro
{
/// <summary>
/// Maps workspace blocks into Java syntax templates.
/// </summary>
public static class JavaBlockMap
{
/// <summary>
/// Converts a <see cref="CodeBlock"/> into its Java code fragment.
/// </summary>
/// <param name="block">The block to translate.</param>
/// <returns>The Java code fragment associated with the supplied block.</returns>
/// <exception cref="InvalidOperationException">
/// Thrown when the block type or variable metadata is not supported for Java generation.
/// </exception>
public static string ToJava(CodeBlock block)
{
return block.BlockType switch
{
CodeBlockType.If => "if (condition) {\n}",
CodeBlockType.While => "while (condition) {\n}",
CodeBlockType.Run => "run();",
CodeBlockType.Print => "System.out.println(value);",
CodeBlockType.Equals => "==",
CodeBlockType.Variable => MapVariable(block),
_ => throw new InvalidOperationException($"Unsupported block type: {block.BlockType}")
};
}
private static string MapVariable(CodeBlock block)
{
string variableName = string.IsNullOrWhiteSpace(block.BlockName) ? "variableName" : block.BlockName;
return block.VariableType switch
{
VariableBlockType.String => $"String {variableName} = \"\";",
VariableBlockType.Int => $"int {variableName} = 0;",
VariableBlockType.Bool => $"boolean {variableName} = false;",
_ => throw new InvalidOperationException("Variable blocks require a valid VariableType.")
};
}
}
}