<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using ArkeOS.Hardware.Architecture;
using ArkeOS.Utilities;

namespace ArkeOS.Tools.Assembler {
    public class Assembler {
        private Dictionary<string, ulong> labels;
        private Dictionary<string, ulong> defines;
        private Dictionary<string, ulong> variables;
        private Dictionary<string, ulong> strings;
        private ulong currentOffset;

		public Assembler() {
            this.labels = new Dictionary<string, ulong>();
            this.defines = new Dictionary<string, ulong>();
            this.variables = new Dictionary<string, ulong>();
            this.strings = new Dictionary<string, ulong>();
			this.currentOffset = 0;
		}

		private static string Sanitize(string input) => Regex.Replace(input, @"\s+", " ").Replace("+ ", "+").Replace(" +", "+").Replace("- ", "-").Replace(" -", "-").Replace("* ", "*").Replace(" *", "*");
		
		public byte[] Assemble(string[] inputLines) {
			IEnumerable<string> lines = inputLines.ToList();

			while (this.ProcessIncludes(ref lines))
				;

			lines = lines.Where(l => !string.IsNullOrWhiteSpace(l) && !l.StartsWith(@"//")).Select(l => Assembler.Sanitize(l));

			this.DiscoverDefines(lines);
			this.DiscoverAddresses(lines);

			using (var stream = new MemoryStream()) {
                using (var writer = new BinaryWriter(stream)) {
                    foreach (var line in lines) {
                        var parts = line.Split(' ');

                        this.currentOffset = (ulong)stream.Position / 8;

                        if (parts[0] == "OFFSET") {
                            stream.Seek((long)this.ParseParameter(parts[1], true).Address * 8, SeekOrigin.Begin);<commit_msg>Added support for end of line comments to the assembler.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using ArkeOS.Hardware.Architecture;
using ArkeOS.Utilities;

namespace ArkeOS.Tools.Assembler {
    public class Assembler {
        private Dictionary<string, ulong> labels;
        private Dictionary<string, ulong> defines;
        private Dictionary<string, ulong> variables;
        private Dictionary<string, ulong> strings;
        private ulong currentOffset;

		public Assembler() {
            this.labels = new Dictionary<string, ulong>();
            this.defines = new Dictionary<string, ulong>();
            this.variables = new Dictionary<string, ulong>();
            this.strings = new Dictionary<string, ulong>();
			this.currentOffset = 0;
		}

		private static string Sanitize(string input) => Regex.Replace(input, @"\s+", " ").Replace("+ ", "+").Replace(" +", "+").Replace("- ", "-").Replace(" -", "-").Replace("* ", "*").Replace(" *", "*");
		
		public byte[] Assemble(string[] inputLines) {
			IEnumerable<string> lines = inputLines.ToList();

			while (this.ProcessIncludes(ref lines))
				;

			lines = lines.Select(l => l.Split(new string[] { "//" }, StringSplitOptions.None)[0].Trim()).Where(l => !string.IsNullOrWhiteSpace(l)).Select(l => Assembler.Sanitize(l));

			this.DiscoverDefines(lines);
			this.DiscoverAddresses(lines);

			using (var stream = new MemoryStream()) {
                using (var writer = new BinaryWriter(stream)) {
                    foreach (var line in lines) {
                        var parts = line.Split(' ');

                        this.currentOffset = (ulong)stream.Position / 8;

                        if (parts[0] == "OFFSET") {
                            stream.Seek((long)this.ParseParameter(parts[1], true).Address * 8, SeekOrigin.Begin);
{'fraction_non_alphanumeric': 0.14807897545357523, 'fraction_numerical': 0.002934898612593383, 'mean_word_length': 2.868937048503612, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4999155', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1043, 'n_words': 246}