public List <byte> Parse(string code, string workingDirectory) { ParserState state = new ParserState(this); state.WorkingDirectory = workingDirectory; m_Lines = Common.SplitString(code, "\n"); m_CurrentLine = 0; // pass 1: assemble the assembly file into machine code for (int i = 0; i < m_Lines.Count; i++) { string line = m_Lines[i]; m_CurrentLine++; // trim whitespace at tail/end and discard empty lines string stripped = StripComments(line); if (string.IsNullOrEmpty(stripped)) { // do nothing, empty line } else { string currentLine = stripped.Trim(); if (currentLine.Length == 0) { continue; } try { AssembleLine(m_CurrentLine, currentLine, state); } catch (Exception ex) { AddMessageLine($"Line {m_CurrentLine} ({m_Lines[m_CurrentLine].Trim()}): {ex.Message}"); ErrorLine = m_CurrentLine; return(null); } } } // check for open scopes... Scopes.Scope openScope; if (state.Scopes.TryGetOpenScope(out openScope)) { AddMessageLine($"Unclosed scope beginning at line {openScope.StartLine}"); ErrorLine = openScope.StartLine; return(null); } // pass 2: update all labels. try { state.UpdateLabelReferences(); } catch (Exception ex) { AddMessageLine($"Error while updating labels. {ex.Message}"); ErrorLine = m_CurrentLine; return(null); } // return the assembled code! return(state.Code); }
public List<byte> Parse(string code, string workingDirectory) { ParserState state = new ParserState(this); state.WorkingDirectory = workingDirectory; m_Lines = Common.SplitString(code, "\n"); m_CurrentLine = 0; // pass 1: assemble the assembly file into machine code for (int i = 0; i < m_Lines.Count; i++) { string line = m_Lines[i]; m_CurrentLine++; // trim whitespace at tail/end and discard empty lines string stripped = StripComments(line); if (string.IsNullOrEmpty(stripped)) { // do nothing, empty line } else { string currentLine = stripped.Trim(); if (currentLine.Length == 0) continue; try { AssembleLine(m_CurrentLine, currentLine, state); } catch (Exception ex) { AddMessageLine($"Line {m_CurrentLine} ({m_Lines[m_CurrentLine].Trim()}): {ex.Message}"); ErrorLine = m_CurrentLine; return null; } } } // check for open scopes... Scopes.Scope openScope; if (state.Scopes.TryGetOpenScope(out openScope)) { AddMessageLine($"Unclosed scope beginning at line {openScope.StartLine}"); ErrorLine = openScope.StartLine; return null; } // pass 2: update all labels. try { state.UpdateLabelReferences(); } catch (Exception ex) { AddMessageLine($"Error while updating labels. {ex.Message}"); ErrorLine = m_CurrentLine; return null; } // return the assembled code! return state.Code; }