Commits

Jordan Earls committed 6e61bf9

more full implementation with basic error check

  • Participants
  • Parent commits 04db364

Comments (0)

Files changed (2)

File liblasm/lasm.c

  * Split into array of 4 strings. Afterwards, parse on opcode-by-opcode basis.
  * It's a bit more work, but the best way I can think to do it atm*/
 
-/*assumes groups is populated with enough memory for 4 strings of 4 characters + null terminator. Should accomodate up to `xcal` */
+/*assumes groups is populated with enough memory for 4 strings of 4 characters + null terminator. Should accomodate up to `xcal` 
+  returns position after last opcode character (should point to whitespace or null)
+  returns -1 if error (such as if opcode name is longer than 4 characters
+ */
 int lasm_parse_opcode(char* op, char** groups)
 {
   int pos=0;
   for(int i=0;i<4;i++)
   {
     int j;
+    bool foundend=false;
     for(j=0;j<4;j++, pos++)
     {
       if(IS_WHITESPACE(op[pos]) || op[pos]=='\0')
       {
-        pos--; //get through next loops
+        foundend=true;
         break;
       }
       if(op[pos]=='.')
       {
         pos++;
+        foundend=true;
         break;
       }
       groups[i][j]=op[pos];
     }
+    if(!foundend)
+    {
+      return -1;
+    }
     groups[i][j]='\0';
   }
   return 0;

File liblasm/unittests/unittests.c

   mu_check(strcmp("s", tmp[2])==0);
   mu_check(strcmp("8", tmp[3])==0);
   
-  
+  lasm_parse_opcode("mv.s r0, r1", tmp);
+  mu_check(strcmp("mv", tmp[0])==0);
+  mu_check(strcmp("s", tmp[1])==0);
+  mu_check(strcmp("", tmp[2])==0);
+  mu_check(strcmp("", tmp[3])==0);
+ 
+  mu_check(lasm_parse_opcode("mv.foobar.f r0", tmp) == -1);
   for(int i=0;i<4;i++)
   {
     free(tmp[i]);