1 /** 2 * Interface for assembling a string of assembly containing into bytecode. 3 * 4 * Copyright © 2016, Eric Scrivner 5 * 6 * License: Subject to the terms of the MIT license, as written in the included 7 * LICENSE.txt file. 8 * Authors: Eric Scrivner 9 */ 10 module phlogiston.assembler.assembler; 11 12 import std.exception; 13 import std..string; 14 15 import phlogiston.assembler.parser; 16 import phlogiston.assembler.scanner; 17 18 /// Error raised if an error is encountered while assembling code. 19 class AssemblerError : Exception { 20 @safe pure nothrow this(string msg, 21 string file = __FILE__, 22 size_t line = __LINE__, 23 Throwable next = null) 24 { 25 super(msg, file, line, next); 26 } 27 } 28 29 /// Interface for parsing assembly code into bytecode 30 class Assembler { 31 /** 32 * This routine takes a string of assembly code and produces the bytecode 33 * corresponding to it. 34 * 35 * Throws: AssemblerError if an error is encountered during assembly. 36 * 37 * Params: 38 * assemblyCode = The assembly code 39 * 40 * Returns: The bytecode resulting from compiling the assembly code. 41 */ 42 public ubyte[] assemble(ref string assemblyCode) { 43 ubyte[] byteRepresentation = cast(ubyte[])assemblyCode.representation; 44 45 Scanner scanner = new Scanner(byteRepresentation); 46 Parser parser = new Parser; 47 48 try { 49 return parser.parse(scanner); 50 } catch(InvalidTokenException ite) { 51 throw new AssemblerError(ite.msg); 52 } catch(ParseError pe) { 53 throw new AssemblerError(pe.msg); 54 } 55 } 56 }