mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
puts to lower write instead
This commit is contained in:
parent
a98cc8ca27
commit
bc2b55e22a
16
ex.c
Normal file
16
ex.c
Normal file
|
@ -0,0 +1,16 @@
|
|||
#include <hycron/stdbool.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
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;
|
||||
}
|
|
@ -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;
|
|
@ -8,7 +8,7 @@ pub struct Codegen {
|
|||
}
|
||||
|
||||
const HEADER_INCLUDES: [&str; 3] = [
|
||||
"<stdio.h>",
|
||||
"<unistd.h>",
|
||||
"<string.h>",
|
||||
"<hycron/stdbool.h>",
|
||||
];
|
||||
|
@ -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::<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 {
|
||||
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(", ");
|
||||
|
|
|
@ -237,6 +237,7 @@ fn expr_parser() -> impl Parser<Token, Expr, Error = Simple<Token>> + 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))
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in a new issue