Rewrite the README.

master
Zed A. Shaw 4 years ago
parent 90fc90ade9
commit 7a4b914089
  1. 185
      README.md
  2. 1
      src/node_modules/buttons.js

@ -1,111 +1,102 @@
# sapper-template # Buttons the Computer
The default [Sapper](https://github.com/sveltejs/sapper) template, available for Rollup and webpack. This is a simple demo project for the [Learn JS the Hard Way Course](https://learnjsthehardway.com)
and also the implementation of the first module introducing the concept of a Turing Machine. In the
beginning of the course I'll be introducing a very simple little computer that is a functional
## Getting started Turing Machine, although not something you'd actually want to use. It's purpose is to show how
basic computation works, and to provide a simple assembly language anyone can understand. It also
provides a simply UI that you can code the computer by only clicking on buttons.
### Using `degit`
This project was implemented in one day on a Saturday while playing Diablo III on breaks. If you
[`degit`](https://github.com/Rich-Harris/degit) is a scaffolding tool that lets you create a directory from a branch in a repository. Use either the `rollup` or `webpack` branch in `sapper-template`: find bugs awesome, let me know. I'll most likely find more as I work on it, but don't expect this
to be a billion dollar project.
```bash
# for Rollup # The Machine
npx degit "sveltejs/sapper-template#rollup" my-app
# for webpack Buttons is a simple stack machine with arbitrary registers and a limited amount of power known as
npx degit "sveltejs/sapper-template#webpack" my-app "ticks" of which you have 128. What does that mean in practice?
1. You do most of your calculations by working a stack with PUSH and POP.
2. You can store things in a register and name that register anything you want.
3. Every operation takes 1 tick to process.
4. You have only 128 ticks to complete your calculation.
5. You can do all of this with only a single mouse with two buttons.
6. The data type for the stack is whatever JavaScript does with numbers.
# Operations
The current operations are:
* ADD - Add the two numbers on the top of the stack, replace them with the result.
* SUB - Subtract just like add.
* DIV - Divide just like add.
* MUL - Multiply just like add.
* MOD - Modulus just like add.
* POP - Take the top of the stack off and throw it away.
* PUSH - Push a new value onto the stack.
* HALT - Stop the computer with a message, probably an error.
* JUMP - Jump to a line number. Lines start at 0.
* JZ - JUMP but only if the top of the stack is 0.
* JNZ - JUMP but only if the top of the stack IS NOT 0.
* CLR - Clear the machine, kind of a soft reset, good for debugging in a loop.
* STOR - Take the top of the stack and assign it to a register by a name.
* RSTOR - Take the named register and push it onto the stack.
# Samples
You can try these example programs out at [buttons.computer](https://buttons.computer):
```asm
PUSH 1
PUSH 2
ADD
``` ```
```asm
### Using GitHub templates PUSH 1
PUSH 1
Alternatively, you can use GitHub's template feature with the [sapper-template-rollup](https://github.com/sveltejs/sapper-template-rollup) or [sapper-template-webpack](https://github.com/sveltejs/sapper-template-webpack) repositories. ADD
JUMP 1
### Running the project
However you get the code, you can install dependencies and run the project in development mode with:
```bash
cd my-app
npm install # or yarn
npm run dev
``` ```
Open up [localhost:3000](http://localhost:3000) and start clicking around.
Consult [sapper.svelte.dev](https://sapper.svelte.dev) for help getting started.
## Structure
Sapper expects to find two directories in the root of your project — `src` and `static`.
### src
The [src](src) directory contains the entry points for your app — `client.js`, `server.js` and (optionally) a `service-worker.js` — along with a `template.html` file and a `routes` directory.
#### src/routes
This is the heart of your Sapper app. There are two kinds of routes — *pages*, and *server routes*.
**Pages** are Svelte components written in `.svelte` files. When a user first visits the application, they will be served a server-rendered version of the route in question, plus some JavaScript that 'hydrates' the page and initialises a client-side router. From that point forward, navigating to other pages is handled entirely on the client for a fast, app-like feel. (Sapper will preload and cache the code for these subsequent pages, so that navigation is instantaneous.)
**Server routes** are modules written in `.js` files, that export functions corresponding to HTTP methods. Each function receives Express `request` and `response` objects as arguments, plus a `next` function. This is useful for creating a JSON API, for example.
There are three simple rules for naming the files that define your routes:
* A file called `src/routes/about.svelte` corresponds to the `/about` route. A file called `src/routes/blog/[slug].svelte` corresponds to the `/blog/:slug` route, in which case `params.slug` is available to the route
* The file `src/routes/index.svelte` (or `src/routes/index.js`) corresponds to the root of your app. `src/routes/about/index.svelte` is treated the same as `src/routes/about.svelte`.
* Files and directories with a leading underscore do *not* create routes. This allows you to colocate helper modules and components with the routes that depend on them — for example you could have a file called `src/routes/_helpers/datetime.js` and it would *not* create a `/_helpers/datetime` route
### static
The [static](static) directory contains any static assets that should be available. These are served using [sirv](https://github.com/lukeed/sirv).
In your [service-worker.js](src/service-worker.js) file, you can import these as `files` from the generated manifest...
```js
import { files } from '@sapper/service-worker';
``` ```
PUSH 20
...so that you can cache them (though you can choose not to, for example if you don't want to cache very large files). PUSH 1
SUB
JZ 5
## Bundler config JUMP 1
Sapper uses Rollup or webpack to provide code-splitting and dynamic imports, as well as compiling your Svelte components. With webpack, it also provides hot module reloading. As long as you don't do anything daft, you can edit the configuration files to add whatever plugins you'd like.
## Production mode and deployment
To start a production version of your app, run `npm run build && npm start`. This will disable live reloading, and activate the appropriate bundler plugins.
You can deploy your application to any environment that supports Node 10 or above. As an example, to deploy to [Vercel Now](https://vercel.com) when using `sapper export`, run these commands:
```bash
npm install -g vercel
vercel
``` ```
If your app can't be exported to a static site, you can use the [now-sapper](https://github.com/thgh/now-sapper) builder. You can find instructions on how to do so in its [README](https://github.com/thgh/now-sapper#basic-usage). ```
PUSH -20
PUSH 1
ADD
JNZ 5
JUMP 1
```
## Using external components ```
PUSH 100
STOR AX
PUSH 50
ADD
RSTOR AX
SUB
```
When using Svelte components installed from npm, such as [@sveltejs/svelte-virtual-list](https://github.com/sveltejs/svelte-virtual-list), Svelte needs the original component source (rather than any precompiled JavaScript that ships with the component). This allows the component to be rendered server-side, and also keeps your client-side app smaller. ```
PUSH 0
STOR AX
RSTOR BAD NAME
PUSH 100
ADD
```
Because of that, it's essential that the bundler doesn't treat the package as an *external dependency*. You can either modify the `external` option under `server` in [rollup.config.js](rollup.config.js) or the `externals` option in [webpack.config.js](webpack.config.js), or simply install the package to `devDependencies` rather than `dependencies`, which will cause it to get bundled (and therefore compiled) with your app:
```bash # License
npm install -D @sveltejs/svelte-virtual-list
```
This software is fully Copyright (C) Zed A. Shaw 2020. It's like a painting at a museum. You can
look at it and admire its beauty, but you don't own it and can't take it home with you.
## Bugs and feedback
Sapper is in early development, and may have the odd rough edge here and there. Please be vocal over on the [Sapper issue tracker](https://github.com/sveltejs/sapper/issues).

1
src/node_modules/buttons.js generated vendored

@ -15,7 +15,6 @@ class ButtonMachine {
} }
assert(test, message) { assert(test, message) {
// I should use exceptions but not sure if I want to do that too early in the course // I should use exceptions but not sure if I want to do that too early in the course
if(!test) { if(!test) {
let display_op = this.cur_op ? this.cur_op.join(' ') : 'NONE'; let display_op = this.cur_op ? this.cur_op.join(' ') : 'NONE';

Loading…
Cancel
Save