How are functions declared in Platzi and what elements do they use?
The declaration of functions in the Platzi programming language begins with the keyword procedure
, followed by parentheses with parameters separated by commas. The body of the function is enclosed in square brackets, forming the function node. This concept is similar to languages such as C and JavaScript that use curly braces
to define executable blocks. A key point is that functions can also be passed as parameters within other functions, because they are evaluable expressions.
What tests do we develop for function nodes?
To validate the correct construction of function nodes in an abstract syntax tree (AST), we perform two main tests:
- Node declaration: we check that the node is constructed with the necessary components, such as function body, node type, and appropriate parameters.
- Parameter parsing: We evaluate whether the parameters are parsed correctly, making sure that the parameter list and expressions are accurate.
Example of Test (in pseudocode):
def test_function_literal(): source = "procedure(x, y) { x + y }" lexer = Lexer(source) parser = Parser(lexer) program = parser.parse_program()
assert type(program.statements[0].expression) is Function
func = program.statements[0].expression assert len(func.parameters) == 2 assert func.parameters[0].value == "x" assert func.parameters[1].value == "y"
assert type(func.body.statements[0]) is InfixExpression
Implementing function nodes and their properties
When implementing the function node, we declare the parameter and body properties, using class inheritance to structure and define the internal logic. This is accomplished by extending the Expression
class to form a node that is evaluated as a function.
Example code:
class Function(Expression): def __init__(self, token, parameters=None, body=None): self.token = token self.parameters = parameters if parameters else [] self.body = body
def __str__(self): params = ", ".join(str(p) for p in self.parameters) return f"{self.token.literal}({params}) {str(self.body)}"
What errors and solutions emerge during testing?
During initial testing, possible errors in function recognition are identified in the parser. These errors are resolved by checking the double cast
functions and the correct import of classes and methods. This prepares our system to detect and correct errors early, ensuring a more robust implementation.
Scalable testing and recommendations
Achieving accurate results requires iterating through continuous testing, tweaking the code and ensuring that all possible combinations and exceptions are evaluated. I recommend:
- Implement advanced testing to evaluate behavior in different scenarios.
- Encourage community discussion to improve and optimize tests and solutions.
Testing should always be a central part of any development to ensure code quality. With each challenge solved, we improve not only in technical skill, but also in our problem solving ability. Keep exploring and learning!
Want to see more contributions, questions and answers from the community?