diff --git a/a.out b/a.out new file mode 100755 index 0000000..c25abd2 Binary files /dev/null and b/a.out differ diff --git a/ex.c b/ex.c new file mode 100644 index 0000000..e233928 --- /dev/null +++ b/ex.c @@ -0,0 +1,16 @@ +#include +#include +#include +int foo = 1; +char *bar = "str"; +bool baz = true; +int USER_DEFINED_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");