| 197 | } |
| 198 | |
| 199 | pub fn parse(lines: &Vec<&str>) -> Program { |
| 200 | let mut program = Program { |
| 201 | start_address: Operand::Number { value: 0u8.into() }, |
| 202 | blocks: vec![], |
| 203 | }; |
| 204 | |
| 205 | for (source_line, &line) in lines.iter().enumerate() { |
| 206 | let (line, comment) = match line.split_once(";") { |
| 207 | None => (line, None), |
| 208 | Some((line, comment)) => (line, Some(comment)), |
| 209 | }; |
| 210 | |
| 211 | match line.split_once(" ") { |
| 212 | Some(("SADD", address)) => program.start_address = Operand::parse(address), |
| 213 | Some(("ORG", address)) => { |
| 214 | let beginning_address = Operand::parse_number(address).unwrap(); |
| 215 | |
| 216 | let block = OrgBlock { |
| 217 | beginning_address, |
| 218 | lines: vec![], |
| 219 | }; |
| 220 | |
| 221 | program.blocks.push(block); |
| 222 | } |
| 223 | _ => { |
| 224 | let (label, line) = match line.strip_prefix(">") { |
| 225 | None => (None, line), |
| 226 | Some(line) => { |
| 227 | let (label, line) = line.split_once(" ").unwrap(); |
| 228 | (Some(label), line) |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | let even_align = line.trim().starts_with("EVEN"); |
| 233 | |
| 234 | let code = match line.split_once(" ") { |
| 235 | None if line == "" => None, |
| 236 | _ if line.starts_with("EVEN") => None, |
| 237 | _ if line.starts_with("END") => None, |
| 238 | Some(("DATA", value)) => { |
| 239 | let value = Operand::parse(value); |
| 240 | Some(Code::Data { value }) |
| 241 | } |
| 242 | Some(("AS", value)) => { |
| 243 | let data = value.as_bytes().to_vec(); |
| 244 | Some(Code::AsciiString { data, wide: false }) |
| 245 | } |
| 246 | Some(("AW", value)) => { |
| 247 | let data = value.as_bytes().to_vec(); |
| 248 | Some(Code::AsciiString { data, wide: true }) |
| 249 | } |
| 250 | None => { |
| 251 | let instruction = Instruction::parse_string(line).unwrap_or_else(|| { |
| 252 | eprintln!( |
| 253 | "Failed to parse instruction on line {}: '{}'", |
| 254 | source_line + 1, |
| 255 | line |
| 256 | ); |