Advertisement
Guest User

execute_code

a guest
Oct 31st, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1.  
  2. public class execute_code {
  3. public static int[] variables=new int[26];//need to initialize to nulls (-10000)
  4. public static int curr_line;
  5. public static String stream;
  6.  
  7. //assumption: st is a valid cmd
  8. public static int determine_type_cmd(String st){
  9. //print
  10. if(st.startsWith("print(") && st.endsWith(")")){
  11. return 4;
  12. }
  13. //goto
  14. if(st.startsWith("goto ")){
  15. return 3;
  16. }
  17. if(st.startsWith("if(")){
  18. return 2;
  19. }
  20. //else - assignment
  21. return 1;
  22. }
  23.  
  24. public static int run_line(String st, int cmd){
  25. cmd(st.substring(st.indexOf(':')+2, st.length()-2),cmd);
  26. return curr_line;
  27. }
  28.  
  29. public static void cmd(String st, int cmd){
  30. curr_line=Integer.MIN_VALUE;
  31. switch(cmd){
  32. case(1): assignment_cmd(st, variables); break;
  33. case(2): if_cmd(st); break;
  34. case(3): curr_line=goto_cmd(st); break;
  35. case(4): print_cmd(st); break;
  36. }
  37. }
  38.  
  39. //#######################################################################
  40.  
  41. public static int compute_exp(String st){
  42. stream=st;
  43. return compute_exp_helper();
  44. }
  45.  
  46. public static int compute_exp_helper(){
  47.  
  48. String token=get_token();
  49. if(myclass.is_Basic_num(token))
  50. return Integer.parseInt(token);
  51.  
  52. if(myclass.is_Basic_var(token)){
  53. int ch=token.charAt(0);
  54. if(variables[ch-'a']==Integer.MAX_VALUE ){
  55. curr_line=Integer.MAX_VALUE;///////////////runtime error
  56. return Integer.MAX_VALUE;
  57. }
  58. else
  59. return variables[ch-'a'];
  60. }
  61.  
  62. //else, it is 'binary exp'
  63. char op=token.charAt(0);
  64. int left_operand, right_operand;
  65.  
  66. left_operand=compute_exp_helper();
  67. right_operand=compute_exp_helper();
  68.  
  69. switch(op){
  70. case('+'):return left_operand+right_operand;
  71. case('-'):return left_operand-right_operand;
  72. case('*'):return left_operand*right_operand;
  73. case('\\'):return left_operand/right_operand;
  74. default: return -1;
  75. }
  76.  
  77. }
  78.  
  79. public static String get_token(){
  80. int pos=stream.indexOf(" ");
  81. String st;
  82.  
  83. if(pos>=0){
  84. st=stream.substring(0,pos);
  85. stream=stream.substring(pos+1);
  86. }
  87.  
  88. else{
  89. st=stream;
  90. stream="";
  91. }
  92. return st;
  93. }
  94.  
  95. public static int goto_cmd(String st){
  96. return Integer.parseInt(st.substring(st.indexOf(" ")+1));
  97. }
  98.  
  99. public static void if_cmd(String st){
  100. boolean cond=compute_boolean_exp(st.substring(3,st.indexOf(')')));
  101. String cmd_st="";
  102.  
  103. if(!cond)
  104. return;
  105.  
  106. cmd_st=st.substring(st.indexOf(')')+2);
  107. cmd(cmd_st, determine_type_cmd(cmd_st));
  108. }
  109.  
  110. static boolean compute_boolean_exp(String st) {
  111. String[] ops={"<=",">=","<",">","==","!="};
  112. int op_pos=0;
  113. int i;
  114. int left_operand,right_operand;
  115.  
  116. i=0;
  117. while(i<6){
  118. op_pos=st.indexOf(ops[i]);
  119. if(op_pos>=0)// we found it
  120. break;
  121. ++i;
  122. }
  123.  
  124. left_operand=compute_exp(st.substring(0,op_pos-1));
  125. right_operand=compute_exp(st.substring(op_pos+2));
  126.  
  127. switch(i){
  128. case(0): return left_operand<=right_operand;
  129. case(1): return left_operand>=right_operand;
  130. case(2): return left_operand<right_operand;
  131. case(3): return left_operand>right_operand;
  132. case(4): return left_operand==right_operand;
  133. case(5): return left_operand!=right_operand;
  134. default: return false;
  135. }
  136. }
  137.  
  138. public static void assignment_cmd(String st, int[] variables){
  139. int index=st.indexOf(" := ");
  140. String var=st.substring(0,index);
  141. String exp=st.substring(index + 4);// 4 is reliant upon " := "
  142. variables[var.charAt(0)-'a']=compute_exp(exp);
  143. }
  144.  
  145. public static void print_cmd(String st){
  146. String exp= st.substring(6,st.length()-1);//6 because of 'print('
  147. int result= compute_exp(exp);
  148. if(result == Integer.MAX_VALUE){
  149. return;
  150. }
  151. Print(compute_exp(exp));
  152. }
  153.  
  154. //#######################################################################
  155.  
  156. public static void Print( int val ) {
  157. System.out.println (""+val);
  158. }
  159.  
  160. public static void initial_Array(){
  161. int i=0;
  162. while(i!=26){
  163. variables[i]=Integer.MAX_VALUE;
  164. ++i;
  165. }
  166.  
  167. }
  168.  
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement