1
0
mirror of https://github.com/g012/l65.git synced 2024-06-11 10:29:27 +00:00

Added ability to pass command line arguments to the l65 script file.

This commit is contained in:
g012 2017-10-11 14:35:19 +02:00
parent 716e053244
commit 10fac6307b
3 changed files with 46 additions and 17 deletions

View File

@ -8,7 +8,7 @@ set(L65_SOURCE_DIR ${PROJECT_SOURCE_DIR})
set(L65_BINARY_DIR ${PROJECT_BINARY_DIR}) set(L65_BINARY_DIR ${PROJECT_BINARY_DIR})
set(L65_VERSION_MAJOR "1") set(L65_VERSION_MAJOR "1")
set(L65_VERSION_MINOR "1") set(L65_VERSION_MINOR "2")
set(L65_VERSION_REVISION "0") set(L65_VERSION_REVISION "0")
set(L65_VERSION "${L65_VERSION_MAJOR}.${L65_VERSION_MINOR}.${L65_VERSION_REVISION}") set(L65_VERSION "${L65_VERSION_MAJOR}.${L65_VERSION_MINOR}.${L65_VERSION_REVISION}")
configure_file("${L65_SOURCE_DIR}/l65cfg.lua.in" "${L65_BINARY_DIR}/l65cfg.lua") configure_file("${L65_SOURCE_DIR}/l65cfg.lua.in" "${L65_BINARY_DIR}/l65cfg.lua")

View File

