Introducci贸n al desarrollo de int茅rpretes y lenguajes de programaci贸n

1

Aprende a desarrollar lenguajes de programaci贸n con int茅rpretes

2

Desarrolla LPP o Lenguaje de Programaci贸n Platzi

Construcci贸n del lexer o tokenizador

3

驴Qu茅 es an谩lisis l茅xico? Funcionamiento del lexer y tokens

4

Estructura y definici贸n de tokens en Python

5

Lectura de caracteres y tokens

6

Tokens ilegales, operadores de un solo car谩cter y delimitadores

7

Reconocimiento y diferenciaci贸n entre letras y n煤meros

8

Declaraci贸n y ejecuci贸n de funciones

9

Extensi贸n del lexer: condicionales, operaciones y booleanos

10

Operadores de dos caracteres

11

Primera versi贸n del REPL con tokens

Construcci贸n del parser o analizador sint谩ctico

12

驴Qu茅 es un parser y AST?

13

Estructura y definici贸n de nodos del AST en Python

14

Parseo del programa o nodo principal

15

Parseo de assignment statements

16

Parseo de let statements

17

Parseo de errores

18

Parseo del return statement

19

T茅cnicas de parsing y pratt parsing

20

Pruebas del AST

21

Implementaci贸n del pratt parser

22

Parseo de Identifiers: testing

23

Parseo de Identifiers: implementaci贸n

24

Parseo de enteros

25

Prefix operators: negaci贸n y negativos

26

Infix operators y orden de las operaciones: testing

27

Infix operators y orden de las operaciones: implementaci贸n

28

Parseo de booleanos

29

Desaf铆o: testing de infix operators y booleanos

30

Parseo de expresiones agrupadas

31

Parseo de condicionales: testing y AST

32

Parseo de condicionales: implementaci贸n

33

Parseo de declaraci贸n de funciones: testing

34

Parseo de declaraci贸n de funciones: AST e implementaci贸n

35

Parseo de llamadas a funciones: testing y AST

36

Parseo de llamadas a funciones: implementaci贸n

37

Completando los TODOs o pendientes del lexer

38

Segunda versi贸n del REPL con AST

Evaluaci贸n o an谩lisis sem谩ntico

39

Significado de s铆mbolos

40

Estrategias de evaluaci贸n para int茅rpretes de software

41

Representaci贸n de objetos

42

Evaluaci贸n de expresiones: enteros

43

Evaluaci贸n de expresiones: booleanos y nulos

44

Evaluaci贸n de expresiones: prefix

45

Evaluaci贸n de expresiones: infix

46

Evaluaci贸n de condicionales

47

Evaluaci贸n del return statement

48

Manejo de errores

49

Ambiente

50

Bindings

51

Evaluaci贸n de funciones

52

Llamadas a funciones

Mejora del int茅rprete

53

Implementaci贸n de strings

54

Operaciones con strings

55

Built-in functions: objeto y tests

56

Built-in functions: evaluaci贸n

Siguientes pasos

57

Retos para expandir tu int茅rprete

58

Contin煤a con el Curso de Creaci贸n de Compiladores de Software

You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
4 Hrs
43 Min
35 Seg

Evaluaci贸n de condicionales

46/58
Resources

How to understand conditionals in programming?

In the fascinating world of software development, conditionals are fundamental to make decisions within a program. In essence, they are based on evaluating conditions and acting on them, allowing an application to behave in different ways depending on certain circumstances. This concept is crucial in programming and its understanding is essential for any developer who wants to master the basics of programming.

What are code blocks?

Blocks, in the context of programming, are small, grouped segments of code that are executed together. In Platzi, as in other C-derived languages, they are defined by brackets {}. This is because they allow you to conditionally evaluate parts of the code and, later, define the body of a function. In essence, they act as mini-programs that, depending on certain conditions, can be executed or not.

How are tests designed to evaluate conditionals?

To ensure that conditionals work correctly, it is necessary to create specific tests. These tests will allow us to verify:

  • If the condition is true, the consequence is executed.
  • If there is no explicit alternative and the condition is false, the result will be none.
  • Expressions within the conditions must be able to evaluate to a boolean.
  • If there is an alternative and the condition is false, the alternative will be executed, allowing us to check multiple code outputs.

For example, if the condition is 1 < 2, the consequence would be a specific output. In case the condition 1 > 2 is false, the alternative should provide a different output.

def test_if_else_evaluation(): # This is where the different behaviors of the 'if' and 'else' assertions = [ (True, 10), # Consequence if condition is true (False, None) # No alternative if false ] for condition, expected in assertions: assert evaluate_condition(condition) == expected

How is the evaluation of if nodes implemented?

Within a programming language evaluation system, the evaluation of if nodes involves several functions.

  1. Evaluate If Expressions: Evaluates whether the condition is true or false to execute the consequence or alternative.
  2. Is Truthy: Determines whether a value should be considered true. In Platzi, an integer is considered truthy.
def evaluate_if_expression(if_expression): condition = evaluate(if_expression.condition) if is_truthy(condition): return evaluate(if_expression.consequence) elif if_expression.alternative is not None: return evaluate(if_expression.alternative) return Null

What is the role of helper functions?

Helper functions are essential to ensure an effective and organized evaluation. They allow you to modularize the code and handle different functionalities separately. Among them we find Is Truthy, which evaluates the 'truth' of an object using predefined constants.

def is_truthy(obj): if obj == Null: return False elif obj == True: return True elif obj == False: return False return True

In the end, knowing and understanding conditional evaluation in a programming language brings not only a deeper understanding of the language, but also skills to write more efficient and effective code. Continue exploring these and other tools to enrich your path as a developer, keep learning and don't stop!

Contributions 3

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Me encanta que podemos empezar a ponernos creativos con esto jaja
.

.
Por cierto, me di cuenta que en este pedazo de c贸digo:

def _evaluate_statements(statements: List[ast.Statement]) -> Optional[Object]:

    result: Optional[Object] = None

    for statement in statements:

        result = evaluate(statement)

    return result

No importa cu谩ntos statements pasemos, solamente vamos a tomar el valor del 煤ltimo, porque a cada rato se est谩 sobreescribiendo el valor de result, as铆 que actualmente el LPP no puede ejecutar m谩s de un statement 馃

##Una optimizaci贸n de c贸digo.
En este punto, podemos cambiar nuestra funci贸n _evaluate_bang_operator_expression para que simplemente devuelva lo contrario de _is_truthy.
.
En mi caso qued贸 as铆:

def _evaluate_bang_operator_expression(right: Object) -> Object:
    return FALSE if _is_truthy(right) else TRUE

.
De esta forma, as铆, si en alg煤n momento decidimos cambiar la forma en que se qu茅 es verdadero y qu茅 es falso, no tendremos que cambiarlo en ambos lugares. Remember the principle: Don鈥檛 repeat your self.

Lo que veo en este caso es que nuestros If no soportar m煤ltiples condiciones ni tenemos el token And y Or en nuestro lenguaje. Me he puesto a pensar como hacerlo pero todav铆a no lo he implementado.