Jordan Earls avatar Jordan Earls committed 04db364

Added opcode splitting logic

Comments (0)

Files changed (3)

   for(int i=0;i<len;i++)
   {
     char c=part[i];
-    if(!(c==' ' || c=='\n' || c=='\r' || c=='\t'))
+    if(!IS_WHITESPACE(c))   //(c==' ' || c=='\n' || c=='\r' || c=='\t'))
     {
       return &part[i];
     }
   return NULL;
 }
 
+static char* opcode_roots[]={
+  "xor",
+  "or",
+  "and",
+  "mv",
+  "call"
+};
+
+/*plan: parse opcode "words" into groups separated by `.`
+ * 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` */
+int lasm_parse_opcode(char* op, char** groups)
+{
+  int pos=0;
+  for(int i=0;i<4;i++)
+  {
+    groups[i][0]='\0'; //set to zero-length strings first
+  }
+  for(int i=0;i<4;i++)
+  {
+    int j;
+    for(j=0;j<4;j++, pos++)
+    {
+      if(IS_WHITESPACE(op[pos]) || op[pos]=='\0')
+      {
+        pos--; //get through next loops
+        break;
+      }
+      if(op[pos]=='.')
+      {
+        pos++;
+        break;
+      }
+      groups[i][j]=op[pos];
+    }
+    groups[i][j]='\0';
+  }
+  return 0;
+}
+
+
 int lasm_parse_part(lasm_parse_state_t* parse, char* part)
 {
   int len=strlen(part);
 
 
 
-
+#define IS_WHITESPACE(c) (c==' ' || c=='\t' || c=='\r' || c=='\n')
 
 //"public" interface
 
 
 void lasm_resolve_labels(lasm_state_t* state);
 int lasm_parse_part(lasm_parse_state_t *parse, char* part);
+int lasm_parse_opcode(char* op, char** groups);
 
 
 #endif

liblasm/unittests/unittests.c

   mu_check(lasm_parse_part(&parse, "             ")==1);
 }
 
+MU_TEST(opcodes_split_properly)
+{
+  char* tmp[4];
+  for(int i=0;i<4;i++)
+  {
+    tmp[i]=malloc(5);
+  }
+  
+  lasm_parse_opcode("or.lt.s.8", tmp);
+  mu_check(strcmp("or", tmp[0])==0);
+  mu_check(strcmp("lt", tmp[1])==0);
+  mu_check(strcmp("s", tmp[2])==0);
+  mu_check(strcmp("8", tmp[3])==0);
+  
+  
+  for(int i=0;i<4;i++)
+  {
+    free(tmp[i]);
+  }
+}
+
 
 
 MU_TEST_SUITE(test_suite) {
     MU_RUN_TEST(correctly_handles_end_of_line);
     MU_RUN_TEST(correctly_handles_beginning_comments);
+    MU_RUN_TEST(opcodes_split_properly);
     
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.