From 051c264a2311606b164275979209044d32e92362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vili=20Sinerv=C3=A4?= Date: Wed, 26 Feb 2025 18:36:03 +0200 Subject: [PATCH] Initial working submission, without errors --- Dockerfile | 11 +++++++++++ src/compiler.rs | 1 + src/compiler/assembler.rs | 11 +++-------- src/server.rs | 7 ++----- 4 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6f232af --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM rust AS builder +WORKDIR /home/vili/School/Compilers/compiler-course +COPY . . +RUN cargo install --path . + +FROM debian:stable-slim +RUN apt-get update && apt-get install -y libc6 gcc && rm -rf /var/lib/apt/lists/* +COPY --from=builder /usr/local/cargo/bin/compiler-course /usr/local/bin/compiler-course +EXPOSE 3000 +CMD ["compiler-course"] + diff --git a/src/compiler.rs b/src/compiler.rs index 51ea4d6..e55b393 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -29,6 +29,7 @@ pub fn compile(code: &str) -> String { type_check(&mut ast, &mut SymTab::new_type_table()); let ir = generate_ir(&ast); let assembly = generate_assembly(&ir); + general_purpose::STANDARD.encode(&assemble(assembly)) } diff --git a/src/compiler/assembler.rs b/src/compiler/assembler.rs index c15f660..1210b18 100644 --- a/src/compiler/assembler.rs +++ b/src/compiler/assembler.rs @@ -41,26 +41,21 @@ pub fn assemble(assembly: String) -> Vec { file.write_all(assembly.as_bytes()) .expect("Can't write to temp file!"); - let as_out1 = Command::new("as") + Command::new("as") .args(["-g", "-o", stdlib_obj, stdlib_asm]) .output() .expect("Could not run 'as' command!"); - let as_out2 = Command::new("as") + Command::new("as") .args(["-g", "-o", program_obj, program_asm]) .output() .expect("Could not run 'as' command!"); - let ld_out = Command::new("ld") + Command::new("ld") .args(["-o", output_file, "-static", stdlib_obj, program_obj]) .output() .expect("Could not run 'as' command!"); - println!("{as_out1:?}"); - println!("{as_out2:?}"); - println!("{ld_out:?}"); - println!("{workdir:?}"); - let mut file = File::open(output_file).expect("Can't open compiler output!"); let mut output = Vec::new(); file.read_to_end(&mut output) diff --git a/src/server.rs b/src/server.rs index b8e31bd..4f2b416 100644 --- a/src/server.rs +++ b/src/server.rs @@ -37,11 +37,8 @@ fn handle_connection(mut stream: TcpStream) { let program = json_request["code"].as_str().unwrap(); let output = compiler::compile(program); - let json_response = json::object! { - "program": output, - }; - let response = json_response.as_str().unwrap().as_bytes(); - stream.write_all(response).unwrap(); + let response = format!("{{\"program\": \"{output}\"}}"); + stream.write_all(response.as_bytes()).unwrap(); } _ => panic!("Unexpected command!"), }