diff --git a/examples/interop/cpp/BUILD b/examples/interop/cpp/BUILD new file mode 100644 index 000000000000..310f126ea6a8 --- /dev/null +++ b/examples/interop/cpp/BUILD @@ -0,0 +1,12 @@ +# Part of the Carbon Language project, under the Apache License v2.0 with LLVM +# Exceptions. See /LICENSE for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +load("//bazel/carbon_rules:defs.bzl", "carbon_binary") + +carbon_binary( + name = "hello_world", + srcs = ["hello_world.carbon"], + # TODO: Remove when macos can find cstdio. + tags = ["manual"], +) diff --git a/examples/interop/cpp/hello_world.carbon b/examples/interop/cpp/hello_world.carbon new file mode 100644 index 000000000000..619edf1299a0 --- /dev/null +++ b/examples/interop/cpp/hello_world.carbon @@ -0,0 +1,33 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +import Cpp inline "#include "; + +fn Run() { + // TODO: Requires operator <<, declaration type Var (cout) and class with + // virtual bases (basic_ostream). + // Cpp.std.cout << "Hello world!\n"; + + // TODO: Requires variadic function. + // Cpp.printf("Hello world!\n"); + + // TODO: Requires nullable pointer. + // Cpp.puts("Hello world!\n"); + + // TODO: Requires nullable void pointer. + // Cpp.write(1, "Hello world!\n", 13); + + // TODO: Requires Core.String API. + // let message: str = "Hello world!\n\n"; + // for (c: Core.Char in msg) { + // Cpp.putchar((c as u8) as i32); + // } + + let message: array(Core.Char, 13) = + ('H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n'); + for (c: Core.Char in message) { + // TODO: u8 should probably have an implicit cast to i32. + Cpp.putchar((c as u8) as i32); + } +}