How are programs written in interpreted languages ​​executed if they are never translated into machine language?

Computers can only understand machine language. Then, as happens, translators execute the program directly, without translating it into machine language? For instance:

<?php
    echo "Hello, World!" ;

This is a simple Hello World program written in PHP. How is this done on a machine until the machine knows what it is echo? How does this output what was expected, in this case the string Hello, World !?

+4
source share
2 answers

, PHP, , ( , ), . PHP- :

def execute_program(prog)
  for statement in prog.toplevel_statements:
    execute_statement(statement)

def execute_statement(statement):
  if statement is an echo statement:
    print( evaluate_expression(statement.argument) )
  else if statement is a for loop:
    execute_statement(statement.init)
    while evaluate_expression(statement.condition).is_truthy():
      for inner_statement in statement.body:
        execute_statement(inner_statement)
      execute_statement(statement.increment)
  else if ...

, if-else-if AST, .

, , : " , ..".

, , , , , , "", . CPU , "", - if user_input == rectangle: [code to draw a rectangle] , .

+6

, , , , , . ( , ?).

, , , QA. JSON. JSON - , . . , "" JSON - , "", JSON, , , JSON.

+2

Source: https://habr.com/ru/post/1682117/


All Articles