Section courante

A propos

Section administrative du site

Exemple de code source permettant d'affichage en mode télétypé de différentes couleurs un code source de langage Pascal. Ainsi par exemple des mots réservé comme «Procedure» ou «Begin» sortiront en blanc et en gras tandis que les symboles en cyan par exemple. Voici donc l'exemple montrant le fonctionnement :

  1. <?php
  2. // Bibliothèque de Mentronix
  3. // Tous droits réservés par Gladir.com XXIe siècle
  4. // Programmeur: Sylvain Maltais
  5.  
  6. function ShowTextToPascalHighlighting($Text) {
  7.     echo "<TABLE BGCOLOR=\"#000080\" Width=100%><TR><TD>";
  8.     echo TextToPascalHighlighting($Text);
  9.     echo "</TD></TR></TABLE>";
  10. }
  11.  
  12. function TextToPascalHighlighting($Text) {
  13.     $ResWord = array("AND","ARRAY","ASM","ASSEMBLER","BEGIN","CASE","CONST","CONSTRUCTOR","DESTRUCTOR",
  14.                      "DIV","DO","ELSE","END","FAR","FOR","FUNCTION","IF","IN","INLINE","MOD","NEAR","NIL","NOT",
  15.                      "OBJECT","OF","OR","TO","PROCEDURE","PROGRAM","RECORD","REPEAT","SET","STRING","THEN","TYPE",
  16.                      "UNIT","UNTIL","USES","VAR","WHILE","XOR");
  17.     $ResWordAsm = array("AAA","AAD","AAM","ADC","ADD","AND","CLD","CLI","CMP","DEC","DIV","IN","INC","JA","JB",
  18.                         "JC","JCXZ","JE","JMP","JNB","JNC","JNE","JNZ","JZ","INT","LDS","LES","LODSB","LODSD",
  19.                         "LODSW","LOOP","MOV","MOVSB","MOVSD","MOVSW","MUL","NOT","OUT","OUTSB","OUTSD","OUTSD",
  20.                         "POP","POPF","PUSH","PUSHF","OR","REP","ROL","SHL","SHR","STC","STD","STI","STOSB","STOSD",
  21.                         "STOSW","SBB","SUB","TEST","XCHG","XOR");
  22.     $RegAsm     = array("AH","AL","AX","BH","BL","BP","BX","CH","CL","CX","DI","DH","DL","DS","DX","ES","SI","SP","SS");
  23.     $Output = "";
  24.     $ModeASM = false;
  25.     $Rem = false;
  26.     $Line = explode("\n",$Text);
  27.     for($J=0;$J<count($Line);$J++) {
  28.         $CurrLine = "";
  29.         for($I=0;$I<strlen($Line[$J]);$I++) {
  30.             $Chr = strtoupper($Line[$J][$I]);
  31.             if((($Chr>='A')&&($Chr<='Z'))||($Chr=='_')) {
  32.                 $Word = "";
  33.                 while((($Chr>='A')&&($Chr<='Z'))||(($Chr>='0')&&($Chr<='9'))||($Chr=='_')) {
  34.                     $Word .= $Line[$J][$I];
  35.                     $I++;
  36.                     $Chr = strtoupper($Line[$J][$I]);
  37.                 }
  38.                 $I--;
  39.                 if($ModeASM) {
  40.                     if(strtoupper($Word) == "END") {
  41.                         $ModeASM = false;
  42.                         $CurrLine .= "<B><FONT COLOR=\"#ffffff\">" . $Word . "</FONT></B>";
  43.                     } else {
  44.                         if(array_keys($RegAsm,strtoupper($Word))) {
  45.                             $CurrLine .= "<FONT COLOR=\"#008000\">" . $Word . "</FONT>";
  46.                         } elseif(array_keys($ResWordAsm,strtoupper($Word))) {
  47.                             $CurrLine .= "<FONT COLOR=\"#00FF00\">" . $Word . "</FONT>";
  48.                         } else {
  49.                             $CurrLine .= "<FONT COLOR=\"#ff00ff\">" . $Word . "</FONT>";
  50.                         }
  51.                     }
  52.                 } else {
  53.                     if(array_keys($ResWord,strtoupper($Word))) {
  54.                         $CurrLine .= "<B><FONT COLOR=\"#ffffff\">" . $Word . "</FONT></B>";
  55.                     } else {
  56.                         $CurrLine .= "<FONT COLOR=\"#ff00ff\">" . $Word . "</FONT>";
  57.                     }
  58.                 }
  59.                 if(!$ModeASM) {
  60.                     if(strtoupper($Word) == "ASM") $ModeASM = true;
  61.                 }
  62.             } elseif(($Chr>='0')&&($Chr<='9')) {
  63.                 $Word = "";
  64.                 if($ModeASM) {
  65.                     while((($Chr>='A')&&($Chr<='F'))||(($Chr>='0')&&($Chr<='9'))) {
  66.                         $Word .= $Line[$J][$I];
  67.                         $I++;
  68.                         $Chr = strtoupper($Line[$J][$I]);
  69.                     }
  70.                 } else {
  71.                     while(($Chr>='0')&&($Chr<='9')) {
  72.                         $Word .= $Line[$J][$I];
  73.                         $I++;
  74.                         $Chr = $Line[$J][$I];
  75.                     }
  76.                 }
  77.                 if(strtoupper($Chr)=="H") {
  78.                     $Word .= $Line[$J][$I];
  79.                     $I++;
  80.                 }
  81.                 $I--;
  82.                 $CurrLine .= "<FONT COLOR=\"#7777ff\">" . $Word . "</FONT>";
  83.             } else switch($Line[$J][$I]) {
  84.                 case "$":
  85.                     $Word = $Line[$J][$I];
  86.                     $I++;
  87.                     $Chr = strtoupper($Line[$J][$I]);
  88.                     while((($Chr>='A')&&($Chr<='F'))||(($Chr>='0')&&($Chr<='9'))) {
  89.                         $Word .= $Line[$J][$I];
  90.                         $I++;
  91.                         $Chr = strtoupper($Line[$J][$I]);
  92.                     }
  93.                     $I--;
  94.                     $CurrLine .= "<FONT COLOR=\"#7777ff\">" . $Word . "</FONT>";
  95.                     break;
  96.                 case "@":
  97.                     if($ModeASM) {
  98.                         $Rem = true;
  99.                         $Word = "";
  100.                         while(($Chr!=':')&&($Chr!=' ')&&($Chr!=null)) {
  101.                             $Word .= $Line[$J][$I];
  102.                             $I++;
  103.                             $Chr = strtoupper($Line[$J][$I]);
  104.                         }
  105.                         if($Chr=='}') {
  106.                             $Word .= $Line[$J][$I];
  107.                             $I++;
  108.                         }
  109.                         $I--;
  110.                         $CurrLine .= "<FONT COLOR=\"#ccffff\">" . $Word . "</FONT>";
  111.                         break;
  112.                     }
  113.                 case "(":case ")":case ";":case ":":case "=":case "-":case "+":case ",":case "[":case "]":
  114.                 case ".":case ">":case "<":case "*":case "/":
  115.                     $CurrLine .= "<FONT COLOR=\"#00ffff\">" . $Line[$J][$I] . "</FONT>";
  116.                     break;
  117.                 case "{":
  118.                     $Rem = true;
  119.                     $Word = "";
  120.                     while(($Chr!='}')&&($Chr!=null)) {
  121.                         $Word .= $Line[$J][$I];
  122.                         $I++;
  123.                         $Chr = strtoupper($Line[$J][$I]);
  124.                     }
  125.                     if($Chr=='}') {
  126.                         $Word .= $Line[$J][$I];
  127.                         $I++;
  128.                     }
  129.                     $I--;
  130.                     $CurrLine .= "<FONT COLOR=\"#ffff00\">" . $Word . "</FONT>";
  131.                     break;
  132.                 case "'":
  133.                     $Rem = true;
  134.                     $Word = $Line[$J][$I];
  135.                     $I++;
  136.                     $Chr = strtoupper($Line[$J][$I]);
  137.                     while(($Chr!="'")&&($Chr!=null)) {
  138.                         $Word .= $Line[$J][$I];
  139.                         $I++;
  140.                         $Chr = strtoupper($Line[$J][$I]);
  141.                     }
  142.                     if($Chr=="'") {
  143.                         $Word .= $Line[$J][$I];
  144.                         $I++;
  145.                     }
  146.                     $I--;
  147.                     $CurrLine .= "<FONT COLOR=\"#ff0000\">" . $Word . "</FONT>";
  148.                     break;
  149.                 case " ":
  150.                     $CurrLine .= " ";
  151.                     break;
  152.                 default:
  153.                     $CurrLine .= $Line[$J][$I];
  154.                     break;
  155.             }
  156.         }
  157.         $Output .= $CurrLine . "</BR>";
  158.     }
  159.     return $Output;
  160. }
  161. ?>


Dernière mise à jour : Lundi, le 19 octobre 2009