fixed some bugs with file picker & loading different file types - will start working on brainf##k interpreter tomorrow because a compiler isn't enough.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
|
fs,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::mpsc::Sender,
|
sync::mpsc::Sender,
|
||||||
};
|
};
|
||||||
@@ -147,7 +148,29 @@ impl Editor {
|
|||||||
|
|
||||||
if let Some(path) = &self.path {
|
if let Some(path) = &self.path {
|
||||||
// Save to existing path
|
// Save to existing path
|
||||||
if let Err(why) = std::fs::write(path, &self.text) {
|
self.buffer = self.text.clone();
|
||||||
|
|
||||||
|
let text = if path.extension().is_some_and(|ext| ext == "dsb") {
|
||||||
|
let mut res = Vec::new();
|
||||||
|
for line in self.text.lines() {
|
||||||
|
for line in line.split_whitespace() {
|
||||||
|
match u32::from_str_radix(line, 16) {
|
||||||
|
Ok(num) => res.push(num),
|
||||||
|
Err(e) => {
|
||||||
|
self.error = Some(format!("Failed to parse file: {e}"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.into_iter()
|
||||||
|
.flat_map(u32::to_be_bytes)
|
||||||
|
.collect::<Vec<u8>>()
|
||||||
|
} else {
|
||||||
|
self.text.as_bytes().to_vec()
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(why) = std::fs::write(path, text) {
|
||||||
self.error = Some(format!("Failed to save file: {why}"));
|
self.error = Some(format!("Failed to save file: {why}"));
|
||||||
} else {
|
} else {
|
||||||
self.unsaved = false;
|
self.unsaved = false;
|
||||||
@@ -231,15 +254,39 @@ impl Editor {
|
|||||||
if let Some(dialog) = &mut self.open_file_dialog {
|
if let Some(dialog) = &mut self.open_file_dialog {
|
||||||
if dialog.show(ctx).selected() {
|
if dialog.show(ctx).selected() {
|
||||||
if let Some(file) = dialog.path() {
|
if let Some(file) = dialog.path() {
|
||||||
match std::fs::read_to_string(file) {
|
// check if the file is a binary file
|
||||||
Ok(content) => {
|
if file.extension().is_some_and(|ext| ext == "dsb") {
|
||||||
self.text = content;
|
match std::fs::read(file) {
|
||||||
self.path = Some(file.to_path_buf());
|
Ok(content) => {
|
||||||
self.unsaved = false;
|
let mut res = String::new();
|
||||||
self.error = None;
|
for (i, b) in content.iter().enumerate() {
|
||||||
|
res.push_str(&format!("{b:02x}"));
|
||||||
|
if i % 4 == 3 {
|
||||||
|
res.push('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.text = res.clone();
|
||||||
|
self.buffer = res;
|
||||||
|
self.path = Some(file.to_path_buf());
|
||||||
|
self.unsaved = false;
|
||||||
|
self.error = None;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
self.error = Some(format!("Failed to read file: {e}"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
} else {
|
||||||
self.error = Some(format!("Failed to read file: {e}"));
|
match std::fs::read_to_string(file) {
|
||||||
|
Ok(content) => {
|
||||||
|
self.text = content.clone();
|
||||||
|
self.buffer = content;
|
||||||
|
self.path = Some(file.to_path_buf());
|
||||||
|
self.unsaved = false;
|
||||||
|
self.error = None;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
self.error = Some(format!("Failed to read file: {e}"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -251,7 +298,30 @@ impl Editor {
|
|||||||
if let Some(dialog) = &mut self.save_file_dialog {
|
if let Some(dialog) = &mut self.save_file_dialog {
|
||||||
if dialog.show(ctx).selected() {
|
if dialog.show(ctx).selected() {
|
||||||
if let Some(file) = dialog.path() {
|
if let Some(file) = dialog.path() {
|
||||||
match std::fs::write(file, &self.text) {
|
self.buffer = self.text.clone();
|
||||||
|
|
||||||
|
let content = if file.extension().is_some_and(|ext| ext == "dsb") {
|
||||||
|
let mut res = Vec::new();
|
||||||
|
for line in self.text.lines() {
|
||||||
|
for line in line.split_whitespace() {
|
||||||
|
match u32::from_str_radix(line, 16) {
|
||||||
|
Ok(num) => res.push(num),
|
||||||
|
Err(e) => {
|
||||||
|
self.error =
|
||||||
|
Some(format!("Failed to parse file: {e}"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.into_iter()
|
||||||
|
.flat_map(u32::to_be_bytes)
|
||||||
|
.collect::<Vec<u8>>()
|
||||||
|
} else {
|
||||||
|
self.text.clone().as_bytes().to_vec()
|
||||||
|
};
|
||||||
|
|
||||||
|
match std::fs::write(file, content) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
self.path = Some(file.to_path_buf());
|
self.path = Some(file.to_path_buf());
|
||||||
self.unsaved = false;
|
self.unsaved = false;
|
||||||
@@ -420,6 +490,41 @@ impl Editor {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build(&mut self) {
|
||||||
|
if let Some(path) = &self.path {
|
||||||
|
match path.extension().and_then(|ext| ext.to_str()) {
|
||||||
|
Some("dsa") => {
|
||||||
|
let mut compiler = CompilerEngine::new();
|
||||||
|
compiler.start_compilation(path);
|
||||||
|
|
||||||
|
// Or block until done
|
||||||
|
let instructions = match compiler.wait_for_result() {
|
||||||
|
Ok(instructions) => instructions,
|
||||||
|
Err(e) => {
|
||||||
|
self.error = Some(e.to_string());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.output = instructions
|
||||||
|
.iter()
|
||||||
|
.flat_map(|i| i.encode().to_be_bytes().to_vec())
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
Some("dsb") => {
|
||||||
|
if let Ok(bytes) = fs::read(path) {
|
||||||
|
self.output = bytes;
|
||||||
|
} else {
|
||||||
|
self.error = Some("Failed to read file".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
self.error = Some(format!("Invalid file type: {}", self.filename()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn render_toolbar(&mut self, _state: &mut State, ui: &mut Ui, ctx: &Context) {
|
fn render_toolbar(&mut self, _state: &mut State, ui: &mut Ui, ctx: &Context) {
|
||||||
self.handle_file_dialogs(ctx);
|
self.handle_file_dialogs(ctx);
|
||||||
|
|
||||||
@@ -451,24 +556,7 @@ impl Editor {
|
|||||||
|
|
||||||
// builds the current file
|
// builds the current file
|
||||||
if ui.button("Build").clicked() && !self.unsaved {
|
if ui.button("Build").clicked() && !self.unsaved {
|
||||||
if let Some(path) = &self.path {
|
self.build();
|
||||||
let mut compiler = CompilerEngine::new();
|
|
||||||
compiler.start_compilation(path);
|
|
||||||
|
|
||||||
// Or block until done
|
|
||||||
let instructions = match compiler.wait_for_result() {
|
|
||||||
Ok(instructions) => instructions,
|
|
||||||
Err(e) => {
|
|
||||||
self.error = Some(e.to_string());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
self.output = instructions
|
|
||||||
.iter()
|
|
||||||
.flat_map(|i| i.encode().to_be_bytes().to_vec())
|
|
||||||
.collect();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loads the generated binary into the assembler at the provided offset
|
// Loads the generated binary into the assembler at the provided offset
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
include print "./lib/print.dsa"
|
include print "./lib/print.dsa"
|
||||||
|
|
||||||
dw stack: 0x10000
|
dw stack: 0x10000
|
||||||
db string: "Hello world frfrfr"
|
db string: "Hello world"
|
||||||
|
|
||||||
init:
|
init:
|
||||||
// set up a stack.
|
// set up a stack.
|
||||||
@@ -9,12 +9,10 @@ init:
|
|||||||
mov bpr, spr
|
mov bpr, spr
|
||||||
|
|
||||||
start:
|
start:
|
||||||
// string, rg1
|
lwi string, rg1
|
||||||
lli 87, rg1
|
|
||||||
|
|
||||||
push rg1
|
push rg1
|
||||||
|
call print::print
|
||||||
call print::print_byte
|
|
||||||
pop rg1
|
pop rg1
|
||||||
|
|
||||||
hlt
|
hlt
|
||||||
Reference in New Issue
Block a user