Fix up represent() method for expressions

This commit is contained in:
germax26 2024-11-21 10:58:03 +11:00
parent 6f5c5c3f62
commit d62df53703
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM

View File

@ -13,14 +13,14 @@ class TupleTypeExpr(TypeExpression):
types: List[TypeExpression] types: List[TypeExpression]
def represent(self) -> str: def represent(self) -> str:
assert False, ("Unimplemented") return f"({', '.join([type.represent() for type in self.types])})"
@dataclass @dataclass
class ListTypeExpr(TypeExpression): class ListTypeExpr(TypeExpression):
type: TypeExpression type: TypeExpression
def represent(self) -> str: def represent(self) -> str:
assert False, ("Unimplemented") return self.type.represent()+"[]"
@dataclass @dataclass
class ArrayTypeExpr(TypeExpression): class ArrayTypeExpr(TypeExpression):
@ -28,14 +28,14 @@ class ArrayTypeExpr(TypeExpression):
number: int number: int
def represent(self) -> str: def represent(self) -> str:
assert False, ("Unimplemented") return self.type.represent()+f"[{self.number}]"
@dataclass @dataclass
class TypeName(TypeExpression): class TypeName(TypeExpression):
name: str name: str
def represent(self) -> str: def represent(self) -> str:
assert False, ("Unimplemented") return self.name
@dataclass @dataclass
class TypeSpecification(TypeExpression): class TypeSpecification(TypeExpression):
@ -43,7 +43,7 @@ class TypeSpecification(TypeExpression):
types: List[TypeExpression] types: List[TypeExpression]
def represent(self) -> str: def represent(self) -> str:
assert False, ("Unimplemented") return f'{self.type.represent()}<{', '.join([type.represent() for type in self.types])}>'
@dataclass @dataclass
class FunctionTypeExpr(TypeExpression): class FunctionTypeExpr(TypeExpression):
@ -79,6 +79,9 @@ class TypeDeclaration:
name: str name: str
type: TypeExpression type: TypeExpression
def represent(self) -> str:
return f"{self.name}: {self.type.represent()}"
@dataclass @dataclass
class StructDefinition(Statement): class StructDefinition(Statement):
name: str name: str
@ -141,7 +144,7 @@ class Array(Expression):
array: List[Expression] array: List[Expression]
def represent(self) -> str: def represent(self) -> str:
return "["+', '.join(map(str, self.array))+"]" return f"[:{self.element_type.represent()}{''.join([', '+element.represent() for element in self.array])}]"
def precedence(self) -> int: return 13 def precedence(self) -> int: return 13
@ -178,7 +181,7 @@ class TupleExpr(Expression):
elements: List[Expression] elements: List[Expression]
def represent(self) -> str: def represent(self) -> str:
return f"([{', '.join([element.represent() for element in self.elements])}])" return f"({', '.join([element.represent() for element in self.elements])})"
def precedence(self) -> int: return 13 def precedence(self) -> int: return 13
@ -200,7 +203,7 @@ class LoopComprehension(Expression):
array: Expression array: Expression
def represent(self) -> str: def represent(self) -> str:
assert False, ("Unimplemented") return f"[:{self.element_type.represent()}, {self.body.represent()} for {self.variable} in {self.array.represent()}]"
def precedence(self) -> int: return 13 def precedence(self) -> int: return 13
@ -210,7 +213,7 @@ class Lambda(Expression):
expression: Expression expression: Expression
def represent(self) -> str: def represent(self) -> str:
assert False, ("Unimplemented") return f"lambda {', '.join([parameter.represent() for parameter in self.parameters])} => {self.expression.represent()}"
def precedence(self) -> int: return 0 def precedence(self) -> int: return 0