private FirstPassResult HandleCodeFirstPass(
            IExpression <T> expression,
            AssemblyContext <T> assemblyContext)
        {
            Code <T> code = (Code <T>)expression;

            if (code.IsEmpty)
            {
                return(FirstPassResult.Invalid());
            }

            if (HandleBuiltInCode(code, assemblyContext, false))
            {
                return(FirstPassResult.Invalid());
            }

            var paramTypes    = code.Parameters.Select(EAType.GetType).ToArray();
            var templateError = storer.FindTemplate(code.CodeName.Name, paramTypes);

            if (templateError.CausedError)
            {
                assemblyContext.AddError(code, templateError);
                return(FirstPassResult.Invalid());
            }
            else
            {
                var template  = templateError.Result;
                int oldOffset = assemblyContext.CurrentOffset;
                assemblyContext.CurrentOffset += template.GetLengthBytes(code.Parameters);
                return(FirstPassResult.Valid(code, oldOffset, template));
            }
        }
Пример #2
0
        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
                                                                  )));
                        }
                    }
                }
            }
        }