Merge pull request #3 from catseye/develop-2019-1
Develop 2019 1
Chris Pressey authored 6 years ago
GitHub committed 6 years ago
0 | 0 | Velo |
1 | 1 | ==== |
2 | 2 | |
3 | _Language version 0.1, distribution revision 2015.0103_ | |
3 | _Language version 0.1, distribution revision 2019.0306_ | |
4 | 4 | |
5 | 5 | > "Jaws was never my scene, and I don't like Star Wars." |
6 | 6 | > -- Queen, "Bicycle Race" |
0 | #!/bin/sh | |
1 | ||
2 | SCRIPT=`readlink $0` | |
3 | if [ "x$SCRIPT" = "x" ]; then SCRIPT=$0; fi | |
4 | SCRIPTDIR=`dirname $SCRIPT` | |
5 | lua $SCRIPTDIR/../impl/velo.lua/src/velo.lua $* |
0 | if ({X}.equals {Y}), {IO.print {Yes}}, {IO.print {No}} | |
1 | if ({X}.equals {X}), {IO.print {Yes}}, {IO.print {No}} | |
0 | yes = {IO.print {Yes}} | |
1 | no = {IO.print {No}} | |
2 | if ({X}.equals {Y}), yes, no | |
3 | if ({X}.equals {X}), yes, no |
0 | examplePrograms = [ | |
1 | [ | |
2 | "class.velo", | |
3 | "Jonkers = {\n IO.print {Hello}\n}.create new\n" | |
4 | ], | |
5 | [ | |
6 | "concat.velo", | |
7 | "IO.print ({Hello, }.\n concat {world!})\n" | |
8 | ], | |
9 | [ | |
10 | "hello-world.velo", | |
11 | "extend IO\na = {Hello, world!}\nprint a\n" | |
12 | ], | |
13 | [ | |
14 | "if.velo", | |
15 | "yes = {IO.print {Yes}}\nno = {IO.print {No}}\nif ({X}.equals {Y}), yes, no\nif ({X}.equals {X}), yes, no\n" | |
16 | ], | |
17 | [ | |
18 | "method-args.velo", | |
19 | "bar = {IO.print {Hello, }.concat #1}.method\nbar {there.}\n" | |
20 | ], | |
21 | [ | |
22 | "recur.velo", | |
23 | "count = {\n temp = #1\n if (temp.equals {XXXXXX}), { IO.print {Done!}}, {\n IO.print temp\n count temp.concat {X}\n }\n}.method\ncount {X}\n" | |
24 | ], | |
25 | [ | |
26 | "script-method.velo", | |
27 | "eeyore = IO\nfoo = {eeyore.print {Hello, world! Sincerely yours, foo.}}.method\nfoo\n" | |
28 | ] | |
29 | ]; |
0 | count = { | |
1 | temp = #1 | |
2 | if (temp.equals {XXXXXX}), { IO.print {Done!}}, { | |
3 | IO.print temp | |
4 | count temp.concat {X} | |
5 | } | |
6 | }.method | |
7 | count {X} |
0 | ||
1 | -> Functionality "Interpret Velo Script" is implemented by shell command | |
2 | -> "impl/velo.lua/src/velo.lua %(test-body-file)" |
0 | ||
1 | -> Functionality "Interpret Velo Script" is implemented by shell command | |
2 | -> "bin/velo %(test-body-file)" |
0 | velo.lua | |
1 | ======== | |
2 | ||
3 | This directory contains an implementation of Velo in Lua 5.1, | |
4 | called `velo.lua`. Its source resides in the `src` subdirectory. | |
5 | ||
6 | It also contains a demonstration of running `velo.lua` in a | |
7 | web browser, under [Fengari][], in the `demo` subdirectory. | |
8 | ||
9 | In order to for this demonstration to work locally, you'll need | |
10 | to run a local webserver from the *root* directory of this | |
11 | repository. For example, if you have Python installed, | |
12 | ||
13 | python -m SimpleHTTPServer | |
14 | ||
15 | Then open | |
16 | ||
17 | http://127.0.0.1:8000/impl/velo.lua/demo/velo.html | |
18 | ||
19 | in your web browser. If you don't have Python, other options | |
20 | (and more information on running web installations locally) | |
21 | can be found here: [how to run things locally][]. | |
22 | ||
23 | Additionally, in the `bin` directory in the root of this repo, | |
24 | there is a driver script called `velo_lua` whose purpose is to | |
25 | properly run `velo.lua` regardless of the current directory. | |
26 | In other words, you can just put that bin dir on your `PATH` | |
27 | and type `velo_lua` to run it. | |
28 | ||
29 | [Fengari]: https://fengari.io/ | |
30 | [how to run things locally]: https://github.com/mrdoob/three.js/wiki/How-to-run-things-locally#run-local-server⏎ |
0 | /* | |
1 | * fengari-web.js and velo.lua must be loaded before this source. | |
2 | * After loading this source, call launch() to create and start the interpreter. | |
3 | */ | |
4 | ||
5 | function launch(config) { | |
6 | config.container.innerHTML = ` | |
7 | <textarea id="editor" rows="10" cols="80"></textarea> | |
8 | <div id="control-panel"></div> | |
9 | <button onclick="run()">Run</button> | |
10 | <pre id="output"></pre> | |
11 | `; | |
12 | ||
13 | function makeSelect(container, labelText, optionsArray, fun) { | |
14 | var label = document.createElement('label'); | |
15 | label.innerHTML = labelText; | |
16 | container.appendChild(label); | |
17 | var select = document.createElement("select"); | |
18 | for (var i = 0; i < optionsArray.length; i++) { | |
19 | var op = document.createElement("option"); | |
20 | op.value = optionsArray[i].value; | |
21 | op.text = optionsArray[i].text; | |
22 | select.options.add(op); | |
23 | } | |
24 | select.onchange = function(e) { | |
25 | fun(optionsArray[select.selectedIndex]); | |
26 | }; | |
27 | select.selectedIndex = 0; | |
28 | label.appendChild(select); | |
29 | return select; | |
30 | }; | |
31 | ||
32 | function selectOptionByText(selectElem, text) { | |
33 | var optElem; | |
34 | for (var i = 0; optElem = selectElem.options[i]; i++) { | |
35 | if (optElem.text === text) { | |
36 | selectElem.selectedIndex = i; | |
37 | selectElem.dispatchEvent(new Event('change')); | |
38 | return; | |
39 | } | |
40 | } | |
41 | } | |
42 | ||
43 | var controlPanel = document.getElementById('control-panel'); | |
44 | var optionsArray = []; | |
45 | for (var i = 0; i < examplePrograms.length; i++) { | |
46 | optionsArray.push({ | |
47 | value: examplePrograms[i][1], | |
48 | text: examplePrograms[i][0] | |
49 | }); | |
50 | } | |
51 | ||
52 | var select = makeSelect(controlPanel, "example program:", optionsArray, function(option) { | |
53 | document.getElementById('editor').value = option.value; | |
54 | }); | |
55 | selectOptionByText(select, "hello-world.velo"); | |
56 | } | |
57 | ||
58 | function setUpPrint(elem) { | |
59 | elem.innerHTML = ''; | |
60 | fengari.interop.push(fengari.L, function() { | |
61 | var s = fengari.interop.tojs(fengari.L, 2); | |
62 | elem.innerHTML += s + "\n"; | |
63 | }); | |
64 | fengari.lua.lua_setglobal(fengari.L, "veloPrint"); | |
65 | } | |
66 | ||
67 | function loadVeloProg(progText) { | |
68 | fengari.interop.push(fengari.L, progText); | |
69 | fengari.lua.lua_setglobal(fengari.L, "veloProg"); | |
70 | } | |
71 | ||
72 | function runVeloProg() { | |
73 | var luaProg = ` | |
74 | local program = Parser.new(veloProg) | |
75 | local script = program.script() | |
76 | local object = VeloObject.new('main-script') | |
77 | local result = script.eval(object, {}) | |
78 | return result | |
79 | `; | |
80 | ||
81 | fengari.load(luaProg)(); | |
82 | } | |
83 | ||
84 | function run() { | |
85 | setUpPrint(document.getElementById("output")); | |
86 | loadVeloProg(document.getElementById("editor").value); | |
87 | runVeloProg(); | |
88 | } |
0 | <!DOCTYPE html> | |
1 | <head> | |
2 | <meta charset="utf-8"> | |
3 | <title>velo.lua (running under Fengari)</title> | |
4 | <style> | |
5 | #installation-container { } | |
6 | </style> | |
7 | </head> | |
8 | <body> | |
9 | ||
10 | <h1>Velo</h1> | |
11 | ||
12 | <p>(velo.lua running under Fengari)</p> | |
13 | ||
14 | <div id="installation-container"> | |
15 | <div id="installation"> | |
16 | </div> | |
17 | </div> | |
18 | ||
19 | <script src="https://catseye.tc/contrib/fengari-web-v0.1.4/fengari-web.js"></script> | |
20 | <script src="../../../eg/index.js"></script> | |
21 | <script src="velo-fengari-launcher.js"></script> | |
22 | <script type="application/lua" src="../src/velo.lua" async></script> | |
23 | <script> | |
24 | launch({ container: document.getElementById('installation') }); | |
25 | </script> | |
26 | ||
27 | </body> |
0 | #!/usr/bin/env lua | |
1 | ||
2 | 0 | --[[ ========== DEBUG ========= ]]-- |
3 | 1 | |
4 | 2 | local do_debug = false |
431 | 429 | Parser = {} |
432 | 430 | Parser.new = function(s) |
433 | 431 | local scanner = Scanner.new(s) |
434 | ||
435 | local methods = {} | |
436 | ||
432 | ||
433 | local methods = {} | |
434 | ||
437 | 435 | methods.script = function() |
438 | 436 | debug "parsing Script production" |
439 | 437 | local exprs = {} |
728 | 726 | end)) |
729 | 727 | |
730 | 728 | |
729 | veloPrint = function(s) | |
730 | print(s) | |
731 | end | |
732 | ||
731 | 733 | IO = VeloObject.new 'IO' |
732 | 734 | IO.set('print', VeloMethod.new('print', function(obj, args) |
733 | print(args[1].contents()) | |
735 | veloPrint(args[1].contents()) | |
734 | 736 | end)) |
735 | 737 | |
736 | 738 | Object.set('Object', Object) |
758 | 760 | |
759 | 761 | local dump_ast = false |
760 | 762 | |
761 | while #arg > 0 do | |
762 | if arg[1] == "--ast" then | |
763 | dump_ast = true | |
764 | elseif arg[1] == "--debug" then | |
765 | do_debug = true | |
766 | elseif arg[1] == "--scan" then | |
767 | debug_scan = true | |
768 | else | |
769 | text = "" | |
770 | for line in io.lines(arg[1]) do | |
771 | text = text .. line .. "\n" | |
772 | end | |
773 | ||
774 | local p = Parser.new(text) | |
775 | local s = p.script() | |
776 | if dump_ast then | |
777 | print(s.to_s()) | |
763 | function main(arg) | |
764 | while #arg > 0 do | |
765 | if arg[1] == "--ast" then | |
766 | dump_ast = true | |
767 | elseif arg[1] == "--debug" then | |
768 | do_debug = true | |
769 | elseif arg[1] == "--scan" then | |
770 | debug_scan = true | |
778 | 771 | else |
779 | local o = VeloObject.new('main-script') | |
780 | s.eval(o, {}) -- XXX could pass command-line arguments here... | |
781 | end | |
782 | end | |
783 | table.remove(arg, 1) | |
784 | end | |
772 | text = "" | |
773 | for line in io.lines(arg[1]) do | |
774 | text = text .. line .. "\n" | |
775 | end | |
776 | ||
777 | local p = Parser.new(text) | |
778 | local s = p.script() | |
779 | if dump_ast then | |
780 | print(s.to_s()) | |
781 | else | |
782 | local o = VeloObject.new('main-script') | |
783 | s.eval(o, {}) -- XXX could pass command-line arguments here... | |
784 | end | |
785 | end | |
786 | table.remove(arg, 1) | |
787 | end | |
788 | end | |
789 | ||
790 | if arg ~= nil then | |
791 | main(arg) | |
792 | end |
0 | 0 | #!/bin/sh |
1 | 1 | |
2 | FIXTURES="" | |
2 | APPLIANCES="" | |
3 | 3 | if [ `which ruby`x != x ]; then |
4 | FIXTURES="$FIXTURES fixture/velo.rb.markdown" | |
4 | APPLIANCES="$APPLIANCES tests/appliances/velo.rb.md" | |
5 | 5 | fi |
6 | 6 | if [ `which lua`x != x ]; then |
7 | FIXTURES="$FIXTURES fixture/velo.lua.markdown" | |
7 | APPLIANCES="$APPLIANCES tests/appliances/velo.lua.md" | |
8 | 8 | fi |
9 | if [ "${FIXTURES}x" = x ]; then | |
9 | if [ "${APPLIANCES}x" = x ]; then | |
10 | 10 | echo "Neither ruby nor lua found on search path." |
11 | 11 | exit 1 |
12 | 12 | fi |
13 | falderal $FIXTURES README.markdown | |
13 | falderal $APPLIANCES README.markdown |