public NativeProgram Compile(IEnumerable <string> data) { int lineIndex = 0; NativeProgram program = new NativeProgram(); foreach (string line in data) { foreach (ASMInstruction instruction in line.Split('|').Select(s => ASMInstruction.Parse(s.Trim()))) { if (instruction == null) { continue; } var template = instructionMap.Where(t => t.Accepts(instruction)).OrderBy(t => t.Delay).FirstOrDefault(); int offset = 0; foreach (var map in template.Compile(instruction, lineIndex + 1)) { foreach (var entry in map) { program[lineIndex + offset].Add(entry.Key, entry.Value); } ++offset; } } ++lineIndex; } return(program); }
public static ASMInstruction Parse(string str) { if (string.IsNullOrEmpty(str)) { return(null); } ASMInstruction instruction = new ASMInstruction(); string[] split = str.Split(' '); if (split[0][0] == '+') { instruction.DoesAdd = true; instruction.Token = split[0].Substring(1); } else { instruction.Token = split[0]; } instruction.Parameter = new ASMParameter[split.Length - 1]; for (int i = 0; i < instruction.Parameter.Length; ++i) { instruction.Parameter[i] = ASMParameter.Parse(split[i + 1]); } return(instruction); }
public bool Accepts(ASMInstruction instr) { if (parameter.Length != instr.Parameter.Length || !Token.Equals(instr.Token)) { return(false); } for (int i = 0; i < parameter.Length; i++) { if (!parameter[i].Accepts(instr.Parameter[i])) { return(false); } } return(true); }
public Dictionary <string, int>[] Compile(ASMInstruction instruction, int lineNumber) { Dictionary <string, int>[] result = new Dictionary <string, int> [nativeInstructions.Count]; for (int i = 0; i < result.Length; ++i) { result[i] = new Dictionary <string, int>(); foreach (var entry in nativeInstructions[i]) { string value = entry.Value.Replace("__LINE__", lineNumber.ToString()); for (int j = 0; j < parameter.Length; ++j) { var p = instruction.Parameter[j]; if (p.IsRegister) { value = value.Replace(parameter[j].Token + ".a", p.Value.ToString()); value = value.Replace(parameter[j].Token + ".c", p.Offset.ToString()); value = value.Replace(parameter[j].Token + ".p", "0"); } else if (p.IsConstant) { value = value.Replace(parameter[j].Token + ".a", "0"); value = value.Replace(parameter[j].Token + ".c", p.Value.ToString()); value = value.Replace(parameter[j].Token + ".p", "0"); } else // if (p.IsPointer) { value = value.Replace(parameter[j].Token + ".p", p.Value.ToString()); value = value.Replace(parameter[j].Token + ".a", p.Offset.ToString()); value = value.Replace(parameter[j].Token + ".c", "0"); } } for (int j = 0; j < parameter.Length; ++j) { value = value.Replace(parameter[j].Token, instruction.Parameter[j].Value.ToString()); } result[i].Add(entry.Key, Program.ParseAdditionAndSubstraction(value)); } } return(result); }