Skip to content

Commit

Permalink
Update error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tjcampanella committed Apr 25, 2024
1 parent 0709364 commit ebc0b98
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
Binary file modified example-asm/digit
Binary file not shown.
23 changes: 17 additions & 6 deletions example-asm/digit.s
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ print:
add x0, x0, num@PAGEOFF

// Load value from the stack.
ldr x1, [sp], #64
ldr x1, [sp], #16

// Convert number in x1 to its ASCII representation
mov x2, #10 // Base 10
Expand All @@ -28,21 +28,32 @@ convert_loop:
mov x1, x4 // Update x1 with quotient

cmp x1, #0 // Check if quotient is zero
bne convert_loop // If not zero, continue loop
beq zero_done // If quotient is zero, terminate early

cmp x3, #0 // Check if index is zero
beq zero_done // If index is zero, terminate early

b convert_loop // If not zero, continue loop

zero_done:
// Recompute the address of num
adrp x4, num@PAGE
add x4, x4, num@PAGEOFF

mov x1, x4 // Load the correct address into x1
// Find the actual length of the converted number
mov x2, #20 // Length of num

sub x2, x2, x3 // Subtract remaining index from the total length
add x4, x4, x3 // Move the address forward by the remaining index

mov x1, x4 // Load the correct address into x1

mov x0, #1 // 1 = StdOut
mov x2, #20 // Length of num
mov x16, #4 // Unix write system call
svc #0x80 // Call kernel to output the string

// Print newline character
mov x0, #0
adrp x0, newline@PAGE
add x0, x0, newline@PAGEOFF

Expand All @@ -65,11 +76,11 @@ loop:
_start:
// push
ldr x0, =34
str x0, [sp, #-64]!
str x0, [sp, #-16]!

// push
ldr x0, =12345678912345678912
str x0, [sp, #-64]!
str x0, [sp, #-16]!

bl print

Expand Down
Binary file added example-asm/test.txt
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/stack.rorth
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@

10 20 < print // prints 1
100 20 < print // prints 0

2 rot
27 changes: 8 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ fn simulate_program(program: &[Op]) {
}
ip += 1;
}
OpKind::If => {
OpKind::If | OpKind::Do => {
if let Some(a) = stack.pop() {
if a == 1 {
ip += 1;
Expand All @@ -326,17 +326,6 @@ fn simulate_program(program: &[Op]) {
OpKind::While => {
ip += 1;
}
OpKind::Do => {
if let Some(a) = stack.pop() {
if a == 1 {
ip += 1;
} else if let Some(ind) = op.value {
if let Ok(ind) = ind.try_into() {
ip = ind;
}
}
}
}
OpKind::End => {
if let Some(ind) = op.value {
if let Ok(ind) = ind.try_into() {
Expand Down Expand Up @@ -562,7 +551,7 @@ fn compile_program_darwin_arm64(program: &[Op], filename: &str) {
fn main() {

Check warning on line 551 in src/main.rs

View workflow job for this annotation

GitHub Actions / Lint & Test

this function has too many lines (135/100)
let args: Vec<String> = env::args().collect();
if args.len() < 3 {
eprintln!("[ERROR] You have to pass in a mode and a file path.");
eprintln!("ERROR: You have to pass in a mode and a file path.");
eprintln!("Usage: rorth [OPTIONS] <SUBCOMMAND> [ARGS]");
eprintln!(" SUBCOMMAND:");
eprintln!(" sim <file> Simulate the program");
Expand All @@ -583,7 +572,7 @@ fn main() {
} else if &args[2] == "-s" {
silence_flag = true;
} else {
eprintln!("[ERROR] Unknown option: {}", &args[2]);
eprintln!("ERROR: Unknown option: {}", &args[2]);
eprintln!("Usage: rorth [OPTIONS] <SUBCOMMAND> [ARGS]");
eprintln!(" SUBCOMMAND:");
eprintln!(" sim <file> Simulate the program");
Expand All @@ -598,7 +587,7 @@ fn main() {

if args.len() > 4 {
if &args[2] != "-r" && &args[2] != "-s" {
eprintln!("[ERROR] Unknown option: {}", &args[2]);
eprintln!("ERROR: Unknown option: {}", &args[2]);
eprintln!("Usage: rorth [OPTIONS] <SUBCOMMAND> [ARGS]");
eprintln!(" SUBCOMMAND:");
eprintln!(" sim <file> Simulate the program");
Expand All @@ -610,7 +599,7 @@ fn main() {
}

if &args[3] != "-r" && &args[3] != "-s" {
eprintln!("[ERROR] Unknown option: {}", &args[3]);
eprintln!("ERROR: Unknown option: {}", &args[3]);
eprintln!("Usage: rorth [OPTIONS] <SUBCOMMAND> [ARGS]");
eprintln!(" SUBCOMMAND:");
eprintln!(" sim <file> Simulate the program");
Expand Down Expand Up @@ -691,19 +680,19 @@ fn main() {
}

if let Some(err) = res.err() {
eprintln!("[ERROR] Failed to execute compiled program: {err}");
eprintln!("ERROR: Failed to execute compiled program: {err}");
}
}
}
}
}
}
} else {
eprintln!("[ERROR] Unknown mode '{mode}'");
eprintln!("ERROR: Unknown mode '{mode}'");
exit(1);
}
} else {
eprintln!("[ERROR] Cannot read file: {filename}");
eprintln!("ERROR: Cannot read file: {filename}");
exit(1);
}
}

0 comments on commit ebc0b98

Please sign in to comment.