From b029a778bd3a4bf5f17377347f0cbaca493f53a6 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Thu, 19 Sep 2024 20:42:39 -0700 Subject: [PATCH] Make luacheck-suggested changes. --- antifnl/lexer.lua | 12 +++++---- antifnl/lua_ast.lua | 64 +++++++++++++++++++++++++-------------------- antifnl/parser.lua | 12 ++++----- antifnl/reader.lua | 2 +- 4 files changed, 49 insertions(+), 41 deletions(-) diff --git a/antifnl/lexer.lua b/antifnl/lexer.lua index 170af3b..49c1b1c 100644 --- a/antifnl/lexer.lua +++ b/antifnl/lexer.lua @@ -9,12 +9,17 @@ local ASCII_A, ASCII_Z = 65, 90 local END_OF_STREAM = -1 -local ReservedKeyword = {['and'] = 1, ['break'] = 2, ['do'] = 3, ['else'] = 4, ['elseif'] = 5, ['end'] = 6, ['false'] = 7, ['for'] = 8, ['function'] = 9, ['goto'] = 10, ['if'] = 11, ['in'] = 12, ['local'] = 13, ['nil'] = 14, ['not'] = 15, ['or'] = 16, ['repeat'] = 17, ['return'] = 18, ['then'] = 19, ['true'] = 20, ['until'] = 21, ['while'] = 22 } +local ReservedKeyword = {['and'] = 1, ['break'] = 2, ['do'] = 3, ['else'] = 4, + ['elseif'] = 5, ['end'] = 6, ['false'] = 7, ['for'] = 8, + ['function'] = 9, ['goto'] = 10, ['if'] = 11, ['in'] = 12, ['local'] = 13, + ['nil'] = 14, ['not'] = 15, ['or'] = 16, ['repeat'] = 17, ['return'] = 18, + ['then'] = 19, ['true'] = 20, ['until'] = 21, ['while'] = 22 } local uint64, int64 = ffi.typeof('uint64_t'), ffi.typeof('int64_t') local complex = ffi.typeof('complex') -local TokenSymbol = { TK_ge = '>=', TK_le = '<=' , TK_concat = '..', TK_eq = '==', TK_ne = '~=', TK_eof = '', +local TokenSymbol = { TK_ge = '>=', TK_le = '<=' , TK_concat = '..', + TK_eq = '==', TK_ne = '~=', TK_eof = '', TK_shl = '<<', TK_shr = '>>' } local function token2str(tok) @@ -514,7 +519,6 @@ end local LexerClass = { __index = Lexer } local function lex_setup(read_func, chunkname, comments) - local header = false local ls = { n = 0, tklookahead = 'TK_eof', -- No look-ahead token. @@ -531,7 +535,6 @@ local function lex_setup(read_func, chunkname, comments) ls.n = ls.n - 2 ls.p = ls.p + 2 nextchar(ls) - header = true end if ls.current == '#' then local function nc() @@ -541,7 +544,6 @@ local function lex_setup(read_func, chunkname, comments) end nc() inclinenumber(ls) - header = true end return setmetatable(ls, LexerClass) end diff --git a/antifnl/lua_ast.lua b/antifnl/lua_ast.lua index 3087c65..561c9f4 100644 --- a/antifnl/lua_ast.lua +++ b/antifnl/lua_ast.lua @@ -1,4 +1,4 @@ -local id_generator = require("lang.id_generator") +local id_generator = require("antifnl.id_generator") local function build(kind, node) node.kind = kind @@ -30,10 +30,14 @@ local function func_decl(id, body, params, vararg, locald, firstline, lastline) end local function func_expr(body, params, vararg, firstline, lastline) - return build("FunctionExpression", { body = body, params = params, vararg = vararg, firstline = firstline, lastline = lastline }) + return build("FunctionExpression", { body = body, + params = params, + vararg = vararg, + firstline = firstline, + lastline = lastline }) end -function AST.expr_function(ast, args, body, proto) +function AST.expr_function(_ast, args, body, proto) return func_expr(body, args, proto.varargs, proto.firstline, proto.lastline) end @@ -42,7 +46,7 @@ function AST.local_function_decl(ast, name, args, body, proto) return func_decl(id, body, args, proto.varargs, true, proto.firstline, proto.lastline) end -function AST.function_decl(ast, path, args, body, proto) +function AST.function_decl(_ast, path, args, body, proto) return func_decl(path, body, args, proto.varargs, false, proto.firstline, proto.lastline) end @@ -57,7 +61,7 @@ function AST.func_parameters_decl(ast, args, vararg) return params end -function AST.chunk(ast, body, chunkname, firstline, lastline) +function AST.chunk(_ast, body, chunkname, firstline, lastline) return build("Chunk", { body = body, chunkname = chunkname, firstline = firstline, lastline = lastline }) end @@ -69,11 +73,11 @@ function AST.local_decl(ast, vlist, exps, line) return build("LocalDeclaration", { names = ids, expressions = exps, line = line }) end -function AST.assignment_expr(ast, vars, exps, line) +function AST.assignment_expr(_ast, vars, exps, line) return build("AssignmentExpression", { left = vars, right = exps, line = line }) end -function AST.expr_index(ast, v, index, line) +function AST.expr_index(_ast, v, index, line) return build("MemberExpression", { object = v, property = index, computed = true, line = line }) end @@ -82,20 +86,20 @@ function AST.expr_property(ast, v, prop, line) return build("MemberExpression", { object = v, property = index, computed = false, line = line }) end -function AST.literal(ast, val) +function AST.literal(_ast, val) return build("Literal", { value = val }) end -function AST.expr_vararg(ast) +function AST.expr_vararg(_ast) return build("Vararg", { }) end -function AST.expr_brackets(ast, expr) +function AST.expr_brackets(_ast, expr) expr.bracketed = true return expr end -function AST.set_expr_last(ast, expr) +function AST.set_expr_last(_ast, expr) if expr.bracketed and does_multi_return(expr) then expr.bracketed = nil return build("ExpressionValue", { value = expr }) @@ -104,11 +108,11 @@ function AST.set_expr_last(ast, expr) end end -function AST.expr_table(ast, keyvals, line) +function AST.expr_table(_ast, keyvals, line) return build("Table", { keyvals = keyvals, line = line }) end -function AST.expr_unop(ast, op, v, line) +function AST.expr_unop(_ast, op, v, line) return build("UnaryExpression", { operator = op, argument = v, line = line }) end @@ -121,7 +125,7 @@ local function concat_append(ts, node) end end -function AST.expr_binop(ast, op, expa, expb, line) +function AST.expr_binop(_ast, op, expa, expb, line) local binop_body = (op ~= '..' and { operator = op, left = expa, right = expb, line = line }) if binop_body then if op == 'and' or op == 'or' then @@ -146,57 +150,59 @@ function AST.expr_method_call(ast, v, key, args, line) return build("SendExpression", { receiver = v, method = m, arguments = args, line = line }) end -function AST.expr_function_call(ast, v, args, line) +function AST.expr_function_call(_ast, v, args, line) return build("CallExpression", { callee = v, arguments = args, line = line }) end -function AST.return_stmt(ast, exps, line) +function AST.return_stmt(_ast, exps, line) return build("ReturnStatement", { arguments = exps, line = line }) end -function AST.break_stmt(ast, line) +function AST.break_stmt(_ast, line) return build("BreakStatement", { line = line }) end -function AST.label_stmt(ast, name, line) +function AST.label_stmt(_ast, name, line) return build("LabelStatement", { label = name, line = line }) end -function AST.new_statement_expr(ast, expr, line) +function AST.new_statement_expr(_ast, expr, line) return build("ExpressionStatement", { expression = expr, line = line }) end -function AST.if_stmt(ast, tests, cons, else_branch, line) +function AST.if_stmt(_ast, tests, cons, else_branch, line) return build("IfStatement", { tests = tests, cons = cons, alternate = else_branch, line = line }) end -function AST.do_stmt(ast, body, line, lastline) +function AST.do_stmt(_ast, body, line, lastline) return build("DoStatement", { body = body, line = line, lastline = lastline}) end -function AST.while_stmt(ast, test, body, line, lastline) +function AST.while_stmt(_ast, test, body, line, lastline) return build("WhileStatement", { test = test, body = body, line = line, lastline = lastline }) end -function AST.repeat_stmt(ast, test, body, line, lastline) +function AST.repeat_stmt(_ast, test, body, line, lastline) return build("RepeatStatement", { test = test, body = body, line = line, lastline = lastline }) end -function AST.for_stmt(ast, var, init, last, step, body, line, lastline) +function AST.for_stmt(_ast, var, init, last, step, body, line, lastline) local for_init = build("ForInit", { id = var, value = init, line = line }) - return build("ForStatement", { init = for_init, last = last, step = step, body = body, line = line, lastline = lastline }) + return build("ForStatement", { init = for_init, last = last, step = step, + body = body, line = line, lastline = lastline }) end -function AST.for_iter_stmt(ast, vars, exps, body, line, lastline) +function AST.for_iter_stmt(_ast, vars, exps, body, line, lastline) local names = build("ForNames", { names = vars, line = line }) - return build("ForInStatement", { namelist = names, explist = exps, body = body, line = line, lastline = lastline }) + return build("ForInStatement", { namelist = names, explist = exps, + body = body, line = line, lastline = lastline }) end -function AST.comment(ast, contents) +function AST.comment(_ast, contents) return build("Comment", { contents = contents }) end -function AST.goto_stmt(ast, name, line) +function AST.goto_stmt(_ast, name, line) return build("GotoStatement", { label = name, line = line }) end diff --git a/antifnl/parser.lua b/antifnl/parser.lua index d2822e0..c30b084 100644 --- a/antifnl/parser.lua +++ b/antifnl/parser.lua @@ -1,4 +1,4 @@ -local operator = require("lang.operator") +local operator = require("antifnl.operator") local LJ_52 = false @@ -484,7 +484,7 @@ local function parse_stmt(ast, ls) return stmt, false end -local function parse_params(ast, ls, needself) +local function parse_params(_ast, ls, needself) lex_check(ls, "(") local args = { } local vararg = false @@ -513,14 +513,14 @@ local function parse_params(ast, ls, needself) return args, vararg end -local function new_proto(ls, varargs) +local function new_proto(_ls, varargs) return { varargs = varargs } end local function parse_block_stmts(ast, ls) local firstline = ls.linenumber - local stmt, islast = nil, false - local body = { } + local islast, stmt = false + local body = {} while not islast and not EndOfBlock[ls.token] do if ls.token == 'TK_comment' then stmt = ast:comment(ls.tokenval) @@ -535,7 +535,7 @@ local function parse_block_stmts(ast, ls) end local function parse_chunk(ast, ls) - local body, firstline, lastline = parse_block_stmts(ast, ls) + local body, _, lastline = parse_block_stmts(ast, ls) return ast:chunk(body, ls.chunkname, 0, lastline) end diff --git a/antifnl/reader.lua b/antifnl/reader.lua index 03cbcd2..03c6961 100644 --- a/antifnl/reader.lua +++ b/antifnl/reader.lua @@ -23,7 +23,7 @@ local function new_file_reader(filename) return reader end -return { +return { string = new_string_reader, file = new_file_reader, } -- 2.45.2