private c_value create_value(string input) { c_value result; if (registers.ContainsKey(input[0])) { result = registers[input[0]]; } else { Int64 value; if (Int64.TryParse(input, out value)) { result = new c_value { value = Int64.Parse(input) }; } else { throw new Exception(String.Format("Bad value input '{0}'", input)); } } return(result); }
public void add_instruction(string input) { string[] split_input = input.Split(' '); c_value a = create_value(split_input[1]); c_value b = null; if (split_input.Length > 2) { b = create_value(split_input[2]); } i_instruction instruction; switch (split_input[0]) { case "inp": instruction_groups[instruction_groups_count] = new List <i_instruction>(); instruction_groups_count++; instruction = new c_input_instruction { a = a, alu = this }; break; case "add": instruction = new c_add_instruction { a = a, b = b }; break; case "mul": instruction = new c_multiply_instruction { a = a, b = b }; break; case "div": instruction = new c_divide_instruction { a = a, b = b }; break; case "mod": instruction = new c_modulo_instruction { a = a, b = b }; break; case "eql": instruction = new c_equals_instruction { a = a, b = b }; break; default: throw new Exception(String.Format("Bad instruction input '{0}'", split_input[0])); } instruction_groups[instruction_groups_count - 1].Add(instruction); }
public c_alu() { registers = new Dictionary <char, c_value>(); registers['w'] = new c_value { name = "w", value = 0 }; registers['x'] = new c_value { name = "x", value = 0 }; registers['y'] = new c_value { name = "y", value = 0 }; registers['z'] = new c_value { name = "z", value = 0 }; instruction_groups = new List <i_instruction> [14]; instruction_groups_count = 0; }