From bc2b55e22a83eeac7dd52c21ec079f7a8b415f9d Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Fri, 25 Feb 2022 04:37:43 +0700 Subject: [PATCH] puts to lower write instead --- a.out | Bin 0 -> 16120 bytes ex.c | 16 ++++++++++++++++ example/ex.hyc | 3 ++- src/back/c.rs | 36 ++++++++++++++++++++++++++---------- src/front/parse.rs | 1 + src/main.rs | 1 + 6 files changed, 46 insertions(+), 11 deletions(-) create mode 100755 a.out create mode 100644 ex.c diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..c25abd26cc81ddbd6742669d3e8d9be3c8c6f747 GIT binary patch literal 16120 zcmeHOZEPGz8J;^QjZ5l}e7Lv?O}3>eZdG6Gd>EI?_0D`FF*+QLL=;kk`l8~}c&o?6MNs;@WC-G_S2fXj zkGfOcLgh`G^Wp2Bk9dVEsiQWAt67ujw_>{i4MxGL=gBAy%* zK6)jdK{QCl$9;o%+VbuHDdXR1hEVXaz)lUnV=bw4(8k z0hjrN)VI-f=#cmWCA?~XCOz46Xn!WvnMr4hrOr}sPiN1eSRofXpeyEfaUDFSj*gvD z(FL`{9PA2$$$@)O4fGr6oYVNYh* z>-20cD_$*6wXPzyGfFLPpudPlZhutmCm!RhxGQ}tU#$m)``Nd?OP4jO64FQ9|G4ks z+dM%5Ad4|PjC5%hdh=zwn!BpFGF36R(w2e7t|G zw#PjU=HwBg3B$+x$FHXyp2G-)5eOp?Mj(tp7=bVXVFdnH5%_b9omc4Pxae0Pi*D6&?`+m!1o^oEhLPhr1{l}5= zTaTILx6N}`SH~w3-Amn<%@_OlzYNv3B&q*pD%P_1S?!!pi23STgBf|leCvk0lN_{(gT`uQs%7uc zO?z(NLf;sPRq@~{^IYHiG0K|dcibj(v2PFY)wT*zD@p3i8=LnL5qb2|&GufJx|!#A zu)k~5q$}+^MXt(cua}E!i;3$Ei8a41>$9BVZTu>6ox4>&yJnV)tJM8xnzCEe}m<3H;t>+ z>f?mHgeK#A)#?j`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ED_qux(int lhs, int rhs) +{ + return lhs + rhs; +} +int main(int ARGC, char **ARGV) +{ + char *msg = "Hello, World!"; + write(1, msg, strlen(msg)); + return 0; +} diff --git a/example/ex.hyc b/example/ex.hyc index b19f784..c800236 100644 --- a/example/ex.hyc +++ b/example/ex.hyc @@ -4,6 +4,7 @@ let baz: bool = true; fun qux (lhs: int rhs: int) -> int = lhs + rhs; fun main () -> int = do - puts("Hello, World"); + let msg: string = "Hello, World!"; + write(msg); 0; end; \ No newline at end of file diff --git a/src/back/c.rs b/src/back/c.rs index 29c39f4..7297bb9 100644 --- a/src/back/c.rs +++ b/src/back/c.rs @@ -8,7 +8,7 @@ pub struct Codegen { } const HEADER_INCLUDES: [&str; 3] = [ - "", + "", "", "", ]; @@ -43,10 +43,22 @@ impl Codegen { self.emit(";\n"); }, IR::Fun { name, return_type_hint, args, body } => { - let args = args.iter().map(|(name, type_hint)| { + let args_str = args.iter().map(|(name, type_hint)| { format!("{} {}", type_hint, name) }).collect::>().join(", "); - self.emit(format!("{} {}({}) {{", return_type_hint, name, args)); + self.emit(format!( + "{} {}({}) {{", + return_type_hint, + match name.as_str() { + "main" => "main".to_string(), + _ => format!("USER_DEFINED_{}", name), + }, + match name.as_str() { + "main" => format!("{}{}{}", "int ARGC, char **ARGV", if args.len() == 0 { "" } else { "," }, args_str.as_str()), + _ => args_str, + } + )); + match &**body { IR::Value { value } => { self.emit("return "); @@ -57,9 +69,11 @@ impl Codegen { for (i, node) in body.iter().enumerate() { if i == body.len() - 1 { self.emit("return "); - }; - self.gen_ir(node); - self.emit(";"); + self.gen_ir(node); + self.emit(";"); + } else { + self.gen_ir(node); + } } }, IR::Binary { op, left, right } => { @@ -75,13 +89,15 @@ impl Codegen { }, IR::Call { name, args } => { match name.as_str() { - "puts" => { - self.emit("printf("); + "write" => { + self.emit("write(1, "); self.gen_ir(&args[0]); - self.emit(")"); + self.emit(", strlen("); + self.gen_ir(&args[0]); + self.emit("));"); }, _ => { - self.emit(format!("{}(", name)); + self.emit(format!("USER_DEFINED_{}(", name)); for (i, arg) in args.iter().enumerate() { if i != 0 { self.emit(", "); diff --git a/src/front/parse.rs b/src/front/parse.rs index 6f1c29e..77b4a2d 100644 --- a/src/front/parse.rs +++ b/src/front/parse.rs @@ -237,6 +237,7 @@ fn expr_parser() -> impl Parser> + Clone { let do_block = just(Token::Do) .ignore_then( expr.clone() + .or(decl.clone()) .then_ignore(just(Token::Semicolon)) .repeated()) .then_ignore(just(Token::End)) diff --git a/src/main.rs b/src/main.rs index e180fb9..a7d854f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,6 +61,7 @@ fn main() { log(0, "Running clang-format..."); let mut clang_format_status = Command::new("clang-format") .arg("-i") + .arg("-style=Microsoft") .arg(&out_file_name) .spawn() .expect("Failed to run clang-format, make sure you have it installed");