Commits

Jordan Earls committed 3beade1

Fixing argument parsing

Comments (0)

Files changed (2)

   int i=0;
   while(true)
   {
-    char c=*arg;
+    char c=*arg++;
     if(IS_WHITESPACE(c) || c == ']' || c==',' || c==0)
     {
       buffer[8]=0;
   if(skipped[0]=='[')
   {
     buf->indirect=true;
+    skipped=skip_whitespace(&skipped[1], -1);
   }
-  skipped=skip_whitespace(&skipped[1], -1);
   if(IS_NUMBER(skipped[0])) //immediate number
   {
     buf->immediate=true;
     buf->value=strtol(skipped, &skipped, 0);
   }else if(skipped[0]=='$') //label
   {
-    skipped=parse_label(skipped+2, buf->label);
+    skipped=parse_label(skipped+1, buf->label);
   }else{ //register
     buf->reg=true;
     assert_that(skipped[0]=='r');
       buf->label[2]=0;
     }
     buf->label[3]=0;
-    strtol(&buf->label[1], (char**)NULL, 10);
+    buf->value=strtol(&buf->label[1], (char**)NULL, 10);
   }
   return 0;
 }

liblasm/unittests/unittests.c

   tmp[3][0]=0;
   strcpy(tmp[1], "foo");
   mu_check(lasm_handle_opcode(&parse, tmp)==-1);
+}
+
+MU_TEST(properly_parses_arguments)
+{
+  lasm_opcode_argument_t arg;
+  char *tmp="[0x1234], $foo";
+  mu_check(lasm_parse_argument(tmp, &arg)==0);
+  mu_check(arg.value==0x1234);
+  mu_check(arg.immediate==true);
+  mu_check(arg.reg==false);
+  mu_check(arg.indirect==true);
   
+  tmp="  942, [1234]";
+  mu_check(lasm_parse_argument(tmp, &arg)==0);
+  mu_check(arg.value==942);
+  mu_check(arg.immediate==true);
+  mu_check(arg.reg==false);
+  mu_check(arg.indirect==false);
+  
+  tmp="[r12], [1234]";
+  mu_check(lasm_parse_argument(tmp, &arg)==0);
+  mu_check(arg.value==12);
+  mu_check(arg.immediate==false);
+  mu_check(arg.reg==true);
+  mu_check(arg.indirect==true);
+  
+  tmp="r2, [1234]";
+  mu_check(lasm_parse_argument(tmp, &arg)==0);
+  mu_check(arg.value==2);
+  mu_check(arg.immediate==false);
+  mu_check(arg.reg==true);
+  mu_check(arg.indirect==false);
+  
+  tmp="$foobar, [1234]";
+  mu_check(lasm_parse_argument(tmp, &arg)==0);
+  mu_check(arg.immediate==false);
+  mu_check(arg.reg==false);
+  mu_check(arg.indirect==false);
+  mu_check(strcmp("foobar", arg.label)==0);
 }
 
 
     MU_RUN_TEST(correctly_handles_beginning_comments);
     MU_RUN_TEST(opcodes_split_properly);
     MU_RUN_TEST(handle_opcode_creates_proper_state);
+    MU_RUN_TEST(properly_parses_arguments);
     
 }