Skip to content

Commit cc7d1ef

Browse files
committed
Add PIE
1 parent aef40e8 commit cc7d1ef

31 files changed

+2295
-0
lines changed

pie/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8">
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5+
<title></title>
6+
</head>
7+
8+
<body>
9+
10+
<pre id="storyteller-stories" style="display:none">pie</pre>
11+
<pre id="easycoder-script" style="display:none">
12+
13+
script Boot
14+
variable Script
15+
require js `https://easycoder.github.io/dist/plugins/showdown.js?v=` cat now
16+
17+
rest get Script from `https://easycoder.github.io/scripts/storyteller/storyteller.ecs?v=` cat now
18+
! rest get Script from `/storyteller.txt?` cat now
19+
run Script
20+
21+
</pre>
22+
23+
<!-- <script type='text/javascript' src='https://cdn.jsdelivr.net/gh/easycoder/easycoder.github.io/dist/easycoder.js?v=2.7.6'></script>-->
24+
25+
<script type='text/javascript' src='https://easycoder.github.io/dist/easycoder.js?v=2.7.6'></script>
26+
27+
</body>
28+
</html>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Commands
2+
3+
In this book I don't want to be prescriptive about what commands should be implemented. This is a book about building your own language so it's not up to me to specify details. I will show examples that demonstrate how to use some of the variables listed in the last page. You may choose to follow my syntax or you may prefer to do something completely different. This is entirely up to you - it's your own language you're creating.
4+
5+
The primary core variable type(s) hold numeric, string or boolean values, either as distinct variable types or as a single type that can hold any of those kinds of data. On the ~tid:Example code~ page are examples of how you might choose to implement arithmetic operations.
6+
7+
To set up a numeric variable you can use a syntax such as
8+
```
9+
put 12345 into Count
10+
```
11+
12+
String operations are also quite straightforward. To assign a string value, use
13+
```
14+
put `This is a string` into Title
15+
```
16+
To append to an existing string you can use the _add_ keyword, but your compiler will need to check if it's being asked to do an arithmetic or a string operation, which may not be easy if your variables can hold either type. So it's probably better to use `append`:
17+
```
18+
append `Some more text` to MyText
19+
```
20+
If you are building a composite string this can be a little cumbersome, so I recommend implementing a `cat` (catenate) operator in your value processor (see the relevant page). Here's a sample usage based on an array variable `Numbers` and a `print` command that outputs to the browser console:
21+
```
22+
index Numbers to N
23+
print `The value of element ` cat N cat ` of Numbers is ` cat Numbers
24+
```
25+
All languages need a selection of conditional commands, the minimum being `if` and `while`. Because this language will probably lack any advanced structuring you'll also need the infamous `goto` command. To avoid repetition you'll most likely want `gosub` and `return` (or their equivalents). If you're feeling really adventurous you might include a `with` clause in your `gosub` so you can pass in private variables, but it could get rather complex as you'll need some way of identiying them at the entry point of a subroutine.
26+
27+
When you're programming browser-based apps you'll almost certainly want concurrent processing at some point. JavaScript itself is single-threaded but there are ways to implement concurrency (or at least something that looks and feels like it) in your scripts. All I'll say here is that with this capability you can implement commands like `wait` to pause execution for a given time, `fork` to run different parts of your script concurrently with another, and `run` to load and concurrently run different script modules. There's a whole chapter of this book devoted to the subject.
28+
29+
Another thing you'll almost certainly want at some point is a set of REST commands so you can GET data from your own or remote websites and POST data back again.
30+
31+
## DOM commands
32+
A large number of commands in the browser domain will be those dealing with the Document Object Model. This is where the language is likely to be most frequently used; as a replacement for JavaScript when aspects of a web page need to be tweaked.
33+
34+
Each of the DOM variables such as `<div>` will be designed to handle actions on a corresponding entity in the DOM. So first you need to connect the two, for example using an _attach_ keyword:
35+
```
36+
attach Screen to `id_screen`
37+
```
38+
This assumes that in your page there is a `<div>` with an id of `id_screen`. Your runtime JavaScript looks for this id and "attaches" its owner to the script variable. You can now perform operations on the variable; the runtime instantly and faithfully mirrors changes on the `<div>` itself. Your language will have to recognise all the commands that are valid for this variable type, so you might decide to implement a `set` command to do the hard work, with constructs like
39+
```
40+
set the background of Screen to `yellow`
41+
```
42+
which is a lot easier for non-programmers to read and remember than its JS equivalent.
43+
44+
As well as attaching to existing objects you will probably want to create new ones using an existing object as a parent. Here's a typical syntax:
45+
```
46+
create LeftPanel in Screen
47+
```
48+
49+
With a full set of `create` syntax you can build an entire DOM using just script and hang it on any element in your web page. This is great for portability; you have a script that will run anywhere.
50+
51+
~sid:3 Compilation:The compiler~
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# The coding environment
2+
3+
Before starting to look at language syntax we need to consider where our new language will run and what build environment it needs. The outline brief for the new language is that it should be easy to use and to be accessible to as many people as possible. Naturally, the perfect language will run everywhere and not require any build environment, compiling instantly on the fly directly from source text files and running immediately following compilation, but is this feasible? Most modern build environments are as complex and hard to learn as the language itself so it's highly desirable not to need one, but how much of a performance hit will we suffer by doing everything in real time?
4+
5+
Rather than beat about the bush I'll come straight to the point. The techniques described in this book are based on real compilers written in JavaScript and Python. Both compile very quickly. The JavaScript version will compile a 500-line script in a few tens of milliseconds, even on a smartphone, and its run-time performance is more than up to the needs of most websites. So because we know it's possible that's what we should aim for.
6+
7+
The other decision concerns where the code is to run. If the compiler is coded in Python, the applications you build are basically restricted to running on a PC of some kind. I'm personally doubtful of the value of this, especially as Python is itself already a fairly high-level language and you'd get more bang from your buck by just learning that.
8+
9+
Some might prefer to code in C++ or Java. Many compilers are written in C++ so there's something to be said for that, though if you want to distrbute your efforts you'll have to do versions for Windows, for Mac and for Linux.
10+
11+
There are many, many other alternatives but the one that stands out is JavaScript, because it runs in every browser on the planet. Many people, most notably Chromebook owners, rarely venture outside the browser. Your apps can reside on a webserver and be available simply by typing a URL. Add to this a superb graphics environment and the case is unanswerable.
12+
13+
In this book I will assume the browser is the environment and JavaScript is what we'll use to write the new language. JavaScript is incredibly powerful and is well up to the job of being an "assembly language" in which to code higher-level software.
14+
15+
The way it will work is like this. The language itself will be shipped as a single JavaScript file and called by the web page that needs it. Elsewhere in the page will be the script that implements the functions of the required web app. This will be in a specially named `<div>` that the JavaScript module looks for when the page has loaded. If it finds the script it compiles and runs it.
16+
17+
In the browser, JavaScript can be called upon to do anything from minor cosmetic tweaks to full-scale applications. Since our language will be written in JavaScript, as long as it has a full range of commands it can do all these things too. Although it'll run more slowly, you won't notice the difference in most cases.
18+
19+
~tid:Vocabulary~
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Example code
2+
3+
Let's look at the kind of statements that would form part of an English-like programming language. I'll start with arithmetic. The language will handle integer 4-function arithmetic; add, subtract multiply and divide, plus modulo (remainder). Floating-point is only infrequently needed in general programming, and where it is there are ways of handling it without mixing it up with integer processing.
4+
5+
Before I launch into a description of the actual arithmetic functions I'll first introduce the notion of _constants_, _values_ and _variables_. These three can only be defined in terms of each other, so I'll take them in alphabetical order.
6+
7+
## Constants, values and variables
8+
9+
A _constant_ is an entity whose value is fixed for all time, such as 14, 3996 or "Kg". Some computer languages let you give a name to a constant (these are called _manifest constants_); this helps if you use the same constant in many places and later want to give it a different value; it saves having to hunt down every occurrence.
10+
11+
A _value_ is any entity that can be used as part of a calculation or other language command. It might be a constant as defined above, it might be the contents of a variable as defined next, or it might be something else that is only valid at the point you actually use it, such as the time of day or the size of a file. To cope with all this variation we'll implement a "value" as a _specification_ of how to obtain the actual numeric, boolean or textual value to use in the command.
12+
13+
A _variable_ is a named storage location into which you put a value. In this language, variables are _typed_; that is, they hold a particular kind of value and have particular actions that are valid for that variable type. This is in fact how English works; a dog cannot be changed into a cat or a chair. Each has its own set of properties, some of which it shares with other variable types.
14+
15+
## Arithmetic
16+
17+
So let's now look at how we do arithmetic, starting with addition. At its simplest, this involves taking two values, adding them together and putting the result somewhere, where _somewhere_ is a variable able to hold a value of the type resulting from the addition.
18+
19+
Most computer languages represent addition like this:
20+
```
21+
C = A + B
22+
```
23+
where A and B are _value_s and C is a _variable_. There are no constraints on what kinds of values are represented by A or B; they could be a constant, a variable or a complex value.
24+
25+
There's a variant on this:
26+
```
27+
C = C + B
28+
```
29+
where the result replaces one of the original values, which must itself be a _variable_.
30+
31+
Remember I insisted some while back about not using symbols and being able to read out a program? OK, so we need to figure what this might look like in our new language. In English you'd likely say
32+
```
33+
add A to B giving C
34+
```
35+
for the first form, and
36+
```
37+
add B to C
38+
```
39+
for the second. So why not do just this? Let's adopt the exact same syntax we use in speech. And the same goes for the other 3 functions:
40+
```
41+
take A from B giving C
42+
take B from C
43+
multiply B by A giving C
44+
multiply C by B
45+
divide B by A giving C
46+
divide C by B
47+
```
48+
Note how I've shown multiply and divide having the two values in the opposite order to add and subtract. This of course regards B as a "primary" value and A as something being used to modify it, which might not always be the case, but it serves to point out how the new language can follow English quite closely.
49+
50+
~tid:Variables~
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Variables
2+
3+
Earlier I said that our new language should follow the example of English in that the entities it uses all have distinct types. These can be added to by the use of plugin modules, as also mentioned earlier.
4+
5+
The types provided by the core language fall into two groups. First there's a small set of types that are needed by any language to deal with numeric and string values. Along with these are special types such as one to handle supplementary scripts downloaded while the app is running. The second group deals with the visual environment of a browser, in particular the Document Object Model (DOM). There will need to be types for each of the most commonly used DOM types such as `<div>`, `<p>`, `<img>`, `<a>`, `<input>` and so on. Each of these will have its own syntax for the things it can do.
6+
7+
At this point I'll note that for speed the compiler of our language will be single-pass; that is, it processes source scripts just once. The consequence of this is that variables must be declared before they are used, so it will be usual to see a block of declarations at the top of the script looking something like this:
8+
```
9+
div Main
10+
div LeftPanel
11+
div RightPanel
12+
p Intro
13+
a Continue
14+
img ContinueImage
15+
variable N
16+
variable Count
17+
...
18+
```
19+
The syntax is of the type name followed by the name of the variable, which is capitalized (because names are always capitalized in English).
20+
21+
Variables can start with default values or with no value at all, but the latter is preferable as it allows the runtime system to detect when a variable has been accessed before being assigned a value - a common programming mistake. The compiler should also save a flag into the record held for the variable, to be set when code is encountered that accesses the variable. This allows a check to run after compilation to discover unused variables. These are small points but they make life simpler for programmers.
22+
23+
The primary core variable types hold numeric, string or boolean values. You can if you wish use distinct types for each of these, or you can use a single type that adapts itself to the data being placed in it. This is up to you, the designer of the language.
24+
25+
## Arrays
26+
27+
In most computer languages, arrays are represented using notation like `MyVariable[5]`, where the sixth element (counting from zero, not 1) is being referred to. To avoid using symbols we need a different way to represent an array. The approach we will use is found in SQL, where a _cursor_ selects one record of a set returned from a database query.
28+
29+
In our language, all variables will be arrays, initially holding just a single element. They also have an internal "index" value that points to the currently-selected element of the array; this is initially zero. The language will have a command that sets the number of elements of the array to any chosen value; also a command that sets the index value to any given position in the array. When the array is accessed, the runtime engine uses the indexed item as if it were the only element in the array - a simple variable, in other words. The actual syntax of the commands is up to the designer of the language, but here are some examples:
30+
```
31+
set the elements of MyVariable to 10
32+
index MyVariable to 5
33+
add 1 to MyVariable
34+
```
35+
The first command sets up the array, the second one points to a specific element and the third one accesses the data at the given index.
36+
37+
When the runtime encounters the command `set the elements of` it will either expand or contract the array, depending if the requested number is greater or smaller than the current size. So although on expansion no data will be lost, on contraction the higher-indexed elements will be.
38+
39+
~tid:Commands~
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Vocabulary
2+
3+
One of the most noticeable differences between human and computer languages is in the richness of their vocabularies. English has an unknown number of distinct words; estimates range from low to high hundreds of thousands. No computer language is blessed with such a rich vocabulary, though to be fair none need it. Even the most accomplished native English speakers never use the full range of words available, and similarly, computer programs function with just a few tens of words.
4+
5+
However, things are not quite a simple as that. A computer language may have 50 or fewer words in its vocabulary but there are usually far more distinct entities to be represented. Most modern computer languages deal with this by providing _classes_ that perform a little like native language objects. So you can create a _dog_, _cat_ or _car_ object and use its properties in the same ways as simple integers or strings. The effect of doing this isn't completely seamless; the syntax of a library _File_ object is such that if never quite behaves as if it were built-in, but it's the best most computer languages can offer.
6+
7+
In our own custom language we don't need to be constrained in this way. The core language can have as many special types and abilities as we are able to devise unique syntax for, only being limited by the size of the JavaScript file we have to deliver to the browser. We can go on adding as many features as we like, but eventually it will get so large it will degrade page load times to an unacceptable degree. As long as we're prepared to accept a compacted file size that's not too much more than 100k bytes or so then the load time can still be good.
8+
9+
Beyond that we need some kind of library mechanism to bring in special functions that are only required by particular apps. A good example is code to handle Google Maps. There's no point in bundling the necessary JS libraries unless the page actually needs to use them, so let's have a load-on-demand strategy instead. The main thing is to ensure that plug-ins of this kind add functionality in a seamless manner, where the added keywords and behaviors act as though they are a part of the core language. This allows unlimited expansion of the language capabilities.
10+
11+
Along with this, let's also have a mechanism for loading source scripts on demand, compiling them on the fly and releasing them when they're no longer wanted. Again, this allows the system to offer unlimited capabilities without the risk of running out of memory.
12+
13+
The technique proposed here enables functionality of almost any complexity to be wrapped in new language syntax and delivered on demand. This means that there is no web page or web app that cannot be built using our new language. As with maps, any large chunk of functionality that is inconvenient or clumsy to code in the syntax of our new language can be written as a self-contained JS functional unit that presents a suitable API. It can then be added to the language using a small plug-in module. The ultimate conclusion of this is a web page that contains little more than the word _doit_, which loads everything else as pure JavaScript, though this goes somewhat against the idea of making code more readable. All I'm saying is that our new language is able to play well with any other JS-based coding system. It's not a rival; it's a partner.
14+
15+
~tid:Example code~

0 commit comments

Comments
 (0)