Skip to content

Commit 09e4b68

Browse files
committed
I added assembler and linker
1 parent b16133d commit 09e4b68

5 files changed

Lines changed: 89 additions & 60 deletions

File tree

‎Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[workspace]
22
members = [
33
"crates/cli",
4-
"compiler",
4+
"compiler",
55
]
66

77
[package]
@@ -11,4 +11,4 @@ edition = "2024"
1111

1212
[dependencies]
1313
cli = { path = "./crates/cli"}
14-
compiler = { path = "./compiler"}
14+
compiler = { path = "./compiler"}

‎compiler/src/errors.rs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use validator::errors::ValidatorError;
77
#[derive(Debug, PartialEq)]
88
pub enum BackendError {
99
Qbe,
10-
Asembly,
10+
BinUtils,
1111
}
1212
impl Display for BackendError {
1313
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1414
match self {
1515
BackendError::Qbe => writeln!(f, "QBE doesn't installed"),
16-
BackendError::Asembly => writeln!(f, "binutils doesn't installed"),
16+
BackendError::BinUtils => writeln!(f, "binutils doesn't installed"),
1717
}
1818
}
1919
}
@@ -62,7 +62,3 @@ impl From<ParserError> for CompilerError {
6262
CompilerError::Parser(e)
6363
}
6464
}
65-
66-
67-
68-

‎compiler/src/lib.rs‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
use std::path::{Path, PathBuf};
1+
use std::{
2+
path::{Path, PathBuf},
3+
process::Command,
4+
};
25

36
use parser::parser;
47
use which::which;
58
mod errors;
9+
mod libc_checker;
610
#[cfg(test)]
711
mod tests;
812

@@ -17,21 +21,9 @@ pub fn compiler(path: &str) -> Result<(), CompilerError> {
1721
let (_, program) = validator.validate(parsed_program)?;
1822

1923
which("qbe").map_err(|_| CompilerError::Backend(BackendError::Qbe))?;
20-
which("as").map_err(|_| CompilerError::Backend(BackendError::Asembly))?;
24+
which("as").map_err(|_| CompilerError::Backend(BackendError::BinUtils))?;
25+
which("ld").map_err(|_| CompilerError::Backend(BackendError::BinUtils))?;
26+
let linker = libc_checker::libc_link_checker().expect("Error");
2127

22-
// let main_file: PathBuf = PathBuf::from("main.qbe");
23-
// file_system::write_file(
24-
// &main_file,
25-
// "export function w $main() { # Main function
26-
// @start
27-
// %r =w call $add(w 1, w 1) # Call add(1, 1)
28-
// call $printf(l $fmt, ..., w %r) # Show the result
29-
// ret 0
30-
// }
31-
// "
32-
// .to_string(),
33-
// )?;
34-
//
3528
Ok(())
3629
}
37-

‎compiler/src/libc_checker.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::path::Path;
2+
3+
pub fn libc_link_checker() -> Result<&'static str, &'static str> {
4+
if Path::new("/usr/lib/Scrt1.o").exists()
5+
&& Path::new("/usr/lib/crtn.o").exists()
6+
&& Path::new("/lib64/ld-linux-x86-64.so.2").exists()
7+
{
8+
return Ok("/usr/bin/ld");
9+
// return Ok(format!(
10+
// "
11+
// ld \
12+
// -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
13+
// /usr/lib/Scrt1.o \
14+
// file.o \
15+
// -L/usr/lib \
16+
// -lc \
17+
// azrt.o \
18+
// printf.o \
19+
// /usr/lib/crtn.o \
20+
// -o app
21+
//
22+
// "
23+
// ));
24+
}
25+
Err("In your system has not libc ")
26+
}

‎compiler/src/tests/compiler_test.rs‎

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,57 @@
1-
use crate::{errors::CompilerError, parser};
1+
use std::{path::PathBuf, process::Command};
22

3-
#[test]
4-
fn compiler_binary_op_test() {
5-
let sdk = file_system::read_file("../examples/binary_ops.az");
6-
assert!(sdk.is_ok());
7-
8-
let parsed_program = parser(sdk.unwrap());
9-
assert!(parsed_program.is_ok());
3+
use crate::{errors::CompilerError, libc_checker, parser};
4+
/*
5+
*
6+
* I change my mind, github actions will not test this code.
7+
*
8+
* */
109

11-
let _ = parsed_program.unwrap();
12-
}
1310
#[test]
14-
fn compiler_float_test() {
15-
let sdk = file_system::read_file("../examples/float.az");
16-
assert!(sdk.is_ok());
17-
18-
let parsed_program = parser(sdk.unwrap());
19-
assert!(parsed_program.is_ok());
20-
21-
let _ = parsed_program.unwrap();
11+
fn compiler_output_file() -> Result<(), CompilerError> {
12+
let linker = libc_checker::libc_link_checker().expect("Error");
13+
14+
let main_file: PathBuf = PathBuf::from("main.ssa");
15+
file_system::write_file(
16+
&main_file,
17+
"
18+
function w $add(w %a, w %b) { # Define a function add
19+
@start
20+
%c =w add %a, %b # Adds the 2 arguments
21+
ret %c # Return the result
2222
}
23-
#[test]
24-
fn compiler_condition_test() {
25-
let sdk = file_system::read_file("../examples/if.az");
26-
assert!(sdk.is_ok());
27-
28-
let parsed_program = parser(sdk.unwrap());
29-
assert!(parsed_program.is_ok());
3023
31-
let _ = parsed_program.unwrap();
24+
export function w $main() { # Main function
25+
@start
26+
%r =w call $add(w 1, w 1) # Call add(1, 1)
27+
call $printf(l $fmt, ..., w %r) # Show the result
28+
ret 0
29+
}
30+
data $fmt = { b \"One and one make %d!\n\", b 0 }
31+
"
32+
.to_string(),
33+
)?;
34+
Command::new("qbe")
35+
.args(["-o", "main.s", "main.ssa"])
36+
.status()
37+
.expect("Error");
38+
Command::new("as")
39+
.args(["main.s", "-o", "main.o"])
40+
.status()
41+
.expect("Error");
42+
Command::new(linker)
43+
.args([
44+
"-dynamic-linker",
45+
"/lib64/ld-linux-x86-64.so.2",
46+
"/usr/lib/Scrt1.o",
47+
"main.o",
48+
"-L/usr/lib",
49+
"-lc",
50+
"/usr/lib/crtn.o",
51+
"-o",
52+
"app",
53+
])
54+
.status()
55+
.expect("Error");
56+
Ok(())
3257
}
33-
#[test]
34-
fn compiler_function_test() {
35-
let sdk = file_system::read_file("../examples/square.az");
36-
assert!(sdk.is_ok());
37-
38-
let parsed_program = parser(sdk.unwrap());
39-
assert!(parsed_program.is_ok());
40-
41-
let _ = parsed_program.unwrap();
42-
}

0 commit comments

Comments
 (0)