@nikso/tu v0.0.4
Tu language
An experimental implementation of the Io language in Javascript.
Account := Object clone do(
balance := 0.0
deposit := method(v, balance = balance + v)
show := method(write("Account balance: $", balance, "\n"))
)
"Inital: " print
Account show
"Depositing $10\n" print
Account deposit(10.0)
"Final: " print
Account show:warning: This is a hobby work in progress. :warning: many things are not yet implemented, and the API is subject to change.
Installation
To install globally, run:
npm install -g @nikso/tuUsing the REPL
You can now start an interactive session with tu:
$ tu
⟩ a := 1 + 1
2
⟩ a + 40
42Using in the browser
You can also use Tu in the browser. To do so, you can use the tu.js file
in the dist folder. You can also use the tu.cjs file for CommonJS.
Alternatively, you can also use the index.mjs file in the root folder.
This is the same as the tu.js file, but it is written in ES6 modules.
<html>
<body>
<script type="module">
import { createEnvironment } from './index.mjs';
const env = createEnvironment();
window.Lobby = env.Lobby;
window.tu = env.tu;
const result = await tu`
a := 1 + 1
a + 40
`;
console.log(result); // 42
</script>
</body>
</html>Development
Clone this repository and run:
1. npm install to install the dependencies
2. npm run build to build the project dependencies in dist
3. npm run dev to serve the root folder
4. open http://localhost:3000 in your browser
5. open the devtools console to see the output of tests and use tu
You can also run npm link -g to get a tu command wich will use the local
version of the project.
How does it work?
lib/environment.jscontains thecreateEnvironmentfunction which creates a new environment with theLobbyobject and thetufunction.lib/receiver.jscontains theReceivermain object (calledObjectin Io), as well as the definition of theLobbyobject.lib/primitives.jshave some low level implementation of javascript primitives (likeNumber,String,Array, etc.) to get things rolling.lib/message.jsis where the magic happens. It contains theMessageobject which is used to represent the entirety of a Tu program. The special function here isevalMessagethat takes aMessageand an start interpreting it in the given context.lib/parser.jsis a Ohm grammar that parses a Tu program into aMessage.
Check out also some strip down, early versions of the project in the study
folder. Those should be more readable and easier to understand.
Recently evalMessage was made async, so that it can await on each message
allowing to write something like this:
fetch("https://jsonplaceholder.typicode.com/todos/1") json userId printlnThis works and I must say it's rather cool. Unfortunately it made the interpreter much slower.
Where to go from here?
My ideas for this project would be:
- Complete the implementation of
evalMessageto properly support all the features of Io (in particular thestopStatusto allow forbreak,continue). - Attempt a DOM library to allow for DOM manipulation in the browser. See how it
would feel to write a web app in Tu. Possibly integrate with Svelte to write
something like (pseudo code):
<script lang="tu"> name := "World" greeting := "Hello "..name.."!" </script> <div> <input type="text" bind:value={name} /> <p>{greeting}</p> </div> - Compile Tu into Javascript. This has some early attempts in
study/try4and it seams promising. Compiled Tu would not need to maintain the dynamic nature of Tu and it could be much more performant.
Not sure if and when I'll ever get to do any of those things :P
3 years ago