/* assembler.vala * Copyright 2011, Sebastian Reichel * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ const OptionEntry[] option_entries = { {"assemble", 'a', OptionFlags.NO_ARG, OptionArg.CALLBACK, (void*) parse_mode, "use assemble mode", null}, {"disassemble", 'd', OptionFlags.NO_ARG, OptionArg.CALLBACK, (void*) parse_mode, "use disassemble mode", null}, { "", 0, 0, OptionArg.FILENAME_ARRAY, ref files, "input file", "FILE" }, {null} }; enum mode { ASSEMBLE, DISASSEMBLE } static string[] files; static mode m; bool parse_mode(string key, string? val) throws OptionError { switch(key) { case "--assemble": case "-a": m = mode.ASSEMBLE; return true; case "--disassemble": case "-d": m = mode.DISASSEMBLE; return true; default: throw new OptionError.UNKNOWN_OPTION("Unknown Option " + key); } } string assemble(FileStream stream) throws lp5523.ParsingError { string line; string result = ""; /* read until EOF */ while((line = stream.read_line()) != null) { /* skip comment lines */ line = line.strip(); if(line.has_prefix("#")) continue; /* remove inline comments */ line = line.split("#", 2)[0]; line = line.strip(); result += "%04hx".printf(lp5523.assemble(line)); } /* size check */ if(result.length/4 > lp5523.PROGSPACE_SIZE) warning("this does not fit into lp5523 progspace"); result += "\n"; return result; } public string disassemble(FileStream stream) throws lp5523.ParsingError { string line; string result = ""; line = stream.read_line(); if(line == null) return ""; /* remove trailing newline */ if(line.length % 2 == 1 && line.has_suffix("\n")) line = line.substring(0, line.length-1); /* disassemble */ for(int i=0; i