mirror of
https://github.com/Reuh/candran.git
synced 2025-10-27 09:59:29 +00:00
Making the REPL great again
This commit is contained in:
parent
d410606dc0
commit
6be81267d2
7 changed files with 168 additions and 38 deletions
44
README.md
44
README.md
|
|
@ -11,9 +11,9 @@ Unlike Moonscript, Candran tries to stay close to the Lua syntax, and existing L
|
|||
local function calculate(toadd=25) -- default parameters
|
||||
local result = thing.do()
|
||||
result += toadd
|
||||
#if debug then -- preprocessor conditionals
|
||||
print("Did something")
|
||||
#end
|
||||
#if debug then -- preprocessor conditionals
|
||||
print("Did something")
|
||||
#end
|
||||
return result
|
||||
end
|
||||
|
||||
|
|
@ -50,14 +50,16 @@ print("Hello %s":format("world")) -- methods calls on strings (and tables) litte
|
|||
|
||||
````
|
||||
|
||||
**Current status**: Candran in heavily used in several of my personal projects and works as expected.
|
||||
**Current status**: Candran is heavily used in several of my personal projects and works as expected.
|
||||
|
||||
Candran is released under the MIT License (see ```LICENSE``` for details).
|
||||
|
||||
#### Quick setup
|
||||
Install Candran automatically using LuaRocks: ```sudo luarocks install rockspec/candran-0.8.0-1.rockspec```.
|
||||
Install Candran automatically using LuaRocks: ```sudo luarocks install rockspec/candran-0.10.0-1.rockspec```.
|
||||
|
||||
Or manually install LPegLabel (```luarocks install LPegLabel```, version 1.5 or above), download this repository and use Candran through the scripts in ```bin/``` or use it as a library with the self-contained ```candran.lua```.
|
||||
Or manually install LPegLabel (```luarocks install lpeglabel```, version 1.5 or above), download this repository and use Candran through the scripts in ```bin/``` or use it as a library with the self-contained ```candran.lua```.
|
||||
|
||||
You can optionally install lua-linenoise (```luarocks install linenoise```) for an improved REPL. The rockspec does not install linenoise by default.
|
||||
|
||||
You can register the Candran package searcher in your main Lua file (`require("candran").setup()`) and any subsequent `require` call in your project will automatically search for Candran modules.
|
||||
|
||||
|
|
@ -100,7 +102,7 @@ Right and left operator can be used at the same time.
|
|||
##### Default function parameters
|
||||
```lua
|
||||
function foo(bar = "default", other = thing.do())
|
||||
-- stuff
|
||||
-- stuff
|
||||
end
|
||||
```
|
||||
If an argument isn't provided or set to ```nil``` when the function is called, it will automatically be set to its default value.
|
||||
|
|
@ -328,11 +330,11 @@ The library can be used standalone through the ```canc``` and ```can``` utility:
|
|||
|
||||
_options_ is of type ````-somearg -anotherarg thing=somestring other=5````, which will generate a Lua table ```{ somearg = true, anotherarg = true, thing = "somestring", other = 5 }```.
|
||||
|
||||
You can choose to use another directory where files should be written using the ```dest=destinationDirectory``` argument.
|
||||
You can choose to use another directory where files should be written using the ```dest=destinationDirectory``` argument.
|
||||
|
||||
You can choose the output filename using ```out=filename```. By default, compiled files have the same name as their input file, but with a ```.lua``` extension.
|
||||
You can choose the output filename using ```out=filename```. By default, compiled files have the same name as their input file, but with a ```.lua``` extension.
|
||||
|
||||
```canc``` can write to the standard output instead of creating files using the ```-print``` argument.
|
||||
```canc``` can write to the standard output instead of creating files using the ```-print``` argument.
|
||||
|
||||
You can choose to run only the preprocessor or compile using the ```-preprocess``` and ```-compile``` flags.
|
||||
|
||||
|
|
@ -342,31 +344,35 @@ The library can be used standalone through the ```canc``` and ```can``` utility:
|
|||
|
||||
Instead of providing filenames, you can use ```-``` to read from standard input.
|
||||
|
||||
* example uses:
|
||||
Use the ```-h``` or ```-help``` option to display a short help text.
|
||||
|
||||
````canc foo.can````
|
||||
Example uses:
|
||||
|
||||
* ````canc foo.can````
|
||||
|
||||
preprocess and compile _foo.can_ and write the result in _foo.lua_.
|
||||
|
||||
````canc indentation=" " foo.can````
|
||||
* ````canc indentation=" " foo.can````
|
||||
|
||||
preprocess and compile _foo.can_ with 2-space indentation (readable code!) and write the result in _foo.lua_.
|
||||
|
||||
````canc foo.can -verbose -print | lua````
|
||||
* ````canc foo.can -verbose -print | lua````
|
||||
|
||||
preprocess _foo.can_ with _verbose_ set to _true_, compile it and execute it.
|
||||
|
||||
````canc -parse foo.can````
|
||||
* ````canc -parse foo.can````
|
||||
|
||||
checks foo.can for syntaxic errors.
|
||||
|
||||
* ```can```
|
||||
|
||||
Start a simplisitic Candran REPL.
|
||||
Start a simplisitic Candran REPL.
|
||||
|
||||
If you want a better REPL (autocompletion, history, ability to move the cursor), install lua-linenoise: ```luarocks install linenoise```.
|
||||
|
||||
* ````can [options] filename````
|
||||
|
||||
Preprocess, compile and run _filename_ using the options provided.
|
||||
Preprocess, compile and run _filename_ using the options provided.
|
||||
|
||||
This will automatically register the Candran package searcher, so required Candran modules will be compiled as they are needed.
|
||||
|
||||
|
|
@ -374,6 +380,8 @@ The library can be used standalone through the ```canc``` and ```can``` utility:
|
|||
|
||||
Instead of providing a filename, you can use ```-``` to read from standard input.
|
||||
|
||||
Use the ```-h``` or ```-help``` option to display a short help text.
|
||||
|
||||
### Library usage
|
||||
Candran can also be used as a Lua library:
|
||||
````lua
|
||||
|
|
@ -398,7 +406,7 @@ local foo = require("foo")
|
|||
The table returned by _require("candran")_ gives you access to:
|
||||
|
||||
##### Compiler & preprocessor
|
||||
* ````candran.VERSION````: Candran's version string (e.g. `"0.9.0"`).
|
||||
* ````candran.VERSION````: Candran's version string (e.g. `"0.10.0"`).
|
||||
* ````candran.preprocess(code[, options])````: return the Candran code _code_, preprocessed with the _options_ options table.
|
||||
* ````candran.compile(code[, options])````: return the Candran code compiled to Lua with the _options_ option table.
|
||||
* ````candran.make(code[, options])````: return the Candran code, preprocessed and compiled with the _options_ options table.
|
||||
|
|
|
|||
119
bin/can
119
bin/can
|
|
@ -19,6 +19,7 @@ if args.help or args.h then
|
|||
return
|
||||
end
|
||||
|
||||
-- stdin
|
||||
if arg[#arg] == "-" then
|
||||
local f, err = candran.load(io.read("*a"), "stdin", nil, args)
|
||||
if not f then
|
||||
|
|
@ -26,21 +27,123 @@ if arg[#arg] == "-" then
|
|||
os.exit(1)
|
||||
end
|
||||
f()
|
||||
-- file
|
||||
elseif #args >= 1 then
|
||||
candran.dofile(args[1], args)
|
||||
else -- REPL
|
||||
print("Candran " .. candran.VERSION)
|
||||
-- REPL
|
||||
else
|
||||
-- Setup linenoise
|
||||
local s, l = pcall(require, "linenoise")
|
||||
if not s then -- pure Lua compatibility thingy
|
||||
l = {
|
||||
linenoise = function(prompt)
|
||||
io.write(prompt)
|
||||
local s, line = pcall(io.read)
|
||||
if not s then
|
||||
if line == "interrupted!" then
|
||||
return nil
|
||||
else
|
||||
return nil, err
|
||||
end
|
||||
end
|
||||
return line
|
||||
end,
|
||||
historyadd = function() end,
|
||||
setcompletion = function() end,
|
||||
sethints = function() end,
|
||||
enableutf8 = function() end
|
||||
}
|
||||
end
|
||||
local keywords = {
|
||||
-- Lua
|
||||
"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "goto",
|
||||
"if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true",
|
||||
"until", "while",
|
||||
-- Candran
|
||||
"continue", "let", "push"
|
||||
}
|
||||
l.enableutf8()
|
||||
l.setcompletion(function(comp, line)
|
||||
local var = line:match("[a-zA-Z_][a-zA-Z_0-9]*$")
|
||||
if var then
|
||||
for _, k in ipairs(keywords) do
|
||||
if k:match("^"..var) then
|
||||
comp:add(line .. k:sub(#var+1))
|
||||
end
|
||||
end
|
||||
for k in pairs(_ENV) do
|
||||
if k:match("^"..var) then
|
||||
comp:add(line .. k:sub(#var+1))
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
l.sethints(function(line)
|
||||
local var = line:match("[a-zA-Z_][a-zA-Z_0-9]*$")
|
||||
if var then
|
||||
for _, k in ipairs(keywords) do
|
||||
if k:match("^"..var) then
|
||||
return k:sub(#var+1), { color = 2, bold = true }
|
||||
end
|
||||
end
|
||||
for k in pairs(_ENV) do
|
||||
if k:match("^"..var) then
|
||||
return k:sub(#var+1), { color = 2 }
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
-- Introduction
|
||||
print("Candran " .. candran.VERSION .. ", targeting " .. candran.default.target)
|
||||
candran.setup()
|
||||
|
||||
-- REPL loop
|
||||
local multiline = false -- true if wait for another line
|
||||
local buffer
|
||||
while true do
|
||||
io.write("> ")
|
||||
local line = io.read()
|
||||
if line:match("^=") then
|
||||
line = line:gsub("^=", "return tostring(") .. ")"
|
||||
local line, err = l.linenoise(multiline and ">> " or "> ")
|
||||
|
||||
-- exit
|
||||
if not line then
|
||||
if not err then
|
||||
if multiline then
|
||||
multiline = false
|
||||
line = ""
|
||||
else
|
||||
return
|
||||
end
|
||||
else
|
||||
error(err)
|
||||
end
|
||||
end
|
||||
|
||||
local t = { pcall(candran.load, line, "stdin") }
|
||||
-- history
|
||||
if line:match("[^%s]") then
|
||||
l.historyadd(line)
|
||||
end
|
||||
|
||||
-- multiline
|
||||
if multiline then
|
||||
buffer = buffer .. "\n" .. line
|
||||
multiline = false
|
||||
else
|
||||
buffer = line
|
||||
end
|
||||
|
||||
-- print shortcut
|
||||
if buffer:match("^=") then
|
||||
buffer = buffer:gsub("^=", "return tostring(") .. ")"
|
||||
end
|
||||
|
||||
-- exec
|
||||
local t = { pcall(candran.load, buffer, "stdin") }
|
||||
if t[1] == false then
|
||||
print(t[2])
|
||||
if t[2]:match("expected '[end})]+' to close") then
|
||||
multiline = true
|
||||
else
|
||||
print(t[2])
|
||||
end
|
||||
else
|
||||
t = { pcall(t[2]) }
|
||||
if t[1] == false then
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
#import("lib.lua-parser.parser")
|
||||
|
||||
local candran = {
|
||||
VERSION = "0.9.1"
|
||||
VERSION = "0.10.0"
|
||||
}
|
||||
|
||||
--- Default options.
|
||||
|
|
|
|||
|
|
@ -3584,7 +3584,7 @@ return parser -- ./lib/lua-parser/parser.lua:685
|
|||
end -- ./lib/lua-parser/parser.lua:685
|
||||
local parser = _() or parser -- ./lib/lua-parser/parser.lua:689
|
||||
package["loaded"]["lib.lua-parser.parser"] = parser or true -- ./lib/lua-parser/parser.lua:690
|
||||
local candran = { ["VERSION"] = "0.9.1" } -- candran.can:14
|
||||
local candran = { ["VERSION"] = "0.10.0" } -- candran.can:14
|
||||
candran["default"] = { -- candran.can:18
|
||||
["target"] = "lua53", -- candran.can:19
|
||||
["indentation"] = "", -- candran.can:20
|
||||
|
|
|
|||
23
ideas.txt
23
ideas.txt
|
|
@ -24,7 +24,7 @@ Please note that the following ideas are just random though and won't be necessa
|
|||
|
||||
Feel free to open issues about ideas you find useful or want to improve. Otherwise, I will only implements them when I really need them or am bored.
|
||||
|
||||
Actually, open issues for everything. I know of a couple persons using Candran but have no idea what they like and want with it.
|
||||
Actually, open issues for everything. I've heard that a couple of people use Candran, but have no idea what they like and want with it.
|
||||
|
||||
* class keyword
|
||||
class Thing(parents...)
|
||||
|
|
@ -37,6 +37,7 @@ end
|
|||
local a = new Thing()
|
||||
->
|
||||
(TODO: define how classes work. May even use ClassCommons)
|
||||
Not very Lua-ey to impose how to make your classes?
|
||||
|
||||
* try / except|catch / finally / else / other keywords
|
||||
try
|
||||
|
|
@ -52,6 +53,8 @@ local name = articles?[0].author?.name (?[] and ?. index opeators)
|
|||
or
|
||||
local zip = lottery.drawWinner?().address?.zipcode (expr? existence test suffix)
|
||||
|
||||
if one is nil, returns nil
|
||||
|
||||
See http://coffeescript.org/#existential-operator
|
||||
|
||||
* static type checking
|
||||
|
|
@ -69,9 +72,13 @@ Will need to define stuff for a lot of Lua libraries (see the work already done
|
|||
|
||||
While we're talking about static analysis, what about forking luacheck into candrancheck? Or at least make the line mapping work.
|
||||
|
||||
Will require work.
|
||||
|
||||
* array slicing
|
||||
local b = a[3:5:1]
|
||||
|
||||
is it actually useful? even in python I rarely use it, apart from extracting a row or column for a matrix (and we don't have >1D arrays in Lua so...)
|
||||
|
||||
* Destructuring assignment
|
||||
local pos = { x = 5, y = 12 }
|
||||
|
||||
|
|
@ -85,12 +92,24 @@ local x, y $= pos
|
|||
And in implicit assignments:
|
||||
for i, {x, y} in ipairs(positions) do
|
||||
|
||||
Sounds useful, at least the key-value part.
|
||||
|
||||
* String interpolation
|
||||
Delimited by ``:
|
||||
`Hi, my name is ${@name}` -- -> "blaba" .. tostring(@name)
|
||||
Also allows multi-line with this maybe?
|
||||
|
||||
meh
|
||||
|
||||
* Other potential inspiration
|
||||
https://www.ruby-lang.org/fr/
|
||||
|
||||
Well done, you're at the end of the file!
|
||||
* If with assignement
|
||||
if f = io.open("file") then
|
||||
f:close()
|
||||
end
|
||||
|
||||
f is in the if scope
|
||||
|
||||
* Method self bundler
|
||||
obj:method -- returns (...) return method(obj, ...) end
|
||||
|
|
|
|||
|
|
@ -2,24 +2,24 @@ rockspec_format = "3.0"
|
|||
|
||||
package = "candran"
|
||||
|
||||
version = "0.9.0-1"
|
||||
version = "0.10.0-1"
|
||||
|
||||
description = {
|
||||
summary = "A simple Lua dialect and preprocessor.",
|
||||
detailed = [[
|
||||
Candran is a dialect of the Lua 5.3 programming language which compiles to Lua 5.3 and Lua 5.1/LuaJit. It adds several useful syntax additions which aims to make Lua faster and easier to write, and a simple preprocessor.
|
||||
Unlike Moonscript, Candran tries to stay close to the Lua syntax, and existing Lua code can run on Candran unmodified.
|
||||
Candran is a dialect of the Lua 5.3 programming language which compiles to Lua 5.3, LuaJIT and Lua 5.1 compatible code. It adds several useful syntax additions which aims to make Lua faster and easier to write, and a simple preprocessor.
|
||||
Unlike Moonscript, Candran tries to stay close to the Lua syntax, and existing Lua code should be able to run on Candran unmodified.
|
||||
]],
|
||||
license = "MIT",
|
||||
homepage = "https://github.com/Reuh/candran",
|
||||
issues_url = "https://github.com/Reuh/candran",
|
||||
maintainer = "Étienne 'Reuh' Fildadut <fildadut@reuh.eu>",
|
||||
labels = {"lpeg", "commandline"}
|
||||
labels = { "lpeg", "commandline" }
|
||||
}
|
||||
|
||||
source = {
|
||||
url = "git://github.com/Reuh/candran",
|
||||
tag = "v0.9.0"
|
||||
tag = "v0.10.0"
|
||||
}
|
||||
|
||||
dependencies = {
|
||||
|
|
@ -7,14 +7,14 @@ version = "scm-1"
|
|||
description = {
|
||||
summary = "A simple Lua dialect and preprocessor.",
|
||||
detailed = [[
|
||||
Candran is a dialect of the Lua 5.3 programming language which compiles to Lua 5.3 and Lua 5.1/LuaJit. It adds several useful syntax additions which aims to make Lua faster and easier to write, and a simple preprocessor.
|
||||
Unlike Moonscript, Candran tries to stay close to the Lua syntax, and existing Lua code can run on Candran unmodified.
|
||||
Candran is a dialect of the Lua 5.3 programming language which compiles to Lua 5.3, LuaJIT and Lua 5.1 compatible code. It adds several useful syntax additions which aims to make Lua faster and easier to write, and a simple preprocessor.
|
||||
Unlike Moonscript, Candran tries to stay close to the Lua syntax, and existing Lua code should be able to run on Candran unmodified.
|
||||
]],
|
||||
license = "MIT",
|
||||
homepage = "https://github.com/Reuh/candran",
|
||||
issues_url = "https://github.com/Reuh/candran",
|
||||
maintainer = "Étienne 'Reuh' Fildadut <fildadut@reuh.eu>",
|
||||
labels = {"lpeg", "commandline"}
|
||||
labels = { "lpeg", "commandline" }
|
||||
}
|
||||
|
||||
source = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue