Advertisement
ZoriaRPG

ZScript Beam Parser v0.1

Jan 22nd, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.38 KB | None | 0 0
  1. ///Text Parser
  2.  
  3. int instructions[214747];
  4. int command[18];
  5.  
  6.  
  7. int tempbuffer[55];
  8. int outputbuffers[10];
  9.  
  10. void OutputText(int bufferID, int outoutID){
  11.     //copy text from input to output
  12. }
  13.  
  14. void GameText(){
  15.     //string processor
  16.  
  17. }
  18.  
  19. int stack[5]; //holds the four possible command components
  20. int heap[32]; //May not need it, but it might be good for something.
  21. //Perhaps a SAY string?
  22.  
  23. //ASM TYPES
  24. #define ACTION 1;
  25. #define NOUN 2;
  26. #define ADJECTIVE 3;
  27. #define INVALID -1;
  28.  
  29. /*  Do we want to allow constructing very complex chains, such as OLD KEY as two
  30.     compinents, or do we want to hardcode OLD KEY or HIDEOUS TROLL as one string?
  31.  
  32.     Clearly, the latter is easier.
  33. */
  34.  
  35. //ASM DEFINE
  36. #define NORTH 1;
  37. #define EAST 2;
  38. #define WEST 3;
  39. #define SOUTH 4;
  40. #define UP 5;
  41. #define DOWN 6;
  42. #define LOOK 7;
  43. #define TAKE 8;
  44. #define WAIT 9;
  45. #define ASK 10;
  46. #define SAY 11;
  47. #define ATTACK 12;
  48. #define USE 13;
  49. #define EQUIP 14;
  50.  
  51. int matchaction(int command){
  52.    
  53.    
  54. }
  55.  
  56. //Returns how many total WORDS are in one instruction set.
  57. int numinstructions(int _buffer){
  58.     ///count the number of spaces in the buffer. THe number of total instructions
  59.     ///equals the number  of spaces, plus onwe
  60.     int spc; int nextchar; bool trailspacedone;
  61.     INSTRUCTIONS_SIZE = SizeOfArray(_buffer);
  62.     for ( int q = SizeOfArray(_buffer); q >= 0; q-- ) {
  63.         //don't count trailing spaces, or contiguous spaces.
  64.         if ( _buffer[q] == CHAR_SPACE ) {
  65.             if ( __buffer[q-1] !isLetter() && !trailspacedone ) {
  66.                 trailspacedone = false; continue;
  67.             }
  68.             else if ( __buffer[q-1] isLetter() && !trailspacedone ) {
  69.                 trailspacedone = true;
  70.                 spc++; continue;
  71.             }
  72.             else if ( __buffer[q-1] !isLetter() && trailspacedone ) continue;
  73.             else if ( __buffer[q-1] isLetter() && trailspacedone ) {
  74.                 spc++;
  75.             }
  76.         }
  77.     }
  78.     //WORDCOUNT[0] = spc+1; //store the total number of words.
  79.     return spc+1;
  80. }
  81.  
  82. //int WORDS[4];
  83. //holds the indices of each word?
  84.  
  85. #define MAX_WORDS 5; //Max words per instruction. SAY GANDALF GIVE OLD KEY
  86. //Store each word in these buffers.
  87. int WORD_1[18];
  88. int WORD_2[18];
  89. int WORD_3[18];
  90. int WORD_4[18];
  91. int WORD_5[18];
  92. int WordCount; //how many words this time, and the index at which they occur.
  93. int INSTRUCTIONS_SIZE; //Used by functions to minimise loop wastage.
  94.  
  95. //Stores the present commands in the text parsing buffer into command slots.
  96. void GetInstructions(int _buffer){
  97.     int wordcount = numinstructions(_buffer);
  98.     int WORDS[5]={WORD_1, WORD_2, WORD_3, WORD4, WORD_5}; //holds the array pointers.
  99.     //This time, read the array forward.
  100.     int spc; int nextchar; bool trailspacedone; int curword = 0;
  101.    
  102.     for ( int q = 0; q < INSTRUCTIONS_SIZE; q++ ) {
  103.         //don't count trailing spaces, or contiguous spaces.
  104.         if ( curword < wordcount ) {
  105.             do {
  106.                 int ptr = WORDS[curword];
  107.                 ptr[q] = _buffer[q];
  108.             }
  109.             while ( _buffer[q] != CHAR_SPACE && spc < wordcount );
  110.             curword++;
  111.         }
  112.     }
  113.        
  114. }
  115.  
  116.  
  117. //Add instructions to the 'stack', starting with a COMMAND, then its variables.
  118. void StringToCommand(){
  119.     int buf[3] = "0"; int  buf2[2];
  120.     //one string per command
  121.    
  122.     //command lengths
  123.     //this speeds up matching the string to a command by finding one of the same length
  124.     int commandlengths[]={5, 4, 4, 5, 2, 4, 4, 4, 4, 4, 3, 3, 6, 3}; //list the length of each command in order.
  125.     //match the
  126.    
  127.     int legalcommands[]="
  128.         01NORTH
  129.         2EAST
  130.         3WEST
  131.         4SOUTH
  132.         5UP
  133.         6DOWN
  134.         7LOOK
  135.         8TAKE
  136.         9WAIT
  137.         10ASK
  138.         11SAY
  139.         12ATTACK
  140.         13USE
  141.     ";
  142.     //We then check the length of the command
  143.    
  144.     /* We probably don't need a switch for this. Just do:
  145.     */
  146.    
  147.     //Do we want a switch for this?
  148.     //switch (INSTRUCTIONS_SIZE){
  149.     //  case 1:
  150.             //COMMAND only, no verb, no say.
  151.    
  152.     //Parse WORD_1
  153.    
  154.     int curWord = 0;
  155.     int WORDS[5]={WORD_1, WORD_2, WORD_3, WORD4, WORD_5}; //holds the array pointers.
  156.    
  157.     //First, the command
  158.     strcpy(WORDS[curWord], temp_command); //store the present word
  159.    
  160.     for ( int q = 0; q < SizeOfArray(commandlengths); q++ ) {
  161.         bool match;
  162.         if ( commandlengths[q] == strlen(temp_command) {
  163.             //if the position of q is < 10, append a leading 0
  164.             if ( q < 10 ) {
  165.                 itoa(q,buf2);
  166.                 strcat(buf2, but);
  167.             }
  168.             //otherwise, just store it
  169.             itoa(q, buf); //overwrite the temp buffer.
  170.            
  171.             //find that number in legal commands
  172.             for ( int w = 0; 2 < SizeOfArraylegalcommands; w++ ){
  173.                
  174.                 //! stringparser finds a matching string, and returns its position?
  175.                    
  176.                 //if ( stringparser(buf) ) return q; //if the string matches
  177.                
  178.                 //This is the inverse of the old string parser, so instead, let's do this
  179.                
  180.                 int bufcompare[18];
  181.                 stringparser(q, bufcompare); //copy instruction q into the compare buffer
  182.                 for ( int e = 0; e < strlen(bufcompare); e++ ) {
  183.                     //compare each index
  184.                     if ( temp_command[e] != bufcompare[3] ) {
  185.                         //failed to compare
  186.                         match = false;
  187.                         break;
  188.                     }
  189.                     match = true; //we found the match.
  190.                     //return atoi(buf); //return the command ID.
  191.                    
  192.                    
  193.                    
  194.                     //! no, we want to store the commcomamnd, to parse,
  195.                     //! and add variables to it.
  196.                     stack[curWord] = atoi(buf);
  197.                     break;
  198.                 }
  199.                
  200.                 //the parsed string, thast is its command number.
  201.                 //so, return it.
  202.                  
  203.             }
  204.             break; //if we fail, we fall through.
  205.         }
  206.         if ( !match ) stack[curWord] = INVALID; //return INVALID; //invalid command
  207.         //!! no, put INVALID into the stack.
  208.     }
  209.     curWord++; //Increase the SP.
  210.    
  211.     int legal_variables="
  212.    
  213.    
  214.    
  215.    
  216.    
  217.     ";
  218.    
  219.     //Then, its VARIABLES, if any.
  220.     for ( int q = 1; q < WordCount; q++ ) {
  221.         strcpy(WORDS[curWord], temp_command); //store the present word
  222.    
  223.         for ( int q = 0; q < SizeOfArray(commandlengths); q++ ) {
  224.             bool match;
  225.             if ( commandlengths[q] == strlen(temp_command) {
  226.                 //if the position of q is < 10, append a leading 0
  227.                 if ( q < 10 ) {
  228.                     itoa(q,buf2);
  229.                     strcat(buf2, but);
  230.                 }
  231.                 //otherwise, just store it
  232.                 itoa(q, buf); //overwrite the temp buffer.
  233.                
  234.                 //find that number in legal commands
  235.                 for ( int w = 0; 2 < SizeOfArraylegalcommands; w++ ){
  236.                    
  237.                     //! stringparser finds a matching string, and returns its position?
  238.                        
  239.                     //if ( stringparser(buf) ) return q; //if the string matches
  240.                    
  241.                     //This is the inverse of the old string parser, so instead, let's do this
  242.                    
  243.                     int bufcompare[18];
  244.                     stringparser(q, bufcompare); //copy instruction q into the compare buffer
  245.                     for ( int e = 0; e < strlen(bufcompare); e++ ) {
  246.                         //compare each index
  247.                         if ( temp_command[e] != bufcompare[3] ) {
  248.                             //failed to compare
  249.                             match = false;
  250.                             break;
  251.                         }
  252.                         match = true; //we found the match.
  253.                         //return atoi(buf); //return the command ID.
  254.                        
  255.                        
  256.                        
  257.                         //! no, we want to store the commcomamnd, to parse,
  258.                         //! and add variables to it.
  259.                         stack[curWord] = atoi(buf);
  260.                         break;
  261.                     }
  262.                    
  263.                     //the parsed string, thast is its command number.
  264.                     //so, return it.
  265.                      
  266.                 }
  267.                 break; //if we fail, we fall through.
  268.             }
  269.             if ( !match ) stack[curWord] = INVALID; //return INVALID; //invalid command
  270.             //!! no, put INVALID into the stack.
  271.         }
  272.         currWord++; //Increase the SP.
  273.     }
  274. }
  275.        
  276.    
  277. void doaction(int action){
  278.     //get actions off the stack, and pop them
  279.     int tempheap[5]; int command;
  280.     for ( int q = 0; q <= MAX_WORDS; q++ ){
  281.         hempheap[q] = stack[q];
  282.         stack[q] = 0; //popped
  283.     }
  284.    
  285.     switch(NUMWORDS){
  286.         case 1:
  287.            
  288.             //actions only
  289.             command = tempheap[0];
  290.            
  291.             switch(command){
  292.                
  293.                
  294.                
  295.             }
  296.    
  297.        
  298.         case 2:
  299.            
  300.             //action and noun
  301.            
  302.             switch(command){
  303.                 int noun = tempheap[1];
  304.                 case 1:
  305.                     switch(noun){
  306.                        
  307.                         case 1:
  308.                            
  309.                     }
  310.                 }
  311.             }
  312.        
  313.         case 3: //command, and two words
  314.             //can be 'command adjective noun'
  315.        
  316.             switch(command){
  317.                
  318.                 case 1:
  319.                    
  320.                 int adj = tempstack[1];
  321.                 switch(adj){
  322.                    
  323.                     case 1:
  324.                         int noun = tempstack[2];
  325.                        
  326.                         switch(noun){
  327.                             case 1:
  328.                         }
  329.                 }
  330.                
  331.                
  332.                
  333.             }
  334.            
  335.         case 4: //command and three words
  336.             /* this is where things become quite complex, as this includes the SAY
  337.                 instruction. In fact... I believe that 4 must alays be the say instruction.
  338.                 //! Don;t forget 'CARRY'
  339.             */
  340.        
  341.             switch (command){
  342.                 case SAY:
  343.                     int person = tempstack[1];
  344.                     case (person){
  345.                         case 1:
  346.                             int action = tempstack[2];
  347.                             switch(action){
  348.                                 int noun = tempstack[3];
  349.                                 case 1:
  350.                                     switch(noun){
  351.                                         case 1:
  352.                                     }
  353.                             }
  354.                     }
  355.                 default:
  356.                     //not the say command, return an error.
  357.             }
  358.            
  359.         case 5:
  360.             //say, person, action, adjective, noun
  361.         //! Don;t forget 'CARRY'
  362.         switch (command){
  363.                 case SAY:
  364.                     int person = tempstack[1];
  365.                     case (person){
  366.                         case 1:
  367.                             int action = tempstack[2];
  368.                             switch(action){
  369.                                 int adj = tempstack[3];
  370.                                 case 1:
  371.                                     switch(adj){
  372.                                         int noun = tempstack[4];
  373.                                        
  374.                                         case 1:
  375.                                             switch(noun){
  376.                                                 case 1:
  377.                                             }
  378.                                            
  379.                                     }
  380.                             }
  381.                     }
  382.                 default:
  383.                     //not the say command, return an error.
  384.             }
  385.            
  386.     switch(action){
  387.        
  388.         case :
  389.            
  390.         default:
  391.             break;
  392.     }
  393. }
  394.  
  395. //ASM VARIABLES
  396.  
  397. //NOUNS
  398. //Items
  399. #define KEY 1;
  400. #define MAP 2;
  401. #define GOLD 3;
  402. #define SWORD 4;
  403. #define TORCH 5;
  404. #define DAGGER 6;
  405. #define ME 7;
  406.  
  407. //NPCs (person)
  408. #define GANDALF 1;
  409. #define THORIN 2;
  410. #define BARD 3;
  411. #define BEORN 4;
  412. #define ELROND 5;
  413.  
  414. //Enemies
  415. #define TROLL 1;
  416. #define GOBLIN 2;
  417. #define GOLLUM 3;
  418. #define SMAUG 4;
  419.  
  420. //ADJECTIVES
  421. #define STRANGE 1;
  422. #define OLD 2;
  423. #define UNUSUAL 3;
  424. #define GOLDEN 4;
  425. #define UGLY 5;
  426. #define MAGICAL 6;
  427. #define HIDEOUS 7;
  428. #define SHINY 8;
  429.  
  430.  
  431. //Parse types, based on number of commands issued
  432. #define COMMAND 1;
  433. #define VERB 2;
  434. #define NOUN 3;
  435. #define SPEAK 4;
  436.  
  437. //Constructs
  438. //ACTION
  439. //ACTION NOUN
  440. //ACTION VERB NOUN
  441. //SAY NPC SPEAK
  442.  
  443. //SPEAK
  444. //ONE WORD
  445.  
  446. //TWO WORDS
  447. //"GIVE" item
  448.     //item NOUN
  449.     //item VERB NOUN
  450. //"CARRY" item, person
  451.     //item NOUN, person
  452.     //item VERB NOUN, person
  453. //"ATTACK"  npc
  454.     //attack, NPC
  455.     //attack VERB NPC
  456.  
  457.  
  458.  
  459. bool Parse(int inputBuffer){
  460.     int
  461.    
  462.    
  463.    
  464. /*
  465.     switch (strlen(temp_command)){
  466.         int buf[3] = "0"; int  buf2[2];
  467.        
  468.         case 3:
  469.            
  470.             //check commands with a length of 3...
  471.             for ( int q = 0; q < SizeOfArray(commandlengths); q++ ) {
  472.                 if ( commandlengths[q] == 3 ) {
  473.                     //if the position of q is < 10, append a leading 0
  474.                     if ( q < 10 ) {
  475.                         itoa(q,buf2);
  476.                         strcat(buf2, but);
  477.                     }
  478.                     //otherwise, just store it
  479.                     itoa(q, buf); //overwrite the temp buffer.
  480.                    
  481.                     //find that number in legal commands
  482.                     for ( int w = 0; 2 < SizeOfArraylegalcommands; w++ ){
  483.                         if ( stringparser(buf) ) return q; //if the string matches
  484.                             //the parsed string, thast is its command number.
  485.                         //so, return it.
  486.                     }
  487.                     break; //if we fail, we fall through.
  488.                 }
  489.                 break; //fall though
  490.             }
  491.         case 4:
  492.             //check commands with a length of 3.
  493.             for ( int q = 0; q < SizeOfArray(commandlengths); q++ ) {
  494.                 if ( commandlengths[q] == 4 ) {
  495.                     //if the position of q is < 10, append a leading 0
  496.                     if ( q < 10 ) {
  497.                         itoa(q,buf2);
  498.                         strcat(buf2, but);
  499.                     }
  500.                     //otherwise, just store it
  501.                     itoa(q, buf); //overwrite the temp buffer.
  502.                    
  503.                     //find that number in legal commands
  504.                     for ( int w = 0; 2 < SizeOfArraylegalcommands; w++ ){
  505.                         if ( stringparser(buf) ) return q; //if the string matches
  506.                             //the parsed string, thast is its command number.
  507.                         //so, return it.
  508.                     }
  509.                     break; //if we fail, we fall through.
  510.                 }
  511.                 break; //fall though
  512.             }
  513.            
  514.         case 5:
  515.             //check commands with a length of 3.
  516.             for ( int q = 0; q < SizeOfArray(commandlengths); q++ ) {
  517.                 if ( commandlengths[q] == 4 ) {
  518.                     //if the position of q is < 10, append a leading 0
  519.                     if ( q < 10 ) {
  520.                         itoa(q,buf2);
  521.                         strcat(buf2, but);
  522.                     }
  523.                     //otherwise, just store it
  524.                     itoa(q, buf); //overwrite the temp buffer.
  525.                    
  526.                     //find that number in legal commands
  527.                     for ( int w = 0; 2 < SizeOfArraylegalcommands; w++ ){
  528.                         if ( stringparser(buf) ) return q; //if the string matches
  529.                             //the parsed string, thast is its command number.
  530.                         //so, return it.
  531.                     }
  532.                     break; //if we fail, we fall through.
  533.                 }
  534.                 break; //fall though
  535.             }
  536.            
  537.         case 6:
  538.            
  539.         default:
  540.             //invalid command.
  541.     }
  542.     */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement