1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use clap::ArgMatches;
use std::io::Write;
use std::{
    env,
    fmt::{Display, Formatter},
};
use std::{error::Error, fs, fs::File, path::Path};

mod lexer;
pub use lexer::*;

mod preprocessor;
pub use preprocessor::*;

mod parser;
pub use parser::*;

mod output;
pub use output::*;

use kerbalobjects::*;

pub static VERSION: &'static str = "0.9.12";

pub fn run(config: &CLIConfig) -> Result<(), Box<dyn Error>> {

    if !config.file_path.ends_with(".kasm") {
        return Err(format!(
            "Input file must be a KASM file. Found: {}",
            config.file_path
        )
        .into());
    }

    let mut output_path = config.output_path_value.clone();

    // If the output path was not specified
    if output_path == "!" {
        // Create a new string the same as file_path
        output_path = config.file_path.clone();

        // Check if we are only preprocessing or not
        if config.preprocess_only {
            // Replce the extension with _pre.kasm
            output_path.replace_range((output_path.len() - 4).., "_pre.kasm");
        } else {
            // Replace the file extension of .kasm with .ko
            output_path.replace_range((output_path.len() - 4).., "ko");
        }
    } else {
        // Check if we are only preprocessing or not
        if config.preprocess_only {
            if !output_path.ends_with(".kasm") {
                output_path.push_str(".kasm");
            }
        } else {
            if !output_path.ends_with(".ko") {
                output_path.push_str(".ko");
            }
        }
    }

    let mut include_path = config.include_path.clone();

    // If no include path has been specified
    if include_path == "!" {
        // Get it from the current working directory
        let cwd = env::current_dir()?;

        include_path = String::from(cwd.as_path().to_str().unwrap());
    } else {
        if !Path::new(&include_path).is_dir() {
            return Err(KASMError::IncludePathDirectoryError.into());
        }
    }

    // Create all the variables required to perform assembly
    let mut preprocessor = Preprocessor::new(include_path);
    let mut definition_table = DefinitionTable::new();
    let mut macro_table = MacroTable::new();
    let mut label_manager = LabelManager::new();
    let mut input_files = InputFiles::new();
    let settings = PreprocessorSettings {
        expand_definitions: true,
        expand_macros: true,
    };

    input_files.add_file("main");

    // Read the input file
    let main_contents = fs::read_to_string(&config.file_path)?;

    // Create out lexer and lex the main file's tokens
    let mut lexer = Lexer::new();
    let main_tokens = lexer.lex(&main_contents, "main", 0)?;

    // Run preprocessor
    let processed_tokens = preprocessor.process(
        &settings,
        main_tokens,
        &mut definition_table,
        &mut macro_table,
        &mut label_manager,
        &mut input_files,
    )?;

    // If we are just output the preprocessed only
    if config.preprocess_only {
        // If we are, just output that
        let preprocessed = tokens_to_text(&processed_tokens);
        let mut pre_file = File::create(&output_path)?;
        pre_file.write_all(&preprocessed.as_bytes())?;
    }
    // If not
    else {
        // Run the parser
        let output = Parser::parse(processed_tokens, &mut label_manager)?;

        let mut kofile = Generator::generate(output, &mut label_manager)?;

        // Check if an empty comment was specified
        if !config.comment.is_empty() {
            // If it isn't, then check if any comment was specified
            let comment_str = if config.comment != "!" {
                config.comment.to_owned()
            } else {
                format!("Assembled by KASM v{}", VERSION)
            };

            let mut comment_strtab = StringTable::new(".comment");
            // Add the comment as the first and only string
            comment_strtab.add(&comment_str);
            // Add the comment section
            kofile.add_string_table(comment_strtab);
        }

        // Check if a non-empty file name has been specified
        if !config.file.is_empty() {
            // We need to get the file name we will put as the FILE symbol in the object file
            let file_name = if config.file == "!" {
                Path::new(&config.file_path)
                    .file_name()
                    .unwrap()
                    .to_str()
                    .unwrap()
            } else {
                &config.file
            };

            // Create the symbol
            let file_sym = Symbol::new(
                file_name,
                KOSValue::NULL,
                0,
                SymbolInfo::LOCAL,
                SymbolType::FILE,
                0,
            );

            // Add it
            kofile.add_symbol(file_sym);
        }

        // Create a KO file writer
        let mut writer = KOFileWriter::new(&output_path);

        // Actually write the file to disk
        kofile.write(&mut writer)?;
        writer.write_to_file()?;
    }

    Ok(())
}

pub struct CLIConfig {
    pub file_path: String,
    pub output_path_value: String,
    pub preprocess_only: bool,
    pub include_path: String,
    pub comment: String,
    pub file: String,
}

impl CLIConfig {
    pub fn new(matches: ArgMatches) -> CLIConfig {
        CLIConfig {
            file_path: String::from(matches.value_of("INPUT").unwrap()),
            output_path_value: String::from(matches.value_of("output_path").unwrap_or("!")),
            preprocess_only: matches.is_present("preprocess_only"),
            include_path: String::from(matches.value_of("include_path").unwrap_or("!")),
            comment: String::from(matches.value_of("comment").unwrap_or("!")),
            file: String::from(matches.value_of("file").unwrap_or("!")),
        }
    }
}

pub struct InputFiles {
    files: Vec<String>,
}

impl InputFiles {
    pub fn new() -> InputFiles {
        InputFiles { files: Vec::new() }
    }

    /// Adds the file to the internal vector, and returns the file's id
    pub fn add_file(&mut self, file: &str) -> usize {
        self.files.push(String::from(file));

        self.files.len()
    }

    pub fn get_from_id(&self, id: usize) -> String {
        self.files.get(id).unwrap().to_owned()
    }
}

#[derive(Debug)]
pub enum KASMError {
    IncludePathDirectoryError,
}

impl Error for KASMError {}

impl Display for KASMError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            KASMError::IncludePathDirectoryError => {
                write!(f, "Include path must be a directory.")
            }
        }
    }
}