1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

Compare commits

...

2 commits

Author SHA1 Message Date
Natapat Samutpong a956b8c7d2 remove unused 2022-02-25 04:38:03 +07:00
Natapat Samutpong bc2b55e22a puts to lower write instead 2022-02-25 04:37:43 +07:00
4 changed files with 30 additions and 11 deletions

View file

@ -4,6 +4,7 @@ let baz: bool = true;
fun qux (lhs: int rhs: int) -> int = lhs + rhs; fun qux (lhs: int rhs: int) -> int = lhs + rhs;
fun main () -> int = do fun main () -> int = do
puts("Hello, World"); let msg: string = "Hello, World!";
write(msg);
0; 0;
end; end;

View file

@ -8,7 +8,7 @@ pub struct Codegen {
} }
const HEADER_INCLUDES: [&str; 3] = [ const HEADER_INCLUDES: [&str; 3] = [
"<stdio.h>", "<unistd.h>",
"<string.h>", "<string.h>",
"<hycron/stdbool.h>", "<hycron/stdbool.h>",
]; ];
@ -43,10 +43,22 @@ impl Codegen {
self.emit(";\n"); self.emit(";\n");
}, },
IR::Fun { name, return_type_hint, args, body } => { 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) format!("{} {}", type_hint, name)
}).collect::<Vec<_>>().join(", "); }).collect::<Vec<_>>().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 { match &**body {
IR::Value { value } => { IR::Value { value } => {
self.emit("return "); self.emit("return ");
@ -57,9 +69,11 @@ impl Codegen {
for (i, node) in body.iter().enumerate() { for (i, node) in body.iter().enumerate() {
if i == body.len() - 1 { if i == body.len() - 1 {
self.emit("return "); self.emit("return ");
}; self.gen_ir(node);
self.gen_ir(node); self.emit(";");
self.emit(";"); } else {
self.gen_ir(node);
}
} }
}, },
IR::Binary { op, left, right } => { IR::Binary { op, left, right } => {
@ -75,13 +89,15 @@ impl Codegen {
}, },
IR::Call { name, args } => { IR::Call { name, args } => {
match name.as_str() { match name.as_str() {
"puts" => { "write" => {
self.emit("printf("); self.emit("write(1, ");
self.gen_ir(&args[0]); 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() { for (i, arg) in args.iter().enumerate() {
if i != 0 { if i != 0 {
self.emit(", "); self.emit(", ");

View file

@ -237,6 +237,7 @@ fn expr_parser() -> impl Parser<Token, Expr, Error = Simple<Token>> + Clone {
let do_block = just(Token::Do) let do_block = just(Token::Do)
.ignore_then( .ignore_then(
expr.clone() expr.clone()
.or(decl.clone())
.then_ignore(just(Token::Semicolon)) .then_ignore(just(Token::Semicolon))
.repeated()) .repeated())
.then_ignore(just(Token::End)) .then_ignore(just(Token::End))

View file

@ -61,6 +61,7 @@ fn main() {
log(0, "Running clang-format..."); log(0, "Running clang-format...");
let mut clang_format_status = Command::new("clang-format") let mut clang_format_status = Command::new("clang-format")
.arg("-i") .arg("-i")
.arg("-style=Microsoft")
.arg(&out_file_name) .arg(&out_file_name)
.spawn() .spawn()
.expect("Failed to run clang-format, make sure you have it installed"); .expect("Failed to run clang-format, make sure you have it installed");