Beispiel #1
0
        static int Execute(ConsoleOptions Options)
        {
            try {
                bool abort = false;
                PreprocessorResult preprocessorResult = Parser.Preprocess(Options.InputFilename);

                foreach (CodeError error in preprocessorResult.Errors)
                {
                    ConsoleLogHelper.Output(error);
                    abort = (abort || error.Level >= Options.ErrorLevel);
                }

                if (abort)
                {
                    ConsoleLogHelper.Output("Preprocessing failed! Aborting!", ErrorLevel.Critical);
                    return(1);
                }

                ParserResult parserResult = Parser.Parse(preprocessorResult.CodeLines);

                foreach (CodeError error in parserResult.Errors)
                {
                    ConsoleLogHelper.Output(error);
                    abort = (abort || error.Level >= Options.ErrorLevel);
                }

                if (abort)
                {
                    ConsoleLogHelper.Output("Parsing failed! Aborting!", ErrorLevel.Critical);
                    return(1);
                }

                byte[] bytes = Compiler.Compile(parserResult.Tokens);
                File.WriteAllBytes(Options.OutputFilename, bytes);

                return(0);
            } catch (Exception ex) {
                ConsoleLogHelper.Output("Unhandeled error (please report at https://github.com/microcat-dev/ClawAssembler/issues)!" + ex.Message, ErrorLevel.Critical);
                return(1);
            }
        }
Beispiel #2
0
        public static PreprocessorResult Preprocess(string Filename)
        {
            var codeLines = new List <CodeLine>();
            var defines   = new Dictionary <string, string>();
            var errors    = new List <CodeError>();

            string mainContents = File.ReadAllText(Filename);

            mainContents = mainContents.Replace("\r\n", "\n");
            mainContents = mainContents.Replace("\\\n", "");

            string[] lines = mainContents.Split('\n');

            //var replacementRegexPairs = new Dictionary<Regex, string>();
            //foreach (KeyValuePair<string, string> kv in defines)
            //

            uint lineCount = 1;

            // Remove all whitespace and all comments
            foreach (string line in lines)
            {
                string processedLine = commentRegex.Replace(line, "").Trim();

                //	foreach (KeyValuePair<Regex, string> replacementPair in replacementRegexPairs)
                //		processedLine = replacementPair.Key.Replace(line, replacementPair.Value).Trim();

                if (processedLine != "")
                {
                    if (dataRegex.IsMatch(processedLine))
                    {
                        codeLines.Add(new CodeLine(processedLine, lineCount, Filename)
                        {
                            Type = CodeLine.LineType.Data
                        });
                    }
                    else if (instructionRegex.IsMatch(processedLine))
                    {
                        codeLines.Add(new CodeLine(processedLine, lineCount, Filename)
                        {
                            Type = CodeLine.LineType.Instruction
                        });
                    }
                    else if (labelRegex.IsMatch(processedLine))
                    {
                        codeLines.Add(new CodeLine(processedLine, lineCount, Filename)
                        {
                            Type = CodeLine.LineType.Label
                        });
                    }
                    else if (defineRegex.IsMatch(line) && false)
                    {
                        CodeLine thisLine = new CodeLine(processedLine, lineCount, Filename)
                        {
                            Type      = CodeLine.LineType.Preprocessor,
                            Processed = true
                        };

                        Match  match   = defineRegex.Match(line);
                        string search  = match.Groups[1].Value;
                        string replace = match.Groups[2].Value;
                        replace = (replace != "") ? replace : "1";

                        if (defines.ContainsKey(search))
                        {
                            defines[search] = replace;
                        }
                        else
                        {
                            defines.Add(search, replace);
                        }

                        codeLines.Add(thisLine);
                    }
                    else if (undefineRegex.IsMatch(line) && false)
                    {
                        CodeLine thisLine = new CodeLine(processedLine, lineCount, Filename)
                        {
                            Type      = CodeLine.LineType.Preprocessor,
                            Processed = true
                        };

                        Match  match  = defineRegex.Match(line);
                        string search = match.Groups[1].Value.Trim();

                        if (defines.ContainsKey(search))
                        {
                            defines.Remove(search);
                        }
                        else
                        {
                            errors.Add(new CodeError(CodeError.ErrorType.DefineNotExistant, ErrorLevel.Warning, thisLine));
                        }

                        codeLines.Add(thisLine);
                    }
                    else if (includeRegex.IsMatch(line))
                    {
                        CodeLine thisLine = new CodeLine(processedLine, lineCount, Filename)
                        {
                            Type      = CodeLine.LineType.Preprocessor,
                            Processed = true
                        };
                        codeLines.Add(thisLine);

                        Match  match    = includeRegex.Match(line);
                        string filename = match.Groups[1].Value;

                        if (!File.Exists(filename))
                        {
                            errors.Add(new CodeError(CodeError.ErrorType.IncludeNotFound, ErrorLevel.Error, thisLine));
                        }
                        else
                        {
                            PreprocessorResult includeResult = Preprocess(filename);
                            codeLines.AddRange(includeResult.CodeLines);
                            errors.AddRange(includeResult.Errors);
                        }
                    }
                    else
                    {
                        codeLines.Add(new CodeLine(processedLine, lineCount, Filename)
                        {
                            Type = CodeLine.LineType.Unknown
                        });
                    }
                }
                else
                {
                    codeLines.Add(new CodeLine(line, lineCount, Filename)
                    {
                        Type      = CodeLine.LineType.Empty,
                        Processed = true
                    });
                }

                lineCount++;
            }

            foreach (CodeLine line in codeLines)
            {
                if (line.Type == CodeLine.LineType.Unknown)
                {
                    errors.Add(new CodeError(CodeError.ErrorType.SyntaxError, ErrorLevel.Error, line));
                    line.Processed = true;
                }
            }

            return(new PreprocessorResult(codeLines.ToArray(), errors.ToArray()));
        }