You are working with a toy language grammar that supports three kinds of types: primitives, generics, and tuples. This system is used for defining function types, including parameter and return types.
- Primitive types:
"int","char","float" - Generic types: Strings following the pattern
"T"plus a digit, such as"T1","T2". A generic can stand for any type, including a tuple. - Tuple types: Represented as a list of types, for example
["int", "T1", "char"]. Tuples may be nested, such as["int", "char", ["int", "T1"]].
A Node represents either a primitive, a generic, or a tuple.
A Function is described by its parameter list, params, and its return type, returnType. Both parameters and return type are represented as Node objects.
Your task is to implement the string representation of these types, following the exact format below:
-
Node.toString()returns a string for the type:- For a primitive or generic, return its value directly, such as
"int"or"T2". - For a tuple, return its children in square brackets, separated by commas, such as
"[int,T1,char]". There should be no extra spaces between each child. Nested tuples must be handled recursively.
- For a primitive or generic, return its value directly, such as
-
Function.toString()returns the function type as"[<param1>,<param2>,...] -> <returnType>", where parameters and return type are rendered usingNode.toString().
Constraints:
- All type names are valid.
- The
paramslist and tuple children may be empty. - Nesting is allowed to any depth within tuples.
Example 1:
Input: Node representing primitive type "int". Output: "int"
Example 2:
Input: Node representing tuple ["int", "T1", "char"]. Output: "[int,T1,char]"
Example 3:
Input: Function with params ["int", "T1", ["int", "T2"]] and return type "T1" Output: "[int,T1,[int,T2]] -> T1"
