public TokenScanner(IPositionableInputStream input) { this.input = input; this.readTokens = new List <Token>(4096); this.tokenOffset = -1; this.IsAtEnd = false; }
public void Assemble(IPositionableInputStream input, BinaryWriter output, ILog log) { ((Printer)buildInCodes[messagePrinterCode]).PrinterAction = log.AddMessage; ((Printer)buildInCodes[errorPrinterCode]).PrinterAction = log.AddError; ((Printer)buildInCodes[warningPrinterCode]).PrinterAction = log.AddWarning; Context assemblyContext = new Context(); assemblyContext.AddNewScope(); var codes = new List<KeyValuePair<INamed<string>, string[]>>(FirstPass(input, assemblyContext, log)); assemblyContext.Offset = 0; SecondPass(codes, assemblyContext, log, output); }
public void Assemble(IPositionableInputStream input, BinaryWriter output, ILog log) { ((Printer)buildInCodes[messagePrinterCode]).PrinterAction = log.AddMessage; ((Printer)buildInCodes[errorPrinterCode]).PrinterAction = log.AddError; ((Printer)buildInCodes[warningPrinterCode]).PrinterAction = log.AddWarning; Context assemblyContext = new Context(); assemblyContext.AddNewScope(); var codes = new List <KeyValuePair <INamed <string>, string[]> >(FirstPass(input, assemblyContext, log)); assemblyContext.Offset = 0; SecondPass(codes, assemblyContext, log, output); }
public static IEnumerable<Token> Tokenise(IPositionableInputStream input) { while (true) { string line = input.ReadLine(); if (line == null) { break; } foreach (var token in TokeniseLine(line, input.CurrentFile, input.LineNumber)) { yield return token; } yield return new Token(new FilePosition(input.CurrentFile, input.LineNumber, line.Length), TokenType.CodeEnder, "NL"); } yield return new Token(); }
private string[] HandleLabels(IPositionableInputStream input, Context assemblyContext, ILog log, string[] code) { string labelName = code[0].TrimEnd(':'); if (IsValidLableName(labelName)) { assemblyContext.AddLabel(labelName); } else { log.AddError(input.GetErrorString( string.Format("Invalid label name {0}", labelName))); } string[] temp = new string[code.Length - 1]; Array.Copy(code, 1, temp, 0, temp.Length); code = temp; return(code); }
public void Compile(IPositionableInputStream input, TextWriter output, ILog log) { this.log = log; this.offsetHistory = new Stack <int> (); this.protectedRegions = new List <Tuple <int, int> > (); this.scopeStructures = new Dictionary <IExpression <int>, ScopeStructure <int> > (); TokenScanner tokenScanner = new TokenScanner(input); if (!tokenScanner.MoveNext()) { return; } Match <Token> match; IExpression <int> expression = parser.Parse(tokenScanner, out match); if (!match.Success) { log.AddError(match.Error); return; } if (!tokenScanner.IsAtEnd && tokenScanner.Current.Type != TokenType.EndOfStream) { AddNotReachedEnd(tokenScanner.Current); return; } if (log.ErrorCount == 0) { this.currentOffset = 0; // DeclareExternASMCLabels(ExecuteLayoutPass<TextWriter> (expression, null,output), output); ExecuteLayoutPass <TextWriter>(expression, null, output); } if (log.ErrorCount == 0) { this.currentOffset = 0; ExecuteWritePass(output, expression, null); } }
public static IEnumerable <Token> Tokenise(IPositionableInputStream input) { label_1: string line = input.ReadLine(); if (line != null) { using (IEnumerator <Token> enumerator = Tokeniser.TokeniseLine(line, input.CurrentFile, input.LineNumber).GetEnumerator()) { while (enumerator.MoveNext()) { Token token = enumerator.Current; yield return(token); } goto label_1; } } else { yield return(new Token(new FilePosition(input.CurrentFile, input.LineNumber, 0), TokenType.EndOfStream, "END")); } }
public void Assemble(IPositionableInputStream input, BinaryWriter output, ILog log) { var assemblyContext = new AssemblyContext <T>(log, output); var scanner = new TokenScanner(input); if (!scanner.MoveNext()) { return; } Match <Token> match; var tree = parser.Parse(scanner, out match); if (!match.Success) { log.AddError(match.Error);// + " " + inputStream.PeekOriginalLine() return; } if (scanner.IsAtEnd) { log.AddError("Consumed all input. Shouldn't have happened."); return; } if (scanner.Current.Type != TokenType.EndOfStream) { log.AddError(scanner.Current.Position + ": Didn't reach end, currently at " + scanner.Current); return; } assemblyContext.CurrentOffset = (int)output.BaseStream.Position; foreach (var item in FirstPass(tree, assemblyContext)) { assemblyContext.AddCodeData(item.code, item.offset, item.template); } assemblyContext.CurrentOffset = (int)output.BaseStream.Position; SecondPass(tree, assemblyContext); }
private IEnumerable <KeyValuePair <INamed <string>, string[]> > FirstPass(IPositionableInputStream input, Context assemblyContext, ILog log) { while (true) { string line = input.ReadLine(); if (line == null) { break; } string[] code = Nintenlord.Utility.Parser.SplitToParameters(line); if (code.Length > 0) { if (code[0].EndsWith(":")) { code = HandleLabels(input, assemblyContext, log, code); } if (code.Length == 0) { continue; } IBuiltInCode builtIn; if (buildInCodes.TryGetValue(code[0], out builtIn)) { string error; if (builtIn.Matches("Code " + code[0], code.Length - 1, out error)) { var causedError = builtIn.FirstPass(code, assemblyContext); if (causedError) { log.AddError(input.GetErrorString(causedError.ErrorMessage)); } yield return(new KeyValuePair <INamed <string>, string[]>(builtIn, code)); } else { log.AddError(input.GetErrorString(error)); } } else { ICodeTemplate template = codeStorage.FindTemplate(code); if (template != null) { if (assemblyContext.Offset % template.OffsetMod != 0) { log.AddError(input.GetErrorString( string.Format( "Code {0}'s offset {1} is not divisible by {2}", template.Name, assemblyContext.Offset, template.OffsetMod ))); } assemblyContext.Offset += template.GetLengthBytes(code); yield return(new KeyValuePair <INamed <string>, string[]>(template, code)); } else { log.AddError(input.GetErrorString(string.Format( "No code named {0} with {1} parameters found", code[0], code.Length - 1 ))); } } } } }
private string[] HandleLabels(IPositionableInputStream input, Context assemblyContext, ILog log, string[] code) { string labelName = code[0].TrimEnd(':'); if (IsValidLableName(labelName)) { assemblyContext.AddLabel(labelName); } else { log.AddError(input.GetErrorString( string.Format("Invalid label name {0}", labelName))); } string[] temp = new string[code.Length - 1]; Array.Copy(code, 1, temp, 0, temp.Length); code = temp; return code; }
private IEnumerable<KeyValuePair<INamed<string>, string[]>> FirstPass(IPositionableInputStream input, Context assemblyContext, ILog log) { while (true) { string line = input.ReadLine(); if (line == null) break; string[] code = Nintenlord.Utility.Parser.SplitToParameters(line); if (code.Length > 0) { if (code[0].EndsWith(":")) { code = HandleLabels(input, assemblyContext, log, code); } if (code.Length == 0) continue; IBuiltInCode builtIn; if (buildInCodes.TryGetValue(code[0], out builtIn)) { string error; if (builtIn.Matches("Code " + code[0], code.Length - 1, out error)) { var causedError = builtIn.FirstPass(code, assemblyContext); if (causedError) { log.AddError(input.GetErrorString(causedError.ErrorMessage)); } yield return new KeyValuePair<INamed<string>, string[]>(builtIn, code); } else { log.AddError(input.GetErrorString(error)); } } else { ICodeTemplate template = codeStorage.FindTemplate(code); if (template != null) { if (assemblyContext.Offset % template.OffsetMod != 0) { log.AddError(input.GetErrorString( string.Format( "Code {0}'s offset {1} is not divisible by {2}", template.Name, assemblyContext.Offset, template.OffsetMod ))); } assemblyContext.Offset += template.GetLengthBytes(code); yield return new KeyValuePair<INamed<string>, string[]>(template, code); } else { log.AddError(input.GetErrorString(string.Format( "No code named {0} with {1} parameters found", code[0], code.Length - 1 ))); } } } } }
public void Assemble(IPositionableInputStream input, BinaryWriter output) { this.assembler.Assemble(input, output, messageLog); }
public static string GetPositionString(this IPositionableInputStream stream) { return(string.Format("File: {0}, Line: {1}", (object)Path.GetFileName(stream.CurrentFile), (object)stream.LineNumber)); }
public void Assemble(IPositionableInputStream input, BinaryWriter output, ILog log) { assembler.Assemble(input, output, log); }
public void Compile(IPositionableInputStream input, TextWriter output, ILog messageLog) { this.assembler.Compile(input, output, messageLog); }
public static string GetErrorString(this IPositionableInputStream stream, string error) { return(string.Format("{0}: {1}" /*: {2}"*/, (object)stream.GetPositionString(), (object)error /*, (object) stream.PeekOriginalLine()*/)); }
public static string GetErrorString(this IPositionableInputStream stream, CanCauseError error) { return(string.Format("{0}: {1}", (object)stream.GetPositionString(), (object)error.ErrorMessage)); }
public void Assemble(IPositionableInputStream input, BinaryWriter output, ILog messageLog) { this.assembler.Assemble(input, output, messageLog); }