3 ms·
I would add Lua to the latter group (a language used as a tool to build the language you need). Not NEARLY as cool of an implementation as implementing TCP usi
by SomeCallMeTim 14y ago
I would add Lua to the latter group (a language used as a tool to build the language you need).
Not NEARLY as cool of an implementation as implementing TCP using ASCII art, but there's a command-line option parser that parses the help text to determine the command line options. [1] For example:
-- scale.lua
require 'lapp'
local args = lapp [[
Does some calculations
-o,--offset (default 0.0) Offset to add to scaled number
-s,--scale (number) Scaling factor
<number> (number ) Number to be scaled
]]
print(args.offset + args.scale * args.number)
[1] http://lua-users.org/wiki/LappFramework http://lua-users.org/wiki/LappFramework
- chubot 14y agoThat can be done in basically any dynamic language: see http://docopt.org/ http://docopt.org/
- masklinn 14y agoIt can even be done in static languages with an intermediate compilation step.
- SomeCallMeTim 14y agoThat's a pretty cool project. Sure you CAN do it in any language (as another commenter points out, you can do it in C too). Anything that's turing-complete can do it. You could also use C to parse ASCII art and have that create a TCP implementation. It's not the natural way you'd do it in that language, though. lapp has been around for four years; docopt is (looking at git commits) less than a year old. It strikes me as a natural way to do things in Lua, which is why (I suspect) it was done in Lua earlier.
- spc476 14y agoThrere's also LPeg [1], which I find much nicer than lex and yacc (or flex and bison) for writing parsers. I just finished up a quick hack to pull out a single file from a git repository into its own repository, and the parsing of the "git fast-export" output is 70 lines of Lua/LPeg. It may sound like quite a bit, but it's a near dump of the BNF you find on the man page [2]. From there, it was simple enough to grovel through the in-memory representation of the git repository and output only what I needed. [1] http://www.inf.puc-rio.br/~roberto/lpeg/ http://www.inf.puc-rio.br/~roberto/lpeg/ [3] [2] http://kernel.org/pub/software/scm/git/docs/git-fast-import.html http://kernel.org/pub/software/scm/git/docs/git-fast-import.... [3] I actually use the re module [4] as I find the results more readable, and only use the LPeg module when I need something that the re module can't provide. [4] http://www.inf.puc-rio.br/~roberto/lpeg/re.html http://www.inf.puc-rio.br/~roberto/lpeg/re.html
- ufo 14y agoMy problem with Lua DSLs is the verbose function syntax (`function foo() return end`). I'm thinking about experimenting more with Metalua to see if it helps in that regard. One thing that positively impressed me were coroutines though. You can do some very interesting DSL things with it.