From 10fac6307b2694fe02640b97f6800144305a2ff4 Mon Sep 17 00:00:00 2001 From: g012 Date: Wed, 11 Oct 2017 14:35:19 +0200 Subject: [PATCH] Added ability to pass command line arguments to the l65 script file. --- CMakeLists.txt | 2 +- README.md | 34 +++++++++++++++++++++++++++++++--- l65.lua | 27 ++++++++++++++------------- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 21f1122..0c2050e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ set(L65_SOURCE_DIR ${PROJECT_SOURCE_DIR}) set(L65_BINARY_DIR ${PROJECT_BINARY_DIR}) set(L65_VERSION_MAJOR "1") -set(L65_VERSION_MINOR "1") +set(L65_VERSION_MINOR "2") set(L65_VERSION_REVISION "0") 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") diff --git a/README.md b/README.md index 7e4071c..c7048bc 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,8 @@ Table of Contents * [Lua Extensions](#lua-extensions) * [Lambda Functions](#lambda-functions) * [Binary Numbers](#binary-numbers) + * [Globals](#globals) + * [getopt(optstring, ...)](#getoptoptstring-) * [Instruction List](#instruction-list) * [Regular Opcodes](#regular-opcodes) * [Undocumented Opcodes](#undocumented-opcodes) @@ -48,7 +50,7 @@ Table of Contents * [writesym(filename)](#writesymfilename) * [Parser Functions](#parser-functions) * [Pragmas](#pragmas) - * [syntax6502 oo|off](#syntax6502-oooff) + * [syntax6502 on|off](#syntax6502-onoff) * [encapsulate on|off](#encapsulate-onoff) * [encapsulate opcode funcname](#encapsulate-opcode-funcname) * [add_opcode opcode addressing](#add_opcode-opcode-addressing) @@ -73,7 +75,6 @@ Table of Contents * [Credits](#credits) * [License](#license) - ## 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'. @@ -85,13 +86,15 @@ Spatial arrangement follows [nimble65](https://bitbucket.org/kylearan/nimble65) ## Command Line Usage ``` -Usage: l65 [options] file +Usage: l65 [options] file [args] Options: -d Dump the Lua code after l65 parsing into file -h Display this information -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 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. +### 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 #### Regular Opcodes diff --git a/l65.lua b/l65.lua index 6b55f3a..a942d44 100644 --- a/l65.lua +++ b/l65.lua @@ -2682,7 +2682,7 @@ function getopt(optstring, ...) local yield = coroutine.yield local i = 1 while i <= #args do - local arg = args[i] + local arg,argi = args[i], i i = i + 1 if arg == "--" then break @@ -2692,16 +2692,17 @@ function getopt(optstring, ...) if opts[opt] then if opts[opt].hasarg then if j == #arg then - if args[i] then yield(opt, args[i]) i=i+1 - else yield('?', opt) end - else yield(opt, arg:sub(j + 1)) end + if args[i] then yield(opt, args[i], argi) i=i+1 + elseif optstring:sub(1, 1) == ":" then yield(':', opt, argi) + else yield('?', opt, argi) end + else yield(opt, arg:sub(j + 1), argi) end break - else yield(opt, false) end - else yield('?', opt) end + else yield(opt, false, argi) end + else yield('?', opt, argi) end end - else yield(false, arg) end + else yield(false, arg, argi) 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 @@ -2712,7 +2713,7 @@ end local usage = function(f) if not f then f = io.stdout end f:write(string.format([[ -Usage: %s [options] file +Usage: %s [options] file [args] Options: -d Dump the Lua code after l65 parsing into file -h Display this information @@ -2724,13 +2725,13 @@ local invalid_usage = function() usage(io.stderr) end -local inf,dump -for opt,arg in getopt("d:hv", ...) do +local inf,dump,optix +for opt,arg,i in getopt("d:hv", ...) do if opt == '?' then return invalid_usage() end if opt == 'h' then return usage() end if opt == 'v' then return version() 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 if not inf then return invalid_usage() end 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 f = l65.report(l65.loadfile(inf)) -return xpcall(f, l65.msghandler) +return xpcall(f, l65.msghandler, select(optix, ...))