diff --git a/crates/codegen/src/cpp.rs b/crates/codegen/src/cpp.rs index 287a3df..c3d094b 100644 --- a/crates/codegen/src/cpp.rs +++ b/crates/codegen/src/cpp.rs @@ -17,6 +17,7 @@ impl Codegen { pub fn gen(&mut self, irs: Vec) { self.emit("#include \n"); + self.emit("#include \n"); self.emit("#include \n"); self.emit("int main() {\n"); for ir in irs { @@ -30,6 +31,13 @@ impl Codegen { IRKind::Define { name, type_hint, value } => { format!("{} {} = {};\n", type_hint, name, self.gen_ir(value)) }, + IRKind::Call { name, args } => { + match name.as_str() { + "write" => { format!("std::cout << {};\n", self.gen_ir(&args[0])) }, + "read" => { format!("std::cin >> {};\n", self.gen_ir(&args[0])) }, + _ => format!("{}({});\n", name, args.iter().map(|arg| self.gen_ir(arg)).collect::>().join(", ")), + } + }, IRKind::Value { value } => { match value { Value::Int(value) => format!("{}", value), diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index ec5280f..aa4b886 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -43,10 +43,19 @@ pub fn expr_to_ir(expr: &Expr) -> IRKind { let value = expr_to_ir(&value.0); IRKind::Define { name: name.clone(), type_hint: gen_type_hint(type_hint), value: Box::new(value) } }, + Expr::Call { name, args } => { + let name = match &name.0 { + Expr::Identifier(s) => s.clone(), + _ => panic!("Expected identifier") // TODO: Remove panic and use error handling + }; + let args = args.0.iter().map(|arg| expr_to_ir(&arg.0)).collect::>(); + IRKind::Call { name, args } + }, - Expr::Int(value) => IRKind::Value { value: Value::Int(*value) }, - Expr::Boolean(value) => IRKind::Value { value: Value::Boolean(*value) }, - Expr::String(value) => IRKind::Value { value: Value::String(value.clone()) }, + Expr::Int(value) => IRKind::Value { value: Value::Int(*value) }, + Expr::Boolean(value) => IRKind::Value { value: Value::Boolean(*value) }, + Expr::String(value) => IRKind::Value { value: Value::String(value.clone()) }, + Expr::Identifier(value) => IRKind::Value { value: Value::Ident(value.clone()) }, _ => { dbg!(expr); todo!() } } } diff --git a/example/ex.hades b/example/ex.hades index 076ebe6..64179f7 100644 --- a/example/ex.hades +++ b/example/ex.hades @@ -1,3 +1,4 @@ let foo: int = 1; let baz: bool = true; -let qux: string = "Hello, World"; \ No newline at end of file +let qux: string = "Hello, World"; +write(qux); \ No newline at end of file