mercredi 30 décembre 2020

Parse and precompile IF statement from file in parser

I have problem with parsing file that contain if statements. Exactly i'm trying to create VM and it working: it compares, prints, declares functions and other. I need a compiler for my VM that will translate my language to vm's bytecode. Programmer will write this:

if(1 == 1){
   if(2 == 2){
      print(test);
   }
}
if(3 == 3){
print(lol);
}

And it will translate to that WITHOUT closing curly braces:

cmp_nequal(1,1):4;
cmp_nequal(2,2):3;
cmp_nequal(3,3):5;
print(test);

Cause we should to inverse our IF statement to simplify it to bytecode. So actually I can translate cmp_nequal (compare not equals) to something about FF SOH DLE bytes, but I need help with parsing language. Me and my colleague are working about if statements about four days and we don't understand how can we parse it. Compiler works as: text -> special parser text (if -> cmp, print -> PRNT) -> bytecode (cmp -> FF SOH, PRNt -> FF FF). How can we exactly parse and compile all if to cmp_nequals statements? (if(x == z) -> cmp_nequals(x,z), if(x > z) -> cmp_less(x,z), if(x < z) -> cmp_more(x,z), if(x != z) -> cmp_equals(x,z)) Can anyone help with parser? Thanks!

Aucun commentaire:

Enregistrer un commentaire