Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class execute_code {
- public static int[] variables=new int[26];//need to initialize to nulls (-10000)
- public static int curr_line;
- public static String stream;
- //assumption: st is a valid cmd
- public static int determine_type_cmd(String st){
- //print
- if(st.startsWith("print(") && st.endsWith(")")){
- return 4;
- }
- //goto
- if(st.startsWith("goto ")){
- return 3;
- }
- if(st.startsWith("if(")){
- return 2;
- }
- //else - assignment
- return 1;
- }
- public static int run_line(String st, int cmd){
- cmd(st.substring(st.indexOf(':')+2, st.length()-2),cmd);
- return curr_line;
- }
- public static void cmd(String st, int cmd){
- curr_line=Integer.MIN_VALUE;
- switch(cmd){
- case(1): assignment_cmd(st, variables); break;
- case(2): if_cmd(st); break;
- case(3): curr_line=goto_cmd(st); break;
- case(4): print_cmd(st); break;
- }
- }
- //#######################################################################
- public static int compute_exp(String st){
- stream=st;
- return compute_exp_helper();
- }
- public static int compute_exp_helper(){
- String token=get_token();
- if(myclass.is_Basic_num(token))
- return Integer.parseInt(token);
- if(myclass.is_Basic_var(token)){
- int ch=token.charAt(0);
- if(variables[ch-'a']==Integer.MAX_VALUE ){
- curr_line=Integer.MAX_VALUE;///////////////runtime error
- return Integer.MAX_VALUE;
- }
- else
- return variables[ch-'a'];
- }
- //else, it is 'binary exp'
- char op=token.charAt(0);
- int left_operand, right_operand;
- left_operand=compute_exp_helper();
- right_operand=compute_exp_helper();
- switch(op){
- case('+'):return left_operand+right_operand;
- case('-'):return left_operand-right_operand;
- case('*'):return left_operand*right_operand;
- case('\\'):return left_operand/right_operand;
- default: return -1;
- }
- }
- public static String get_token(){
- int pos=stream.indexOf(" ");
- String st;
- if(pos>=0){
- st=stream.substring(0,pos);
- stream=stream.substring(pos+1);
- }
- else{
- st=stream;
- stream="";
- }
- return st;
- }
- public static int goto_cmd(String st){
- return Integer.parseInt(st.substring(st.indexOf(" ")+1));
- }
- public static void if_cmd(String st){
- boolean cond=compute_boolean_exp(st.substring(3,st.indexOf(')')));
- String cmd_st="";
- if(!cond)
- return;
- cmd_st=st.substring(st.indexOf(')')+2);
- cmd(cmd_st, determine_type_cmd(cmd_st));
- }
- static boolean compute_boolean_exp(String st) {
- String[] ops={"<=",">=","<",">","==","!="};
- int op_pos=0;
- int i;
- int left_operand,right_operand;
- i=0;
- while(i<6){
- op_pos=st.indexOf(ops[i]);
- if(op_pos>=0)// we found it
- break;
- ++i;
- }
- left_operand=compute_exp(st.substring(0,op_pos-1));
- right_operand=compute_exp(st.substring(op_pos+2));
- switch(i){
- case(0): return left_operand<=right_operand;
- case(1): return left_operand>=right_operand;
- case(2): return left_operand<right_operand;
- case(3): return left_operand>right_operand;
- case(4): return left_operand==right_operand;
- case(5): return left_operand!=right_operand;
- default: return false;
- }
- }
- public static void assignment_cmd(String st, int[] variables){
- int index=st.indexOf(" := ");
- String var=st.substring(0,index);
- String exp=st.substring(index + 4);// 4 is reliant upon " := "
- variables[var.charAt(0)-'a']=compute_exp(exp);
- }
- public static void print_cmd(String st){
- String exp= st.substring(6,st.length()-1);//6 because of 'print('
- int result= compute_exp(exp);
- if(result == Integer.MAX_VALUE){
- return;
- }
- Print(compute_exp(exp));
- }
- //#######################################################################
- public static void Print( int val ) {
- System.out.println (""+val);
- }
- public static void initial_Array(){
- int i=0;
- while(i!=26){
- variables[i]=Integer.MAX_VALUE;
- ++i;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement