Skip to main content
added 34 characters in body
Source Link
Erik Eidt
  • 34.4k
  • 6
  • 59
  • 94

You can write a simple interpreter.  Let's make a language that "A" means print Hello and "B" means print Space and "C" means print World, then null means end program.

void interpret ( char *input ) {
   for (;;) {
       switch ( *input++ ) {
       case 'A' : printf ( "Hello" ); break;
       case 'B' : printf ( " " ); break;
       case 'C' : printf ( "World" ); break;
       case 'D' : printf ( "\n" ); break;
       case '\0' : return;
       }
   }
}

...

interpret ( "ABCD" );  // prints "Hello World\n"
interpret ( "CBAD" );  // prints "World Hello\n"

There!  That's an interpreter for a simple language.  More complex language, then more complex interpreter.  There's no need for machine code translation here — except that this C program has to be compiled.

The point is that this interpreter, by just being a regular program, can do what is necessary to perform the operations in the input — it has all it needs to do the work of the input language.

You can write a simple interpreter.  Let's make a language that "A" means print Hello and "B" means print Space and "C" means print World, then null means end program.

void interpret ( char *input ) {
   for (;;) {
       switch ( *input++ ) {
       case 'A' : printf ( "Hello" ); break;
       case 'B' : printf ( " " ); break;
       case 'C' : printf ( "World" ); break;
       case 'D' : printf ( "\n" ); break;
       case '\0' : return;
       }
   }
}

...

interpret ( "ABCD" );  // prints "Hello World\n"
interpret ( "CBAD" );  // prints "World Hello\n"

There!  That's an interpreter for a simple language.  More complex language, then more complex interpreter.  There's no need for machine code translation here — except that this C program has to be compiled.

The point is that this interpreter can do what is necessary to perform the operations in the input — it has all it needs to do the work of the input language.

You can write a simple interpreter.  Let's make a language that "A" means print Hello and "B" means print Space and "C" means print World, then null means end program.

void interpret ( char *input ) {
   for (;;) {
       switch ( *input++ ) {
       case 'A' : printf ( "Hello" ); break;
       case 'B' : printf ( " " ); break;
       case 'C' : printf ( "World" ); break;
       case 'D' : printf ( "\n" ); break;
       case '\0' : return;
       }
   }
}

...

interpret ( "ABCD" );  // prints "Hello World\n"
interpret ( "CBAD" );  // prints "World Hello\n"

There!  That's an interpreter for a simple language.  More complex language, then more complex interpreter.  There's no need for machine code translation here — except that this C program has to be compiled.

The point is that this interpreter, by just being a regular program, can do what is necessary to perform the operations in the input — it has all it needs to do the work of the input language.

Source Link
Erik Eidt
  • 34.4k
  • 6
  • 59
  • 94

You can write a simple interpreter.  Let's make a language that "A" means print Hello and "B" means print Space and "C" means print World, then null means end program.

void interpret ( char *input ) {
   for (;;) {
       switch ( *input++ ) {
       case 'A' : printf ( "Hello" ); break;
       case 'B' : printf ( " " ); break;
       case 'C' : printf ( "World" ); break;
       case 'D' : printf ( "\n" ); break;
       case '\0' : return;
       }
   }
}

...

interpret ( "ABCD" );  // prints "Hello World\n"
interpret ( "CBAD" );  // prints "World Hello\n"

There!  That's an interpreter for a simple language.  More complex language, then more complex interpreter.  There's no need for machine code translation here — except that this C program has to be compiled.

The point is that this interpreter can do what is necessary to perform the operations in the input — it has all it needs to do the work of the input language.