Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Enabled via `processors` config:
| Perl | `perl`, `pl`, `pm` | Perl runtime |
| Pascal | `pascal`, `pas`, `pp`, `pas2js` | pas2js (Pascal -> JS, WASM) |
| Ada (Wasm) | `ada`, `ada-wasm`, `adb`, `ads`, `hac`, `wasm.ada` | HAC (Ada -> WASM) |
| D (Wasm) | `d-wasm`, `d`, `di`, `dlang`, `dmd` | DMD (D -> WASM) |
| Gleam | `gleam` | Gleam language |
| Elm | `elm` | Elm compiler (WASM) |
| Haskell | `haskell`, `hs`, `lhs` | MicroHs (not GHC) |
Expand Down
141 changes: 141 additions & 0 deletions docs/docs/languages/d-wasm.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# D (Wasm)

D is a general-purpose, statically typed, compiled programming language with C-like syntax, garbage collection, and a rich standard library.

D in LiveCodes is compiled to WebAssembly by [DMD](https://dlang.org/) and run in the browser, with
no server. It uses the WebAssembly backend of [dkorpel's dmd-explorer](https://dkorpel.github.io/dmd-explorer/),
packaged as [`@live-codes/dlang-wasm`](https://www.npmjs.com/package/@live-codes/dlang-wasm).

## Usage

Demo:

import LiveCodes from '../../src/components/LiveCodes.tsx';

export const dConfig = {
activeEditor: 'script',
script: {
language: 'd-wasm',
content: `import std.stdio;\n
void main()
{
writeln("Hello, World!");
}`,
},
mode: 'simple',
editor: 'auto',
tools: {
status: 'full',
},
};

<LiveCodes config={dConfig}></LiveCodes>

### Communication with JavaScript

The program runs in the context of the result page. A few helpers are available in the browser global
`livecodes.d` object:

- `livecodes.d.input`: the standard input passed to the program.
- `livecodes.d.loaded`: a promise that resolves when the compiler has downloaded and the program
has run. Other helpers should be used after it resolves.
- `livecodes.d.output`: the program's output (standard output and standard error, in the order they
were written).
- `livecodes.d.error`: the compiler diagnostics, if the code failed to compile.
- `livecodes.d.exitCode`: the exit code of the program.
- `livecodes.d.run`: compiles and runs the code again with new input, returning a promise that
resolves with `output`, `error` and `exitCode`.

The same object is also available as `livecodes.dlang`.

Example:

<LiveCodes template="d-wasm" params={{ activeEditor: 'markup' }} height="80vh"></LiveCodes>

```d
import std.stdio;

void main()
{
writeln("Hello, World!");
}
```

Templates, ranges and the algorithms in Phobos behave as expected:

```d
import std.stdio;
import std.algorithm : map, sum;
import std.range : iota;

void main()
{
auto squares = iota(1, 6).map!(x => x * x);
writeln(squares); // [1, 4, 9, 16, 25]
writeln(squares.sum); // 55
}
```

`readln` reads from the **stdin** pane, in the same way as the other console languages.

## Language Info

### Name

`d-wasm`

### Aliases / Extensions

`d`, `di`, `dlang`, `dmd`

### Editor

`script`

## Compiler

[DMD](https://dlang.org/) (the D reference compiler) compiled to WebAssembly, with druntime and
Phobos, from [dkorpel's dmd-explorer](https://dkorpel.github.io/dmd-explorer/).

### Version

`DMD`: v2.113

## Code Formatting

Not currently supported.

## Live Reload

By default, new code changes are sent to the result page for re-evaluation without a full page reload. This behavior can be disabled by adding the code comment `// __livecodes_reload__` to the D code, which forces a full page reload.

This comment can be added in the [`hiddenContent` property of the editor](../configuration/configuration-object.mdx#markup) for embedded playgrounds.

## Example Usage

```d
import std.stdio;

void main()
{
int sum = 0;
foreach (i; 1 .. 6)
{
sum += i;
}
writeln("Sum = ", sum); // Sum = 15
writeln("Mean = ", sum / 5); // Mean = 3
}
```

## Starter Template

https://livecodes.io/?template=d-wasm

## Links

- [D official website](https://dlang.org/)
- [D language specification](https://dlang.org/spec/spec.html)
- [D language tour](https://tour.dlang.org/)
- [Learn D in Y minutes](https://learnxinyminutes.com/docs/d/)
- [@live-codes/dlang-wasm](https://www.npmjs.com/package/@live-codes/dlang-wasm)
1 change: 1 addition & 0 deletions docs/src/components/LanguageSliders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default function Sliders() {
{ name: 'objcpp-wasm', title: 'Objective-C++ (Wasm)' },
{ name: 'cobol-wasm', title: 'COBOL (Wasm)' },
{ name: 'fortran', title: 'Fortran' },
{ name: 'd-wasm', title: 'D (Wasm)' },
{ name: 'rust-wasm', title: 'Rust (Wasm)' },
{ name: 'swift-wasm', title: 'Swift (Wasm)' },
{ name: 'haskell', title: 'Haskell' },
Expand Down
1 change: 1 addition & 0 deletions docs/src/components/TemplateList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const templates = [
{ name: 'c-wasm', title: 'C (Wasm) Starter', thumbnail: 'c.svg' },
{ name: 'cobol-wasm', title: 'COBOL Starter', thumbnail: 'cobol.svg' },
{ name: 'fortran-wasm', title: 'Fortran Starter', thumbnail: 'fortran.svg' },
{ name: 'd-wasm', title: 'D Starter', thumbnail: 'd.svg' },
{ name: 'cpp-wasm', title: 'C++ (Wasm) Starter', thumbnail: 'cpp.svg' },
{ name: 'objc-wasm', title: 'Objective-C (Wasm) Starter', thumbnail: 'objective-c.svg' },
{ name: 'rust-wasm', title: 'Rust (Wasm) Starter', thumbnail: 'rust.svg' },
Expand Down
1 change: 1 addition & 0 deletions functions/vendors/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const starterTemplates = {
"objc-wasm": "Objective-C (Wasm) Starter",
"cobol-wasm": "COBOL Starter",
"fortran-wasm": "Fortran Starter",
"d-wasm": "D Starter",
"rust-wasm": "Rust (Wasm) Starter",
"swift-wasm": "Swift (Wasm) Starter",
"zig-wasm": "Zig (Wasm) Starter",
Expand Down
1 change: 1 addition & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ const iifeBuild = () =>
'languages/cpp/lang-cpp-script.ts',
'languages/clang-wasm/lang-clang-wasm-script.ts',
'languages/fortran-wasm/lang-fortran-wasm-script.ts',
'languages/d-wasm/lang-d-wasm-script.ts',
'languages/cobol-wasm/lang-cobol-wasm-script.ts',
'languages/go-wasm/lang-go-wasm-script.ts',
'languages/csharp-wasm/lang-csharp-wasm-script.ts',
Expand Down
1 change: 1 addition & 0 deletions server/php/inc/starter-templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"objc-wasm": "Objective-C (Wasm) Starter",
"cobol-wasm": "COBOL Starter",
"fortran-wasm": "Fortran Starter",
"d-wasm": "D Starter",
"rust-wasm": "Rust (Wasm) Starter",
"swift-wasm": "Swift (Wasm) Starter",
"zig-wasm": "Zig (Wasm) Starter",
Expand Down
1 change: 1 addition & 0 deletions src/livecodes/UI/command-menu-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export const getCommandMenuActions = ({
'objc-wasm',
'cobol-wasm',
'fortran-wasm',
'd-wasm',
'rust-wasm',
'swift-wasm',
'zig-wasm',
Expand Down
177 changes: 177 additions & 0 deletions src/livecodes/assets/templates/d.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading