Fortran is a compiled, imperative programming language designed for numeric computation and scientific computing.
Fortran in LiveCodes is compiled to WebAssembly by LFortran and run in the
browser, with no server. This is modern Fortran (free form), not a Fortran 77 subset.
Usage
Demo:
show code
- JS
- TS
- React
- Vue
- Svelte
- Solid
- Preact
- Web Components
import { createPlayground } from 'livecodes';
const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "fortran-wasm",
"content": "program hello\nprint *, 'Hello, World!'\nend program"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
createPlayground('#container', options);
import { createPlayground, type EmbedOptions } from 'livecodes';
const options: EmbedOptions = {
"config": {
"activeEditor": "script",
"script": {
"language": "fortran-wasm",
"content": "program hello\nprint *, 'Hello, World!'\nend program"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
createPlayground('#container', options);
import LiveCodes from 'livecodes/react';
export default function App() {
const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "fortran-wasm",
"content": "program hello\nprint *, 'Hello, World!'\nend program"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
return (<LiveCodes {...options} />);
}
<script setup>
import LiveCodes from "livecodes/vue";
const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "fortran-wasm",
"content": "program hello\nprint *, 'Hello, World!'\nend program"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
</script>
<template>
<LiveCodes v-bind="options" />
</template>
<script>
import LiveCodes from 'livecodes/svelte';
const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "fortran-wasm",
"content": "program hello\nprint *, 'Hello, World!'\nend program"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
</script>
<LiveCodes {...options} />
import LiveCodes from 'livecodes/solid';
export default function App() {
const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "fortran-wasm",
"content": "program hello\nprint *, 'Hello, World!'\nend program"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
return (<LiveCodes {...options} />);
}
import LiveCodes from 'livecodes/preact';
export default function App() {
const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "fortran-wasm",
"content": "program hello\nprint *, 'Hello, World!'\nend program"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
return (<LiveCodes {...options} />);
}
<live-codes></live-codes>
<script type="module">
import "livecodes/web-components";
const playground = document.querySelector("live-codes");
playground.config = {
"activeEditor": "script",
"script": {
"language": "fortran-wasm",
"content": "program hello\nprint *, 'Hello, World!'\nend program"
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
};
</script>
Communication with JavaScript
The program runs in the context of the result page. A few helpers are available in the browser global
livecodes.fortran object:
livecodes.fortran.input: the standard input passed to the program.
livecodes.fortran.loaded: a promise that resolves when the compiler has downloaded and the program
has run. Other helpers should be used after it resolves.
livecodes.fortran.output: the program's standard output.
livecodes.fortran.error: the compiler diagnostics, if the code failed to compile.
livecodes.fortran.exitCode: the exit code of the program.
livecodes.fortran.run: compiles and runs the code again with new input, returning a promise that
resolves with output, error and exitCode.
Example:
show code
- JS
- TS
- React
- Vue
- Svelte
- Solid
- Preact
- Web Components
import { createPlayground } from 'livecodes';
const options = {
"template": "fortran-wasm",
"params": {
"activeEditor": "markup"
}
};
createPlayground('#container', options);
import { createPlayground, type EmbedOptions } from 'livecodes';
const options: EmbedOptions = {
"template": "fortran-wasm",
"params": {
"activeEditor": "markup"
}
};
createPlayground('#container', options);
import LiveCodes from 'livecodes/react';
export default function App() {
const options = {
"template": "fortran-wasm",
"params": {
"activeEditor": "markup"
}
};
return (<LiveCodes {...options} />);
}
<script setup>
import LiveCodes from "livecodes/vue";
const options = {
"template": "fortran-wasm",
"params": {
"activeEditor": "markup"
}
};
</script>
<template>
<LiveCodes v-bind="options" />
</template>
<script>
import LiveCodes from 'livecodes/svelte';
const options = {
"template": "fortran-wasm",
"params": {
"activeEditor": "markup"
}
};
</script>
<LiveCodes {...options} />
import LiveCodes from 'livecodes/solid';
export default function App() {
const options = {
"template": "fortran-wasm",
"params": {
"activeEditor": "markup"
}
};
return (<LiveCodes {...options} />);
}
import LiveCodes from 'livecodes/preact';
export default function App() {
const options = {
"template": "fortran-wasm",
"params": {
"activeEditor": "markup"
}
};
return (<LiveCodes {...options} />);
}
<live-codes
template="fortran-wasm"></live-codes>
<script type="module">
import "livecodes/web-components";
const playground = document.querySelector("live-codes");
playground.params = {
"activeEditor": "markup"
};
</script>
program hello
print *, 'Hello, World!'
end program
Array sections, whole-array arithmetic and intrinsics behave as expected:
program slice
real :: a(4)
a = [10.0, 20.0, 30.0, 40.0]
print *, a(2:3) ! 20.0 and 30.0, not all four elements
print *, sum(a) ! 100.0
end program
READ reads from the stdin pane, in the same way as the other console languages.
Language Info
Name
fortran-wasm
Aliases / Extensions
f90, f95, f03, f08, f, for, ftn, fortran, lfortran
Editor
script
Compiler
LFortran compiled to WebAssembly.
Version
LFortran 0.66.0, with LLVM 22.1.8 and LLD, built with Emscripten 4.0.9.
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 Fortran code, which forces a full page reload.
This comment can be added in the hiddenContent property of the editor for embedded playgrounds.
Example Usage
program stats
integer :: i
real :: x(5), total
x = [1.0, 2.0, 3.0, 4.0, 5.0]
total = 0.0
do i = 1, 5
total = total + x(i)
end do
print *, 'Sum = ', total
print *, 'Mean = ', total / 5.0
end program
Starter Template
https://livecodes.io/?template=fortran-wasm
Links