highlightjs_example.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!--
  2. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  3. Exceptions. See /LICENSE for license information.
  4. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. -->
  6. <!-- Grab a released highlight.js from one of its CDNs. -->
  7. <link
  8. rel="stylesheet"
  9. href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/ir-black.min.css"
  10. />
  11. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"></script>
  12. <script type="module">
  13. import Carbon from './highlightjs_carbon_lang.js';
  14. hljs.registerLanguage('Carbon', Carbon);
  15. hljs.highlightAll();
  16. </script>
  17. <pre><code class="language-carbon">
  18. package Sorting api;
  19. import Carbon library "Printing";
  20. fn Partition[T:! Comparable & Movable](s: Slice(T))
  21. -> i64 {
  22. var i: i64 = -1;
  23. let (x: i64, var y: auto) = (1, {.x = {.a = (1, {.k = 42}), .b = 13}, .y = "..."});
  24. for (e: T in s) {
  25. if (e <= s.Last()) {
  26. ++i;
  27. Swap(&s[i], &e);
  28. }
  29. }
  30. return i;
  31. }
  32. fn QuickSort[T:! Comparable & Movable](s: Slice(T)) {
  33. if (s.Size() <= 1) {
  34. return;
  35. }
  36. let p: i64 = Partition(s);
  37. QuickSort(s[:p - 1]));
  38. QuickSort(s[p + 1:]));
  39. }
  40. interface WidgetI {
  41. }
  42. constraint WidgetC {
  43. }
  44. abstract class Abstract {
  45. abstract fn DoSomething[me: Self]();
  46. }
  47. base class Widget {
  48. virtual fn Print[addr me: Self*](var i: i64, x: f32 = 4.2, _: bool);
  49. }
  50. fn Widget.Print[addr me: Self*](var i: i64, x: f32, _: bool, 47) {
  51. Carbon.Print("test" + "t\u{FFFF}his");
  52. Carbon.Print(
  53. """
  54. testing more
  55. with weird stuff \
  56. that does stuff
  57. """);
  58. Carbon.Print(
  59. """c++
  60. template &lt;typename T&gt; class C {};
  61. """);
  62. }
  63. class FancyWidget(T:! Type) extends Widget {
  64. impl fn Print[me: Self]();
  65. impl as Add(T) {
  66. fn Op[me: Self]();
  67. }
  68. }
  69. fn FancyWidget(T:! Type).Print[me: Self]() {
  70. Carbon.Print("more test");
  71. }
  72. impl forall [T:! Type] FancyWidget(T) as Printable {
  73. alias Print = FancyWidget(T).Print;
  74. }
  75. </code></pre>