pocketlang/docs/try/try_now.js

43 lines
1.0 KiB
JavaScript
Raw Normal View History

2021-05-13 18:31:55 +08:00
/*
* Copyright (c) 2021 Thakee Nathees
* Licensed under: MIT License
*/
const _initial_snippet = `\
# A recursive fibonacci function.
def fib(n)
if n < 2 then return n end
return fib(n-1) + fib(n-2)
end
2021-06-23 13:21:18 +08:00
# Print all fibonacci from 0 to 10 exclusive.
for i in 0..10
2021-05-13 18:31:55 +08:00
print(fib(i))
end
`
var highlight_fn = function(editor) {
2021-06-23 13:21:18 +08:00
// highlight.js does not trim old tags,
// let's do it by this hack.
editor.textContent = editor.textContent;
editor.innerHTML = Prism.highlight(editor.textContent,
Prism.languages.ruby, 'ruby');
2021-05-13 18:31:55 +08:00
}
var runSource;
2021-06-23 13:21:18 +08:00
// called after index.html is loaded -> Module is defined.
window.onload = function() {
runSource = Module.cwrap('runSource', 'number', ['string']);
document.getElementById("run-button").onclick = function() {
document.getElementById('output').innerText = '';
const source = document.querySelector('.editor').textContent;
runSource(source);
}
let editor = document.querySelector('.editor')
editor.textContent = _initial_snippet;
highlight_fn(editor);
2021-05-13 18:31:55 +08:00
}