Level 1 Type nodes
A toy language has these types:
- primitives: exactly
int,float,str,bool,char; - generics: any other name, like
T1,T2,S; - tuples: an ordered list of types, possibly nested (and possibly empty).
Implement two classes:
Node(value):valueis either a name (str) or alistofNodes (a tuple). Keep it in an attribute calledvalue.str(node): a name prints as itself; a tuple prints as[+ its children joined by,+], no spaces.node == other: structural equality (same names, same nesting). Comparing with something that isn't aNodeisFalse.
Function(parameters, output_type): a list ofNodes and aNode, kept as attributes with those names.str(function):(+ parameters joined by,+) ->+ the output type.
t = Node([Node("int"), Node([Node("T1"), Node("str")])])
str(t) # "[int,[T1,str]]"
str(Function([t, Node("T1")], Node("T1"))) # "([int,[T1,str]],T1) -> T1"
Node([Node("int")]) == Node("int") # False
No parsing yet: tests build the objects directly.