@ -13,6 +13,8 @@ Table of Contents
* [Lua Extensions](#lua-extensions) * [Lua Extensions](#lua-extensions)
* [Lambda Functions](#lambda-functions) * [Lambda Functions](#lambda-functions)
* [Binary Numbers](#binary-numbers) * [Binary Numbers](#binary-numbers)
* [Globals](#globals)
* [getopt(optstring, ...)](#getoptoptstring-)
* [Instruction List](#instruction-list) * [Instruction List](#instruction-list)
* [Regular Opcodes](#regular-opcodes) * [Regular Opcodes](#regular-opcodes)
* [Undocumented Opcodes](#undocumented-opcodes) * [Undocumented Opcodes](#undocumented-opcodes)
@ -48,7 +50,7 @@ Table of Contents
* [writesym(filename)](#writesymfilename) * [writesym(filename)](#writesymfilename)
* [Parser Functions](#parser-functions) * [Parser Functions](#parser-functions)
* [Pragmas](#pragmas) * [Pragmas](#pragmas)
* [syntax6502 oo|off](#syntax6502-oooff) * [syntax6502 on|off](#syntax6502-onoff)
* [encapsulate on|off](#encapsulate-onoff) * [encapsulate on|off](#encapsulate-onoff)
* [encapsulate opcode funcname](#encapsulate-opcode-funcname) * [encapsulate opcode funcname](#encapsulate-opcode-funcname)
* [add_opcode opcode addressing](#add_opcode-opcode-addressing) * [add_opcode opcode addressing](#add_opcode-opcode-addressing)
@ -73,7 +75,6 @@ Table of Contents
* [Credits](#credits) * [Credits](#credits)
* [License](#license) * [License](#license)
## Concept - How It Works ## Concept - How It Works
l65 hooks into Lua `require` and related functionalities to add a parsing pass before Lua compilation. This pass transforms 6502 statements into regular Lua function calls. It applies only to files with extension '.l65', not '.lua'. l65 hooks into Lua `require` and related functionalities to add a parsing pass before Lua compilation. This pass transforms 6502 statements into regular Lua function calls. It applies only to files with extension '.l65', not '.lua'.
@ -85,13 +86,15 @@ Spatial arrangement follows [nimble65](https://bitbucket.org/kylearan/nimble65)
## Command Line Usage ## Command Line Usage
``` ```
Usage: l65 [options] file Usage: l65 [options] file [args]
Options: Options:
-d <file> Dump the Lua code after l65 parsing into file -d <file> Dump the Lua code after l65 parsing into file
-h Display this information -h Display this information
-v Display the release version -v Display the release version
``` ```
`args` are passed as arguments to the l65 script in `file`. The global function [getopt](#getoptoptstring-) is available to parse them.
## Samples - Getting Started ## Samples - Getting Started
Have a look at these files in the `samples` folder to get started with l65: Have a look at these files in the `samples` folder to get started with l65:
@ -117,6 +120,31 @@ Since anonymous functions are core to l65, the extension syntax such as `\a,b(a+
A simple parsing of number literals in the form of `0b11011` converts this base-2 / binary number into a decimal number after the l65 pass. There is no support for them in other places though - no format type for `string.format` or anything else. A simple parsing of number literals in the form of `0b11011` converts this base-2 / binary number into a decimal number after the l65 pass. There is no support for them in other places though - no format type for `string.format` or anything else.
### Globals
#### getopt(optstring, ...)
Taken from [Posix Get Opt](http://lua-users.org/wiki/PosixGetOpt).
Parse args in `...` using optstring, the same way as `getopt` from Posix.
Usage:
```lua
--
-- opt is either the option character, "?", or ":".
--
-- If opt is the option character, arg is either a string or false--if optstring
-- did not specify a required argument.
--
-- If opt is either "?" or ":", then arg is the unknown option or the option
-- lacking the required argument. Like the standard says, there's really only
-- one scenario for ":" to ever be returned. And per the standard, unless
-- optstring begins with a colon, "?" is returned instead.
--
for opt, arg in getopt(":a:b", ...) do
print("opt:", opt, "arg:", arg)
end
```
### Instruction List ### Instruction List
#### Regular Opcodes #### Regular Opcodes

27
l65.lua
View File

@ -2682,7 +2682,7 @@ function getopt(optstring, ...)
local yield = coroutine.yield local yield = coroutine.yield
local i = 1 local i = 1
while i <= #args do while i <= #args do
local arg = args[i] local arg,argi = args[i], i
i = i + 1 i = i + 1
if arg == "--" then if arg == "--" then
break break
@ -2692,16 +2692,17 @@ function getopt(optstring, ...)
if opts[opt] then if opts[opt] then
if opts[opt].hasarg then if opts[opt].hasarg then
if j == #arg then if j == #arg then
if args[i] then yield(opt, args[i]) i=i+1 if args[i] then yield(opt, args[i], argi) i=i+1
else yield('?', opt) end elseif optstring:sub(1, 1) == ":" then yield(':', opt, argi)
else yield(opt, arg:sub(j + 1)) end else yield('?', opt, argi) end
else yield(opt, arg:sub(j + 1), argi) end
break break
else yield(opt, false) end else yield(opt, false, argi) end
else yield('?', opt) end else yield('?', opt, argi) end
end end
else yield(false, arg) end else yield(false, arg, argi) end
end end
for i = i, #args do yield(false, args[i]) end for i = i, #args do yield(false, args[i], i) end
end) end)
end end
@ -2712,7 +2713,7 @@ end
local usage = function(f) local usage = function(f)
if not f then f = io.stdout end if not f then f = io.stdout end
f:write(string.format([[ f:write(string.format([[
Usage: %s [options] file Usage: %s [options] file [args]
Options: Options:
-d <file> Dump the Lua code after l65 parsing into file -d <file> Dump the Lua code after l65 parsing into file
-h Display this information -h Display this information
@ -2724,13 +2725,13 @@ local invalid_usage = function()
usage(io.stderr) usage(io.stderr)
end end
local inf,dump local inf,dump,optix
for opt,arg in getopt("d:hv", ...) do for opt,arg,i in getopt("d:hv", ...) do
if opt == '?' then return invalid_usage() end if opt == '?' then return invalid_usage() end
if opt == 'h' then return usage() end if opt == 'h' then return usage() end
if opt == 'v' then return version() end if opt == 'v' then return version() end
if opt == 'd' then dump = arg end if opt == 'd' then dump = arg end
if opt == false then inf = arg end if opt == false then inf=arg optix=i+1 break end
end end
if not inf then return invalid_usage() end if not inf then return invalid_usage() end
if dump then l65.format = function(ast) if dump then l65.format = function(ast)
@ -2741,4 +2742,4 @@ end end
local fn='' for i=#inf,1,-1 do local c=inf:sub(i,i) if c==dirsep or c=='/' then break end fn=c..fn if c=='.' then fn='' end end filename=fn local fn='' for i=#inf,1,-1 do local c=inf:sub(i,i) if c==dirsep or c=='/' then break end fn=c..fn if c=='.' then fn='' end end filename=fn
local f = l65.report(l65.loadfile(inf)) local f = l65.report(l65.loadfile(inf))
return xpcall(f, l65.msghandler) return xpcall(f, l65.msghandler, select(optix, ...))