Advertisement
PifyZ

BootMonkey

Aug 18th, 2012
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.99 KB | None | 0 0
  1. # Définition des variables globales
  2.     opcodes = [
  3.         # fonction             entre  adresse
  4.         ["nop.",                '\0', "\x00\x00"], #
  5.         ["#",                   '\0', "\x00\x01"], #
  6.         ["if",                  '(',  "\x00\x02"], #
  7.         ["elseif",              '(',  "\x00\x03"], #
  8.         ["else",                '\0', "\x00\x04"], #
  9.         ["end.",                '\0', "\x00\x05"], #
  10.         ["continue.",           '\0', "\x00\x06"], #
  11.         ["break.",              '\0', "\x00\x07"], #
  12.  
  13.         ["Program.exit.",       '\0', "\x01\x00"], #
  14.  
  15.         ["Console.clean.",      '\0', "\x02\x01"], #
  16.         ["Console.print",       ' ',  "\x02\x01"], #
  17.         ["Console.println",     ' ',  "\x02\x02"], #
  18.         ["Console.sleep",       ' ',  "\x02\x03"], #
  19.  
  20.         ["String.lower",        '(',  "\x03\x00"], #
  21.         ["String.upper",        '(',  "\x03\x01"], #
  22.         ["String.length",       '(',  "\x03\x02"], #
  23.  
  24.         ["Math.floor",          '(',  "\x04\x00"], #
  25.         ["Math.round",          '(',  "\x04\x01"], #
  26.         ["Math.ceil",           '(',  "\x04\x02"], #
  27.         ["Math.random",         '(',  "\x04\x03"], #
  28.  
  29.         ["Array.in",            '(',  "\x05\x00"], #
  30.         ["Array.empty",         '(',  "\x05\x01"], # Non obligatoire (car on peut utiliser Array.size(tableau) == 0)
  31.         ["Array.size",          '(',  "\x05\x02"], #
  32.  
  33.         ["File.open",           '(',  "\x06\x00"], #
  34.         ["File.write",          '(',  "\x06\x01"], #
  35.         ["File.writeLine",      '(',  "\x06\x02"], #
  36.         ["File.writeFirstLine", '(',  "\x06\x03"], #
  37.         ["File.rewrite",        '(',  "\x06\x04"], #
  38.         ["File.read",           '(',  "\x06\x05"], #
  39.         ["File.readLine",       '(',  "\x06\x06"], #
  40.         ["File.close",          '(',  "\x06\x07"]  #
  41.         ["File.countLine",      '(',  "\x06\x08"]  #
  42.     ]
  43.  
  44.     nbrWarnings = 0
  45.     nbrErrors   = 0
  46.  
  47. # Fonction de compilation
  48.     compile (File Src, File Dst)
  49.         Data = [][]                 # contient le fichier source [ligne][colonne]
  50.         nbrLignes = File.lines(Src) # nombre de lignes du fichier
  51.  
  52.         print "Compilation de \"" . Src . "\" -> \"" . Dst . "\"...\n"
  53.  
  54.         if (File.open(Src, 'r') == null) # Si le fichier source est inaccessible en lecture on affiche une erreur
  55.             exit "Le fichier source \"" . Src . "\" n'a pu être ouvert en lecture."
  56.         end.
  57.  
  58.         for (nbrLignes = 0 to nbrLignes)
  59.             if (File.readline(Src, Data[nbrLignes]) != 1)
  60.                 exit "Le fichier source \"" . Src . "\" n'a pu être lu correctement."
  61.             end.
  62.  
  63.             File.close(fpSrc)
  64.         end.
  65.  
  66.         if (nbrLignes == 1 and Data[0][0] == '\0') # Si le fichier source est vide on affiche une erreur
  67.             exit "Le fichier source \"" . Src . "\" est vide.\n"
  68.         end.
  69.  
  70.         if (File.open(Dst, "wb") == null) # Si le fichier de destination n'a pas pu être ouvert en écriture on affiche une erreur
  71.             exit "Le fichier de destination \"" . Dst . "\" n'a pu être ouvert en écriture."
  72.         end.
  73.  
  74.         for (y = 0 to nbrLignes)
  75.             if (Data[y][0] == '\0')
  76.                 continue.
  77.             end.
  78.  
  79.             for (x = 0 to Array.size(opcodes))
  80.                 if (Array.in(Data[y][x], opcodes))
  81.                     File.write(Dst, [j].code)
  82.                     k = 0
  83.  
  84.                     # ---------------------------- #
  85.                     # début partie en modification #
  86.                     # ---------------------------- #
  87.                     if ([j].nextCar != '\0')
  88.                         if ([j].nextCar == ' ')
  89.                             for (k=strlen (opcodes [j].str) ; Data [i][k]==' ' && k < strlen (Data [i]) ; k++)
  90.                             end.
  91.                         else.
  92.                             for (k=strlen (opcodes [j].str) ; (Data [i][k]!=opcodes [j].nextCar || Data [i][k]==' ') && k < strlen (Data [i]) ; k++)
  93.                             end.
  94.                         end.
  95.  
  96.                         if (k >= strlen (Data [i]))
  97.                             dieOnError ("Instruction invalide dans le fichier %s:%d.", Src, i+1) ;
  98.                             break.
  99.                         end.
  100.                     end.
  101.                     # -------------------------- #
  102.                     # fin partie en modification #
  103.                     # -------------------------- #
  104.                 else.
  105.                     exit "Instruction inconnue : \"" . Data[y] . "\" dans le fichier " . Src . ":" . (y + 1) . "."
  106.                 end.
  107.             end.
  108.         end.
  109.  
  110.         File.close(Dst)
  111.         print "Compilation terminée (" . nbrErrors . " erreur(s) et " . nbrWarnings . " warning(s))"
  112.  
  113.         if (nbrErrors > 0)
  114.             return false
  115.         end.
  116.  
  117.         return true
  118.     end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement